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.

SetupΒΆ

We’re simulating an artifact system with several nested folders and artifacts. Such structures are resembled in, for example, the RxRx: cell imaging guide.

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_2
β”‚   β”œβ”€β”€ Collection_1
β”‚   β”œβ”€β”€ raw_data_1.txt
β”‚   β”œβ”€β”€ raw_data_2.txt
β”‚   β”œβ”€β”€ raw_data_4.txt
β”‚   └── raw_data_3.txt
└── preprocessed
    β”œβ”€β”€ result_2.txt
    β”œβ”€β”€ result_4.txt
    β”œβ”€β”€ result_3.txt
    └── result_1.txt
ln.settings.transform.stem_uid = "WIwaNDvlEkwS"
ln.settings.transform.version = "1"
ln.track()
πŸ’‘ notebook imports: lamindb==0.72.1
πŸ’‘ saved: Transform(version='1', uid='WIwaNDvlEkwS5zKv', name='What does the key parameter do under the hood?', key='key', type='notebook', updated_at=2024-05-23 10:58:06 UTC, created_by_id=1)
πŸ’‘ saved: Run(uid='hqV7JBXQSooV6vONvPpT', transform_id=1, created_by_id=1)
πŸ’‘ tracked pip freeze > /home/runner/.cache/lamindb/run_env_pip_hqV7JBXQSooV6vONvPpT.txt

Storing artifacts using Storage, File, and CollectionΒΆ

Lamin has three storage classes that manage different types of in-memory and on-disk objects:

  1. Storage: Manages the default storage root that can be either local or in the cloud. For more details we refer to Storage FAQ.

  2. Artifact: Manages datasets with an optional key that acts as a relative path within the current default storage root (see Storage). An example is a single h5 artifact.

  3. Collection: Manages a collection of datasets with an optional key that acts as a relative path within the current default storage root (see Storage). An example is a collection of h5 artifacts.

For more details we refer to Tutorial: Artifacts.

The current storage root is:

ln.settings.storage
PosixUPath('/home/runner/work/lamindb/lamindb/docs/faq/key-eval')

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.artifact_use_virtual_keys = False. Generally, we discourage disabling this setting manually. For more details we refer to Storage FAQ.

