Skip to content

Commit 1b25e4d

Browse files
committed
1 parent 6bb9916 commit 1b25e4d

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

quantities/quantity.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -697,34 +697,21 @@ def cumprod(self, axis=None, dtype=None, out=None):
697697

698698
# list of unsupported functions: [choose]
699699

700-
def __getstate__(self):
701-
"""
702-
Return the internal state of the quantity, for pickling
703-
purposes.
704-
705-
"""
706-
cf = 'CF'[self.flags.fnc]
707-
state = (1,
708-
self.shape,
709-
self.dtype,
710-
self.flags.fnc,
711-
self.tostring(cf),
712-
self._dimensionality,
713-
)
714-
return state
715-
716700
def __setstate__(self, state):
717-
(ver, shp, typ, isf, raw, units) = state
718-
np.ndarray.__setstate__(self, (shp, typ, isf, raw))
701+
ndarray_state = state[:-1]
702+
units = state[-1]
703+
np.ndarray.__setstate__(self, ndarray_state)
719704
self._dimensionality = units
720705

721706
def __reduce__(self):
722707
"""
723708
Return a tuple for pickling a Quantity.
724709
"""
710+
reconstruct,reconstruct_args,state = super(Quantity,self).__reduce__()
711+
state = state + (self._dimensionality,)
725712
return (_reconstruct_quantity,
726713
(self.__class__, np.ndarray, (0, ), 'b', ),
727-
self.__getstate__())
714+
state)
728715

729716
def __deepcopy__(self, memo_dict):
730717
# constructor copies by default

quantities/tests/test_persistence.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
import pickle
4+
import copy
45

56
from .. import units as pq
67
from ..quantity import Quantity
@@ -47,3 +48,14 @@ def test_uncertainquantity_object_dtype(self):
4748
y = pickle.loads(pickle.dumps(x))
4849
self.assertQuantityEqual(x, y)
4950

51+
def test_copy_quantity(self):
52+
for dtype in [float,object]:
53+
x = (20*pq.m).astype(dtype)
54+
y = pickle.loads(pickle.dumps(x))
55+
self.assertQuantityEqual(x, y)
56+
57+
def test_copy_uncertainquantity(self):
58+
for dtype in [float, object]:
59+
x = UncertainQuantity(20, 'm', 0.2).astype(dtype)
60+
y = pickle.loads(pickle.dumps(x))
61+
self.assertQuantityEqual(x, y)

quantities/uncertainquantity.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,18 @@ def nanargmin(self,axis=None, out=None):
273273
def nanargmax(self,axis=None, out=None):
274274
return np.nanargmax(self.magnitude)
275275

276-
def __getstate__(self):
277-
"""
278-
Return the internal state of the quantity, for pickling
279-
purposes.
280-
281-
"""
282-
state = list(super(UncertainQuantity, self).__getstate__())
283-
state.append(self._uncertainty)
284-
return tuple(state)
285-
286276
def __setstate__(self, state):
287-
(ver, shp, typ, isf, raw, units, sigma) = state
288-
np.ndarray.__setstate__(self, (shp, typ, isf, raw))
277+
ndarray_state = state[:-2]
278+
units, sigma = state[-2:]
279+
np.ndarray.__setstate__(self, ndarray_state)
289280
self._dimensionality = units
290281
self._uncertainty = sigma
282+
283+
def __reduce__(self):
284+
"""
285+
Return a tuple for pickling a Quantity.
286+
"""
287+
reconstruct, reconstruct_args, state = super(UncertainQuantity, self).__reduce__()
288+
state = state + (self._uncertainty,)
289+
return reconstruct, reconstruct_args, state
290+

0 commit comments

Comments
 (0)