Skip to content

Commit 4fd8248

Browse files
committed
Export inferred parameters related to dependencies
1 parent 225e84c commit 4fd8248

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

src/qcodes/dataset/exporters/export_to_xarray.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,52 @@ def _xarray_data_array_direct(
162162
import xarray as xr
163163

164164
meas_paramspec = dataset.description.interdeps.graph.nodes[name]["value"]
165-
_, deps, _ = dataset.description.interdeps.all_parameters_in_tree_by_group(
165+
_, deps, inferred = dataset.description.interdeps.all_parameters_in_tree_by_group(
166166
meas_paramspec
167167
)
168-
dep_axis = {}
168+
# Build coordinate axes from direct dependencies preserving their order
169+
dep_axis: dict[str, npt.NDArray] = {}
169170
for axis, dep in enumerate(deps):
170171
dep_array = subdict[dep.name]
171172
dep_axis[dep.name] = dep_array[
172173
tuple(slice(None) if i == axis else 0 for i in range(dep_array.ndim))
173174
]
174175

175-
da = xr.Dataset(
176+
extra_coords: dict[str, tuple[tuple[str, ...] | tuple[str], npt.NDArray]] = {}
177+
for inf in inferred:
178+
# skip parameters already used as primary coordinate axes
179+
if inf.name in dep_axis:
180+
continue
181+
# add only if data for this parameter is available
182+
if inf.name not in subdict:
183+
continue
184+
185+
inf_related = dataset.description.interdeps.find_all_parameters_in_tree(inf)
186+
187+
related_deps = inf_related.intersection(set(deps))
188+
related_top_level = inf_related.intersection({meas_paramspec})
189+
190+
if len(related_top_level) > 0:
191+
raise NotImplementedError(
192+
"Adding inferred coords related to top level param is not yet supported"
193+
)
194+
195+
inf_data = subdict[inf.name][
196+
tuple(slice(None) if dep in related_deps else 0 for dep in deps)
197+
]
198+
inf_coords = [dep.name for dep in deps if dep in related_deps]
199+
200+
extra_coords[inf.name] = (tuple(inf_coords), inf_data)
201+
202+
# Compose coordinates dict including dependency axes and extra inferred coords
203+
coords: dict[str, tuple[tuple[str, ...] | tuple[str], npt.NDArray] | npt.NDArray]
204+
coords = {**dep_axis, **extra_coords}
205+
206+
ds = xr.Dataset(
176207
{name: (tuple(dep_axis.keys()), subdict[name])},
177-
coords=dep_axis,
178-
)[name]
179-
return da
208+
coords=coords,
209+
)
210+
return ds[name]
180211

181212

182213
def load_to_xarray_dataarray_dict(

tests/dataset/measurement/test_inferred_parameters_fix.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,25 @@ def test_inferred_parameters_in_actual_measurement_2d(
305305
)
306306

307307
assert "meas_parameter" in xarr.data_vars
308+
308309
assert "del_param_1" in xarr.coords
310+
assert xarr.coords["del_param_1"].shape == (num_points_x,)
311+
assert xarr.coords["del_param_1"].dims == ("del_param_1",)
312+
309313
assert "del_param_2" in xarr.coords
310-
# inferred (basis) parameters not exported
311-
assert "dummy_dac_ch1" not in xarr.coords
312-
assert "dummy_dac_ch2" not in xarr.coords
314+
assert xarr.coords["del_param_2"].shape == (num_points_y,)
315+
assert xarr.coords["del_param_2"].dims == ("del_param_2",)
316+
317+
assert "dummy_dac_ch1" in xarr.coords
318+
assert xarr.coords["dummy_dac_ch1"].shape == (num_points_x,)
319+
assert xarr.coords["dummy_dac_ch1"].dims == ("del_param_1",)
320+
321+
assert "dummy_dac_ch2" in xarr.coords
322+
assert xarr.coords["dummy_dac_ch2"].shape == (num_points_y,)
323+
assert xarr.coords["dummy_dac_ch2"].dims == ("del_param_2",)
313324

314-
assert xarr.meas_parameter.dims == ("del_param_1", "del_param_2")
315-
assert xarr.meas_parameter.shape == (num_points_x, num_points_y)
325+
assert xarr["meas_parameter"].dims == ("del_param_1", "del_param_2")
326+
assert xarr["meas_parameter"].shape == (num_points_x, num_points_y)
316327

317328
# pandas export
318329
df = dataset.to_pandas_dataframe()

0 commit comments

Comments
 (0)