Skip to content

Commit a5916c7

Browse files
committed
Add test that roundtripping to xarray preserved order
1 parent ad2af85 commit a5916c7

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

qcodes/tests/dataset/test_dataset_export.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import xarray as xr
66

77
import qcodes
8-
from qcodes.dataset import new_data_set
8+
from qcodes.dataset import load_from_netcdf, new_data_set
99
from qcodes.dataset.descriptions.dependencies import InterDependencies_
1010
from qcodes.dataset.descriptions.param_spec import ParamSpecBase
1111
from qcodes.dataset.descriptions.versioning import serialization as serial
@@ -97,6 +97,34 @@ def _make_mock_dataset_complex():
9797
return dataset
9898

9999

100+
@pytest.mark.usefixtures("experiment")
101+
@pytest.fixture(name="mock_dataset_inverted_coords")
102+
def _make_mock_dataset_inverted_coords():
103+
# this dataset is constructed such
104+
# that the two z parameters have inverted
105+
# coordinates. You almost certainly
106+
# don't want to do this in a real dataset
107+
# but it enables the test to check that
108+
# the order is preserved.
109+
dataset = new_data_set("dataset")
110+
xparam = ParamSpecBase("x", "numeric")
111+
yparam = ParamSpecBase("y", "numeric")
112+
z1param = ParamSpecBase("z1", "numeric")
113+
z2param = ParamSpecBase("z2", "numeric")
114+
idps = InterDependencies_(
115+
dependencies={z1param: (xparam, yparam), z2param: (yparam, xparam)}
116+
)
117+
dataset.set_interdependencies(idps)
118+
119+
dataset.mark_started()
120+
for x in range(10):
121+
for y in range(20, 25):
122+
results = [{"x": x, "y": y, "z1": x + y, "z2": x - y}]
123+
dataset.add_results(results)
124+
dataset.mark_completed()
125+
return dataset
126+
127+
100128
@pytest.mark.usefixtures('experiment')
101129
def test_write_data_to_text_file_save(tmp_path_factory):
102130
dataset = new_data_set("dataset")
@@ -464,6 +492,32 @@ def test_to_xarray_da_dict_paramspec_metadata_is_preserved(mock_dataset_label_un
464492
assert xr_da.attrs[spec_name] == spec_value
465493

466494

495+
def test_inverted_coords_perserved_on_netcdf_roundtrip(
496+
tmp_path_factory, mock_dataset_inverted_coords
497+
):
498+
tmp_path = tmp_path_factory.mktemp("export_netcdf")
499+
path = str(tmp_path)
500+
mock_dataset_inverted_coords.export(
501+
export_type="netcdf", path=tmp_path, prefix="qcodes_"
502+
)
503+
504+
xr_ds = mock_dataset_inverted_coords.to_xarray_dataset()
505+
assert xr_ds["z1"].dims == ("x", "y")
506+
assert xr_ds["z2"].dims == ("y", "x")
507+
508+
expected_path = f"qcodes_{mock_dataset_inverted_coords.captured_run_id}_{mock_dataset_inverted_coords.guid}.nc"
509+
assert os.listdir(path) == [expected_path]
510+
file_path = os.path.join(path, expected_path)
511+
ds = load_from_netcdf(file_path)
512+
513+
with pytest.warns(UserWarning):
514+
xr_ds_reimported = ds.to_xarray_dataset()
515+
516+
assert xr_ds_reimported["z1"].dims == ("x", "y")
517+
assert xr_ds_reimported["z2"].dims == ("y", "x")
518+
assert xr_ds.identical(xr_ds_reimported)
519+
520+
467521
def _get_expected_param_spec_attrs(dataset, dim):
468522
expected_attrs = dict(dataset.paramspecs[str(dim)]._to_dict())
469523
expected_attrs["units"] = expected_attrs["unit"]

0 commit comments

Comments
 (0)