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

Allow serializing and deserializing named astropy units #2475

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions glue/core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from astropy.units import NamedUnit, Unit
from astropy.units import Unit, UnitBase

from astropy.wcs import WCS
import shapely

Expand Down Expand Up @@ -621,6 +622,16 @@ 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())


@loader(NamedUnit)
def _load_named_unit(rec, context):
return Unit(rec["named_unit"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@saver(NamedUnit)
def _save_named_unit(unit, context):
return dict(named_unit=unit.to_string())
@loader(NamedUnit)
def _load_named_unit(rec, context):
return Unit(rec["named_unit"])
@saver(UnitBase)
def _save_unit_base(unit, context):
return dict(unit_base=unit.to_string())
@loader(UnitBase)
def _load_unit_base(rec, context):
return Unit(rec["unit_base"])



@saver(WCS)
def _save_wcs(wcs, context):
return dict(header=wcs.to_header_string())
Expand Down
8 changes: 8 additions & 0 deletions glue/core/tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ def test_datetime_component():
assert isinstance(c2.data[0], np.datetime64)


@requires_astropy
def test_astropy_units():
import astropy.units as u
unit = u.m
unit2 = clone(unit)
assert unit2 is unit
astrofrog marked this conversation as resolved.
Show resolved Hide resolved


class DummyClass(object):
pass

Expand Down