Skip to content

Fix AlignmentError in to_xarray_dataset when data variables have different setpoints - #8244

Open
Jens Hedegaard Nielsen (jenshnielsen) with Copilot wants to merge 3 commits into
mainfrom
copilot/export-to-xarray-dataset-fix
Open

Fix AlignmentError in to_xarray_dataset when data variables have different setpoints#8244
Jens Hedegaard Nielsen (jenshnielsen) with Copilot wants to merge 3 commits into
mainfrom
copilot/export-to-xarray-dataset-fix

Conversation

CopilotAI commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

DataSet.to_xarray_dataset() raises an AlignmentError when two data variables share a setpoint coordinate but one of them has additional non-grid setpoints (triggering the multi-index export path).

Root cause: Per-parameter sub-datasets are built independently. A non-grid array parameter (e.g. spectrum with setpoints (f_stop, freq) where freq values differ per sweep point) gets exported via xr.Coordinates.from_pandas_multiindex, making f_stop a component of a MultiIndex rather than a standalone dimension. A co-registered scalar parameter with only (f_stop,) gets a plain PandasIndex for f_stop. xr.merge cannot align these incompatible Index objects.

meas.register_parameter(a.spectrum, setpoints=(a.f_stop,)) # array, non-grid → multi_index pathmeas.register_parameter(a.bar, setpoints=(a.f_stop,)) # scalar → standalone f_stop dimdataset.to_xarray_dataset()
# AlignmentError: cannot align objects on coordinate 'foobar_f_stop'# because of conflicting indexes

Fix (export_to_xarray.py):

  • Before xr.merge, detect sub-datasets that use a multi_index dimension whose coordinate levels conflict with standalone dimensions in other sub-datasets
  • Resolve by calling ds.unstack("multi_index") on the conflicting sub-datasets, converting to regular independent dimensions (NaN-filling off-grid entries) so all sub-datasets share a consistent f_stop coordinate structure

Test (test_dataset_export.py):

  • Regression test sweeping f_stop over two values with differing frequency grids, verifying both spectrum (2-D, NaN-padded) and bar (1-D) are correctly present with values preserved in the merged dataset

@codecov

codecovBot commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.11%. Comparing base (355ba4b) to head (4d59746).

Additional details and impacted files
@@ Coverage Diff @@## main #8244 +/- ##
==========================================
+ Coverage 71.09% 71.11% +0.01% 
==========================================
Files 305 305 Lines 31934 31947 +13 ==========================================
+ Hits 22705 22718 +13 
Misses 9229 9229 

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

CopilotAI changed the title [WIP] Fix export to xarray dataset when data variables have different setpointsFix AlignmentError in to_xarray_dataset when data variables have different setpointsJun 24, 2026
Comment threadsrc/qcodes/dataset/exporters/export_to_xarray.py Outdated
@jenshnielsen

Copy link
Copy Markdown
Collaborator

Copilot Please look into the comment on this pr

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an AlignmentError in DataSet.to_xarray_dataset() caused by merging per-parameter sub-datasets that represent a shared setpoint coordinate differently (standalone index vs MultiIndex component), by normalizing conflicting MultiIndex sub-datasets before xr.merge.

Changes:

  • Detect merge conflicts between MultiIndex level coordinates and standalone dimensions and resolve them by unstack("multi_index") prior to xr.merge.
  • Add a regression test covering mixed setpoint structures (non-grid array param + scalar param sharing one setpoint).
  • Add a newsfragment documenting the export fix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

FileDescription
src/qcodes/dataset/exporters/export_to_xarray.pyUnstacks conflicting multi_index sub-datasets to avoid xr.merge alignment failures.
tests/dataset/test_dataset_export.pyAdds regression coverage ensuring export succeeds and preserves values with NaN padding for off-grid entries.
docs/changes/newsfragments/8232.improvedDocuments the fixed AlignmentError scenario in release notes.

Comment threadsrc/qcodes/dataset/exporters/export_to_xarray.py Outdated
Comment threadtests/dataset/test_dataset_export.py Outdated
Comment threaddocs/changes/newsfragments/8232.improved Outdated
CopilotAIand others added 3 commits July 31, 2026 15:34
… xarray
When a dataset has two data variables with different setpoints, and one
is a non-grid array parameter (using the multi_index path) while another
shares a setpoint coordinate as a standalone dimension, xr.merge raised
an AlignmentError because the shared coordinate had conflicting Index
objects in the two sub-datasets.
Fix: detect this conflict in load_to_xarray_dataset and unstack the
multi_index into proper independent dimensions before merging, allowing
the shared coordinate to be consistent across all sub-datasets.
Fixes#8232
Co-authored-by: jenshnielsen <548266+jenshnielsen@users.noreply.github.com>
Only run the multi_index conflict detection when there are at least two
sub-datasets, since with a single variable there is nothing to merge and
hence no possible conflict. The existing `standalone_dims` check already
handles the case where all variables share multi_index, but add a clear
comment explaining both short-circuit conditions.
Co-authored-by: jenshnielsen <548266+jenshnielsen@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@jenshnielsen
Jens Hedegaard Nielsen (jenshnielsen) marked this pull request as ready for review July 31, 2026 13:36
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@jenshnielsen