Query & search registries

Find & access data using registries.

Setup

!lamin init --storage ./mydata
Hide code cell output
💡 connected lamindb: testuser1/mydata
import lamindb as ln

ln.settings.verbosity = "info"
💡 connected lamindb: testuser1/mydata

We’ll need some toy data:

ln.Artifact(ln.core.datasets.file_jpg_paradisi05(), description="My image").save()
ln.Artifact.from_df(ln.core.datasets.df_iris(), description="The iris collection").save()
ln.Artifact(ln.core.datasets.file_fastq(), description="My fastq").save()
Hide code cell output
❗ no run & transform get linked, consider calling ln.track()
✅ storing artifact 'sXgR1PiJERA5fEwtMhol' at '/home/runner/work/lamindb/lamindb/docs/mydata/.lamindb/sXgR1PiJERA5fEwtMhol.jpg'
❗ no run & transform get linked, consider calling ln.track()
✅ storing artifact 'AvyYmHPTQjXzn0MaXwuK' at '/home/runner/work/lamindb/lamindb/docs/mydata/.lamindb/AvyYmHPTQjXzn0MaXwuK.parquet'
❗ no run & transform get linked, consider calling ln.track()
✅ storing artifact 'jnCSBSGTm8aQwWNGAgSy' at '/home/runner/work/lamindb/lamindb/docs/mydata/.lamindb/jnCSBSGTm8aQwWNGAgSy.fastq.gz'
Artifact(uid='jnCSBSGTm8aQwWNGAgSy', suffix='.fastq.gz', description='My fastq', size=20, hash='hi7ZmAzz8sfMd3vIQr-57Q', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-14 15:58:21 UTC, storage_id=1, created_by_id=1)

Look up metadata

For entities where we don’t store more than 100k records, a look up object can be a convenient way of selecting a record.

Consider the User registry:

users = ln.User.lookup(field="handle")

With auto-complete, we find a user:

user = users.testuser1
user
User(uid='DzTjkKse', handle='testuser1', name='Test User1', updated_at=2024-05-14 15:58:19 UTC)

Note

You can also auto-complete in a dictionary:

users_dict = ln.User.lookup().dict()

Filter by metadata

Filter for all artifacts created by a user:

ln.Artifact.filter(created_by=user).df()
version uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
1 None sXgR1PiJERA5fEwtMhol 1 None .jpg None My image 29358 r4tnqmKI_SjrkdLzpuWp4g md5 None None None None 1 True 2024-05-14 15:58:21.202380+00:00 2024-05-14 15:58:21.202423+00:00 1
2 None AvyYmHPTQjXzn0MaXwuK 1 None .parquet DataFrame The iris collection 5629 ah24lV9Ncc8nPL0MumEsdw md5 None None None None 1 True 2024-05-14 15:58:21.485892+00:00 2024-05-14 15:58:21.485923+00:00 1
3 None jnCSBSGTm8aQwWNGAgSy 1 None .fastq.gz None My fastq 20 hi7ZmAzz8sfMd3vIQr-57Q md5 None None None None 1 True 2024-05-14 15:58:21.493461+00:00 2024-05-14 15:58:21.493485+00:00 1

To access the results encoded in a filter statement, execute its return value with one of:

  • .df(): A pandas DataFrame with each record stored as a row.

  • .all(): An indexable django QuerySet.

  • .list(): A list of records.

  • .one(): Exactly one record. Will raise an error if there is none.

  • .one_or_none(): Either one record or None if there is no query result.

Note

filter() returns a QuerySet.

The ORMs in LaminDB are Django Models and any Django query works. LaminDB extends Django’s API for data scientists.

Under the hood, any .filter() call translates into a SQL select statement.

.one() and .one_or_none() are two parts of LaminDB’s API that are borrowed from SQLAlchemy.

Search for metadata

ln.Artifact.search("iris").df()
version uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
2 None AvyYmHPTQjXzn0MaXwuK 1 None .parquet DataFrame The iris collection 5629 ah24lV9Ncc8nPL0MumEsdw md5 None None None None 1 True 2024-05-14 15:58:21.485892+00:00 2024-05-14 15:58:21.485923+00:00 1

Let us create 500 notebook objects with fake titles and save them:

ln.save(
    [
        ln.Transform(name=title, type="notebook")
        for title in ln.core.datasets.fake_bio_notebook_titles(n=500)
    ]
)

