Query & search registries

This guide walks through all the ways of finding metadata records in LaminDB registries.

# !pip install lamindb
!lamin init --storage ./test-registries
Hide code cell output
→ connected lamindb: testuser1/test-registries

We’ll need some toy data.

import lamindb as ln

# create 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()

# see the content of the artifact registry
ln.Artifact.df()
Hide code cell output
→ connected lamindb: testuser1/test-registries
! no run & transform got linked, call `ln.track()` & re-run
! no run & transform got linked, call `ln.track()` & re-run
! no run & transform got linked, call `ln.track()` & re-run
uid version is_latest description key suffix type size hash n_objects n_observations _hash_type _accessor visibility _key_is_virtual storage_id transform_id run_id created_at created_by_id
id
3 n3YkRbDIoDmG9a6I0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-10-21 07:29:02.142869+00:00 1
2 Td3xTC0k09B0GhxN0000 None True The iris collection None .parquet dataset 5629 5axHfO_2Pmlun2sJZHVuNg None None md5 DataFrame 1 True 1 None None 2024-10-21 07:29:02.136594+00:00 1
1 1JC6UWvi9o96Fyrt0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-10-21 07:29:01.996933+00:00 1

Look up metadata

For registries with less than 100k records, auto-completing a Lookup object is the most convenient way of finding a record.

For example, take the User registry:

# query the database for all users, optionally pass the field that creates the key
users = ln.User.lookup(field="handle")

# the lookup object is a NamedTuple
users
Hide code cell output
Lookup(testuser1=User(uid='DzTjkKse', handle='testuser1', name='Test User1', created_at=2024-10-21 07:29:00 UTC), dict=<bound method Lookup.dict of <lamin_utils._lookup.Lookup object at 0x7f62a827ac90>>)

With auto-complete, we find a specific user record:

user = users.testuser1
user
Hide code cell output
User(uid='DzTjkKse', handle='testuser1', name='Test User1', created_at=2024-10-21 07:29:00 UTC)

You can also get a dictionary:

users_dict = ln.User.lookup().dict()
users_dict
Hide code cell output
{'testuser1': User(uid='DzTjkKse', handle='testuser1', name='Test User1', created_at=2024-10-21 07:29:00 UTC)}

Query exactly one record

get errors if more than one matching records are found.

# by the universal base62 uid
ln.User.get("DzTjkKse")

# by any expression involving fields
ln.User.get(handle="testuser1")
Hide code cell output
User(uid='DzTjkKse', handle='testuser1', name='Test User1', created_at=2024-10-21 07:29:00 UTC)

Query sets of records

Filter for all artifacts created by a user:

ln.Artifact.filter(created_by=user).df()
Hide code cell output
uid version is_latest description key suffix type size hash n_objects n_observations _hash_type _accessor visibility _key_is_virtual storage_id transform_id run_id created_at created_by_id
id
1 1JC6UWvi9o96Fyrt0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-10-21 07:29:01.996933+00:00 1
2 Td3xTC0k09B0GhxN0000 None True The iris collection None .parquet dataset 5629 5axHfO_2Pmlun2sJZHVuNg None None md5 DataFrame 1 True 1 None None 2024-10-21 07:29:02.136594+00:00 1
3 n3YkRbDIoDmG9a6I0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-10-21 07:29:02.142869+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 in a row.

  • .all(): A QuerySet.

  • .one(): Exactly one record. Will raise an error if there is none. Is equivalent to the .get() method shown above.

  • .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 records

Search the toy data:

ln.Artifact.search("iris").df()
Hide code cell output
uid version is_latest description key suffix type size hash n_objects n_observations _hash_type _accessor visibility _key_is_virtual storage_id transform_id run_id created_at created_by_id
id
2 Td3xTC0k09B0GhxN0000 None True The iris collection None .parquet dataset 5629 5axHfO_2Pmlun2sJZHVuNg None None md5 DataFrame 1 True 1 None None 2024-10-21 07:29:02.136594+00:00 1

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

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

# search
ln.Transform.search("intestine").df().head(5)
Hide code cell output
uid version is_latest name key description type source_code hash reference reference_type _source_code_artifact_id created_at created_by_id
id
15 ABh66VqafNOq0000 None True Igy IgG IgE Neurogliaform cells IgG1 intestine... None None notebook None None None None None 2024-10-21 07:29:03.703865+00:00 1
20 9dc61nnQnOmE0000 None True Spermatozoon Spermatozoon IgA Pancreas intestine. None None notebook None None None None None 2024-10-21 07:29:03.704180+00:00 1
46 cYA1XgTUz7PW0000 None True Intestine intestine IgE Eosinophil granulocyte... None None notebook None None None None None 2024-10-21 07:29:03.705880+00:00 1
58 I5WoOp163MA10000 None True Igg3 intestine Pancreas Nucleus pulposus cell ... None None notebook None None None None None 2024-10-21 07:29:03.706682+00:00 1
64 SJiQXnokuLRa0000 None True Eosinophil Granulocyte Ascending colon Neurogl... None None notebook None None None None None 2024-10-21 07:29:03.707057+00:00 1

