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
! record with similar description exists! did you mean to load it?
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 qFqzTcmtda3MH0ia0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-11-07 12:41:45.130242+00:00 1
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 pyRdT90enXMvOsoP0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-11-07 12:41:45.351442+00:00 1
2 qb1X6C9jfLsJ5R4R0000 None True The iris collection None .parquet dataset 5097 K1jn6pPlqIC6ebZQfW84NQ None None md5 DataFrame 1 True 1 None None 2024-11-07 12:41:45.269141+00:00 1
1 qFqzTcmtda3MH0ia0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-11-07 12:41:45.130242+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-11-07 12:41:41 UTC), dict=<bound method Lookup.dict of <lamin_utils._lookup.Lookup object at 0x7f502edb6750>>)

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-11-07 12:41:41 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-11-07 12:41:41 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-11-07 12:41:41 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 qFqzTcmtda3MH0ia0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-11-07 12:41:45.130242+00:00 1
2 qb1X6C9jfLsJ5R4R0000 None True The iris collection None .parquet dataset 5097 K1jn6pPlqIC6ebZQfW84NQ None None md5 DataFrame 1 True 1 None None 2024-11-07 12:41:45.269141+00:00 1
3 pyRdT90enXMvOsoP0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-11-07 12:41:45.351442+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 qb1X6C9jfLsJ5R4R0000 None True The iris collection None .parquet dataset 5097 K1jn6pPlqIC6ebZQfW84NQ None None md5 DataFrame 1 True 1 None None 2024-11-07 12:41:45.269141+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
91 eSmM4lAwQIY40000 None True Intestine IgY IgG. None None notebook None None None None None 2024-11-07 12:42:02.617762+00:00 1
404 azkueeUEi0Ci0000 None True Intestine investigate IgA rank. None None notebook None None None None None 2024-11-07 12:42:02.665040+00:00 1
406 fDech7Hsmw7z0000 None True Intestine IgG Head direction cells IgG. None None notebook None None None None None 2024-11-07 12:42:02.665234+00:00 1
410 6zz4nHufCzXQ0000 None True Intestine rank IgG IgG1 IgG IgY cluster. None None notebook None None None None None 2024-11-07 12:42:02.665616+00:00 1
84 SXFHv4U8K2OM0000 None True Intestine candidate Erythrocyte efficiency. None None notebook None None None None None 2024-11-07 12:42:02.617123+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 qFqzTcmtda3MH0ia0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-11-07 12:41:45.130242+00:00 1
2 qb1X6C9jfLsJ5R4R0000 None True The iris collection None .parquet dataset 5097 K1jn6pPlqIC6ebZQfW84NQ None None md5 DataFrame 1 True 1 None None 2024-11-07 12:41:45.269141+00:00 1
3 pyRdT90enXMvOsoP0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-11-07 12:41:45.351442+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 qFqzTcmtda3MH0ia0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-11-07 12:41:45.130242+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 qb1X6C9jfLsJ5R4R0000 None True The iris collection None .parquet dataset 5097 K1jn6pPlqIC6ebZQfW84NQ None None md5 DataFrame 1 True 1 None None 2024-11-07 12:41:45.269141+00:00 1
3 pyRdT90enXMvOsoP0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-11-07 12:41:45.351442+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 qFqzTcmtda3MH0ia0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-11-07 12:41:45.130242+00:00 1
3 pyRdT90enXMvOsoP0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-11-07 12:41:45.351442+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 pyRdT90enXMvOsoP0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-11-07 12:41:45.351442+00:00 1
2 qb1X6C9jfLsJ5R4R0000 None True The iris collection None .parquet dataset 5097 K1jn6pPlqIC6ebZQfW84NQ None None md5 DataFrame 1 True 1 None None 2024-11-07 12:41:45.269141+00:00 1
1 qFqzTcmtda3MH0ia0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-11-07 12:41:45.130242+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
38 W0CPQqVfDdrb0000 None True Igm IgG3 investigate IgY Bulbourethral glands ... None None notebook None None None None None 2024-11-07 12:42:02.608620+00:00 1
42 9313WsLeUU0q0000 None True Rank Intercalated duct IgG research. None None notebook None None None None None 2024-11-07 12:42:02.608996+00:00 1
44 L9ZPQQ6Fuj0E0000 None True Classify IgD Bulbourethral glands research IgG... None None notebook None None None None None 2024-11-07 12:42:02.609185+00:00 1
58 kyHT6GdK4IHT0000 None True Rank IgE research Bulbourethral glands Gall bl... None None notebook None None None None None 2024-11-07 12:42:02.610501+00:00 1
60 Tp2W4HJk7HNJ0000 None True Igg IgG3 IgG1 research IgG4 IgG4 Pituicytes. None None notebook None None None None None 2024-11-07 12:42:02.610690+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
38 W0CPQqVfDdrb0000 None True Igm IgG3 investigate IgY Bulbourethral glands ... None None notebook None None None None None 2024-11-07 12:42:02.608620+00:00 1
42 9313WsLeUU0q0000 None True Rank Intercalated duct IgG research. None None notebook None None None None None 2024-11-07 12:42:02.608996+00:00 1
44 L9ZPQQ6Fuj0E0000 None True Classify IgD Bulbourethral glands research IgG... None None notebook None None None None None 2024-11-07 12:42:02.609185+00:00 1
58 kyHT6GdK4IHT0000 None True Rank IgE research Bulbourethral glands Gall bl... None None notebook None None None None None 2024-11-07 12:42:02.610501+00:00 1
60 Tp2W4HJk7HNJ0000 None True Igg IgG3 IgG1 research IgG4 IgG4 Pituicytes. None None notebook None None None None None 2024-11-07 12:42:02.610690+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
120 3H1rToRI9oCz0000 None True Research Bulbourethral glands IgG Diencephalon... None None notebook None None None None None 2024-11-07 12:42:02.620443+00:00 1
130 YTHYgLKbSyff0000 None True Research intestine Hyaline cartilage cluster. None None notebook None None None None None 2024-11-07 12:42:02.621359+00:00 1
148 OiX0fg7vTuBN0000 None True Research Pituicytes IgG4. None None notebook None None None None None 2024-11-07 12:42:02.626821+00:00 1
234 hmgpTR4yeMT40000 None True Research IgG efficiency IgG IgG. None None notebook None None None None None 2024-11-07 12:42:02.638328+00:00 1
359 QcuAK349LCLV0000 None True Research IgG4 IgG1 classify Inner phalangeal c... None None notebook None None None None None 2024-11-07 12:42:02.657089+00:00 1
375 k1kuGTSNAT5Z0000 None True Research efficiency IgG1 IgG Choroid plexus. None None notebook None None None None None 2024-11-07 12:42:02.658580+00:00 1
434 pHomvgPcg6gX0000 None True Research research Diencephalon IgG Head direct... None None notebook None None None None None 2024-11-07 12:42:02.667904+00:00 1
441 TdwTjX0Mx3VW0000 None True Research IgY IgY Intercalated duct IgG IgG4 Ig... None None notebook None None None None None 2024-11-07 12:42:02.668563+00:00 1
483 OyfdwVWrhRPu0000 None True Research intestine IgG4. None None notebook None None None None None 2024-11-07 12:42:02.676145+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 qFqzTcmtda3MH0ia0000 None True My image None .jpg None 29358 r4tnqmKI_SjrkdLzpuWp4g None None md5 None 1 True 1 None None 2024-11-07 12:41:45.130242+00:00 1
3 pyRdT90enXMvOsoP0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-11-07 12:41:45.351442+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 qb1X6C9jfLsJ5R4R0000 None True The iris collection None .parquet dataset 5097 K1jn6pPlqIC6ebZQfW84NQ None None md5 DataFrame 1 True 1 None None 2024-11-07 12:41:45.269141+00:00 1
3 pyRdT90enXMvOsoP0000 None True My fastq None .fastq.gz None 20 hi7ZmAzz8sfMd3vIQr-57Q None None md5 None 1 True 1 None None 2024-11-07 12:41:45.351442+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