We can now search for any combination of terms:

ln.Transform.search("intestine").df().head()
version uid name key description type latest_report_id source_code_id reference reference_type created_at updated_at created_by_id
id
16 None hSpzYi0u7TdR Candidate intestine candidate. None None notebook None None None None 2024-05-14 15:58:22.549331+00:00 2024-05-14 15:58:22.549344+00:00 1
51 None r47Q0yxgqtuH Igm Bowman's gland intestine Thyrotropes Magno... None None notebook None None None None 2024-05-14 15:58:22.554715+00:00 2024-05-14 15:58:22.554729+00:00 1
64 None vMinMGdDTaZb Intestine research Thyroid gland Satellite gli... None None notebook None None None None 2024-05-14 15:58:22.556724+00:00 2024-05-14 15:58:22.556738+00:00 1
71 None 3J14gvPIFglj Iga intestine IgG1 Magnocellular neurosecretor... None None notebook None None None None 2024-05-14 15:58:22.557797+00:00 2024-05-14 15:58:22.557811+00:00 1
74 None TOYmkzmLbgYA Intestine study Place cells Place cells. None None notebook None None None None 2024-05-14 15:58:22.558342+00:00 2024-05-14 15:58:22.558356+00:00 1

Leverage relations

Django has a double-under-score syntax to filter based on related tables.

This syntax enables you to traverse several layers of relations:

ln.Artifact.filter(run__created_by__handle__startswith="testuse").df()
version uid key suffix accessor description size hash hash_type n_objects n_observations visibility key_is_virtual created_at updated_at storage_id transform_id run_id created_by_id
id

The filter selects all artifacts based on the users who ran the generating notebook.

(Under the hood, in the SQL database, it’s joining the artifact table with the run and the user table.)

Beyond __startswith, Django supports about two dozen field comparators field__comparator=value.

Here are some of them.

and

ln.Artifact.filter(suffix=".jpg", created_by=user).df()
version uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
1 None sXgR1PiJERA5fEwtMhol 1 None .jpg None My image 29358 r4tnqmKI_SjrkdLzpuWp4g md5 None None None None 1 True 2024-05-14 15:58:21.202380+00:00 2024-05-14 15:58:21.202423+00:00 1

less than/ greater than

Or subset to artifacts greater than 10kB. Here, we can’t use keyword arguments, but need an explicit where statement.

ln.Artifact.filter(created_by=user, size__lt=1e4).df()
version uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
2 None AvyYmHPTQjXzn0MaXwuK 1 None .parquet DataFrame The iris collection 5629 ah24lV9Ncc8nPL0MumEsdw md5 None None None None 1 True 2024-05-14 15:58:21.485892+00:00 2024-05-14 15:58:21.485923+00:00 1
3 None jnCSBSGTm8aQwWNGAgSy 1 None .fastq.gz None My fastq 20 hi7ZmAzz8sfMd3vIQr-57Q md5 None None None None 1 True 2024-05-14 15:58:21.493461+00:00 2024-05-14 15:58:21.493485+00:00 1

or

from django.db.models import Q

ln.Artifact.filter().filter(Q(suffix=".jpg") | Q(suffix=".fastq.gz")).df()
version uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
1 None sXgR1PiJERA5fEwtMhol 1 None .jpg None My image 29358 r4tnqmKI_SjrkdLzpuWp4g md5 None None None None 1 True 2024-05-14 15:58:21.202380+00:00 2024-05-14 15:58:21.202423+00:00 1
3 None jnCSBSGTm8aQwWNGAgSy 1 None .fastq.gz None My fastq 20 hi7ZmAzz8sfMd3vIQr-57Q md5 None None None None 1 True 2024-05-14 15:58:21.493461+00:00 2024-05-14 15:58:21.493485+00:00 1

in

ln.Artifact.filter(suffix__in=[".jpg", ".fastq.gz"]).df()
version uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
1 None sXgR1PiJERA5fEwtMhol 1 None .jpg None My image 29358 r4tnqmKI_SjrkdLzpuWp4g md5 None None None None 1 True 2024-05-14 15:58:21.202380+00:00 2024-05-14 15:58:21.202423+00:00 1
3 None jnCSBSGTm8aQwWNGAgSy 1 None .fastq.gz None My fastq 20 hi7ZmAzz8sfMd3vIQr-57Q md5 None None None None 1 True 2024-05-14 15:58:21.493461+00:00 2024-05-14 15:58:21.493485+00:00 1

