Skip to content

Commit

Permalink
refactor: allow offsetview upate chaining
Browse files Browse the repository at this point in the history
This also means that _repr_html_ is automatically displayed when update or reset is called.
  • Loading branch information
kmnhan committed Mar 28, 2024
1 parent c315a1a commit 8d5ca4f
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions src/erlab/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,24 @@ def update(
self,
other: dict | Iterable[tuple[str, float]] = None,
**kwargs: dict[str, float],
) -> None:
) -> "OffsetView":
"""Updates the offset view with the provided key-value pairs."""
if other is not None:
for k, v in other.items() if isinstance(other, dict) else other:
self[k] = v
for k, v in kwargs.items():
self[k] = v
return self

def items(self) -> ItemsView[str, float]:
"""Returns a view of the offset view as a list of (key, value) pairs."""
return dict(self).items()

def reset(self) -> None:
def reset(self) -> "OffsetView":
"""Reset all angle offsets to zero."""
for k in self._obj.kspace.valid_offset_keys:
self[k] = 0.0
return self


@xr.register_dataarray_accessor("kspace")
Expand Down Expand Up @@ -469,21 +471,43 @@ def offsets(self) -> OffsetView:
Examples
--------
>>> data.kspace.offsets
{'delta': 0.0, 'xi': 0.0, 'beta': 0.0}
>>> data.kspace.offsets['beta']
0.0
>>> data.kspace.offsets['beta'] = 3.0
>>> data.kspace.offsets
{'delta': 0.0, 'xi': 0.0, 'beta': 3.0}
>>> data.kspace.offsets = dict(delta=1.5, xi=2.7)
>>> data.kspace.offsets
{'delta': 1.5, 'xi': 2.7, 'beta': 0.0}
>>> data.kspace.offsets.update(beta=0.1, xi=0.0)
>>> data.kspace.offsets
{'delta': 1.5, 'xi': 0.0, 'beta': 0.1}
>>> data.kspace.offsets.reset()
{'delta': 0.0, 'xi': 0.0, 'beta': 0.0}
- View all offsets
>>> data.kspace.offsets
{'delta': 0.0, 'xi': 0.0, 'beta': 0.0}
- View single offset
>>> data.kspace.offsets['beta']
0.0
- Offsets to dictionary
>>> dict(data.kspace.offsets)
{'delta': 0.0, 'xi': 0.0, 'beta': 0.0}
- Set single offset
>>> data.kspace.offsets['beta'] = 3.0
>>> data.kspace.offsets
{'delta': 0.0, 'xi': 0.0, 'beta': 3.0}
- Overwrite offsets with dictionary
>>> data.kspace.offsets = dict(delta=1.5, xi=2.7)
>>> data.kspace.offsets
{'delta': 1.5, 'xi': 2.7, 'beta': 0.0}
- Update offsets
>>> data.kspace.offsets.update(beta=0.1, xi=0.0)
{'delta': 1.5, 'xi': 0.0, 'beta': 0.1}
- Reset all offsets
>>> data.kspace.offsets.reset()
{'delta': 0.0, 'xi': 0.0, 'beta': 0.0}
"""
if not hasattr(self, "_offsetview"):
self._offsetview = OffsetView(self._obj)
Expand Down

0 comments on commit 8d5ca4f

Please sign in to comment.