Skip to content

Commit 33f262d

Browse files
authored
Merge pull request #7222 from microsoft/copilot/fix-7221
Bug: load_from_netcdf fails if a dataset does is not completed
2 parents 40da9d9 + 499c1c9 commit 33f262d

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``load_from_netcdf`` failing for non-completed datasets due to missing ``completed_timestamp_raw`` or ``run_timestamp_raw`` attribute.

src/qcodes/dataset/data_set_in_memory.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@ def _load_from_netcdf(
279279
data = data[0]
280280
metadata[str(key)] = data
281281

282+
completed_timestamp_raw = getattr(
283+
loaded_data, "completed_timestamp_raw", None
284+
)
285+
if completed_timestamp_raw is not None:
286+
completed_timestamp_raw = float(completed_timestamp_raw)
287+
288+
run_timestamp_raw = getattr(loaded_data, "run_timestamp_raw", None)
289+
if run_timestamp_raw is not None:
290+
run_timestamp_raw = float(run_timestamp_raw)
291+
282292
ds = cls(
283293
run_id=run_id,
284294
captured_run_id=int(loaded_data.captured_run_id),
@@ -290,8 +300,8 @@ def _load_from_netcdf(
290300
sample_name=loaded_data.sample_name,
291301
guid=loaded_data.guid,
292302
path_to_db=path_to_db,
293-
run_timestamp_raw=float(loaded_data.run_timestamp_raw),
294-
completed_timestamp_raw=float(loaded_data.completed_timestamp_raw),
303+
run_timestamp_raw=run_timestamp_raw,
304+
completed_timestamp_raw=completed_timestamp_raw,
295305
metadata=metadata,
296306
rundescriber=serial.from_json_to_current(loaded_data.run_description),
297307
parent_dataset_links=parent_dataset_links,

tests/dataset/test_dataset_in_memory.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from qcodes.dataset import load_by_id, load_by_run_spec
1515
from qcodes.dataset.data_set_in_memory import DataSetInMem, load_from_file
1616
from qcodes.dataset.data_set_protocol import DataSetType
17+
from qcodes.dataset.descriptions.dependencies import InterDependencies_
18+
from qcodes.dataset.descriptions.param_spec import ParamSpecBase
1719
from qcodes.dataset.sqlite.connection import AtomicConnection, atomic_transaction
1820
from qcodes.station import Station
1921

@@ -511,6 +513,49 @@ def test_load_from_file_by_id(meas_with_registered_param, DMM, DAC, tmp_path) ->
511513
assert not isinstance(loaded_ds_from_db, DataSetInMem)
512514

513515

516+
def test_load_from_netcdf_non_completed_dataset(experiment, tmp_path) -> None:
517+
"""Test that non-completed datasets can be loaded from netcdf files."""
518+
# Create a non-completed dataset by NOT using the measurement context manager
519+
# which automatically completes the dataset on exit
520+
ds = DataSetInMem._create_new_run(name="test-dataset")
521+
522+
# Set up interdependencies with simple parameters following the established pattern
523+
x_param = ParamSpecBase("x", paramtype="numeric")
524+
y_param = ParamSpecBase("y", paramtype="numeric")
525+
idps = InterDependencies_(dependencies={y_param: (x_param,)})
526+
ds.prepare(interdeps=idps, snapshot={})
527+
528+
# Add some data points
529+
for x_val in np.linspace(0, 25, 5):
530+
y_val = x_val**2 # simple function
531+
ds._enqueue_results({x_param: np.array([x_val]), y_param: np.array([y_val])})
532+
533+
# Note: do NOT call ds.mark_completed() to keep it non-completed
534+
535+
# Verify that the dataset is not completed
536+
assert ds.completed_timestamp_raw is None
537+
assert not ds.completed
538+
539+
# Export the non-completed dataset to NetCDF
540+
ds.export(export_type="netcdf", path=str(tmp_path))
541+
542+
# Load the dataset from NetCDF
543+
loaded_ds = DataSetInMem._load_from_netcdf(
544+
tmp_path / f"qcodes_{ds.captured_run_id}_{ds.guid}.nc"
545+
)
546+
547+
# Verify that the loaded dataset is still non-completed
548+
assert isinstance(loaded_ds, DataSetInMem)
549+
assert loaded_ds.completed_timestamp_raw is None
550+
assert not loaded_ds.completed
551+
552+
# Compare other properties
553+
assert loaded_ds.captured_run_id == ds.captured_run_id
554+
assert loaded_ds.guid == ds.guid
555+
assert loaded_ds.name == ds.name
556+
assert loaded_ds.run_timestamp_raw == ds.run_timestamp_raw
557+
558+
514559
def test_load_from_netcdf_legacy_version(non_created_db) -> None:
515560
# Qcodes 0.26 exported netcdf files did not contain
516561
# the parent dataset links and used a different engine to write data

0 commit comments

Comments
 (0)