ln.settings.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/6aPHKa4794PLmOfm6OUr.txt')
πŸ’‘ path content will be copied to default storage upon `save()` with key `None` ('.lamindb/Q8SsfCFGfflyDpbBN5Wk.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 '6aPHKa4794PLmOfm6OUr' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/6aPHKa4794PLmOfm6OUr.txt'
βœ… storing artifact 'Q8SsfCFGfflyDpbBN5Wk' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/Q8SsfCFGfflyDpbBN5Wk.txt'
Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='Q8SsfCFGfflyDpbBN5Wk', suffix='.txt', size=10, hash='mkiS-PwrRx6IwJjvhz3ZzQ', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
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 'RfTPfy874oLm5eUZuH4x' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/RfTPfy874oLm5eUZuH4x.txt'
βœ… storing artifact 'zc40qiqSrDXTrrFWyKCw' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/zc40qiqSrDXTrrFWyKCw.txt'
Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='zc40qiqSrDXTrrFWyKCw', key='raw/raw_data_4.txt', suffix='.txt', size=10, hash='WihPpgHf4VMVFKP6T03xpw', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)

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()
version created_at created_by_id updated_at uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual
id
3 None 2024-05-23 10:58:07.299012+00:00 1 2024-05-23 10:58:07.299059+00:00 RfTPfy874oLm5eUZuH4x 1 raw/raw_data_3.txt .txt None None 10 q9r6ZD0PD32SzJ2iRLNY5Q md5 None None 1 1 1 True
4 None 2024-05-23 10:58:07.302756+00:00 1 2024-05-23 10:58:07.302800+00:00 zc40qiqSrDXTrrFWyKCw 1 raw/raw_data_4.txt .txt None None 10 WihPpgHf4VMVFKP6T03xpw md5 None None 1 1 1 True

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='yShH89xBEQn8l9AgkSgl', name='no key collection', hash='uaPj0V3lhZBp7zE_TARC', 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/RfTPfy874oLm5eUZuH4x.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/RfTPfy874oLm5eUZuH4x.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/zc40qiqSrDXTrrFWyKCw.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()
version created_at updated_at uid key suffix accessor description size hash hash_type n_objects n_observations visibility key_is_virtual created_by_id storage_id transform_id run_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)
RfTPfy874oLm5eUZuH4x
zc40qiqSrDXTrrFWyKCw

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 annotated.

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(updated_at=2024-05-23 10:58:07 UTC, uid='6aPHKa4794PLmOfm6OUr', suffix='.txt', size=10, hash='2h0kRiEWaRmso66E5mZ6Ew', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ key None on existing artifact differs from passed key /raw/raw_data_1.txt
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='Q8SsfCFGfflyDpbBN5Wk', suffix='.txt', size=10, hash='mkiS-PwrRx6IwJjvhz3ZzQ', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ key None on existing artifact differs from passed key /raw/raw_data_2.txt
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='zc40qiqSrDXTrrFWyKCw', key='raw/raw_data_3.txt', suffix='.txt', size=10, hash='WihPpgHf4VMVFKP6T03xpw', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ key raw/raw_data_3.txt on existing artifact differs from passed key /raw/raw_data_4.txt
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_1.txt'
βœ… storing artifact 'wTe7z8XM0Nvk5o703jYN' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/wTe7z8XM0Nvk5o703jYN.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_2.txt'
βœ… storing artifact 'TShIBIM8KweRNexVe6NT' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/TShIBIM8KweRNexVe6NT.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_4.txt'
βœ… storing artifact 'sAgQjmNGFMMqSWCI778T' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/sAgQjmNGFMMqSWCI778T.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_3.txt'
βœ… storing artifact 'yAuajCX5GFETRZJCC04v' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/yAuajCX5GFETRZJCC04v.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_1.txt'
βœ… storing artifact 'uov862A428dqf6PWiRcc' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/uov862A428dqf6PWiRcc.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_2.txt'
βœ… storing artifact 'ohA13SveUCvtMiQlNFjC' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/ohA13SveUCvtMiQlNFjC.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_4.txt'
βœ… storing artifact 'zsbRa2ogJP7rh647fOdR' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/zsbRa2ogJP7rh647fOdR.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_3.txt'
βœ… storing artifact 'Rz6ebz3nuuGCmK3rMZMB' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/Rz6ebz3nuuGCmK3rMZMB.txt'
ln.Artifact.filter(key__startswith="raw").df()
version created_at created_by_id updated_at uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual
id
3 None 2024-05-23 10:58:07.299012+00:00 1 2024-05-23 10:58:07.718005+00:00 RfTPfy874oLm5eUZuH4x 1 raw/raw_data_3.txt .txt None None 10 q9r6ZD0PD32SzJ2iRLNY5Q md5 None None 1 1 1 True
4 None 2024-05-23 10:58:07.302756+00:00 1 2024-05-23 10:58:07.758251+00:00 zc40qiqSrDXTrrFWyKCw 1 raw/raw_data_3.txt .txt None None 10 WihPpgHf4VMVFKP6T03xpw md5 None None 1 1 1 True

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(updated_at=2024-05-23 10:58:07 UTC, uid='6aPHKa4794PLmOfm6OUr', suffix='.txt', size=10, hash='2h0kRiEWaRmso66E5mZ6Ew', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='Q8SsfCFGfflyDpbBN5Wk', suffix='.txt', size=10, hash='mkiS-PwrRx6IwJjvhz3ZzQ', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='zc40qiqSrDXTrrFWyKCw', key='raw/raw_data_3.txt', suffix='.txt', size=10, hash='WihPpgHf4VMVFKP6T03xpw', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='wTe7z8XM0Nvk5o703jYN', key='/raw/Collection_2/raw_data_1.txt', suffix='.txt', size=10, hash='aCoeZ2erUnbtxNt7Cxhuqg', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='TShIBIM8KweRNexVe6NT', key='/raw/Collection_2/raw_data_2.txt', suffix='.txt', size=10, hash='wxbfz89x7l6lAlPd4waSwA', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='sAgQjmNGFMMqSWCI778T', key='/raw/Collection_2/raw_data_4.txt', suffix='.txt', size=10, hash='jAKlhsn38f-WmJWavJ0AMw', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='yAuajCX5GFETRZJCC04v', key='/raw/Collection_2/raw_data_3.txt', suffix='.txt', size=10, hash='ELgU70SpniGJPNDTXM6-mw', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='uov862A428dqf6PWiRcc', key='/raw/Collection_1/raw_data_1.txt', suffix='.txt', size=10, hash='TiIe20L9cfhsLYOwj0YW7g', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='ohA13SveUCvtMiQlNFjC', key='/raw/Collection_1/raw_data_2.txt', suffix='.txt', size=10, hash='2mE0lf923KQ4Os0v2-86Iw', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='zsbRa2ogJP7rh647fOdR', key='/raw/Collection_1/raw_data_4.txt', suffix='.txt', size=10, hash='AjzeMvVwNj3duyY7HdTc0g', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='Rz6ebz3nuuGCmK3rMZMB', key='/raw/Collection_1/raw_data_3.txt', suffix='.txt', size=10, hash='0OwV4nEjoO4Os9R0UQin3Q', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
ln.Collection.filter(name__icontains="data").df()
version created_at created_by_id updated_at uid name description hash reference reference_type transform_id run_id artifact_id visibility
id
1 None 2024-05-23 10:58:07.915095+00:00 1 2024-05-23 10:58:07.915143+00:00 QukYzpL07zOOl3cvjMGJ data None tS_A_oZK6QDUEjywVh0C None None 1 1 None 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(updated_at=2024-05-23 10:58:07 UTC, uid='6aPHKa4794PLmOfm6OUr', suffix='.txt', size=10, hash='2h0kRiEWaRmso66E5mZ6Ew', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ key None on existing artifact differs from passed key /raw/raw_data_1.txt
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='Q8SsfCFGfflyDpbBN5Wk', suffix='.txt', size=10, hash='mkiS-PwrRx6IwJjvhz3ZzQ', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ key None on existing artifact differs from passed key /raw/raw_data_2.txt
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='zc40qiqSrDXTrrFWyKCw', key='raw/raw_data_3.txt', suffix='.txt', size=10, hash='WihPpgHf4VMVFKP6T03xpw', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ key raw/raw_data_3.txt on existing artifact differs from passed key /raw/raw_data_4.txt
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='wTe7z8XM0Nvk5o703jYN', key='/raw/Collection_2/raw_data_1.txt', suffix='.txt', size=10, hash='aCoeZ2erUnbtxNt7Cxhuqg', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='TShIBIM8KweRNexVe6NT', key='/raw/Collection_2/raw_data_2.txt', suffix='.txt', size=10, hash='wxbfz89x7l6lAlPd4waSwA', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='sAgQjmNGFMMqSWCI778T', key='/raw/Collection_2/raw_data_4.txt', suffix='.txt', size=10, hash='jAKlhsn38f-WmJWavJ0AMw', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='yAuajCX5GFETRZJCC04v', key='/raw/Collection_2/raw_data_3.txt', suffix='.txt', size=10, hash='ELgU70SpniGJPNDTXM6-mw', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='uov862A428dqf6PWiRcc', key='/raw/Collection_1/raw_data_1.txt', suffix='.txt', size=10, hash='TiIe20L9cfhsLYOwj0YW7g', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='ohA13SveUCvtMiQlNFjC', key='/raw/Collection_1/raw_data_2.txt', suffix='.txt', size=10, hash='2mE0lf923KQ4Os0v2-86Iw', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='zsbRa2ogJP7rh647fOdR', key='/raw/Collection_1/raw_data_4.txt', suffix='.txt', size=10, hash='AjzeMvVwNj3duyY7HdTc0g', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(updated_at=2024-05-23 10:58:07 UTC, uid='Rz6ebz3nuuGCmK3rMZMB', key='/raw/Collection_1/raw_data_3.txt', suffix='.txt', size=10, hash='0OwV4nEjoO4Os9R0UQin3Q', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
labels = ln.ULabel.lookup()
ln.Artifact.filter(ulabels__in=[labels.data]).df()
version created_at created_by_id updated_at uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual
id
1 None 2024-05-23 10:58:07.275092+00:00 1 2024-05-23 10:58:07.953092+00:00 6aPHKa4794PLmOfm6OUr 1 None .txt None None 10 2h0kRiEWaRmso66E5mZ6Ew md5 None None 1 1 1 True
2 None 2024-05-23 10:58:07.279318+00:00 1 2024-05-23 10:58:07.967252+00:00 Q8SsfCFGfflyDpbBN5Wk 1 None .txt None None 10 mkiS-PwrRx6IwJjvhz3ZzQ md5 None None 1 1 1 True
4 None 2024-05-23 10:58:07.302756+00:00 1 2024-05-23 10:58:07.983285+00:00 zc40qiqSrDXTrrFWyKCw 1 raw/raw_data_3.txt .txt None None 10 WihPpgHf4VMVFKP6T03xpw md5 None None 1 1 1 True
5 None 2024-05-23 10:58:07.763907+00:00 1 2024-05-23 10:58:07.999021+00:00 wTe7z8XM0Nvk5o703jYN 1 /raw/Collection_2/raw_data_1.txt .txt None None 10 aCoeZ2erUnbtxNt7Cxhuqg md5 None None 1 1 1 True
6 None 2024-05-23 10:58:07.771404+00:00 1 2024-05-23 10:58:08.013410+00:00 TShIBIM8KweRNexVe6NT 1 /raw/Collection_2/raw_data_2.txt .txt None None 10 wxbfz89x7l6lAlPd4waSwA md5 None None 1 1 1 True
7 None 2024-05-23 10:58:07.779225+00:00 1 2024-05-23 10:58:08.027594+00:00 sAgQjmNGFMMqSWCI778T 1 /raw/Collection_2/raw_data_4.txt .txt None None 10 jAKlhsn38f-WmJWavJ0AMw md5 None None 1 1 1 True
8 None 2024-05-23 10:58:07.786389+00:00 1 2024-05-23 10:58:08.042639+00:00 yAuajCX5GFETRZJCC04v 1 /raw/Collection_2/raw_data_3.txt .txt None None 10 ELgU70SpniGJPNDTXM6-mw md5 None None 1 1 1 True
9 None 2024-05-23 10:58:07.793677+00:00 1 2024-05-23 10:58:08.057540+00:00 uov862A428dqf6PWiRcc 1 /raw/Collection_1/raw_data_1.txt .txt None None 10 TiIe20L9cfhsLYOwj0YW7g md5 None None 1 1 1 True
10 None 2024-05-23 10:58:07.800574+00:00 1 2024-05-23 10:58:08.072503+00:00 ohA13SveUCvtMiQlNFjC 1 /raw/Collection_1/raw_data_2.txt .txt None None 10 2mE0lf923KQ4Os0v2-86Iw md5 None None 1 1 1 True
11 None 2024-05-23 10:58:07.807577+00:00 1 2024-05-23 10:58:08.086942+00:00 zsbRa2ogJP7rh647fOdR 1 /raw/Collection_1/raw_data_4.txt .txt None None 10 AjzeMvVwNj3duyY7HdTc0g md5 None None 1 1 1 True
12 None 2024-05-23 10:58:07.815103+00:00 1 2024-05-23 10:58:08.101965+00:00 Rz6ebz3nuuGCmK3rMZMB 1 /raw/Collection_1/raw_data_3.txt .txt None None 10 0OwV4nEjoO4Os9R0UQin3Q md5 None None 1 1 1 True

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.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 'dfrIpexubXHmqVd40WD0' 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 'mxTFnM2TSJKDiNnPM4VF' 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 'TCcIy5uovXemGVOQx7lD' 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 'JAwPaYMldby0qkMpbFbU' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/preprocessed/result_1.txt'
ln.Artifact.df()
version created_at created_by_id updated_at uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual
id
16 None 2024-05-23 10:58:08.184583+00:00 1 2024-05-23 10:58:08.184628+00:00 JAwPaYMldby0qkMpbFbU 1 .//preprocessed/result_1.txt .txt None None 10 Q7nsr0w-xXGhv2tKL3B2SQ md5 None None 1 1 1 False
15 None 2024-05-23 10:58:08.178257+00:00 1 2024-05-23 10:58:08.178302+00:00 TCcIy5uovXemGVOQx7lD 1 .//preprocessed/result_3.txt .txt None None 10 A1rEQ33ba9bN-6m7GI4Nuw md5 None None 1 1 1 False
14 None 2024-05-23 10:58:08.171509+00:00 1 2024-05-23 10:58:08.171560+00:00 mxTFnM2TSJKDiNnPM4VF 1 .//preprocessed/result_4.txt .txt None None 10 u6E2N0foT7A9Df41bgchtA md5 None None 1 1 1 False
13 None 2024-05-23 10:58:08.163405+00:00 1 2024-05-23 10:58:08.163450+00:00 dfrIpexubXHmqVd40WD0 1 .//preprocessed/result_2.txt .txt None None 10 am9sgHENqRTk1GYDDFm4Lw md5 None None 1 1 1 False
12 None 2024-05-23 10:58:07.815103+00:00 1 2024-05-23 10:58:08.101965+00:00 Rz6ebz3nuuGCmK3rMZMB 1 /raw/Collection_1/raw_data_3.txt .txt None None 10 0OwV4nEjoO4Os9R0UQin3Q md5 None None 1 1 1 True
11 None 2024-05-23 10:58:07.807577+00:00 1 2024-05-23 10:58:08.086942+00:00 zsbRa2ogJP7rh647fOdR 1 /raw/Collection_1/raw_data_4.txt .txt None None 10 AjzeMvVwNj3duyY7HdTc0g md5 None None 1 1 1 True
10 None 2024-05-23 10:58:07.800574+00:00 1 2024-05-23 10:58:08.072503+00:00 ohA13SveUCvtMiQlNFjC 1 /raw/Collection_1/raw_data_2.txt .txt None None 10 2mE0lf923KQ4Os0v2-86Iw md5 None None 1 1 1 True
9 None 2024-05-23 10:58:07.793677+00:00 1 2024-05-23 10:58:08.057540+00:00 uov862A428dqf6PWiRcc 1 /raw/Collection_1/raw_data_1.txt .txt None None 10 TiIe20L9cfhsLYOwj0YW7g md5 None None 1 1 1 True
8 None 2024-05-23 10:58:07.786389+00:00 1 2024-05-23 10:58:08.042639+00:00 yAuajCX5GFETRZJCC04v 1 /raw/Collection_2/raw_data_3.txt .txt None None 10 ELgU70SpniGJPNDTXM6-mw md5 None None 1 1 1 True
7 None 2024-05-23 10:58:07.779225+00:00 1 2024-05-23 10:58:08.027594+00:00 sAgQjmNGFMMqSWCI778T 1 /raw/Collection_2/raw_data_4.txt .txt None None 10 jAKlhsn38f-WmJWavJ0AMw md5 None None 1 1 1 True
6 None 2024-05-23 10:58:07.771404+00:00 1 2024-05-23 10:58:08.013410+00:00 TShIBIM8KweRNexVe6NT 1 /raw/Collection_2/raw_data_2.txt .txt None None 10 wxbfz89x7l6lAlPd4waSwA md5 None None 1 1 1 True
5 None 2024-05-23 10:58:07.763907+00:00 1 2024-05-23 10:58:07.999021+00:00 wTe7z8XM0Nvk5o703jYN 1 /raw/Collection_2/raw_data_1.txt .txt None None 10 aCoeZ2erUnbtxNt7Cxhuqg md5 None None 1 1 1 True
4 None 2024-05-23 10:58:07.302756+00:00 1 2024-05-23 10:58:07.983285+00:00 zc40qiqSrDXTrrFWyKCw 1 raw/raw_data_3.txt .txt None None 10 WihPpgHf4VMVFKP6T03xpw md5 None None 1 1 1 True
2 None 2024-05-23 10:58:07.279318+00:00 1 2024-05-23 10:58:07.967252+00:00 Q8SsfCFGfflyDpbBN5Wk 1 None .txt None None 10 mkiS-PwrRx6IwJjvhz3ZzQ md5 None None 1 1 1 True
1 None 2024-05-23 10:58:07.275092+00:00 1 2024-05-23 10:58:07.953092+00:00 6aPHKa4794PLmOfm6OUr 1 None .txt None None 10 2h0kRiEWaRmso66E5mZ6Ew md5 None None 1 1 1 True
3 None 2024-05-23 10:58:07.299012+00:00 1 2024-05-23 10:58:07.718005+00:00 RfTPfy874oLm5eUZuH4x 1 raw/raw_data_3.txt .txt None None 10 q9r6ZD0PD32SzJ2iRLNY5Q md5 None None 1 1 1 True
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/wTe7z8XM0Nvk5o703jYN.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()
ds.artifacts.df()
version created_at created_by_id updated_at uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual
id
5 None 2024-05-23 10:58:07.763907+00:00 1 2024-05-23 10:58:07.999021+00:00 wTe7z8XM0Nvk5o703jYN 1 /raw/Collection_2/raw_data_1.txt .txt None None 10 aCoeZ2erUnbtxNt7Cxhuqg md5 None None 1 1 1 True
16 None 2024-05-23 10:58:08.184583+00:00 1 2024-05-23 10:58:08.184628+00:00 JAwPaYMldby0qkMpbFbU 1 .//preprocessed/result_1.txt .txt None None 10 Q7nsr0w-xXGhv2tKL3B2SQ md5 None None 1 1 1 False

Modeling directoriesΒΆ

ln.settings.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_SUFFIXES.add()
3 sub-directories & 15 files with suffixes '.csv', '.mtx.gz', '.h5', '.cloupe', '.html', '.bam', '.tsv.gz', '.bai'
/home/runner/work/lamindb/lamindb/docs/faq/sample_001
β”œβ”€β”€ possorted_genome_bam.bam
β”œβ”€β”€ raw_feature_bc_matrix.h5
β”œβ”€β”€ molecule_info.h5
β”œβ”€β”€ filtered_feature_bc_matrix.h5
β”œβ”€β”€ raw_feature_bc_matrix
β”‚   β”œβ”€β”€ matrix.mtx.gz
β”‚   β”œβ”€β”€ barcodes.tsv.gz
β”‚   └── features.tsv.gz
β”œβ”€β”€ metrics_summary.csv
β”œβ”€β”€ cloupe.cloupe
β”œβ”€β”€ web_summary.html
β”œβ”€β”€ analysis
β”‚   └── analysis.csv
β”œβ”€β”€ possorted_genome_bam.bam.bai
└── filtered_feature_bc_matrix
    β”œβ”€β”€ matrix.mtx.gz
    β”œβ”€β”€ barcodes.tsv.gz
    └── features.tsv.gz

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/")
❗ this creates one artifact per file in the directory - you might simply call ln.Artifact(dir) to get one artifact for the entire directory
❗ 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 '4VOO1mp1XhPaEhkgp60Q' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/4VOO1mp1XhPaEhkgp60Q.mtx.gz'
βœ… storing artifact 'R7nlLqtezxKu44u8SmJN' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/R7nlLqtezxKu44u8SmJN.tsv.gz'
βœ… storing artifact 'w3qNg14qeVj6FlW5Baof' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/w3qNg14qeVj6FlW5Baof.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/bqtQhoFaIxYfCJgo')
βœ… storing artifact 'bqtQhoFaIxYfCJgo37DN' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/bqtQhoFaIxYfCJgo'
Artifact(updated_at=2024-05-23 10:58:08 UTC, uid='bqtQhoFaIxYfCJgo37DN', suffix='', description='cellranger raw', size=18, hash='aNhNN98QHR7O0lvH6QBvXA', hash_type='md5-d', n_objects=3, visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, transform_id=1, run_id=1)
ln.Artifact.filter(key__icontains="raw_feature_bc_matrix").df()
version created_at created_by_id updated_at uid storage_id key suffix accessor description size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual
id
17 None 2024-05-23 10:58:08.305438+00:00 1 2024-05-23 10:58:08.305486+00:00 4VOO1mp1XhPaEhkgp60Q 1 raw_feature_bc_matrix/matrix.mtx.gz .mtx.gz None None 6 0eU9uo607EQ3bHPq24WxiQ md5 None None 1 1 1 True
18 None 2024-05-23 10:58:08.309097+00:00 1 2024-05-23 10:58:08.309142+00:00 R7nlLqtezxKu44u8SmJN 1 raw_feature_bc_matrix/barcodes.tsv.gz .tsv.gz None None 6 hAXir5mmRgExzoSzF9d0MQ md5 None None 1 1 1 True
19 None 2024-05-23 10:58:08.312412+00:00 1 2024-05-23 10:58:08.312455+00:00 w3qNg14qeVj6FlW5Baof 1 raw_feature_bc_matrix/features.tsv.gz .tsv.gz None None 6 XGjBdKBcoLxq2bSHiG6fxw md5 None None 1 1 1 True
ln.Artifact.filter(key__icontains="raw_feature_bc_matrix/matrix.mtx.gz").one().path
PosixUPath('/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/4VOO1mp1XhPaEhkgp60Q.mtx.gz')
artifact = ln.Artifact.filter(description="cellranger raw").one()
artifact.path.glob("*")
<generator object Path.glob at 0x7f27a8f5c8c0>