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()
complex_biological_project (4 sub-directories & 8 files with suffixes '.txt'): 
β”œβ”€β”€ raw
β”‚   β”œβ”€β”€ raw_data_2.txt
β”‚   β”œβ”€β”€ Collection_2
β”‚   β”œβ”€β”€ raw_data_3.txt
β”‚   β”œβ”€β”€ Collection_1
β”‚   β”œβ”€β”€ raw_data_4.txt
β”‚   └── raw_data_1.txt
└── preprocessed
    β”œβ”€β”€ result_4.txt
    β”œβ”€β”€ result_3.txt
    β”œβ”€β”€ result_2.txt
    └── result_1.txt
ln.settings.transform.stem_uid = "WIwaNDvlEkwS"
ln.settings.transform.version = "1"
ln.track()
πŸ’‘ Assuming editor is Jupyter Lab.
πŸ’‘ Attaching notebook metadata
πŸ’‘ notebook imports: lamindb==0.71.0
πŸ’‘ saved: Transform(uid='WIwaNDvlEkwS5zKv', name='What does the key parameter do under the hood?', key='key', version='1', type='notebook', updated_at=2024-05-01 18:50:43 UTC, created_by_id=1)
πŸ’‘ saved: Run(uid='O5WAyyer0RVksIXERDJQ', transform_id=1, created_by_id=1)
πŸ’‘ tracked pip freeze > /home/runner/.cache/lamindb/run_env_pip_O5WAyyer0RVksIXERDJQ.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/orsJ5LefcOqPTeQUAGRR.txt')
πŸ’‘ path content will be copied to default storage upon `save()` with key `None` ('.lamindb/ZQeEa3yDRa81x7oF5evF.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 'orsJ5LefcOqPTeQUAGRR' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/orsJ5LefcOqPTeQUAGRR.txt'
βœ… storing artifact 'ZQeEa3yDRa81x7oF5evF' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/ZQeEa3yDRa81x7oF5evF.txt'
Artifact(uid='ZQeEa3yDRa81x7oF5evF', suffix='.txt', size=10, hash='tQ_LK-RWrqArVjEcOePwWg', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:44 UTC, storage_id=1, transform_id=1, run_id=1, created_by_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 'srHVoFNYQDpxWDeDM3PN' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/srHVoFNYQDpxWDeDM3PN.txt'
βœ… storing artifact 'PQ3IwRUPU0JwH0fyYF3T' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/PQ3IwRUPU0JwH0fyYF3T.txt'
Artifact(uid='PQ3IwRUPU0JwH0fyYF3T', key='raw/raw_data_4.txt', suffix='.txt', size=10, hash='IYGAyBwkgolA4McbYLhzeQ', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:44 UTC, storage_id=1, transform_id=1, run_id=1, created_by_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()
uid storage_id key suffix accessor description version size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
3 srHVoFNYQDpxWDeDM3PN 1 raw/raw_data_3.txt .txt None None None 10 LQ7cAxaiGuurbMJNpHEHTA md5 None None 1 1 1 True 2024-05-01 18:50:44.790391+00:00 2024-05-01 18:50:44.790414+00:00 1
4 PQ3IwRUPU0JwH0fyYF3T 1 raw/raw_data_4.txt .txt None None None 10 IYGAyBwkgolA4McbYLhzeQ md5 None None 1 1 1 True 2024-05-01 18:50:44.794456+00:00 2024-05-01 18:50:44.794479+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='zuX3xxErcbqM3FcfT2vx', name='no key collection', hash='HFTXN1HaL9BTfKjUuE_S', visibility=1, transform_id=1, run_id=1, created_by_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/srHVoFNYQDpxWDeDM3PN.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/srHVoFNYQDpxWDeDM3PN.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/PQ3IwRUPU0JwH0fyYF3T.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()
uid key suffix accessor description version size hash hash_type n_objects n_observations visibility key_is_virtual created_at updated_at storage_id transform_id run_id created_by_id
id

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)
srHVoFNYQDpxWDeDM3PN
PQ3IwRUPU0JwH0fyYF3T

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(uid='ZQeEa3yDRa81x7oF5evF', suffix='.txt', size=10, hash='tQ_LK-RWrqArVjEcOePwWg', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:44 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key None on existing artifact differs from passed key /raw/raw_data_2.txt
❗ returning existing artifact with same hash: Artifact(uid='PQ3IwRUPU0JwH0fyYF3T', key='raw/raw_data_3.txt', suffix='.txt', size=10, hash='IYGAyBwkgolA4McbYLhzeQ', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ 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='orsJ5LefcOqPTeQUAGRR', suffix='.txt', size=10, hash='3SbfSTTz_yG9aa98nV65rA', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:44 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key None on existing artifact differs from passed key /raw/raw_data_1.txt
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_2.txt'
βœ… storing artifact 'yiHDl7nOj5iITUlGAO8N' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/yiHDl7nOj5iITUlGAO8N.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_3.txt'
βœ… storing artifact 'OIX9BImg9qgNcGaaL590' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/OIX9BImg9qgNcGaaL590.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_4.txt'
βœ… storing artifact 'OUju2yiMW3ly7Ta3Qdhd' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/OUju2yiMW3ly7Ta3Qdhd.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_2/raw_data_1.txt'
βœ… storing artifact 'wKusPUWcvxiEtGVHWo34' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/wKusPUWcvxiEtGVHWo34.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_2.txt'
βœ… storing artifact 'CDLVJh6pnwQJYOrQmkXz' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/CDLVJh6pnwQJYOrQmkXz.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_3.txt'
βœ… storing artifact 'SgsI4LhpxhPdYMsYOG6y' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/SgsI4LhpxhPdYMsYOG6y.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_4.txt'
βœ… storing artifact 'Ol8eR9Wsubw1Uqt1VUvJ' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/Ol8eR9Wsubw1Uqt1VUvJ.txt'
πŸ’‘ path content will be copied to default storage upon `save()` with key '/raw/Collection_1/raw_data_1.txt'
βœ… storing artifact 'qflMiD941Io3sNprvOWm' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/qflMiD941Io3sNprvOWm.txt'
ln.Artifact.filter(key__startswith="raw").df()
uid storage_id key suffix accessor description version size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
3 srHVoFNYQDpxWDeDM3PN 1 raw/raw_data_3.txt .txt None None None 10 LQ7cAxaiGuurbMJNpHEHTA md5 None None 1 1 1 True 2024-05-01 18:50:44.790391+00:00 2024-05-01 18:50:45.206904+00:00 1
4 PQ3IwRUPU0JwH0fyYF3T 1 raw/raw_data_3.txt .txt None None None 10 IYGAyBwkgolA4McbYLhzeQ md5 None None 1 1 1 True 2024-05-01 18:50:44.794456+00:00 2024-05-01 18:50:45.239088+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='ZQeEa3yDRa81x7oF5evF', suffix='.txt', size=10, hash='tQ_LK-RWrqArVjEcOePwWg', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ returning existing artifact with same hash: Artifact(uid='PQ3IwRUPU0JwH0fyYF3T', key='raw/raw_data_3.txt', suffix='.txt', size=10, hash='IYGAyBwkgolA4McbYLhzeQ', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key raw/raw_data_3.txt on existing artifact differs from passed key None
❗ returning existing artifact with same hash: Artifact(uid='orsJ5LefcOqPTeQUAGRR', suffix='.txt', size=10, hash='3SbfSTTz_yG9aa98nV65rA', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ returning existing artifact with same hash: Artifact(uid='yiHDl7nOj5iITUlGAO8N', key='/raw/Collection_2/raw_data_2.txt', suffix='.txt', size=10, hash='OQ4C6OAwWwC2STRw7zCqJw', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key /raw/Collection_2/raw_data_2.txt on existing artifact differs from passed key None
❗ returning existing artifact with same hash: Artifact(uid='OIX9BImg9qgNcGaaL590', key='/raw/Collection_2/raw_data_3.txt', suffix='.txt', size=10, hash='dRv2rX8hheftMPIWQ-QHkA', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key /raw/Collection_2/raw_data_3.txt on existing artifact differs from passed key None
❗ returning existing artifact with same hash: Artifact(uid='OUju2yiMW3ly7Ta3Qdhd', key='/raw/Collection_2/raw_data_4.txt', suffix='.txt', size=10, hash='cmjWH7AkMKIX5vrTT_lQJA', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key /raw/Collection_2/raw_data_4.txt on existing artifact differs from passed key None
❗ returning existing artifact with same hash: Artifact(uid='wKusPUWcvxiEtGVHWo34', key='/raw/Collection_2/raw_data_1.txt', suffix='.txt', size=10, hash='_TL6_hx6Keu5Afp8XeE-tw', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key /raw/Collection_2/raw_data_1.txt on existing artifact differs from passed key None
❗ returning existing artifact with same hash: Artifact(uid='CDLVJh6pnwQJYOrQmkXz', key='/raw/Collection_1/raw_data_2.txt', suffix='.txt', size=10, hash='Y9QypnlDOWFZt5FaCJzryA', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key /raw/Collection_1/raw_data_2.txt on existing artifact differs from passed key None
❗ returning existing artifact with same hash: Artifact(uid='SgsI4LhpxhPdYMsYOG6y', key='/raw/Collection_1/raw_data_3.txt', suffix='.txt', size=10, hash='H5v8UBpZL3DpgMQNb4jWNQ', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key /raw/Collection_1/raw_data_3.txt on existing artifact differs from passed key None
❗ returning existing artifact with same hash: Artifact(uid='Ol8eR9Wsubw1Uqt1VUvJ', key='/raw/Collection_1/raw_data_4.txt', suffix='.txt', size=10, hash='ui0LDcEc06RYvvifqzuvOg', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key /raw/Collection_1/raw_data_4.txt on existing artifact differs from passed key None
❗ returning existing artifact with same hash: Artifact(uid='qflMiD941Io3sNprvOWm', key='/raw/Collection_1/raw_data_1.txt', suffix='.txt', size=10, hash='lmVBwrXPsXW8jX474wHf7g', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key /raw/Collection_1/raw_data_1.txt on existing artifact differs from passed key None
ln.Collection.filter(name__icontains="data").df()
uid name description version hash reference reference_type transform_id run_id artifact_id visibility created_at updated_at created_by_id
id
1 RlC9hVY2yjPoCeqryHZk data None None 3TFs81ohbCIQFQ9vGlC3 None None 1 1 None 1 2024-05-01 18:50:45.437133+00:00 2024-05-01 18:50:45.437157+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='ZQeEa3yDRa81x7oF5evF', suffix='.txt', size=10, hash='tQ_LK-RWrqArVjEcOePwWg', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key None on existing artifact differs from passed key /raw/raw_data_2.txt
❗ returning existing artifact with same hash: Artifact(uid='PQ3IwRUPU0JwH0fyYF3T', key='raw/raw_data_3.txt', suffix='.txt', size=10, hash='IYGAyBwkgolA4McbYLhzeQ', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_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(uid='orsJ5LefcOqPTeQUAGRR', suffix='.txt', size=10, hash='3SbfSTTz_yG9aa98nV65rA', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ key None on existing artifact differs from passed key /raw/raw_data_1.txt
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(uid='yiHDl7nOj5iITUlGAO8N', key='/raw/Collection_2/raw_data_2.txt', suffix='.txt', size=10, hash='OQ4C6OAwWwC2STRw7zCqJw', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(uid='OIX9BImg9qgNcGaaL590', key='/raw/Collection_2/raw_data_3.txt', suffix='.txt', size=10, hash='dRv2rX8hheftMPIWQ-QHkA', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(uid='OUju2yiMW3ly7Ta3Qdhd', key='/raw/Collection_2/raw_data_4.txt', suffix='.txt', size=10, hash='cmjWH7AkMKIX5vrTT_lQJA', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(uid='wKusPUWcvxiEtGVHWo34', key='/raw/Collection_2/raw_data_1.txt', suffix='.txt', size=10, hash='_TL6_hx6Keu5Afp8XeE-tw', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(uid='CDLVJh6pnwQJYOrQmkXz', key='/raw/Collection_1/raw_data_2.txt', suffix='.txt', size=10, hash='Y9QypnlDOWFZt5FaCJzryA', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(uid='SgsI4LhpxhPdYMsYOG6y', key='/raw/Collection_1/raw_data_3.txt', suffix='.txt', size=10, hash='H5v8UBpZL3DpgMQNb4jWNQ', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(uid='Ol8eR9Wsubw1Uqt1VUvJ', key='/raw/Collection_1/raw_data_4.txt', suffix='.txt', size=10, hash='ui0LDcEc06RYvvifqzuvOg', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
❗ loaded ULabel record with same name: 'data' (disable via `ln.settings.upon_create_search_names`)
❗ returning existing artifact with same hash: Artifact(uid='qflMiD941Io3sNprvOWm', key='/raw/Collection_1/raw_data_1.txt', suffix='.txt', size=10, hash='lmVBwrXPsXW8jX474wHf7g', hash_type='md5', visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_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()
uid storage_id key suffix accessor description version size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
1 orsJ5LefcOqPTeQUAGRR 1 None .txt None None None 10 3SbfSTTz_yG9aa98nV65rA md5 None None 1 1 1 True 2024-05-01 18:50:44.765449+00:00 2024-05-01 18:50:45.531538+00:00 1
2 ZQeEa3yDRa81x7oF5evF 1 None .txt None None None 10 tQ_LK-RWrqArVjEcOePwWg md5 None None 1 1 1 True 2024-05-01 18:50:44.770542+00:00 2024-05-01 18:50:45.476339+00:00 1
4 PQ3IwRUPU0JwH0fyYF3T 1 raw/raw_data_3.txt .txt None None None 10 IYGAyBwkgolA4McbYLhzeQ md5 None None 1 1 1 True 2024-05-01 18:50:44.794456+00:00 2024-05-01 18:50:45.491545+00:00 1
5 yiHDl7nOj5iITUlGAO8N 1 /raw/Collection_2/raw_data_2.txt .txt None None None 10 OQ4C6OAwWwC2STRw7zCqJw md5 None None 1 1 1 True 2024-05-01 18:50:45.256721+00:00 2024-05-01 18:50:45.554038+00:00 1
6 OIX9BImg9qgNcGaaL590 1 /raw/Collection_2/raw_data_3.txt .txt None None None 10 dRv2rX8hheftMPIWQ-QHkA md5 None None 1 1 1 True 2024-05-01 18:50:45.265733+00:00 2024-05-01 18:50:45.575237+00:00 1
7 OUju2yiMW3ly7Ta3Qdhd 1 /raw/Collection_2/raw_data_4.txt .txt None None None 10 cmjWH7AkMKIX5vrTT_lQJA md5 None None 1 1 1 True 2024-05-01 18:50:45.273465+00:00 2024-05-01 18:50:45.597158+00:00 1
8 wKusPUWcvxiEtGVHWo34 1 /raw/Collection_2/raw_data_1.txt .txt None None None 10 _TL6_hx6Keu5Afp8XeE-tw md5 None None 1 1 1 True 2024-05-01 18:50:45.281542+00:00 2024-05-01 18:50:45.619057+00:00 1
9 CDLVJh6pnwQJYOrQmkXz 1 /raw/Collection_1/raw_data_2.txt .txt None None None 10 Y9QypnlDOWFZt5FaCJzryA md5 None None 1 1 1 True 2024-05-01 18:50:45.289942+00:00 2024-05-01 18:50:45.640295+00:00 1
10 SgsI4LhpxhPdYMsYOG6y 1 /raw/Collection_1/raw_data_3.txt .txt None None None 10 H5v8UBpZL3DpgMQNb4jWNQ md5 None None 1 1 1 True 2024-05-01 18:50:45.297411+00:00 2024-05-01 18:50:45.662805+00:00 1
11 Ol8eR9Wsubw1Uqt1VUvJ 1 /raw/Collection_1/raw_data_4.txt .txt None None None 10 ui0LDcEc06RYvvifqzuvOg md5 None None 1 1 1 True 2024-05-01 18:50:45.305102+00:00 2024-05-01 18:50:45.684189+00:00 1
12 qflMiD941Io3sNprvOWm 1 /raw/Collection_1/raw_data_1.txt .txt None None None 10 lmVBwrXPsXW8jX474wHf7g md5 None None 1 1 1 True 2024-05-01 18:50:45.314273+00:00 2024-05-01 18:50:45.708389+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.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_4.txt
πŸ’‘ path content will be copied to default storage upon `save()` with key './/preprocessed/result_4.txt'
βœ… storing artifact 'ArVHNUQa23tl2K84p2MT' 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 '8nnRNTO2IqM4rIYAbnzB' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/preprocessed/result_3.txt'
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 '3vaZheeVXRMCeSMKUDuP' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/preprocessed/result_2.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 'oGkk41rBxPfYcIKLGcXw' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/preprocessed/result_1.txt'
ln.Artifact.df()
uid storage_id key suffix accessor description version size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
16 oGkk41rBxPfYcIKLGcXw 1 .//preprocessed/result_1.txt .txt None None None 10 xqhkX6I9IqCfVjt8GE5nxQ md5 None None 1 1 1 False 2024-05-01 18:50:45.803451+00:00 2024-05-01 18:50:45.803485+00:00 1
15 3vaZheeVXRMCeSMKUDuP 1 .//preprocessed/result_2.txt .txt None None None 10 wRZNZYZ06A1WEpo1bYku7g md5 None None 1 1 1 False 2024-05-01 18:50:45.795430+00:00 2024-05-01 18:50:45.795464+00:00 1
14 8nnRNTO2IqM4rIYAbnzB 1 .//preprocessed/result_3.txt .txt None None None 10 z0-I8oRLvehi9gdM_66T0g md5 None None 1 1 1 False 2024-05-01 18:50:45.786897+00:00 2024-05-01 18:50:45.786919+00:00 1
13 ArVHNUQa23tl2K84p2MT 1 .//preprocessed/result_4.txt .txt None None None 10 01rRxkzXUVac49KTA1UOnA md5 None None 1 1 1 False 2024-05-01 18:50:45.778332+00:00 2024-05-01 18:50:45.778357+00:00 1
12 qflMiD941Io3sNprvOWm 1 /raw/Collection_1/raw_data_1.txt .txt None None None 10 lmVBwrXPsXW8jX474wHf7g md5 None None 1 1 1 True 2024-05-01 18:50:45.314273+00:00 2024-05-01 18:50:45.708389+00:00 1
11 Ol8eR9Wsubw1Uqt1VUvJ 1 /raw/Collection_1/raw_data_4.txt .txt None None None 10 ui0LDcEc06RYvvifqzuvOg md5 None None 1 1 1 True 2024-05-01 18:50:45.305102+00:00 2024-05-01 18:50:45.684189+00:00 1
10 SgsI4LhpxhPdYMsYOG6y 1 /raw/Collection_1/raw_data_3.txt .txt None None None 10 H5v8UBpZL3DpgMQNb4jWNQ md5 None None 1 1 1 True 2024-05-01 18:50:45.297411+00:00 2024-05-01 18:50:45.662805+00:00 1
9 CDLVJh6pnwQJYOrQmkXz 1 /raw/Collection_1/raw_data_2.txt .txt None None None 10 Y9QypnlDOWFZt5FaCJzryA md5 None None 1 1 1 True 2024-05-01 18:50:45.289942+00:00 2024-05-01 18:50:45.640295+00:00 1
8 wKusPUWcvxiEtGVHWo34 1 /raw/Collection_2/raw_data_1.txt .txt None None None 10 _TL6_hx6Keu5Afp8XeE-tw md5 None None 1 1 1 True 2024-05-01 18:50:45.281542+00:00 2024-05-01 18:50:45.619057+00:00 1
7 OUju2yiMW3ly7Ta3Qdhd 1 /raw/Collection_2/raw_data_4.txt .txt None None None 10 cmjWH7AkMKIX5vrTT_lQJA md5 None None 1 1 1 True 2024-05-01 18:50:45.273465+00:00 2024-05-01 18:50:45.597158+00:00 1
6 OIX9BImg9qgNcGaaL590 1 /raw/Collection_2/raw_data_3.txt .txt None None None 10 dRv2rX8hheftMPIWQ-QHkA md5 None None 1 1 1 True 2024-05-01 18:50:45.265733+00:00 2024-05-01 18:50:45.575237+00:00 1
5 yiHDl7nOj5iITUlGAO8N 1 /raw/Collection_2/raw_data_2.txt .txt None None None 10 OQ4C6OAwWwC2STRw7zCqJw md5 None None 1 1 1 True 2024-05-01 18:50:45.256721+00:00 2024-05-01 18:50:45.554038+00:00 1
1 orsJ5LefcOqPTeQUAGRR 1 None .txt None None None 10 3SbfSTTz_yG9aa98nV65rA md5 None None 1 1 1 True 2024-05-01 18:50:44.765449+00:00 2024-05-01 18:50:45.531538+00:00 1
4 PQ3IwRUPU0JwH0fyYF3T 1 raw/raw_data_3.txt .txt None None None 10 IYGAyBwkgolA4McbYLhzeQ md5 None None 1 1 1 True 2024-05-01 18:50:44.794456+00:00 2024-05-01 18:50:45.491545+00:00 1
2 ZQeEa3yDRa81x7oF5evF 1 None .txt None None None 10 tQ_LK-RWrqArVjEcOePwWg md5 None None 1 1 1 True 2024-05-01 18:50:44.770542+00:00 2024-05-01 18:50:45.476339+00:00 1
3 srHVoFNYQDpxWDeDM3PN 1 raw/raw_data_3.txt .txt None None None 10 LQ7cAxaiGuurbMJNpHEHTA md5 None None 1 1 1 True 2024-05-01 18:50:44.790391+00:00 2024-05-01 18:50:45.206904+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/wKusPUWcvxiEtGVHWo34.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()
uid storage_id key suffix accessor description version size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
8 wKusPUWcvxiEtGVHWo34 1 /raw/Collection_2/raw_data_1.txt .txt None None None 10 _TL6_hx6Keu5Afp8XeE-tw md5 None None 1 1 1 True 2024-05-01 18:50:45.281542+00:00 2024-05-01 18:50:45.619057+00:00 1
16 oGkk41rBxPfYcIKLGcXw 1 .//preprocessed/result_1.txt .txt None None None 10 xqhkX6I9IqCfVjt8GE5nxQ md5 None None 1 1 1 False 2024-05-01 18:50:45.803451+00:00 2024-05-01 18:50:45.803485+00:00 1

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

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 'eGRYBsolR07lzuwfTaLc' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/eGRYBsolR07lzuwfTaLc.tsv.gz'
βœ… storing artifact 'qKlpMaFqVsdyTcZYPLGI' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/qKlpMaFqVsdyTcZYPLGI.tsv.gz'
βœ… storing artifact 'MQrRbxPYY1BcWyfUGmvO' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/MQrRbxPYY1BcWyfUGmvO.mtx.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/mvNEsfOzHbGzm9c9')
βœ… storing artifact 'mvNEsfOzHbGzm9c9PMt8' at '/home/runner/work/lamindb/lamindb/docs/faq/key-eval/.lamindb/mvNEsfOzHbGzm9c9'
Artifact(uid='mvNEsfOzHbGzm9c9PMt8', suffix='', description='cellranger raw', size=18, hash='R-pBNmsQjoZl4qTb1_dRYQ', hash_type='md5-d', n_objects=3, visibility=1, key_is_virtual=True, updated_at=2024-05-01 18:50:45 UTC, storage_id=1, transform_id=1, run_id=1, created_by_id=1)
ln.Artifact.filter(key__icontains="raw_feature_bc_matrix").df()
uid storage_id key suffix accessor description version size hash hash_type n_objects n_observations transform_id run_id visibility key_is_virtual created_at updated_at created_by_id
id
17 eGRYBsolR07lzuwfTaLc 1 raw_feature_bc_matrix/barcodes.tsv.gz .tsv.gz None None None 6 lSlBpZ_qpRbrHW1MsYahTw md5 None None 1 1 1 True 2024-05-01 18:50:45.933100+00:00 2024-05-01 18:50:45.933124+00:00 1
18 qKlpMaFqVsdyTcZYPLGI 1 raw_feature_bc_matrix/features.tsv.gz .tsv.gz None None None 6 2JPs0bbW29xehiOHMntX-Q md5 None None 1 1 1 True 2024-05-01 18:50:45.937120+00:00 2024-05-01 18:50:45.937143+00:00 1
19 MQrRbxPYY1BcWyfUGmvO 1 raw_feature_bc_matrix/matrix.mtx.gz .mtx.gz None None None 6 Rp2m5MgtpoLVKWbPhzB1iQ md5 None None 1 1 1 True 2024-05-01 18:50:45.941396+00:00 2024-05-01 18:50:45.941432+00:00 1
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/MQrRbxPYY1BcWyfUGmvO.mtx.gz')
artifact = ln.Artifact.filter(description="cellranger raw").one()
artifact.path.glob("*")
<generator object Path.glob at 0x7fed2577abd0>