Skip to content

Commit 635c0b4

Browse files
author
Darren Dale
committed
some improvements to UncertainQuantity
1 parent 110a597 commit 635c0b4

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

quantities/quantity.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ def set_units(self, units):
130130
)
131131
units = property(get_units, set_units)
132132

133+
def mean(self):
134+
return Quantity(self.magnitude.mean(), self.units)
135+
133136
def rescale(self, units):
134137
"""
135138
Return a copy of the quantity converted to the specified units
@@ -252,15 +255,18 @@ def __ge__(self, other):
252255

253256
class UncertainQuantity(Quantity):
254257

258+
# TODO: what is an appropriate value?
259+
__array_priority__ = 21
260+
255261
def __new__(
256-
cls, data, units='', dtype='d', uncertainty=0, mutable=True
262+
cls, data, units='', uncertainty=0, dtype='d', mutable=True
257263
):
258264
return Quantity.__new__(
259265
cls, data, units, dtype, mutable
260266
)
261267

262268
def __init__(
263-
self, data, units='', dtype='d', uncertainty=0, mutable=True
269+
self, data, units='', uncertainty=0, dtype='d', mutable=True
264270
):
265271
Quantity.__init__(
266272
self, data, units, dtype, mutable
@@ -291,7 +297,7 @@ def set_uncertainty(self, uncertainty):
291297
try:
292298
if len(uncertainty.shape) != 0:
293299
# make sure we can calculate relative uncertainty:
294-
uncertainty / self.magnitude
300+
uncertainty.magnitude / self.magnitude
295301
uncertainty.units = self.units
296302
self._uncertainty = uncertainty
297303
except:
@@ -314,6 +320,12 @@ def rescale(self, units):
314320
copy.units = units
315321
return copy
316322

323+
def __array_finalize__(self, obj):
324+
Quantity.__array_finalize__(self, obj)
325+
self._uncertainty = getattr(
326+
obj, 'uncertainty', Quantity(0, self.units)
327+
)
328+
317329
def __add__(self, other):
318330
res = Quantity.__add__(self, other)
319331
u = (self.uncertainty**2+other.uncertainty**2)**0.5
@@ -331,7 +343,11 @@ def __mul__(self, other):
331343
try:
332344
sru = self.relative_uncertainty
333345
oru = other.relative_uncertainty
334-
u = res*(sru**2+oru**2)**0.5
346+
ru = (sru**2+oru**2)**0.5
347+
if len(ru.shape) == 0:
348+
u = res.mean() * ru
349+
else:
350+
u = res * ru
335351
except AttributeError:
336352
u = (self.uncertainty**2*other**2)**0.5
337353
# TODO: use .view:
@@ -342,21 +358,34 @@ def __truediv__(self, other):
342358
try:
343359
sru = self.relative_uncertainty
344360
oru = other.relative_uncertainty
345-
u = res*(sru**2+oru**2)**0.5
361+
ru = (sru**2+oru**2)**0.5
362+
if len(ru.shape) == 0:
363+
u = res.mean() * ru
364+
else:
365+
u = res * ru
346366
except AttributeError:
347367
u = (self.uncertainty**2/other**2)**0.5
348368
# TODO: use .view:
349369
return UncertainQuantity(res, uncertainty=u)
350370

351371
def __pow__(self, other):
352372
res = Quantity.__pow__(self, other)
353-
u = res * other * self.relative_uncertainty
373+
ru = other * self.relative_uncertainty
374+
if len(ru.shape) == 0:
375+
u = res.mean() * ru
376+
else:
377+
u = res * ru
354378
return UncertainQuantity(res, uncertainty=u)
355379

356380
def __getitem__(self, key):
357381
return UncertainQuantity(
358382
self.magnitude[key],
359383
self.units,
360-
self.dtype,
361384
copy.copy(self.uncertainty)
362385
)
386+
387+
def __repr__(self):
388+
return '%s*%s\n+/-%s (1 sigma)'\
389+
%(numpy.ndarray.__str__(self), self.units, self.uncertainty)
390+
391+
__str__ = __repr__

0 commit comments

Comments
 (0)