Skip to content

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

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/export-to-xarray-dataset-fix
Draft

Fix AlignmentError in to_xarray_dataset when data variables have different setpoints#8244
Copilot wants to merge 2 commits into
mainfrom
copilot/export-to-xarray-dataset-fix

Conversation

Copilot AI 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 path
meas.register_parameter(a.bar, setpoints=(a.f_stop,))       # scalar → standalone f_stop dim

dataset.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

codecov Bot commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.03%. Comparing base (a095809) to head (59d0ba4).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8244      +/-   ##
==========================================
+ Coverage   71.02%   71.03%   +0.01%     
==========================================
  Files         301      301              
  Lines       31888    31900      +12     
==========================================
+ Hits        22647    22659      +12     
  Misses       9241     9241              

☔ 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.

… 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>
Copilot AI changed the title [WIP] Fix export to xarray dataset when data variables have different setpoints Fix AlignmentError in to_xarray_dataset when data variables have different setpoints Jun 24, 2026
Copilot AI requested a review from jenshnielsen June 24, 2026 13:15
Sign up for free to 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.

2 participants