Quickstart
Goal
In this tutorial, the SDK is introduced to create, structure and upload a dataset with files and additional metadata. Several QDL concepts will be introduced like scope, dataset, file, collection, sequence and attributes.
Prerequisites
- The SDK, CLI and daemon have been installed.
- You must be a member of at least one scope to add data to QDL.
Note
If you do not know if you are a member of any scopes, ask your supervisor, who is likely a scope admin. You can check if you are part of any scopes by using the CLI:
Key concepts
QDL is mainly organized in scopes, datasets and files. Scopes represent long-running research initiatives containing a log of datasets ordered on time when they are collected. Datasets can represent measurements or simulation runs for instance. They are containers of files and always belong to one scope. The data is stored in these files, which always belong to one dataset. Collections, sequences and attributes are used to provide more structure, context and findability of datasets.
Login to QDL
Follow the post-installation steps if not already done so.
Configure an API token for authentication
To use the SDK without having to log in after each system reboot, a token can be configured:
The provided token will be printed to screen only once. So, you need to copy the token value and set it:
The token is meant to be secret and not to be shared between different users. If stored, it should be done securely. To remove a token, using the token created in the previous example:
Now the token is deprecated and can not be used anymore. But for the purpose of this tutorial, please have a token configured.
Note
These tokens do not have scope admin privileges, even if they are created by scope admins.
Initialize and create a dataset
To instantiate the dataset, a name and the unique identifier (UID) of the relevant scope needs to be defined. Subsequently, additional metadata can be added before creating the dataset. The management of files and directories is discussed later in this tutorial. Remember that each dataset is always part of one scope and one scope only.
Note
For dataset names, it is recommended to not use any arbitrary value,
but follow a structure which can be descriptive enough
with minimal information. Within QuTech the following standard is commonly applied:
YYYYmmDD-HHMMSS-sss-<random 6 character string>-<experiment name>,
e.g. 20250412-115613-abcdef-measuring-temperature
from qdl import Scope, Dataset
scope = "<SCOPE_UID>"
name = "<DATASET_UID>"
# double check if the scope exists
Scope.get(scope)
# create the dataset
dataset = Dataset(scope_uid=scope, name=name)
dataset.create()
print(dataset) # only prints the name of the dataset
print(dataset.to_dict()) # prints the entire dataset as a dictionary
By printing the dictionary of the dataset, you see that certain values like collected
(when the dataset has been generated) are generated by the SDK, but can also bet set after instantiation.
Note
A dataset can be uniquely identified by the combination of scope UID and dataset UID, or by the dataset universally unique identifier (UUID). If the UID is not provided when using the SDK, the dataset UID will be set by QDL after creation of the dataset. If the UID is provided, but is not unique within the provided scope, you will get an error. The dataset name does not need to be unique.
Retrieve a dataset
A dataset can be retrieved from QDL by specifying its scope and UID, or by its scope and name:
from qdl import Dataset
scope = "<SCOPE_UID>"
uid = "<DATASET_UID>"
name = "<DATASET_NAME>"
dataset = Dataset.get(scope, uid)
print(dataset)
dataset = Dataset.get_by_name(scope, name)
print(dataset)
Note
If multiple datasets are defined with the same name within the given scope,
the method get_by_name raises an error. You have to use the list method and search for name
Update a dataset
An existing dataset has immutable (e.g. scope, UID, UUID) and mutable fields.
To update a mutable field, we can change the field and update the dataset, for instance to increase the rank of a dataset.
The update method "pushes" the changes made to the local dataset to QDL,
and must be called whenever dataset changes should become permanent.
from qdl import Dataset
scope = "<SCOPE_UID>"
uid = "<DATASET_UID>"
dataset = Dataset.get(scope, uid)
dataset.rank = 3
dataset.update()
Add files to a dataset and upload
A dataset can contain multiple files and/or directories. This is demonstrated in the snippet below by adding
a simple text file to a dataset. When files are added to a new dataset,
you first need to create the dataset before uploading. The file path on the local machine is referred to
as "local path" in all methods related to files, e.g. add_file. When stored in QDL, the file path is referred to as
"qdl path". The "qdl path" needs to be unique for each file in the dataset.
Add and upload one file
from qdl import Dataset
scope = "<SCOPE_UID>"
name = "<DATASET_NAME>"
dataset = Dataset(scope_uid=scope, name=name)
# create the files in the current directory
for i in range(2):
with open(file=f"random_file_{i}.txt", mode="w", encoding="utf-8") as file:
file.write("hello world!")
# add a file to the root directory of the dataset
dataset.add_file("random_file_0.txt")
# add a file to a subdirectory in the dataset with a different name
dataset.add_file(local_path="random_file_1.txt", qdl_path="random/file.txt")
dataset.create()
dataset.upload()
Info
When files are added to a dataset, this is referring to the metadata of the files (e.g. the file path).
The actual file contents are uploaded to QDL with the upload method. This is done asynchronous (in the background)
by default. In scenarios where the code needs to wait for the upload to finish, the parameter blocked
must be set to true.
Add and upload a directory
The snippet below creates a folder in the current working directory with a few files, and adds them to an existing dataset. Note that now you don't need to create or update beforehand.
from os import makedirs
from qdl import Dataset
scope = "<SCOPE_UID>"
uid = "<DATASET_UID>"
dataset = Dataset.get(scope_uid=scope, uid=uid)
# create the directories for the files
makedirs("items_root", exist_ok=True)
makedirs("items_sub", exist_ok=True)
# create four files in each directory
for i in range(4):
with open(file=f"items_root/root_file_{i}.txt", mode="w", encoding="utf-8") as file:
file.write("hello world!")
with open(file=f"items_sub/sub_file_{i}.txt", mode="w", encoding="utf-8") as file:
file.write("hello world!")
# add the files in the root directory to the root directory of the dataset
dataset.add_dir("items_root")
# add the files in the subdirectory to a subdirectory in the dataset
dataset.add_dir(local_path="items_sub", qdl_path="subdirectory")
dataset.files_by_name #this gets you the dictionary of files within the dataset by filename
dataset.upload()
After adding the directories from the previous snippet, the file structure in the dataset is the following:
root/
│ root_file_0.txt
│ root_file_1.txt
│ root_file_2.txt
│ root_file_3.txt
│
└─── subdirectory/
│ │ sub_file_0.txt
│ │ sub_file_1.txt
│ │ sub_file_2.txt
│ │ sub_file_3.txt
Note
The same file can be added to QDL with the same name, but in different directories. This is allowed, but not recommended!
Warning
Changing the file contents during an upload will cause the upload to fail!
Download files
The download method downloads the files of a dataset in the current working directory under the scope UID
and dataset UID:
from qdl import Dataset
scope = "<SCOPE_UID>"
uid = "<DATASET_UID>"
dataset = Dataset.get(scope, uid)
dataset.download()
The file paths in this directory are identical to their QDL paths. Using the example from the add directory section, the downloaded directory will have the following structure:
<SCOPE_UID>/
└── <DATASET_UID>/
│ root_file_0.txt
│ root_file_1.txt
│ root_file_2.txt
│ root_file_3.txt
│
└─── subdirectory/
│ │ sub_file_0.txt
│ │ sub_file_1.txt
│ │ sub_file_2.txt
│ │ sub_file_3.txt
Instead of using the current working directory, another download directory can be given using the local_path
parameter. Similar to the upload method, if the code needs to wait for the download to finish, the blocked
parameter can be used.
Note
When the local_path parameter is used, the <SCOPE_UID> folder will not be created!
Add attributes to a dataset
To find datasets more easily, attributes can be added. Attributes are key-value pairs. The values can be either strings or numerical. When adding attributes with string values, by default, only a limited set of keys are allowed. When adding attributes with numerical values, there is no restriction on the keys by default.
Add an attribute
Attributes are added to a dataset using:
from qdl import Dataset
scope = "<SCOPE_UID>"
uid = "<DATASET_UID>"
dataset = Dataset.get(scope_uid=scope, uid=uid)
dataset.add_attribute(key="project", value="workshop")
dataset.add_attribute(key="setup", value="x")
dataset.add_attribute(key="laser_power", value=20)
dataset.update()
Note that here we first retrieve an existing dataset and then update, but you can also add attributes to a new dataset.
Use the attribute in a query
When using the list method we can filter the list by dataset fields but also on attributes:
from qdl import Dataset
scope = "<SCOPE_UID>"
datasets = Dataset.list(scope_uid=scope, project="workshop")
To filter the list on numerical values, one of the following operators has to be added to the key as a suffix:
__gt, __lt or __eq.
Below we list datasets with attribute key laser_power being greater than 10:
Add a dataset to a collection
A collection is an unordered set of datasets based on common factors belonging to the datasets. A dataset can be added to multiple collections. Like datasets, collections can be defined without specifying a UID. In this case, the name is equal to its UID and needs to be unique.
Add a collection
from qdl import Collection, Dataset
scope_uid = "<SCOPE_UID>"
collection_uid = "<COLLECTION_UID>"
dataset_uid = "<DATASET_UID>"
collection = Collection(scope_uid=scope_uid, name=collection_uid)
collection.create()
dataset = Dataset.get(scope_uid=scope_uid, uid=dataset_uid)
dataset.add_collection(collection.uid)
dataset.update()
Use the collection in a query
from qdl import Dataset
scope = "<SCOPE_UID>"
collection = "<COLLECTION_UID>"
datasets = Dataset.list(scope_uid=scope, collection_names=[collection])
Note
As a dataset can be added to multiple collections, a list of strings is expected for the collection_names
parameter.
Add a dataset to a sequence
Another way of adding metadata to a dataset is by adding it to a sequence. A sequence is an ordered set of datasets that are linked together. Unlike a collection, a dataset can only be added to one sequence.
Add a sequence
from qdl import Dataset, Sequence
scope_uid = "<SCOPE_UID>"
sequence_uid = "<SEQUENCE_UID>"
dataset_uid = "<DATASET_UID>"
sequence = Sequence(scope_uid=scope_uid, name=sequence_uid)
sequence.create()
dataset = Dataset.get(scope_uid=scope_uid, uid=dataset_uid)
dataset.add_sequence(sequence.uid)
dataset.update()
Note that as long as a sequence is not immutable, you can overwrite the sequence of a dataset.
Use the sequence in a query
from qdl import Dataset
scope = "<SCOPE_UID>"
sequence = "<SEQUENCE_UID>"
datasets = Dataset.list(scope_uid=scope, sequence_uid=sequence)
Find datasets based on text
Additional metadata can be added to the dataset, like tags, descriptions etc. All of these are attributes of the Dataset instance which can be set. QDL is capable of finding datasets based on text contained in these attributes. Earlier mentioned options like collections and attributes are more structured ways of storing and finding datasets, but the text search can be helpful in certain situations.
from qdl import Dataset
scope = "<SCOPE_UID>"
# query based on multiple words contained in the dataset
query_1 = "never, gonna, give, you, up"
# query based on a sentence
query_2 = "never gonna let you down"
# query based on both, mind the double quotes!
query_3 = '"never gonna run around", and, desert, you'
datasets = Dataset.list(scope_uid=scope, q=query_1)
Attribute selection
It can be convenient to have an idea of which attributes are added to datasets, especially when listing datasets. To this end, related attributes within a scope can be queried. Two attributes are related, if they are both at least once used as attributes for the same dataset.
from qdl import Scope
scope = "<SCOPE_UID>"
attributes = Scope.list_related_attributes(uid=scope, sample="some_sample", setup="some_setup")
Further reading
To find practical information about advanced features of QDL, consult the how-to guides. When setting up QDL in a measurement lab, the sync service can be especially convenient to setup. For an explanation of the concepts used in QDL, consult the topic guides.