Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Recursively query parents and children #2106

Merged
merged 5 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions lamindb/_parents.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,39 @@
if TYPE_CHECKING:
from lnschema_core.types import StrField

from lamindb.core import QuerySet

LAMIN_GREEN_LIGHTER = "#10b981"
LAMIN_GREEN_DARKER = "#065f46"
GREEN_FILL = "honeydew"
TRANSFORM_EMOJIS = {"notebook": "📔", "app": "🖥️", "pipeline": "🧩"}
is_run_from_ipython = getattr(builtins, "__IPYTHON__", False)


# this is optimized to have fewer recursive calls
# also len of QuerySet can be costly at times
def _query_relatives(
records: QuerySet | list[Record],
kind: Literal["parents", "children"],
cls: type[HasParents],
) -> QuerySet:
relatives = cls.objects.none()
if len(records) == 0:
return relatives
for record in records:
relatives = relatives.union(getattr(record, kind).all())
relatives = relatives.union(_query_relatives(relatives, kind, cls))
return relatives


def query_parents(self) -> QuerySet:
return _query_relatives([self], "parents", self.__class__)


def query_children(self) -> QuerySet:
return _query_relatives([self], "children", self.__class__)


def _transform_emoji(transform: Transform):
if transform is not None:
return TRANSFORM_EMOJIS.get(transform.type, "💫")
Expand Down Expand Up @@ -474,9 +500,7 @@ def _df_edges_from_runs(df_values: list):
return df


METHOD_NAMES = [
"view_parents",
]
METHOD_NAMES = ["view_parents", "query_parents", "query_children"]

if ln_setup._TESTING: # type: ignore
from inspect import signature
Expand Down
2 changes: 1 addition & 1 deletion sub/lnschema-core
20 changes: 20 additions & 0 deletions tests/core/test_parents.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ def test_view_parents():
label2.delete()


def test_query_parents_children():
label1 = ln.ULabel(name="label1")
label2 = ln.ULabel(name="label2")
label3 = ln.ULabel(name="label3")
label1.save()
label2.save()
label3.save()
label1.children.add(label2)
label2.children.add(label3)
parents = label3.query_parents()
assert len(parents) == 2
assert label1 in parents and label2 in parents
children = label1.query_children()
assert len(children) == 2
assert label2 in children and label3 in children
label1.delete()
label2.delete()
label3.delete()


def test_add_emoji():
record = ln.Transform(type="app")
assert _add_emoji(record, label="transform") == "🖥️ transform"
Expand Down
Loading