Skip to content

Commit

Permalink
Merge pull request #6643 from samantha-ho/samanthaho/DatasetDefinitio…
Browse files Browse the repository at this point in the history
…n_metadata

Add metadata attribute to DataSetDefinition
  • Loading branch information
samantha-ho authored Nov 21, 2024
2 parents 8e9bf7c + 71eb451 commit 3956de5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changes/newsfragments/6643.improved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improves the DataSetDefinition class to allow users to specify metadata to be added to the resulting dataset
8 changes: 8 additions & 0 deletions src/qcodes/dataset/measurement_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class DataSetDefinition:
experiment: Experiment | None = None
"""An optional argument specifying which Experiment this dataset should be
written to"""
metadata: dict[str, Any] | None = None
"""An optional dictionary of metadata that will be added to the dataset
generated by this definition"""


def setup_measurement_instances(
Expand Down Expand Up @@ -107,6 +110,11 @@ def datasaver_builder(
stack.enter_context(measurement.run(parent_span=datasaver_builder_span))
for measurement in measurement_instances
]
for i, datasaver in enumerate(datasavers):
ds_def = dataset_definitions[i]
if ds_def.metadata is not None:
for key, value in ds_def.metadata.items():
datasaver.dataset.add_metadata(tag=key, metadata=value)
yield datasavers


Expand Down
14 changes: 13 additions & 1 deletion tests/dataset/test_measurement_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,17 @@ def default_database_and_experiment_(tmp_path):
def test_context(default_params, default_database_and_experiment):
_ = default_database_and_experiment
set1, set2, set3, meas1, meas2, meas3 = default_params
metadata_dict = { # Nesting dictionaries does NOT work here
"test_metadata": "test_meta_value_1",
"test_metadata_2": "test_meta_value_2",
}
dataset_definition = [
DataSetDefinition(name="dataset_1", independent=[set1], dependent=[meas1]),
DataSetDefinition(
name="dataset_1",
independent=[set1],
dependent=[meas1],
metadata=metadata_dict,
),
DataSetDefinition(
name="dataset_2", independent=[set1, set2, set3], dependent=[meas2, meas3]
),
Expand Down Expand Up @@ -133,6 +142,8 @@ def test_context(default_params, default_database_and_experiment):
},
data_vars=(meas1.name,),
)
assert datasets[0].metadata == metadata_dict

assert_dataset_as_expected(
datasets[1],
dims_dict={
Expand All @@ -142,6 +153,7 @@ def test_context(default_params, default_database_and_experiment):
},
data_vars=(meas2.name, meas3.name),
)
assert datasets[1].metadata == {}


def test_dond_into(default_params, default_database_and_experiment):
Expand Down

0 comments on commit 3956de5

Please sign in to comment.