Skip to content

Commit 50e6dab

Browse files
committed
Add warning on invalid shape
1 parent 3bedb01 commit 50e6dab

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

src/qcodes/dataset/exporters/export_to_xarray.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ def _add_inferred_data_vars(
113113
dims,
114114
flat.reshape(tuple(xr_dataset.sizes[d] for d in dims)),
115115
)
116+
else:
117+
_LOG.warning(
118+
"Cannot add inferred parameter '%s' to xarray dataset for '%s' "
119+
"(run_id=%s): data size %d does not match its parent parameter "
120+
"'%s' size %d. This is likely a user error in the measurement setup.",
121+
inf.name,
122+
name,
123+
dataset.run_id,
124+
flat.shape[0],
125+
parent_name,
126+
expected_size,
127+
)
116128

117129
return xr_dataset
118130

tests/dataset/test_parameter_with_setpoints_has_control.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
import logging
12
from typing import TYPE_CHECKING
23

34
import numpy as np
45
import numpy.testing as npt
6+
import xarray as xr
57

68
from qcodes.dataset import Measurement
9+
from qcodes.dataset.exporters.export_to_xarray import _add_inferred_data_vars
710
from qcodes.parameters import ManualParameter, ParameterWithSetpoints
811
from qcodes.validators import Arrays
912

1013
if TYPE_CHECKING:
14+
import pytest
15+
1116
from qcodes.dataset.experiment_container import Experiment
1217

1318

@@ -132,3 +137,57 @@ def unpack_self(self, value):
132137
# which differs from either individual dimension.
133138
assert "p1" in xds.data_vars
134139
npt.assert_array_almost_equal(xds["p1"].values, p1_all_arr)
140+
141+
142+
def test_parameter_with_setpoints_has_control_size_mismatch_warns(
143+
experiment: "Experiment", caplog: "pytest.LogCaptureFixture"
144+
) -> None:
145+
"""Test that a warning is emitted when the inferred parameter has a
146+
different data size than its parent parameter."""
147+
148+
class MySp(ParameterWithSetpoints):
149+
def unpack_self(self, value):
150+
res = super().unpack_self(value)
151+
res.append((p1, p1()))
152+
return res
153+
154+
mp_data = np.arange(10)
155+
156+
mp = ManualParameter("mp", vals=Arrays(shape=(10,)), initial_value=mp_data)
157+
p1 = ParameterWithSetpoints(
158+
"p1", vals=Arrays(shape=(10,)), setpoints=(mp,), set_cmd=None
159+
)
160+
p2 = MySp("p2", vals=Arrays(shape=(10,)), setpoints=(mp,), set_cmd=None)
161+
p2.has_control_of.add(p1)
162+
163+
p1(np.linspace(-1, 1, 10))
164+
p2(np.random.randn(10))
165+
166+
meas = Measurement()
167+
meas.register_parameter(p2)
168+
with meas.run() as ds:
169+
ds.add_result((p2, p2()))
170+
171+
# Build an xarray dataset and sub_dict with mismatched p1 data to
172+
# exercise the warning path in _add_inferred_data_vars directly.
173+
174+
raw_data = ds.dataset.get_parameter_data()
175+
sub_dict = dict(raw_data["p2"])
176+
# Replace p1 with wrong-sized data (5 instead of 10)
177+
sub_dict["p1"] = np.zeros(5)
178+
179+
xr_dataset = xr.Dataset(
180+
{"p2": (("mp",), sub_dict["p2"].ravel())},
181+
coords={"mp": sub_dict["mp"].ravel()},
182+
)
183+
184+
with caplog.at_level(
185+
logging.WARNING, logger="qcodes.dataset.exporters.export_to_xarray"
186+
):
187+
result = _add_inferred_data_vars(ds.dataset, "p2", sub_dict, xr_dataset)
188+
189+
assert "p1" not in result.data_vars
190+
assert any(
191+
"Cannot add inferred parameter 'p1'" in msg and "'p2'" in msg
192+
for msg in caplog.messages
193+
)

0 commit comments

Comments
 (0)