Skip to content
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

Add support for setitem in inferencedata #2374

Merged
merged 3 commits into from
Sep 11, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New features
- Add optimized simultaneous ECDF confidence bands ([2368](https://github.com/arviz-devs/arviz/pull/2368))
- Add support for setting groups with `idata[group]` ([2374](https://github.com/arviz-devs/arviz/pull/2374))

### Maintenance and fixes

Expand Down
8 changes: 8 additions & 0 deletions arviz/data/inference_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@
raise KeyError(key)
return getattr(self, key)

def __setitem__(self, key: str, value: xr.Dataset):
"""Set item by key and update group list accordingly."""
if key.startswith(WARMUP_TAG):
self._groups_warmup.append(key)

Check warning on line 272 in arviz/data/inference_data.py

View check run for this annotation

Codecov / codecov/patch

arviz/data/inference_data.py#L272

Added line #L272 was not covered by tests
else:
self._groups.append(key)
setattr(self, key, value)

def groups(self) -> List[str]:
"""Return all groups present in InferenceData object."""
return self._groups_all
Expand Down
5 changes: 5 additions & 0 deletions arviz/tests/base_tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,11 @@ def test_repr_html(self):
assert escape(repr(idata)) in html
xr.set_options(display_style=display_style)

def test_setitem(self, data_random):
data_random["new_group"] = data_random.posterior
assert "new_group" in data_random.groups()
assert hasattr(data_random, "new_group")

def test_add_groups(self, data_random):
data = np.random.normal(size=(4, 500, 8))
idata = data_random
Expand Down
Loading