What does the key parameter do under the hood?¶
LaminDB is designed around associating biological metadata to artifacts and collections. This enables querying for them in storage by metadata and removes the requirement for semantic artifact and collection names.
Here, we will discuss trade-offs for using the key
parameter, which allows for semantic keys, in various scenarios.
We’re simulating an artifact system with several nested folders and artifacts. Such structures are resembled in, for example, the RxRx: cell imaging guide.
# !pip install 'lamindb[jupyter]'
import random
import string
from pathlib import Path
def create_complex_biological_hierarchy(root_folder):
root_path = Path(root_folder)
if root_path.exists():
print("Folder structure already exists. Skipping...")
else:
root_path.mkdir()
raw_folder = root_path / "raw"
preprocessed_folder = root_path / "preprocessed"
raw_folder.mkdir()
preprocessed_folder.mkdir()
for i in range(1, 5):
artifact_name = f"raw_data_{i}.txt"
with (raw_folder / artifact_name).open("w") as f:
random_text = "".join(
random.choice(string.ascii_letters) for _ in range(10)
)
f.write(random_text)
for i in range(1, 3):
collection_folder = raw_folder / f"Collection_{i}"
collection_folder.mkdir()
for j in range(1, 5):
artifact_name = f"raw_data_{j}.txt"
with (collection_folder / artifact_name).open("w") as f:
random_text = "".join(
random.choice(string.ascii_letters) for _ in range(10)
)
f.write(random_text)
for i in range(1, 5):
artifact_name = f"result_{i}.txt"
with (preprocessed_folder / artifact_name).open("w") as f:
random_text = "".join(
random.choice(string.ascii_letters) for _ in range(10)
)
f.write(random_text)
root_folder = "complex_biological_project"
create_complex_biological_hierarchy(root_folder)
!lamin init --storage ./key-eval
→ connected lamindb: testuser1/key-eval
import lamindb as ln
ln.settings.verbosity = "hint"
→ connected lamindb: testuser1/key-eval
ln.UPath("complex_biological_project").view_tree()
4 sub-directories & 8 files with suffixes '.txt'
/home/runner/work/lamindb/lamindb/docs/faq/complex_biological_project
├── raw/
│ ├── Collection_1/
│ ├── raw_data_4.txt
│ ├── Collection_2/
│ ├── raw_data_3.txt
│ ├── raw_data_1.txt
│ └── raw_data_2.txt
└── preprocessed/
├── result_2.txt
├── result_4.txt
├── result_3.txt
└── result_1.txt
ln.track("WIwaNDvlEkwS0000")
→ notebook imports: lamindb==0.76.13
• tracked pip freeze > /home/runner/.cache/lamindb/run_env_pip_VpCv0g2iluX2Z6JmgMVH.txt
→ created Transform('WIwaNDvl'), started new Run('VpCv0g2i') at 2024-10-18 15:57:28 UTC
Storing artifacts using Storage
, File
, and Collection
¶
Lamin has three storage classes that manage different types of in-memory and on-disk objects:
Storage
: Manages the default storage root that can be either local or in the cloud. For more details we refer to Storage FAQ.Artifact
: Manages datasets with an optionalkey
that acts as a relative path within the current default storage root (seeStorage
). An example is a single h5 artifact.Collection
: Manages a collection of datasets with an optionalkey
that acts as a relative path within the current default storage root (seeStorage
). An example is a collection of h5 artifacts.
For more details we refer to Tutorial: Artifacts.
The current storage root is:
ln.settings.storage
StorageSettings(root='/home/runner/work/lamindb/lamindb/docs/faq/key-eval', uid='yKwgXjqHYUcH')
By default, Lamin uses virtual keys
that are only reflected in the database but not in storage.
It is possible to turn this behavior off by setting ln.settings.creation._artifact_use_virtual_keys = False
.
Generally, we discourage disabling this setting manually. For more details we refer to Storage FAQ.
ln.settings.creation._artifact_use_virtual_keys
True
We will now create File
objects with and without semantic keys using key
and also save them as Collections
.
artifact_no_key_1 = ln.Artifact("complex_biological_project/raw/raw_data_1.txt")
artifact_no_key_2 = ln.Artifact("complex_biological_project/raw/raw_data_2.txt")
• path content will be copied to default storage upon `save()` with key `None` ('.lamindb/LpuQzN4zSfuQVzv50000.txt')
• path content will be copied to default storage upon `save()` with key `None` ('.lamindb/BhULphrzX7uL4t0m0000.txt')
The logging suggests that the artifacts will be saved to our current default storage with auto generated storage keys.
artifact_no_key_1.save()
artifact_no_key_2.save()
✓ storing artifact 'LpuQzN4zSfuQVzv50000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/LpuQzN4zSfuQVzv50000.txt'
✓ storing artifact 'BhULphrzX7uL4t0m0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/BhULphrzX7uL4t0m0000.txt'
Artifact(uid='BhULphrzX7uL4t0m0000', is_latest=True, suffix='.txt', size=10, hash='xgo6vEZNxoGdFJVyZFplZQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
artifact_key_3 = ln.Artifact(
"complex_biological_project/raw/raw_data_3.txt", key="raw/raw_data_3.txt"
)
artifact_key_4 = ln.Artifact(
"complex_biological_project/raw/raw_data_4.txt", key="raw/raw_data_4.txt"
)
artifact_key_3.save()
artifact_key_4.save()
• path content will be copied to default storage upon `save()` with key 'raw/raw_data_3.txt'
• path content will be copied to default storage upon `save()` with key 'raw/raw_data_4.txt'
✓ storing artifact 'ta1ZIkrcraKQUpcc0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/ta1ZIkrcraKQUpcc0000.txt'
✓ storing artifact 'rw9bmNrJArg01Vcp0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/rw9bmNrJArg01Vcp0000.txt'
Artifact(uid='rw9bmNrJArg01Vcp0000', is_latest=True, key='raw/raw_data_4.txt', suffix='.txt', size=10, hash='oC-Af8mM34pE06YAYc5INQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
Files
with keys are not stored in different locations because of the usage of virtual keys
.
However, they are still semantically queryable by key
.
ln.Artifact.filter(key__contains="raw").df().head()
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 | ta1ZIkrcraKQUpcc0000 | None | True | None | raw/raw_data_3.txt | .txt | None | 10 | 30oxgeh1vlNv3e67FuJpDg | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.296021+00:00 | 1 |
4 | rw9bmNrJArg01Vcp0000 | None | True | None | raw/raw_data_4.txt | .txt | None | 10 | oC-Af8mM34pE06YAYc5INQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.300355+00:00 | 1 |
Collection
does not have a key
parameter because it does not store any additional data in Storage
.
In contrast, it has a name
parameter that serves as a semantic identifier of the collection.
ds_1 = ln.Collection([artifact_no_key_1, artifact_no_key_2], name="no key collection")
ds_2 = ln.Collection([artifact_key_3, artifact_key_4], name="sample collection")
ds_1
Collection(uid='TbOrlpUu0U7WWba30000', is_latest=True, name='no key collection', hash='yD0XegPaofszGrox2MYdLA', visibility=1, created_by_id=1, transform_id=1, run_id=1)
Advantages and disadvantages of semantic keys¶
Semantic keys have several advantages and disadvantages that we will discuss and demonstrate in the remaining notebook:
Advantages:¶
Simple: It can be easier to refer to specific collections in conversations
Familiarity: Most people are familiar with the concept of semantic names
Disadvantages¶
Length: Semantic names can be long with limited aesthetic appeal
Inconsistency: Lack of naming conventions can lead to confusion
Limited metadata: Semantic keys can contain some, but usually not all metadata
Inefficiency: Writing lengthy semantic names is a repetitive process and can be time-consuming
Ambiguity: Overly descriptive artifact names may introduce ambiguity and redundancy
Clashes: Several people may attempt to use the same semantic key. They are not unique
Renaming artifacts¶
Renaming Files
that have associated keys can be done on several levels.
In storage¶
A artifact can be locally moved or renamed:
artifact_key_3.path
PosixUPath('/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/ta1ZIkrcraKQUpcc0000.txt')
loaded_artifact = artifact_key_3.load()
!mkdir complex_biological_project/moved_artifacts
!mv complex_biological_project/raw/raw_data_3.txt complex_biological_project/moved_artifacts
artifact_key_3.path
PosixUPath('/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/ta1ZIkrcraKQUpcc0000.txt')
After moving the artifact locally, the storage location (the path) has not changed and the artifact can still be loaded.
artifact_3 = artifact_key_3.load()
The same applies to the key
which has not changed.
artifact_key_3.key
'raw/raw_data_3.txt'
By key¶
Besides moving the artifact in storage, the key
can also be renamed.
artifact_key_4.key
'raw/raw_data_4.txt'
artifact_key_4.key = "bad_samples/sample_data_4.txt"
artifact_key_4.key
'bad_samples/sample_data_4.txt'
Due to the usage of virtual keys
, modifying the key does not change the storage location and the artifact stays accessible.
artifact_key_4.path
PosixUPath('/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/rw9bmNrJArg01Vcp0000.txt')
artifact_4 = artifact_key_4.load()
Modifying the path
attribute¶
However, modifying the path
directly is not allowed:
try:
artifact_key_4.path = f"{ln.settings.storage}/here_now/sample_data_4.txt"
except AttributeError as e:
print(e)
property of 'Artifact' object has no setter
Clashing semantic keys¶
Semantic keys should not clash. Let’s attempt to use the same semantic key twice
print(artifact_key_3.key)
print(artifact_key_4.key)
raw/raw_data_3.txt
bad_samples/sample_data_4.txt
artifact_key_4.key = "raw/raw_data_3.txt"
print(artifact_key_3.key)
print(artifact_key_4.key)
raw/raw_data_3.txt
raw/raw_data_3.txt
When filtering for this semantic key it is now unclear to which artifact we were referring to:
ln.Artifact.filter(key__icontains="sample_data_3").df()
! No records found
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 |
When querying by key
LaminDB cannot resolve which artifact we actually wanted.
In fact, we only get a single hit which does not paint a complete picture.
print(artifact_key_3.uid)
print(artifact_key_4.uid)
ta1ZIkrcraKQUpcc0000
rw9bmNrJArg01Vcp0000
Both artifacts still exist though with unique uids
that can be used to get access to them.
Most importantly though, saving these artifacts to the database will result in an IntegrityError
to prevent this issue.
try:
artifact_key_3.save()
artifact_key_4.save()
except Exception as e:
print(
"It is not possible to save artifacts to the same key. This results in an"
" Integrity Error!"
)
We refer to What happens if I save the same artifacts & records twice? for more detailed explanations of behavior when attempting to save artifacts multiple times.
Hierarchies¶
Another common use-case of keys
are artifact hierarchies.
It can be useful to resemble the artifact structure in “complex_biological_project” from above also in LaminDB to allow for queries for artifacts that were stored in specific folders.
Common examples of this are folders specifying different processing stages such as raw
, preprocessed
, or curated
.
Note that this use-case may also be overlapping with Collection
which also allows for grouping Files
.
However, Collection
cannot model hierarchical groupings.
Key¶
import os
for root, _, artifacts in os.walk("complex_biological_project/raw"):
for artifactname in artifacts:
file_path = os.path.join(root, artifactname)
key_path = file_path.removeprefix("complex_biological_project")
ln_artifact = ln.Artifact(file_path, key=key_path)
ln_artifact.save()
→ returning existing artifact with same hash: Artifact(uid='rw9bmNrJArg01Vcp0000', is_latest=True, key='raw/raw_data_3.txt', suffix='.txt', size=10, hash='oC-Af8mM34pE06YAYc5INQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
! key raw/raw_data_3.txt on existing artifact differs from passed key /raw/raw_data_4.txt
→ returning existing artifact with same hash: Artifact(uid='LpuQzN4zSfuQVzv50000', is_latest=True, suffix='.txt', size=10, hash='EPIH3fUb7rUOOIvkhdEiSw', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
! key None on existing artifact differs from passed key /raw/raw_data_1.txt
→ returning existing artifact with same hash: Artifact(uid='BhULphrzX7uL4t0m0000', is_latest=True, suffix='.txt', size=10, hash='xgo6vEZNxoGdFJVyZFplZQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
! key None on existing artifact differs from passed key /raw/raw_data_2.txt
• path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_4.txt'
✓ storing artifact '2MSurueQ2YtdCPhl0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/2MSurueQ2YtdCPhl0000.txt'
• path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_3.txt'
✓ storing artifact 'yjN2XPi9wY0P9vFC0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/yjN2XPi9wY0P9vFC0000.txt'
• path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_1.txt'
✓ storing artifact '5axfyFWb8Ns9hMXy0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/5axfyFWb8Ns9hMXy0000.txt'
• path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_2.txt'
✓ storing artifact 'tVkDdiDtlsNezrgz0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/tVkDdiDtlsNezrgz0000.txt'
• path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_4.txt'
✓ storing artifact 'R1jPyJB9qy6XzRgY0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/R1jPyJB9qy6XzRgY0000.txt'
• path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_3.txt'
✓ storing artifact 'xIGmLhto51qHrZ8Y0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/xIGmLhto51qHrZ8Y0000.txt'
• path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_1.txt'
✓ storing artifact 'nIZipP6uB85uf3aj0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/nIZipP6uB85uf3aj0000.txt'
• path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_2.txt'
✓ storing artifact 'HQPVHq7MnZeHymBq0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/HQPVHq7MnZeHymBq0000.txt'
ln.Artifact.filter(key__startswith="raw").df()
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 | ta1ZIkrcraKQUpcc0000 | None | True | None | raw/raw_data_3.txt | .txt | None | 10 | 30oxgeh1vlNv3e67FuJpDg | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.296021+00:00 | 1 |
4 | rw9bmNrJArg01Vcp0000 | None | True | None | raw/raw_data_3.txt | .txt | None | 10 | oC-Af8mM34pE06YAYc5INQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.300355+00:00 | 1 |
Collection¶
Alternatively, it would have been possible to create a Collection
with a corresponding name:
all_data_paths = []
for root, _, artifacts in os.walk("complex_biological_project/raw"):
for artifactname in artifacts:
file_path = os.path.join(root, artifactname)
all_data_paths.append(file_path)
all_data_artifacts = []
for path in all_data_paths:
all_data_artifacts.append(ln.Artifact(path))
data_ds = ln.Collection(all_data_artifacts, name="data")
data_ds.save()
→ returning existing artifact with same hash: Artifact(uid='rw9bmNrJArg01Vcp0000', is_latest=True, key='raw/raw_data_3.txt', suffix='.txt', size=10, hash='oC-Af8mM34pE06YAYc5INQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing artifact with same hash: Artifact(uid='LpuQzN4zSfuQVzv50000', is_latest=True, suffix='.txt', size=10, hash='EPIH3fUb7rUOOIvkhdEiSw', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing artifact with same hash: Artifact(uid='BhULphrzX7uL4t0m0000', is_latest=True, suffix='.txt', size=10, hash='xgo6vEZNxoGdFJVyZFplZQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing artifact with same hash: Artifact(uid='2MSurueQ2YtdCPhl0000', is_latest=True, key='/raw/Collection_1/raw_data_4.txt', suffix='.txt', size=10, hash='fxTvV35y6u4YdIJS7V5MYQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing artifact with same hash: Artifact(uid='yjN2XPi9wY0P9vFC0000', is_latest=True, key='/raw/Collection_1/raw_data_3.txt', suffix='.txt', size=10, hash='mN0xVCuqQEhquz9mCwl1pA', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing artifact with same hash: Artifact(uid='5axfyFWb8Ns9hMXy0000', is_latest=True, key='/raw/Collection_1/raw_data_1.txt', suffix='.txt', size=10, hash='lim6sj9HJnW-H4qgwcBMWQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing artifact with same hash: Artifact(uid='tVkDdiDtlsNezrgz0000', is_latest=True, key='/raw/Collection_1/raw_data_2.txt', suffix='.txt', size=10, hash='eouFP5e1LrinSOQ4qC6VVA', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing artifact with same hash: Artifact(uid='R1jPyJB9qy6XzRgY0000', is_latest=True, key='/raw/Collection_2/raw_data_4.txt', suffix='.txt', size=10, hash='JI6M1Kw_j7Z6xVSo3R1_8Q', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing artifact with same hash: Artifact(uid='xIGmLhto51qHrZ8Y0000', is_latest=True, key='/raw/Collection_2/raw_data_3.txt', suffix='.txt', size=10, hash='XaPa6ssA8nyzeB3Nlixi_Q', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing artifact with same hash: Artifact(uid='nIZipP6uB85uf3aj0000', is_latest=True, key='/raw/Collection_2/raw_data_1.txt', suffix='.txt', size=10, hash='qH27yHgMEBjYPHBNVFQ5WQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing artifact with same hash: Artifact(uid='HQPVHq7MnZeHymBq0000', is_latest=True, key='/raw/Collection_2/raw_data_2.txt', suffix='.txt', size=10, hash='UoPMBmntT-80ZptUD110eA', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
Collection(uid='4nWb83PBUDpImGAm0000', is_latest=True, name='data', hash='XXVSoFutPOdx-NOX8OUTjA', visibility=1, created_by_id=1, transform_id=1, run_id=1, created_at=2024-10-18 15:57:29 UTC)
ln.Collection.filter(name__icontains="data").df()
uid | version | is_latest | name | description | hash | reference | reference_type | visibility | transform_id | meta_artifact_id | run_id | created_at | created_by_id | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
id | ||||||||||||||
1 | 4nWb83PBUDpImGAm0000 | None | True | data | None | XXVSoFutPOdx-NOX8OUTjA | None | None | 1 | 1 | None | 1 | 2024-10-18 15:57:29.902667+00:00 | 1 |
This approach will likely lead to clashes. Alternatively, Ulabels
can be added to Files
to resemble hierarchies.
Ulabels¶
for root, _, artifacts in os.walk("complex_biological_project/raw"):
for artifactname in artifacts:
file_path = os.path.join(root, artifactname)
key_path = file_path.removeprefix("complex_biological_project")
ln_artifact = ln.Artifact(file_path, key=key_path)
ln_artifact.save()
data_label = ln.ULabel(name="data")
data_label.save()
ln_artifact.ulabels.add(data_label)
→ returning existing artifact with same hash: Artifact(uid='rw9bmNrJArg01Vcp0000', is_latest=True, key='raw/raw_data_3.txt', suffix='.txt', size=10, hash='oC-Af8mM34pE06YAYc5INQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
! key raw/raw_data_3.txt on existing artifact differs from passed key /raw/raw_data_4.txt
→ returning existing artifact with same hash: Artifact(uid='LpuQzN4zSfuQVzv50000', is_latest=True, suffix='.txt', size=10, hash='EPIH3fUb7rUOOIvkhdEiSw', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
! key None on existing artifact differs from passed key /raw/raw_data_1.txt
→ returning existing ULabel record with same name: 'data'
→ returning existing artifact with same hash: Artifact(uid='BhULphrzX7uL4t0m0000', is_latest=True, suffix='.txt', size=10, hash='xgo6vEZNxoGdFJVyZFplZQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
! key None on existing artifact differs from passed key /raw/raw_data_2.txt
→ returning existing ULabel record with same name: 'data'
→ returning existing artifact with same hash: Artifact(uid='2MSurueQ2YtdCPhl0000', is_latest=True, key='/raw/Collection_1/raw_data_4.txt', suffix='.txt', size=10, hash='fxTvV35y6u4YdIJS7V5MYQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing ULabel record with same name: 'data'
→ returning existing artifact with same hash: Artifact(uid='yjN2XPi9wY0P9vFC0000', is_latest=True, key='/raw/Collection_1/raw_data_3.txt', suffix='.txt', size=10, hash='mN0xVCuqQEhquz9mCwl1pA', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing ULabel record with same name: 'data'
→ returning existing artifact with same hash: Artifact(uid='5axfyFWb8Ns9hMXy0000', is_latest=True, key='/raw/Collection_1/raw_data_1.txt', suffix='.txt', size=10, hash='lim6sj9HJnW-H4qgwcBMWQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing ULabel record with same name: 'data'
→ returning existing artifact with same hash: Artifact(uid='tVkDdiDtlsNezrgz0000', is_latest=True, key='/raw/Collection_1/raw_data_2.txt', suffix='.txt', size=10, hash='eouFP5e1LrinSOQ4qC6VVA', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing ULabel record with same name: 'data'
→ returning existing artifact with same hash: Artifact(uid='R1jPyJB9qy6XzRgY0000', is_latest=True, key='/raw/Collection_2/raw_data_4.txt', suffix='.txt', size=10, hash='JI6M1Kw_j7Z6xVSo3R1_8Q', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing ULabel record with same name: 'data'
→ returning existing artifact with same hash: Artifact(uid='xIGmLhto51qHrZ8Y0000', is_latest=True, key='/raw/Collection_2/raw_data_3.txt', suffix='.txt', size=10, hash='XaPa6ssA8nyzeB3Nlixi_Q', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing ULabel record with same name: 'data'
→ returning existing artifact with same hash: Artifact(uid='nIZipP6uB85uf3aj0000', is_latest=True, key='/raw/Collection_2/raw_data_1.txt', suffix='.txt', size=10, hash='qH27yHgMEBjYPHBNVFQ5WQ', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing ULabel record with same name: 'data'
→ returning existing artifact with same hash: Artifact(uid='HQPVHq7MnZeHymBq0000', is_latest=True, key='/raw/Collection_2/raw_data_2.txt', suffix='.txt', size=10, hash='UoPMBmntT-80ZptUD110eA', _hash_type='md5', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:29 UTC)
→ returning existing ULabel record with same name: 'data'
labels = ln.ULabel.lookup()
ln.Artifact.filter(ulabels__in=[labels.data]).df()
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 | ||||||||||||||||||||
4 | rw9bmNrJArg01Vcp0000 | None | True | None | raw/raw_data_3.txt | .txt | None | 10 | oC-Af8mM34pE06YAYc5INQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.300355+00:00 | 1 |
1 | LpuQzN4zSfuQVzv50000 | None | True | None | None | .txt | None | 10 | EPIH3fUb7rUOOIvkhdEiSw | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.262798+00:00 | 1 |
2 | BhULphrzX7uL4t0m0000 | None | True | None | None | .txt | None | 10 | xgo6vEZNxoGdFJVyZFplZQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.267342+00:00 | 1 |
5 | 2MSurueQ2YtdCPhl0000 | None | True | None | /raw/Collection_1/raw_data_4.txt | .txt | None | 10 | fxTvV35y6u4YdIJS7V5MYQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.757428+00:00 | 1 |
6 | yjN2XPi9wY0P9vFC0000 | None | True | None | /raw/Collection_1/raw_data_3.txt | .txt | None | 10 | mN0xVCuqQEhquz9mCwl1pA | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.765479+00:00 | 1 |
7 | 5axfyFWb8Ns9hMXy0000 | None | True | None | /raw/Collection_1/raw_data_1.txt | .txt | None | 10 | lim6sj9HJnW-H4qgwcBMWQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.773341+00:00 | 1 |
8 | tVkDdiDtlsNezrgz0000 | None | True | None | /raw/Collection_1/raw_data_2.txt | .txt | None | 10 | eouFP5e1LrinSOQ4qC6VVA | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.781377+00:00 | 1 |
9 | R1jPyJB9qy6XzRgY0000 | None | True | None | /raw/Collection_2/raw_data_4.txt | .txt | None | 10 | JI6M1Kw_j7Z6xVSo3R1_8Q | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.789584+00:00 | 1 |
10 | xIGmLhto51qHrZ8Y0000 | None | True | None | /raw/Collection_2/raw_data_3.txt | .txt | None | 10 | XaPa6ssA8nyzeB3Nlixi_Q | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.797610+00:00 | 1 |
11 | nIZipP6uB85uf3aj0000 | None | True | None | /raw/Collection_2/raw_data_1.txt | .txt | None | 10 | qH27yHgMEBjYPHBNVFQ5WQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.805926+00:00 | 1 |
12 | HQPVHq7MnZeHymBq0000 | None | True | None | /raw/Collection_2/raw_data_2.txt | .txt | None | 10 | UoPMBmntT-80ZptUD110eA | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.814168+00:00 | 1 |
However, Ulabels
are too versatile for such an approach and clashes are also to be expected here.
Metadata¶
Due to the chance of clashes for the aforementioned approaches being rather high, we generally recommend not to store hierarchical data with solely semantic keys.
Biological metadata makes Files
and Collections
unambiguous and easily queryable.
Legacy data and multiple storage roots¶
Distributed Collections¶
LaminDB can ingest legacy data that already had a structure in their storage.
In such cases, it disables _artifact_use_virtual_keys
and the artifacts are ingested with their actual storage location.
It might be therefore be possible that Files
stored in different storage roots may be associated with a single Collection
.
To simulate this, we are disabling _artifact_use_virtual_keys
and ingest artifacts stored in a different path (the “legacy data”).
ln.settings.creation._artifact_use_virtual_keys = False
for root, _, artifacts in os.walk("complex_biological_project/preprocessed"):
for artifactname in artifacts:
file_path = os.path.join(root, artifactname)
key_path = file_path.removeprefix("complex_biological_project")
print(file_path)
print()
ln_artifact = ln.Artifact(file_path, key=f"./{key_path}")
ln_artifact.save()
complex_biological_project/preprocessed/result_2.txt
• path content will be copied to default storage upon `save()` with key './/preprocessed/result_2.txt'
✓ storing artifact 'V6xFqaSwBl5j74bz0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/preprocessed/result_2.txt'
complex_biological_project/preprocessed/result_4.txt
• path content will be copied to default storage upon `save()` with key './/preprocessed/result_4.txt'
✓ storing artifact 'vu4ZAy4X2Ije0TrJ0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/preprocessed/result_4.txt'
complex_biological_project/preprocessed/result_3.txt
• path content will be copied to default storage upon `save()` with key './/preprocessed/result_3.txt'
✓ storing artifact 'F6J3kVC5AnI8Vjor0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/preprocessed/result_3.txt'
complex_biological_project/preprocessed/result_1.txt
• path content will be copied to default storage upon `save()` with key './/preprocessed/result_1.txt'
✓ storing artifact 'gR97Q8tPjt9UkFSG0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/preprocessed/result_1.txt'
ln.Artifact.df()
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 | ||||||||||||||||||||
16 | gR97Q8tPjt9UkFSG0000 | None | True | None | .//preprocessed/result_1.txt | .txt | None | 10 | rWxjRLuBgf09DcnCCN-_2w | None | None | md5 | None | 1 | False | 1 | 1 | 1 | 2024-10-18 15:57:30.215607+00:00 | 1 |
15 | F6J3kVC5AnI8Vjor0000 | None | True | None | .//preprocessed/result_3.txt | .txt | None | 10 | XZp4jkSasnKAi8Gje9XVww | None | None | md5 | None | 1 | False | 1 | 1 | 1 | 2024-10-18 15:57:30.206786+00:00 | 1 |
14 | vu4ZAy4X2Ije0TrJ0000 | None | True | None | .//preprocessed/result_4.txt | .txt | None | 10 | c1Tpmn01kRIJojbeejZ2XA | None | None | md5 | None | 1 | False | 1 | 1 | 1 | 2024-10-18 15:57:30.198025+00:00 | 1 |
13 | V6xFqaSwBl5j74bz0000 | None | True | None | .//preprocessed/result_2.txt | .txt | None | 10 | n4fdQSOQIds1C7PyefEOQQ | None | None | md5 | None | 1 | False | 1 | 1 | 1 | 2024-10-18 15:57:30.189200+00:00 | 1 |
12 | HQPVHq7MnZeHymBq0000 | None | True | None | /raw/Collection_2/raw_data_2.txt | .txt | None | 10 | UoPMBmntT-80ZptUD110eA | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.814168+00:00 | 1 |
11 | nIZipP6uB85uf3aj0000 | None | True | None | /raw/Collection_2/raw_data_1.txt | .txt | None | 10 | qH27yHgMEBjYPHBNVFQ5WQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.805926+00:00 | 1 |
10 | xIGmLhto51qHrZ8Y0000 | None | True | None | /raw/Collection_2/raw_data_3.txt | .txt | None | 10 | XaPa6ssA8nyzeB3Nlixi_Q | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.797610+00:00 | 1 |
9 | R1jPyJB9qy6XzRgY0000 | None | True | None | /raw/Collection_2/raw_data_4.txt | .txt | None | 10 | JI6M1Kw_j7Z6xVSo3R1_8Q | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.789584+00:00 | 1 |
8 | tVkDdiDtlsNezrgz0000 | None | True | None | /raw/Collection_1/raw_data_2.txt | .txt | None | 10 | eouFP5e1LrinSOQ4qC6VVA | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.781377+00:00 | 1 |
7 | 5axfyFWb8Ns9hMXy0000 | None | True | None | /raw/Collection_1/raw_data_1.txt | .txt | None | 10 | lim6sj9HJnW-H4qgwcBMWQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.773341+00:00 | 1 |
6 | yjN2XPi9wY0P9vFC0000 | None | True | None | /raw/Collection_1/raw_data_3.txt | .txt | None | 10 | mN0xVCuqQEhquz9mCwl1pA | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.765479+00:00 | 1 |
5 | 2MSurueQ2YtdCPhl0000 | None | True | None | /raw/Collection_1/raw_data_4.txt | .txt | None | 10 | fxTvV35y6u4YdIJS7V5MYQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.757428+00:00 | 1 |
2 | BhULphrzX7uL4t0m0000 | None | True | None | None | .txt | None | 10 | xgo6vEZNxoGdFJVyZFplZQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.267342+00:00 | 1 |
1 | LpuQzN4zSfuQVzv50000 | None | True | None | None | .txt | None | 10 | EPIH3fUb7rUOOIvkhdEiSw | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.262798+00:00 | 1 |
4 | rw9bmNrJArg01Vcp0000 | None | True | None | raw/raw_data_3.txt | .txt | None | 10 | oC-Af8mM34pE06YAYc5INQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.300355+00:00 | 1 |
3 | ta1ZIkrcraKQUpcc0000 | None | True | None | raw/raw_data_3.txt | .txt | None | 10 | 30oxgeh1vlNv3e67FuJpDg | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.296021+00:00 | 1 |
artifact_from_raw = ln.Artifact.filter(key__icontains="Collection_2/raw_data_1").first()
artifact_from_preprocessed = ln.Artifact.filter(
key__icontains="preprocessed/result_1"
).first()
print(artifact_from_raw.path)
print(artifact_from_preprocessed.path)
/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/nIZipP6uB85uf3aj0000.txt
/home/runner/work/lamindb/lamindb/docs/faq/key-eval/preprocessed/result_1.txt
Let’s create our Collection
:
ds = ln.Collection(
[artifact_from_raw, artifact_from_preprocessed], name="raw_and_processed_collection_2"
)
ds.save()
Collection(uid='2IpLQVZpisP31kVZ0000', is_latest=True, name='raw_and_processed_collection_2', hash='OF1fJRKQhnI9wBz0czRXHQ', visibility=1, created_by_id=1, transform_id=1, run_id=1, created_at=2024-10-18 15:57:30 UTC)
ds.artifacts.df()
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 | ||||||||||||||||||||
11 | nIZipP6uB85uf3aj0000 | None | True | None | /raw/Collection_2/raw_data_1.txt | .txt | None | 10 | qH27yHgMEBjYPHBNVFQ5WQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:29.805926+00:00 | 1 |
16 | gR97Q8tPjt9UkFSG0000 | None | True | None | .//preprocessed/result_1.txt | .txt | None | 10 | rWxjRLuBgf09DcnCCN-_2w | None | None | md5 | None | 1 | False | 1 | 1 | 1 | 2024-10-18 15:57:30.215607+00:00 | 1 |
Modeling directories¶
ln.settings.creation._artifact_use_virtual_keys = True
dir_path = ln.core.datasets.dir_scrnaseq_cellranger("sample_001")
ln.UPath(dir_path).view_tree()
• file has more than one suffix (path.suffixes), using only last suffix: '.bai' - if you want your composite suffix to be recognized add it to lamindb.core.storage.VALID_SIMPLE_SUFFIXES.add()
3 sub-directories & 15 files with suffixes '.h5', '.mtx.gz', '.html', '.tsv.gz', '.csv', '.bam', '.bai', '.cloupe'
/home/runner/work/lamindb/lamindb/docs/faq/sample_001
├── cloupe.cloupe
├── web_summary.html
├── filtered_feature_bc_matrix/
│ ├── features.tsv.gz
│ ├── matrix.mtx.gz
│ └── barcodes.tsv.gz
├── raw_feature_bc_matrix/
│ ├── features.tsv.gz
│ ├── matrix.mtx.gz
│ └── barcodes.tsv.gz
├── metrics_summary.csv
├── filtered_feature_bc_matrix.h5
├── raw_feature_bc_matrix.h5
├── possorted_genome_bam.bam.bai
├── possorted_genome_bam.bam
├── analysis/
│ └── analysis.csv
└── molecule_info.h5
There are two ways to create Artifact
objects from directories: from_dir()
and Artifact
.
cellranger_raw_artifact = ln.Artifact.from_dir("sample_001/raw_feature_bc_matrix/")
! folder is outside existing storage location, will copy files from sample_001/raw_feature_bc_matrix/ to /home/runner/work/lamindb/lamindb/docs/faq/key-eval/raw_feature_bc_matrix
✓ created 3 artifacts from directory using storage /home/runner/work/lamindb/lamindb/docs/faq/key-eval and key = raw_feature_bc_matrix/
for artifact in cellranger_raw_artifact:
artifact.save()
✓ storing artifact '6RjqK0WlUsSpd1TY0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/6RjqK0WlUsSpd1TY0000.tsv.gz'
✓ storing artifact '0n70exrjy1uisaYD0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/0n70exrjy1uisaYD0000.mtx.gz'
✓ storing artifact 'IALSNq225ymWa1z70000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/IALSNq225ymWa1z70000.tsv.gz'
cellranger_raw_folder = ln.Artifact(
"sample_001/raw_feature_bc_matrix/", description="cellranger raw"
)
cellranger_raw_folder.save()
• path content will be copied to default storage upon `save()` with key `None` ('.lamindb/SiDiAFsQOhb5N9gR')
✓ storing artifact 'SiDiAFsQOhb5N9gR0000' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/SiDiAFsQOhb5N9gR'
Artifact(uid='SiDiAFsQOhb5N9gR0000', is_latest=True, description='cellranger raw', suffix='', size=18, hash='mGo5L4sjJ0RQFGHLf5NrQg', n_objects=3, _hash_type='md5-d', visibility=1, _key_is_virtual=True, storage_id=1, transform_id=1, run_id=1, created_by_id=1, created_at=2024-10-18 15:57:30 UTC)
ln.Artifact.filter(key__icontains="raw_feature_bc_matrix").df()
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 | ||||||||||||||||||||
17 | 6RjqK0WlUsSpd1TY0000 | None | True | None | raw_feature_bc_matrix/features.tsv.gz | .tsv.gz | None | 6 | j_DRqPfCpiMc6T0fbP4PCQ | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:30.339026+00:00 | 1 |
18 | 0n70exrjy1uisaYD0000 | None | True | None | raw_feature_bc_matrix/matrix.mtx.gz | .mtx.gz | None | 6 | Y9wiEGYT-DYWW655CfHClA | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:30.343010+00:00 | 1 |
19 | IALSNq225ymWa1z70000 | None | True | None | raw_feature_bc_matrix/barcodes.tsv.gz | .tsv.gz | None | 6 | cpP92U5IX5bipXLjqtnGaA | None | None | md5 | None | 1 | True | 1 | 1 | 1 | 2024-10-18 15:57:30.346591+00:00 | 1 |
ln.Artifact.get(key__icontains="raw_feature_bc_matrix/matrix.mtx.gz").path
PosixUPath('/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/0n70exrjy1uisaYD0000.mtx.gz')
artifact = ln.Artifact.get(description="cellranger raw")
artifact.path.glob("*")
<generator object Path.glob at 0x7f6af715fbc0>