|
5 | 5 | import xarray as xr |
6 | 6 |
|
7 | 7 | import qcodes |
8 | | -from qcodes.dataset import new_data_set |
| 8 | +from qcodes.dataset import load_from_netcdf, new_data_set |
9 | 9 | from qcodes.dataset.descriptions.dependencies import InterDependencies_ |
10 | 10 | from qcodes.dataset.descriptions.param_spec import ParamSpecBase |
11 | 11 | from qcodes.dataset.descriptions.versioning import serialization as serial |
@@ -97,6 +97,34 @@ def _make_mock_dataset_complex(): |
97 | 97 | return dataset |
98 | 98 |
|
99 | 99 |
|
| 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 | + |
100 | 128 | @pytest.mark.usefixtures('experiment') |
101 | 129 | def test_write_data_to_text_file_save(tmp_path_factory): |
102 | 130 | dataset = new_data_set("dataset") |
@@ -464,6 +492,32 @@ def test_to_xarray_da_dict_paramspec_metadata_is_preserved(mock_dataset_label_un |
464 | 492 | assert xr_da.attrs[spec_name] == spec_value |
465 | 493 |
|
466 | 494 |
|
| 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 | + |
467 | 521 | def _get_expected_param_spec_attrs(dataset, dim): |
468 | 522 | expected_attrs = dict(dataset.paramspecs[str(dim)]._to_dict()) |
469 | 523 | expected_attrs["units"] = expected_attrs["unit"] |
|
0 commit comments