Skip to content

Commit

Permalink
Update to allow serialization of any UnitBase.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Jan 12, 2024
1 parent 0104030 commit 72ed676
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 7 additions & 7 deletions glue/core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def load(rec, context)
import numpy as np
from matplotlib.colors import Colormap
from matplotlib import cm
from astropy.units import NamedUnit, Unit
from astropy.units import UnitBase, Unit
from astropy.wcs import WCS
import shapely

Expand Down Expand Up @@ -622,14 +622,14 @@ def _load_slice(rec, context):
return slice(rec['start'], rec['stop'], rec['step'])


@saver(NamedUnit)
def _save_named_unit(unit, context):
return dict(named_unit=unit.to_string())
@saver(UnitBase)
def _save_unit_base(unit, context):
return dict(unit_base=unit.to_string())


@loader(NamedUnit)
def _load_named_unit(rec, context):
return Unit(rec["named_unit"])
@loader(UnitBase)
def _load_unit_base(rec, context):
return Unit(rec["unit_base"])


@saver(WCS)
Expand Down
15 changes: 15 additions & 0 deletions glue/core/tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,21 @@ def test_astropy_units():
unit2 = clone(unit)
assert unit2 is unit

unit = u.km
unit2 = clone(unit)
assert unit2 is unit


@requires_astropy
def test_astropy_compound_units():
import astropy.units as u
unit = u.m / u.s
unit2 = clone(unit)
assert unit2 == unit
unit = u.W / u.m**2 / u.nm
unit2 = clone(unit)
assert unit2 == unit


class DummyClass(object):
pass
Expand Down

0 comments on commit 72ed676

Please sign in to comment.