Skip to content

Commit

Permalink
Validate units are in the unit registry on set (#1013)
Browse files Browse the repository at this point in the history
* Validate units are in the unit registry on set

Closes #1010

* add test for units, or rather add unit, unit tests ;P

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* actually do the change on datasets not group

* remove extraneous array in unit test

Co-authored-by: Morrow <dmorrow@anl.gov>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 21, 2021
1 parent aef5eee commit 669d14d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
### Changed
- complete units overhaul, now using pint library
- explicitly store axes as fixed string dtype
- validate units are in the unit registry on set

### Fixed
- Fixed bug in `from_COLORS` that misordered the relationship between variables and channels for 1D datasets.
Expand Down
2 changes: 2 additions & 0 deletions WrightTools/_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ def units(self, value):
if value is None:
if "units" in self.attrs.keys():
self.attrs.pop("units")
elif value not in wt_units.ureg:
raise ValueError(f"'{value}' is not in the unit registry")
else:
try:
self.attrs["units"] = value
Expand Down
12 changes: 12 additions & 0 deletions WrightTools/data/_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ def size(self) -> int:
"""Size."""
return functools.reduce(operator.mul, self.shape)

@property
def units(self):
return self._units

@units.setter
def units(self, value):
if value == "None":
value = None
if value is not None and value not in wt_units.ureg:
raise ValueError(f"'{value}' is not in the unit registry")
self._units = value

@property
def units_kind(self) -> str:
"""Units kind."""
Expand Down
2 changes: 2 additions & 0 deletions tests/data/from_Aramis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

here = pathlib.Path(__file__).parent

wt.units.ureg.define("@alias count = Cnt")

# --- test ----------------------------------------------------------------------------------------


Expand Down
1 change: 1 addition & 0 deletions tests/data/from_PyCMDS.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

here = os.path.abspath(os.path.dirname(__file__))

wt.units.ureg.define("steps = []")

# --- test ----------------------------------------------------------------------------------------

Expand Down
24 changes: 24 additions & 0 deletions tests/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,27 @@ def test_axis_convert_exception():
def test_in_mm_conversion():
assert np.isclose(wt.units.convert(25.4, "mm", "in"), 1.0)
assert np.isclose(wt.units.convert(1.0, "in", "mm"), 25.4)


def unit_registry_test1():
values = np.linspace(-1, 1, 51)
d = wt.Data(name="test")
try:
d.create_variable("Bgood", values=values, units="tesla")
d.transform("Bgood")
except ValueError:
assert False
else:
assert True


def unit_registry_test2():
values = np.linspace(-1, 1, 51)
d = wt.Data(name="test")
try:
d.create_variable("Bbad", values=values, units="Tesla")
d.transform("Bbad")
except ValueError:
assert True
else:
assert False

0 comments on commit 669d14d

Please sign in to comment.