order by

ln.Artifact.filter().order_by("-updated_at").df()
version uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
3 None jnCSBSGTm8aQwWNGAgSy 1 None .fastq.gz None My fastq 20 hi7ZmAzz8sfMd3vIQr-57Q md5 None None None None 1 True 2024-05-14 15:58:21.493461+00:00 2024-05-14 15:58:21.493485+00:00 1
2 None AvyYmHPTQjXzn0MaXwuK 1 None .parquet DataFrame The iris collection 5629 ah24lV9Ncc8nPL0MumEsdw md5 None None None None 1 True 2024-05-14 15:58:21.485892+00:00 2024-05-14 15:58:21.485923+00:00 1
1 None sXgR1PiJERA5fEwtMhol 1 None .jpg None My image 29358 r4tnqmKI_SjrkdLzpuWp4g md5 None None None None 1 True 2024-05-14 15:58:21.202380+00:00 2024-05-14 15:58:21.202423+00:00 1

contains

ln.Transform.filter(name__contains="search").df().head(10)
version uid name key description type latest_report_id source_code_id reference reference_type created_at updated_at created_by_id
id
15 None hv0sP6hmZpW3 Visualize IgE IgE Gonadotropes research Cecum ... None None notebook None None None None 2024-05-14 15:58:22.549176+00:00 2024-05-14 15:58:22.549190+00:00 1
21 None TM5HXVrjWSux Research Cajal–Retzius cells neurotensin-secre... None None notebook None None None None 2024-05-14 15:58:22.550101+00:00 2024-05-14 15:58:22.550114+00:00 1
29 None jo75RtfsbhoB Cluster efficiency Satellite glial cells IgG2 ... None None notebook None None None None 2024-05-14 15:58:22.551328+00:00 2024-05-14 15:58:22.551342+00:00 1
60 None Uhhnv7pkCuHw Igd Place cells IgG4 research Lugaro cells. None None notebook None None None None 2024-05-14 15:58:22.556112+00:00 2024-05-14 15:58:22.556125+00:00 1
62 None XxlF3SX1pNSr Cecum IgE Magnocellular neurosecretory cells r... None None notebook None None None None 2024-05-14 15:58:22.556419+00:00 2024-05-14 15:58:22.556432+00:00 1
64 None vMinMGdDTaZb Intestine research Thyroid gland Satellite gli... None None notebook None None None None 2024-05-14 15:58:22.556724+00:00 2024-05-14 15:58:22.556738+00:00 1
68 None ZAtFIC9Us6Xq Research candidate IgE rank Thyrotropes IgG3 r... None None notebook None None None None 2024-05-14 15:58:22.557338+00:00 2024-05-14 15:58:22.557352+00:00 1
127 None HLfhGnAsW8Wv Igm Cecum IgD Cecum research IgG1 IgE. None None notebook None None None None 2024-05-14 15:58:22.569350+00:00 2024-05-14 15:58:22.569363+00:00 1
140 None y22fNojjA2gj Visualize IgG1 IgG3 intestinal Place cells res... None None notebook None None None None 2024-05-14 15:58:22.571245+00:00 2024-05-14 15:58:22.571258+00:00 1
143 None E6w3xYRP8I83 Igm IgG2 IgG1 research Lugaro cells Place cell... None None notebook None None None None 2024-05-14 15:58:22.571698+00:00 2024-05-14 15:58:22.571712+00:00 1

And case-insensitive:

