Skip to content

🎉 init: unxt-xarray#498

Draft
nstarman wants to merge 3 commits intoGalacticDynamics:mainfrom
nstarman:xarray
Draft

🎉 init: unxt-xarray#498
nstarman wants to merge 3 commits intoGalacticDynamics:mainfrom
nstarman:xarray

Conversation

@nstarman
Copy link
Copy Markdown
Contributor

No description provided.

@nstarman nstarman added this to the v1.8.0 milestone Dec 20, 2025
@github-actions github-actions bot added 📝 Add / update documentation Add or update documentation. 👷 Add / update CI build system Add or update CI build system. 🔧 Add / update configuration Add or update configuration files. 🎉 Begin a project Begin a project. labels Dec 20, 2025
@nstarman nstarman force-pushed the xarray branch 2 times, most recently from d1c0f93 to e71531b Compare December 20, 2025 01:23
@codecov
Copy link
Copy Markdown

codecov bot commented Dec 20, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.11%. Comparing base (fef3b6e) to head (3e6250b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #498      +/-   ##
==========================================
- Coverage   96.30%   96.11%   -0.20%     
==========================================
  Files          57       46      -11     
  Lines        2547     2417     -130     
  Branches      128      122       -6     
==========================================
- Hits         2453     2323     -130     
+ Misses         71       70       -1     
- Partials       23       24       +1     

☔ View full report in Codecov by Sentry.
📢 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.

@nstarman nstarman marked this pull request as ready for review December 20, 2025 01:30
@nstarman nstarman requested a review from a team as a code owner December 20, 2025 01:30
Copilot AI review requested due to automatic review settings December 20, 2025 01:30
@nstarman nstarman marked this pull request as draft December 20, 2025 01:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR initializes the unxt-xarray package, which provides seamless integration between unxt's JAX-based physical quantities and xarray's labeled multi-dimensional arrays through .unxt accessors.

Key Changes:

  • Introduces the new unxt-xarray workspace package with quantify/dequantify functionality
  • Adds xarray accessor registration for DataArray and Dataset objects
  • Includes comprehensive test coverage with unit, conversion, and property-based tests

Reviewed changes

Copilot reviewed 20 out of 22 changed files in this pull request and generated no comments.

Show a summary per file
File Description
pyproject.toml Added unxt-xarray to workspace dependencies and test groups
packages/unxt-xarray/pyproject.toml Package configuration for unxt-xarray
packages/unxt-xarray/src/unxt_xarray/init.py Main package entry point, registers accessors
packages/unxt-xarray/src/unxt_xarray/_src/init.py Internal module exports
packages/unxt-xarray/src/unxt_xarray/_src/accessors.py Implements .unxt accessor for DataArray and Dataset
packages/unxt-xarray/src/unxt_xarray/_src/conversion.py Core unit conversion functions for xarray objects
packages/unxt-xarray/tests/unit/test_accessors.py Tests for xarray accessor functionality
packages/unxt-xarray/tests/unit/test_conversion.py Tests for conversion functions
packages/unxt-xarray/tests/unit/test_properties.py Property-based tests using hypothesis
packages/unxt-xarray/docs/index.md Package documentation overview
packages/unxt-xarray/docs/xarray-guide.md Comprehensive usage guide
packages/unxt-xarray/docs/_data/README.md Documentation for sample data files
packages/unxt-xarray/docs/_data/generate_sample_data.py Script to generate sample NetCDF data
packages/unxt-xarray/README.md Package README with quick start
packages/unxt-hypothesis/docs/testing-guide.md Minor header formatting update
noxfile.py Added xarray package support to nox sessions
docs/packages/unxt-xarray Symlink to package documentation
docs/index.md Added unxt-xarray to documentation index
.github/workflows/cd-unxt-xarray.yml CD workflow for unxt-xarray package
.github/copilot-instructions.md Added documentation formatting guidelines

@nstarman nstarman modified the milestones: v1.8.0, v1.9.0 Dec 20, 2025
@nstarman nstarman modified the milestones: v1.9.0, v1.10.0 Jan 2, 2026
@nstarman nstarman requested a review from adrn January 13, 2026 13:36
@nstarman nstarman removed this from the v1.10.0 milestone Jan 13, 2026
@nstarman nstarman added this to the future milestone Jan 24, 2026
@nstarman
Copy link
Copy Markdown
Contributor Author

@adrn I'd love some input on design decisions. Not sure this is the API we want.

@nstarman nstarman requested a review from Copilot February 19, 2026 18:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 20 out of 22 changed files in this pull request and generated 8 comments.

Comment on lines +210 to +216
return DataArray(
data=new_data,
coords=new_coords,
dims=obj.dims,
name=obj.name,
attrs=obj.attrs,
)
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

These constructors pass through obj.attrs by reference. Since dequantify() later mutates attrs (and coord attrs), this can unexpectedly mutate the original xarray object's metadata if xarray doesn't defensively copy. Prefer passing a shallow copy (e.g., attrs=dict(obj.attrs)) and similarly copying coord.attrs when building Variables to avoid shared-metadata side effects.

Copilot uses AI. Check for mistakes.
Comment on lines +277 to +283
return DataArray(
data=new_data,
coords=new_coords,
dims=obj.dims,
name=obj.name,
attrs=obj.attrs,
)
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

These constructors pass through obj.attrs by reference. Since dequantify() later mutates attrs (and coord attrs), this can unexpectedly mutate the original xarray object's metadata if xarray doesn't defensively copy. Prefer passing a shallow copy (e.g., attrs=dict(obj.attrs)) and similarly copying coord.attrs when building Variables to avoid shared-metadata side effects.

Copilot uses AI. Check for mistakes.
Comment on lines +201 to +208
new_coords = {}
for name, coord in obj.coords.items():
unit = units.get(name)
new_coords[name] = (
coord
if unit is None
else Variable(coord.dims, u.Q(coord.data, unit), coord.attrs)
)
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

Coordinate handling duplicates unit-attachment logic and bypasses _array_attach_units(), which already centralizes parsing/None handling. Consider using the helper (and then wrapping into Variable) to keep unit parsing behavior consistent between data variables and coordinates and reduce divergence over time.

Copilot uses AI. Check for mistakes.
Comment on lines +38 to +51
@st.composite
def dataarray_with_quantity(draw):
"""Generate a DataArray containing a Quantity."""
# Generate a quantity - ensure it's at least 1D
q = draw(ust.quantities())

# If scalar, make it 1D by wrapping in an array
if q.ndim == 0:
q = u.Quantity(jnp.array([q.value]), q.unit)

# Create DataArray
da = xr.DataArray(q, dims=["x"])

return da, u.unit_of(q)
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

dataarray_with_quantity() is currently unused in this test module. Either remove it to keep the property test suite focused, or add a test that consumes it. If you do keep it, note that ust.quantities() can produce arrays with ndim > 1, which will make dims=['x'] invalid—consider constraining the strategy to 1D quantities or generating dims based on q.ndim.

Copilot uses AI. Check for mistakes.
Comment on lines +181 to +188
# Format units as strings for attributes
unit_strs: dict[Hashable, str] = {}
for name, unit in units.items():
if unit is not None:
if format is not None:
unit_strs[name] = format.format(unit)
else:
unit_strs[name] = str(unit)
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

The format branch in dequantify() introduces behavior that isn't covered by the new tests (they exercise the default str(unit) path). Add a unit test that calls dequantify(format=...) and asserts the produced attribute string matches the expected formatted output (or at least differs from the default) to prevent regressions.

Copilot uses AI. Check for mistakes.

:::

:::{tab-item} uv
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

The uv installation block has mismatched / nested fence markers (bash + ```bash + closing ), which is likely to break MyST/Sphinx rendering. Use a single fenced code block inside the tab item (matching the working pattern used elsewhere, e.g. in xarray-guide.md).

Copilot uses AI. Check for mistakes.
Comment on lines +27 to +41
# Create a DataArray with unit attributes
da = xr.DataArray(
data=[1.0, 2.0, 3.0],
dims=["x"],
coords={"x": [0.0, 1.0, 2.0]},
attrs={"units": "m"},
)

# Quantify: convert to unxt Quantities
quantified = da.unxt.quantify()
print(quantified)
# <xarray.DataArray (x: 3)>
# <Quantity([1. 2. 3.], 'm')>
# Coordinates:
# * x (x) <Quantity([0. 1. 2.], 's')>
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

This example/output is internally inconsistent: the coordinate x is created without a units attribute, but the printed output shows it as a Quantity with unit 's'. Also, elsewhere in the docs you note that dimension coordinates are typically unwrapped by xarray, so showing a dimension coordinate as a Quantity is misleading. Update the example to (a) set coordinate unit metadata if you want it quantified, and (b) use a non-dimension coordinate if the intent is to demonstrate preserved Quantity coordinates.

Copilot uses AI. Check for mistakes.
Comment on lines +78 to +97
````

:::

:::{tab-item} uv

````bash

```bash
uv add unxt
````

:::

::::

```

```

Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

The documented tab-set installation snippet includes malformed nested code fences for the uv example (bash … ```bash … ). Since this file is guidance for contributors, it should show a known-good MyST pattern (a single fenced block inside each tab-item) to avoid propagating broken docs formatting into package docs.

Suggested change
````
:::
:::{tab-item} uv
````bash
```bash
uv add unxt
````
:::
::::
```
```
:::
:::{tab-item} uv
```bash
uv add unxt

:::

::::

```

Copilot uses AI. Check for mistakes.
@github-actions github-actions bot added the ✨ Introduce new features Introduce new features. label Mar 27, 2026
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

👷 Add / update CI build system Add or update CI build system. 🔧 Add / update configuration Add or update configuration files. 📝 Add / update documentation Add or update documentation. 🎉 Begin a project Begin a project. ✨ Introduce new features Introduce new features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants