Skip to content

Commit 12ab522

Browse files
jenshnielsenCopilot
andcommitted
Fix reshape crash in _add_inferred_data_vars with multiple parents
Check inferred parameter data size against the xr_dataset dimensions directly instead of iterating over individual parent sizes. The previous logic could match a parent whose size differed from the dataset grid, causing a ValueError on reshape. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0b677f6 commit 12ab522

2 files changed

Lines changed: 388 additions & 28 deletions

File tree

src/qcodes/dataset/exporters/export_to_xarray.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -99,40 +99,26 @@ def _add_inferred_data_vars(
9999
else:
100100
flat = inf_data.ravel()
101101

102-
# Only add if the data has the same size as one of the parameters
103-
# it is inferred from. A parameter may be inferred from multiple
104-
# parents so we iterate over all of them.
105-
inferred_from_params = interdeps.inferences.get(inf)
106-
if not inferred_from_params:
107-
continue
108-
109-
matched_parent = False
110-
for parent in inferred_from_params:
111-
if parent.name not in sub_dict:
112-
continue
113-
expected_size = sub_dict[parent.name].ravel().shape[0]
114-
if flat.shape[0] == expected_size:
115-
xr_dataset[inf.name] = (
116-
dims,
117-
flat.reshape(tuple(xr_dataset.sizes[d] for d in dims)),
118-
)
119-
matched_parent = True
120-
break
121-
122-
if not matched_parent:
123-
available_parents = [
124-
p.name for p in inferred_from_params if p.name in sub_dict
125-
]
102+
# Only add if the flattened data can be reshaped to the dataset
103+
# dimensions. This is more robust than checking individual parent
104+
# sizes because an inferred parameter may have multiple parents
105+
# with different sizes.
106+
expected_shape = tuple(xr_dataset.sizes[d] for d in dims)
107+
expected_size = prod(expected_shape)
108+
if flat.shape[0] == expected_size:
109+
xr_dataset[inf.name] = (dims, flat.reshape(expected_shape))
110+
else:
126111
_LOG.warning(
127112
"Cannot add inferred parameter '%s' to xarray dataset for '%s' "
128-
"(run_id=%s): data size %d does not match any of its parent "
129-
"parameters %s. This is likely a user error in the measurement "
130-
"setup.",
113+
"(run_id=%s): data size %d does not match the dataset "
114+
"dimensions %s (size %d). This is likely a user error in the "
115+
"measurement setup.",
131116
inf.name,
132117
name,
133118
dataset.run_id,
134119
flat.shape[0],
135-
available_parents,
120+
dict(zip(dims, expected_shape)),
121+
expected_size,
136122
)
137123

138124
return xr_dataset

0 commit comments

Comments
 (0)