ln.Transform.filter(name__icontains="Search").df().head(10)
version uid name key description type latest_report_id source_code_id reference reference_type created_at updated_at created_by_id
id
15 None hv0sP6hmZpW3 Visualize IgE IgE Gonadotropes research Cecum ... None None notebook None None None None 2024-05-14 15:58:22.549176+00:00 2024-05-14 15:58:22.549190+00:00 1
21 None TM5HXVrjWSux Research Cajal–Retzius cells neurotensin-secre... None None notebook None None None None 2024-05-14 15:58:22.550101+00:00 2024-05-14 15:58:22.550114+00:00 1
29 None jo75RtfsbhoB Cluster efficiency Satellite glial cells IgG2 ... None None notebook None None None None 2024-05-14 15:58:22.551328+00:00 2024-05-14 15:58:22.551342+00:00 1
60 None Uhhnv7pkCuHw Igd Place cells IgG4 research Lugaro cells. None None notebook None None None None 2024-05-14 15:58:22.556112+00:00 2024-05-14 15:58:22.556125+00:00 1
62 None XxlF3SX1pNSr Cecum IgE Magnocellular neurosecretory cells r... None None notebook None None None None 2024-05-14 15:58:22.556419+00:00 2024-05-14 15:58:22.556432+00:00 1
64 None vMinMGdDTaZb Intestine research Thyroid gland Satellite gli... None None notebook None None None None 2024-05-14 15:58:22.556724+00:00 2024-05-14 15:58:22.556738+00:00 1
68 None ZAtFIC9Us6Xq Research candidate IgE rank Thyrotropes IgG3 r... None None notebook None None None None 2024-05-14 15:58:22.557338+00:00 2024-05-14 15:58:22.557352+00:00 1
127 None HLfhGnAsW8Wv Igm Cecum IgD Cecum research IgG1 IgE. None None notebook None None None None 2024-05-14 15:58:22.569350+00:00 2024-05-14 15:58:22.569363+00:00 1
140 None y22fNojjA2gj Visualize IgG1 IgG3 intestinal Place cells res... None None notebook None None None None 2024-05-14 15:58:22.571245+00:00 2024-05-14 15:58:22.571258+00:00 1
143 None E6w3xYRP8I83 Igm IgG2 IgG1 research Lugaro cells Place cell... None None notebook None None None None 2024-05-14 15:58:22.571698+00:00 2024-05-14 15:58:22.571712+00:00 1

startswith

ln.Transform.filter(name__startswith="Research").df()
version uid name key description type latest_report_id source_code_id reference reference_type created_at updated_at created_by_id
id
21 None TM5HXVrjWSux Research Cajal–Retzius cells neurotensin-secre... None None notebook None None None None 2024-05-14 15:58:22.550101+00:00 2024-05-14 15:58:22.550114+00:00 1
68 None ZAtFIC9Us6Xq Research candidate IgE rank Thyrotropes IgG3 r... None None notebook None None None None 2024-05-14 15:58:22.557338+00:00 2024-05-14 15:58:22.557352+00:00 1
324 None Rl92KyRjHFTe Research IgG4 IgG Magnocellular neurosecretory... None None notebook None None None None 2024-05-14 15:58:22.605932+00:00 2024-05-14 15:58:22.605944+00:00 1
359 None 4jtL6Oz0DGPG Research IgG2 Thyrotropes Satellite glial cells. None None notebook None None None None 2024-05-14 15:58:22.610967+00:00 2024-05-14 15:58:22.610979+00:00 1
371 None 5iRYDFBwSrX8 Research result IgG2. None None notebook None None None None 2024-05-14 15:58:22.612702+00:00 2024-05-14 15:58:22.612715+00:00 1
380 None 5GQMJOPIaplh Research IgG3 visualize IgM Seminal vesicles r... None None notebook None None None None 2024-05-14 15:58:22.613992+00:00 2024-05-14 15:58:22.614005+00:00 1
Hide code cell content
# clean up test instance
!lamin delete --force mydata
!rm -r mydata
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.11.9/x64/bin/lamin", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/rich_click/rich_command.py", line 367, in __call__
    return super().__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/rich_click/rich_command.py", line 152, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/lamin_cli/__main__.py", line 103, in delete
    return delete(instance, force=force)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/lamindb_setup/_delete.py", line 97, in delete
    n_objects = check_storage_is_empty(
                ^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/lamindb_setup/core/upath.py", line 824, in check_storage_is_empty
    raise InstanceNotEmpty(message)
lamindb_setup.core.upath.InstanceNotEmpty: Storage /home/runner/work/lamindb/lamindb/docs/mydata/.lamindb contains 3 objects ('_is_initialized' ignored) - delete them prior to deleting the instance
['/home/runner/work/lamindb/lamindb/docs/mydata/.lamindb/AvyYmHPTQjXzn0MaXwuK.parquet', '/home/runner/work/lamindb/lamindb/docs/mydata/.lamindb/_is_initialized', '/home/runner/work/lamindb/lamindb/docs/mydata/.lamindb/jnCSBSGTm8aQwWNGAgSy.fastq.gz', '/home/runner/work/lamindb/lamindb/docs/mydata/.lamindb/sXgR1PiJERA5fEwtMhol.jpg']