Note

Currently, the LaminHub UI search is more powerful than the search of the lamindb open-source package.

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 and leverage different comparators.

ln.Artifact.filter(created_by__handle__startswith="testuse").df()  
Hide code cell output
uid version is_latest description key suffix type size hash n_objects n_observations _hash_type _accessor visibility _key_is_virtual storage_id transform_id run_id created_at created_by_id
id
1 1JC6UWvi9o96Fyrt0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-10-21 07:29:01.996933+00:00 1
2 Td3xTC0k09B0GhxN0000 None True The iris collection None .parquet dataset 5629 5axHfO_2Pmlun2sJZHVuNg None None md5 DataFrame 1 True 1 None None 2024-10-21 07:29:02.136594+00:00 1
3 n3YkRbDIoDmG9a6I0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-10-21 07:29:02.142869+00:00 1

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.

Comparators

You can qualify the type of comparison in a query by using a comparator.

Below follows a list of the most import, but Django supports about two dozen field comparators field__comparator=value.

and

ln.Artifact.filter(suffix=".jpg", created_by=user).df()
Hide code cell output
uid version is_latest description key suffix type size hash n_objects n_observations _hash_type _accessor visibility _key_is_virtual storage_id transform_id run_id created_at created_by_id
id
1 1JC6UWvi9o96Fyrt0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-10-21 07:29:01.996933+00:00 1

less than/ greater than

Or subset to artifacts smaller 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()
Hide code cell output
uid version is_latest description key suffix type size hash n_objects n_observations _hash_type _accessor visibility _key_is_virtual storage_id transform_id run_id created_at created_by_id
id
2 Td3xTC0k09B0GhxN0000 None True The iris collection None .parquet dataset 5629 5axHfO_2Pmlun2sJZHVuNg None None md5 DataFrame 1 True 1 None None 2024-10-21 07:29:02.136594+00:00 1
3 n3YkRbDIoDmG9a6I0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-10-21 07:29:02.142869+00:00 1

in

ln.Artifact.filter(suffix__in=[".jpg", ".fastq.gz"]).df()
Hide code cell output
uid version is_latest description key suffix type size hash n_objects n_observations _hash_type _accessor visibility _key_is_virtual storage_id transform_id run_id created_at created_by_id
id
1 1JC6UWvi9o96Fyrt0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-10-21 07:29:01.996933+00:00 1
3 n3YkRbDIoDmG9a6I0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-10-21 07:29:02.142869+00:00 1

order by

ln.Artifact.filter().order_by("-updated_at").df()
Hide code cell output
uid version is_latest description key suffix type size hash n_objects n_observations _hash_type _accessor visibility _key_is_virtual storage_id transform_id run_id created_at created_by_id
id
3 n3YkRbDIoDmG9a6I0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-10-21 07:29:02.142869+00:00 1
2 Td3xTC0k09B0GhxN0000 None True The iris collection None .parquet dataset 5629 5axHfO_2Pmlun2sJZHVuNg None None md5 DataFrame 1 True 1 None None 2024-10-21 07:29:02.136594+00:00 1
1 1JC6UWvi9o96Fyrt0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-10-21 07:29:01.996933+00:00 1

contains

ln.Transform.filter(name__contains="search").df().head(5)
Hide code cell output
uid version is_latest name key description type source_code hash reference reference_type _source_code_artifact_id created_at created_by_id
id
1 AumwfndINPAu0000 None True Research efficiency Mammary glands IgG3 Bronch... None None notebook None None None None None 2024-10-21 07:29:03.702881+00:00 1
7 iIk44RNQExAF0000 None True Spermatozoon classify Goblet cell Bowman's gla... None None notebook None None None None None 2024-10-21 07:29:03.703329+00:00 1
60 wukQ6a9AFfB20000 None True Ige research IgG Eosinophil granulocyte Neurog... None None notebook None None None None None 2024-10-21 07:29:03.706807+00:00 1
70 ufyTNwoXJah40000 None True Taste Receptor Cells IgG3 IgD IgG Efferent duc... None None notebook None None None None None 2024-10-21 07:29:03.710440+00:00 1
72 E7UNgEIbnsjL0000 None True Eosinophil Granulocyte IgY research Goblet cel... None None notebook None None None None None 2024-10-21 07:29:03.710559+00:00 1

And case-insensitive:

