Skip to content

ensure coords are always kept in rvs #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## v0.9.1 (2025 June 18)
### Maintenance and fixes
* Ensure coords are preserved in `.rvs` method of RV wrappers {pull}`73`

## v0.9.0 (2025 May 22)
### New features
* Support PreliZ distributions as input to RV wrappers {pull}`70`
Expand Down
2 changes: 1 addition & 1 deletion src/xarray_einstats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"EinopsAccessor",
]

__version__ = "0.9.0"
__version__ = "0.9.1"


def sort(da, dim, kind=None, stable=None, **kwargs):
Expand Down
16 changes: 8 additions & 8 deletions src/xarray_einstats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,15 @@ def rvs(
output_core_dims=[output_core_dims],
**apply_kwargs,
)
if not isinstance(out, xr.DataArray):
for elem in (*dist_args, *args, *dist_kwargs.values(), *kwargs.values()):
if isinstance(elem, xr.DataArray):
for name, values in elem.coords.items():
if name in coords:
continue
if set(values.dims) < set(output_core_dims):
coords[name] = values
for elem in (*dist_args, *args, *dist_kwargs.values(), *kwargs.values()):
if isinstance(elem, xr.DataArray):
for name, values in elem.coords.items():
if name in coords:
continue
if set(values.dims) < set(output_core_dims):
coords[name] = values

if not isinstance(out, xr.DataArray):
return xr.DataArray(out, dims=output_core_dims, coords=coords)
return out.assign_coords(coords)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ def test_rv_method(self, data, wrapper, dim_names, in_type):
assert_dims_in_da(out, dim_names)
assert len(out[dim_names[0]]) == 2
assert len(out[dim_names[1]]) == 7
assert all(np.all(out.coords[name] == coords) for name, coords in data.coords.items())

@pytest.mark.parametrize("in_type", ("scalar", "dataarray"))
def test_rv_method_coords(self, data, wrapper, in_type):
dist = get_dist_and_clean_method(wrapper, data, in_type=in_type)
coords = {"new_dim": ["true", "false"], "extra_dim": list("abcdefg")}
out = dist.rvs(coords=coords)
assert_dims_in_da(out, list(coords))
assert len(out["new_dim"]) == 2
assert len(out["extra_dim"]) == 7
assert np.all(out.coords["new_dim"] == np.array(coords["new_dim"]))
assert np.all(out.coords["extra_dim"] == np.array(coords["extra_dim"]))
assert all(np.all(out.coords[name] == coord) for name, coord in data.coords.items())

@pytest.mark.parametrize("size", (1, 10))
@pytest.mark.parametrize("dims", (None, "name", ["name"]))
Expand Down