Skip to content

Commit 5100d16

Browse files
committed
Fixed statcoulomb definition, which was expressed in units of SI
equivalence, but not in units that are meaningful to cgs. Make angles regular unit quantities with dimensionless reference values. It doesnt really make sense to make them irreducible quantities. Added a _reference property, masked because it is really only intended to be used behind the scenes to calculate conversion factors. The _reference property was necessary in order to implement customizable default units. Added a set_default_units function, which allows the user to customize how the units of a simplified quantity are expressed, for example in SI or cgs, or some other customized system.
1 parent 86424f1 commit 5100d16

9 files changed

Lines changed: 236 additions & 198 deletions

File tree

quantities/quantity.py

Lines changed: 27 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def prepare_compatible_units(s, o):
1616
o = Quantity(o, copy=False)
1717
try:
1818
assert s.dimensionality.simplified == o.dimensionality.simplified
19-
return s.simplified, o.simplified
19+
return s._reference, o._reference
2020
except AssertionError:
2121
raise ValueError(
2222
'can not compare quantities with units of %s and %s'\
@@ -52,8 +52,8 @@ def validate_dimensionality(value):
5252
def get_conversion_factor(from_u, to_u):
5353
validate_unit_quantity(from_u)
5454
validate_unit_quantity(to_u)
55-
from_u = from_u.simplified
56-
to_u = to_u.simplified
55+
from_u = from_u._reference
56+
to_u = to_u._reference
5757
assert from_u.dimensionality == to_u.dimensionality
5858
return from_u.magnitude / to_u.magnitude
5959

@@ -77,6 +77,13 @@ def __new__(cls, data, units='', dtype=None, copy=True):
7777
def dimensionality(self):
7878
return self._dimensionality.copy()
7979

80+
@property
81+
def _reference(self):
82+
rq = 1*unit_registry['dimensionless']
83+
for u, d in self.dimensionality.iteritems():
84+
rq = rq * u._reference**d
85+
return rq * self.magnitude
86+
8087
@property
8188
def magnitude(self):
8289
return self.view(type=numpy.ndarray)
@@ -148,69 +155,24 @@ def astype(self, dtype=None):
148155
def __array_finalize__(self, obj):
149156
self._dimensionality = getattr(obj, 'dimensionality', Dimensionality())
150157

151-
def __array_wrap__(self, obj, context):
152-
# this is experimental right now, there is probably a better
153-
# way to implement it, but for now lets identify which
154-
# ufuncs need to be addressed. Maybe a good way to do this would
155-
# be something like a dictionary mapping of ufuncs to functions
156-
# that return a proper dimensionality based on the inputs.
157-
# print obj, context
158-
uf, objs, huh = context
159-
160-
result = obj.view(type(self))
161-
if uf is numpy.multiply:
162-
result._dimensionality = objs[0].dimensionality * objs[1].dimensionality
163-
elif uf is numpy.sqrt:
164-
result._dimensionality = objs[0].dimensionality**(0.5)
165-
elif uf is numpy.rint:
166-
result._dimensionality = objs[0].dimensionality
167-
elif uf is numpy.conjugate:
168-
result._dimensionality = objs[0].dimensionality
169-
return result
170-
171-
# def __array_wrap__(self, obj, context=None):
172-
# """
173-
# Special hook for ufuncs.
174-
# Wraps the numpy array and sets the mask according to context.
175-
# """
176-
# result = obj.view(type(self))
158+
# def __array_wrap__(self, obj, context):
159+
# # this is experimental right now, there is probably a better
160+
# # way to implement it, but for now lets identify which
161+
# # ufuncs need to be addressed. Maybe a good way to do this would
162+
# # be something like a dictionary mapping of ufuncs to functions
163+
# # that return a proper dimensionality based on the inputs.
164+
## print obj, context
165+
# uf, objs, huh = context
177166
#
178-
# if context is not None:
179-
# result._dimensionality = result._dimensionality.copy()
180-
# (func, args, _) = context
181-
# m = reduce(mask_or, [getmaskarray(arg) for arg in args])
182-
# # Get the domain mask................
183-
# domain = ufunc_domain.get(func, None)
184-
# if domain is not None:
185-
# if len(args) > 2:
186-
# d = reduce(domain, args)
187-
# else:
188-
# d = domain(*args)
189-
# # Fill the result where the domain is wrong
190-
# try:
191-
# # Binary domain: take the last value
192-
# fill_value = ufunc_fills[func][-1]
193-
# except TypeError:
194-
# # Unary domain: just use this one
195-
# fill_value = ufunc_fills[func]
196-
# except KeyError:
197-
# # Domain not recognized, use fill_value instead
198-
# fill_value = self.fill_value
199-
# result = result.copy()
200-
# np.putmask(result, d, fill_value)
201-
# # Update the mask
202-
# if m is nomask:
203-
# if d is not nomask:
204-
# m = d
205-
# else:
206-
# m |= d
207-
# # Make sure the mask has the proper size
208-
# if result.shape == () and m:
209-
# return masked
210-
# else:
211-
# result._mask = m
212-
# result._sharedmask = False
213-
# #....
167+
# result = obj.view(type(self))
168+
# if uf is numpy.multiply:
169+
# result._dimensionality = objs[0].dimensionality * objs[1].dimensionality
170+
# elif uf is numpy.sqrt:
171+
# result._dimensionality = objs[0].dimensionality**(0.5)
172+
# elif uf is numpy.rint:
173+
# result._dimensionality = objs[0].dimensionality
174+
# elif uf is numpy.conjugate:
175+
# result._dimensionality = objs[0].dimensionality
214176
# return result
215177

216178
@with_doc(numpy.ndarray.__add__)

quantities/registry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __getitem__(self, string):
2121
return eval(string, self.__context)
2222
except NameError:
2323
# could return self['UnitQuantity'](string)
24+
print self.__context
2425
raise LookupError(
2526
'Unable to parse units: "%s"'%string
2627
)

quantities/uncertainquantity.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ def _set_units(self, units):
3434
self.uncertainty.units = self._dimensionality
3535
units = property(Quantity._get_units, _set_units)
3636

37+
@property
38+
def _reference(self):
39+
ret = super(UncertainQuantity, self)._reference.view(UncertainQuantity)
40+
ret.uncertainty = self.uncertainty._reference
41+
return ret
42+
3743
@property
3844
def simplified(self):
3945
ret = super(UncertainQuantity, self).simplified.view(UncertainQuantity)

0 commit comments

Comments
 (0)