From 0104030fabe7780bf39a648c50b2a8ec92888c6a Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Fri, 12 Jan 2024 00:26:38 -0600 Subject: [PATCH] Allow serializing and deserializing of named astropy units. --- glue/core/state.py | 11 +++++++++++ glue/core/tests/test_state.py | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/glue/core/state.py b/glue/core/state.py index 449005c6a..333f9ffc8 100644 --- a/glue/core/state.py +++ b/glue/core/state.py @@ -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 from astropy.wcs import WCS import shapely @@ -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"]) + + @saver(WCS) def _save_wcs(wcs, context): return dict(header=wcs.to_header_string()) diff --git a/glue/core/tests/test_state.py b/glue/core/tests/test_state.py index 514e9174c..c2be7e0b0 100644 --- a/glue/core/tests/test_state.py +++ b/glue/core/tests/test_state.py @@ -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 + + class DummyClass(object): pass