Skip to content

Commit 0b677f6

Browse files
jenshnielsenCopilot
andcommitted
Handle multiple inferred-from parents in _add_inferred_data_vars
Instead of assuming a single parent by taking inferred_from_params[0], iterate over all inferred-from parents and use the first one whose flattened data size matches. Log a warning listing all available parents when no match is found. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 35a5f5f commit 0b677f6

1 file changed

Lines changed: 27 additions & 18 deletions

File tree

src/qcodes/dataset/exporters/export_to_xarray.py

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

102-
# Only add if the data has the same size as the parameter
103-
# it is inferred from
104-
inferred_from_params = interdeps.inferences.get(inf, ())
105-
if len(inferred_from_params) == 0:
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:
106107
continue
107-
parent_name = inferred_from_params[0].name
108-
if parent_name not in sub_dict:
109-
continue
110-
expected_size = sub_dict[parent_name].ravel().shape[0]
111-
if flat.shape[0] == expected_size:
112-
xr_dataset[inf.name] = (
113-
dims,
114-
flat.reshape(tuple(xr_dataset.sizes[d] for d in dims)),
115-
)
116-
else:
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+
]
117126
_LOG.warning(
118127
"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.",
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.",
121131
inf.name,
122132
name,
123133
dataset.run_id,
124134
flat.shape[0],
125-
parent_name,
126-
expected_size,
135+
available_parents,
127136
)
128137

129138
return xr_dataset

0 commit comments

Comments
 (0)