ln.Transform.filter(name__icontains="Search").df().head(5)
Hide code cell output
uid version is_latest name key description type source_code hash reference reference_type _source_code_artifact_id created_at created_by_id
id
1 AumwfndINPAu0000 None True Research efficiency Mammary glands IgG3 Bronch... None None notebook None None None None None 2024-10-21 07:29:03.702881+00:00 1
7 iIk44RNQExAF0000 None True Spermatozoon classify Goblet cell Bowman's gla... None None notebook None None None None None 2024-10-21 07:29:03.703329+00:00 1
60 wukQ6a9AFfB20000 None True Ige research IgG Eosinophil granulocyte Neurog... None None notebook None None None None None 2024-10-21 07:29:03.706807+00:00 1
70 ufyTNwoXJah40000 None True Taste Receptor Cells IgG3 IgD IgG Efferent duc... None None notebook None None None None None 2024-10-21 07:29:03.710440+00:00 1
72 E7UNgEIbnsjL0000 None True Eosinophil Granulocyte IgY research Goblet cel... None None notebook None None None None None 2024-10-21 07:29:03.710559+00:00 1

startswith

ln.Transform.filter(name__startswith="Research").df()
Hide code cell output
uid version is_latest name key description type source_code hash reference reference_type _source_code_artifact_id created_at created_by_id
id
1 AumwfndINPAu0000 None True Research efficiency Mammary glands IgG3 Bronch... None None notebook None None None None None 2024-10-21 07:29:03.702881+00:00 1
75 tIjHUWOqk4Uq0000 None True Research IgD IgG3 intestine IgD Nucleus pulpos... None None notebook None None None None None 2024-10-21 07:29:03.710741+00:00 1
201 8JXdHkLCdDzh0000 None True Research IgE classify efficiency IgG. None None notebook None None None None None 2024-10-21 07:29:03.723494+00:00 1
236 ugJsNkuGKzYq0000 None True Research intestinal Bronchi IgA IgG3 cluster. None None notebook None None None None None 2024-10-21 07:29:03.725566+00:00 1
256 DSoDHGM4l9na0000 None True Research IgY IgA candidate. None None notebook None None None None None 2024-10-21 07:29:03.726733+00:00 1
259 GAWZj62NOZ6T0000 None True Research IgA Neurogliaform cells visualize. None None notebook None None None None None 2024-10-21 07:29:03.726908+00:00 1
323 VqQSMy7NDmVE0000 None True Research candidate Bowman's gland. None None notebook None None None None None 2024-10-21 07:29:03.733133+00:00 1
377 pPUHpG9i4Uvx0000 None True Research Taste receptor cells IgM IgD Goblet c... None None notebook None None None None None 2024-10-21 07:29:03.738859+00:00 1
441 GwWjhxlyMbft0000 None True Research IgG3 Nucleus pulposus cell IgG3. None None notebook None None None None None 2024-10-21 07:29:03.745191+00:00 1
468 LKP2st00Ml6n0000 None True Research cluster IgG3 research result IgY inte... None None notebook None None None None None 2024-10-21 07:29:03.749281+00:00 1
477 7gFYWy4veex90000 None True Research Nucleus pulposus cell Skin IgG IgE re... None None notebook None None None None None 2024-10-21 07:29:03.749822+00:00 1
490 vUXupVFcx5nU0000 None True Research research intestinal. None None notebook None None None None None 2024-10-21 07:29:03.750590+00:00 1

or

ln.Artifact.filter(ln.Q(suffix=".jpg") | ln.Q(suffix=".fastq.gz")).df()
Hide code cell output
uid version is_latest description key suffix type size hash n_objects n_observations _hash_type _accessor visibility _key_is_virtual storage_id transform_id run_id created_at created_by_id
id
1 1JC6UWvi9o96Fyrt0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-10-21 07:29:01.996933+00:00 1
3 n3YkRbDIoDmG9a6I0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-10-21 07:29:02.142869+00:00 1

negate/ unequal

ln.Artifact.filter(~ln.Q(suffix=".jpg")).df()
Hide code cell output
uid version is_latest description key suffix type size hash n_objects n_observations _hash_type _accessor visibility _key_is_virtual storage_id transform_id run_id created_at created_by_id
id
2 Td3xTC0k09B0GhxN0000 None True The iris collection None .parquet dataset 5629 5axHfO_2Pmlun2sJZHVuNg None None md5 DataFrame 1 True 1 None None 2024-10-21 07:29:02.136594+00:00 1
3 n3YkRbDIoDmG9a6I0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-10-21 07:29:02.142869+00:00 1

Clean up the test instance.

!rm -r ./test-registries
!lamin delete --force test-registries
Hide code cell output
• deleting instance testuser1/test-registries