[yt-svn] commit/yt: jzuhone: Merged in ngoldbaum/yt/yt-3.0 (pull request #848)
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Wed Apr 23 06:35:25 PDT 2014
1 new commit in yt:
https://bitbucket.org/yt_analysis/yt/commits/2dd2d810e60c/
Changeset: 2dd2d810e60c
Branch: yt-3.0
User: jzuhone
Date: 2014-04-23 15:35:12
Summary: Merged in ngoldbaum/yt/yt-3.0 (pull request #848)
Ensure binary operations preserve unit registries. Closes #834.
Affected #: 2 files
diff -r f0dcca6509b72331853fd900470a8ed10275cd62 -r 2dd2d810e60cebaaab21688cf06e69580a3ad76d yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -592,3 +592,38 @@
yield assert_array_equal, arr.value, np.array(arr)
yield assert_array_equal, arr.v, np.array(arr)
+
+
+def test_registry_association():
+ ds = fake_random_pf(64, nprocs=1, length_unit=10)
+ a = ds.quan(3, 'cm')
+ b = YTQuantity(4, 'm')
+ c = ds.quan(6, '')
+ d = 5
+
+ yield assert_equal, id(a.units.registry), id(ds.unit_registry)
+
+ def binary_op_registry_comparison(op):
+ e = op(a, b)
+ f = op(b, a)
+ g = op(c, d)
+ h = op(d, c)
+
+ assert_equal(id(e.units.registry), id(ds.unit_registry))
+ assert_equal(id(f.units.registry), id(b.units.registry))
+ assert_equal(id(g.units.registry), id(h.units.registry))
+ assert_equal(id(g.units.registry), id(ds.unit_registry))
+
+ def unary_op_registry_comparison(op):
+ c = op(a)
+ d = op(b)
+
+ assert_equal(id(c.units.registry), id(ds.unit_registry))
+ assert_equal(id(d.units.registry), id(b.units.registry))
+
+ for op in [operator.add, operator.sub, operator.mul, operator.div,
+ operator.truediv]:
+ yield binary_op_registry_comparison, op
+
+ for op in [operator.abs, operator.neg, operator.pos]:
+ yield unary_op_registry_comparison, op
diff -r f0dcca6509b72331853fd900470a8ed10275cd62 -r 2dd2d810e60cebaaab21688cf06e69580a3ad76d yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -497,12 +497,16 @@
def __isub__(self, other):
""" See __sub__. """
oth = sanitize_units_add(self, other, "subtraction")
- return np.subtract(self, other, out=self)
+ return np.subtract(self, oth, out=self)
def __neg__(self):
""" Negate the data. """
return YTArray(super(YTArray, self).__neg__())
+ def __pos__(self):
+ """ Posify the data. """
+ return YTArray(super(YTArray, self).__pos__(), self.units)
+
def __mul__(self, right_object):
"""
Multiply this YTArray by the object on the right of the `*` operator.
@@ -665,7 +669,7 @@
def __eq__(self, other):
""" Test if this is equal to the object on the right. """
# Check that other is a YTArray.
- if other == None:
+ if other is None:
# self is a YTArray, so it can't be None.
return False
if isinstance(other, YTArray):
@@ -679,7 +683,7 @@
def __ne__(self, other):
""" Test if this is not equal to the object on the right. """
# Check that the other is a YTArray.
- if other == None:
+ if other is None:
return True
if isinstance(other, YTArray):
if not self.units.same_dimensions_as(other.units):
@@ -763,7 +767,7 @@
return ret
elif context[0] in unary_operators:
u = getattr(context[1][0], 'units', None)
- if u == None:
+ if u is None:
u = Unit()
try:
unit = self._ufunc_registry[context[0]](u)
@@ -774,10 +778,10 @@
elif context[0] in binary_operators:
unit1 = getattr(context[1][0], 'units', None)
unit2 = getattr(context[1][1], 'units', None)
- if unit1 == None:
- unit1 = Unit()
- if unit2 == None and context[0] is not power:
- unit2 = Unit()
+ if unit1 is None:
+ unit1 = Unit(registry=getattr(unit2, 'registry', None))
+ if unit2 is None and context[0] is not power:
+ unit2 = Unit(registry=getattr(unit1, 'registry', None))
elif context[0] is power:
unit2 = context[1][1]
if isinstance(unit2, np.ndarray):
@@ -817,8 +821,8 @@
See the documentation for the standard library pickle module:
http://docs.python.org/2/library/pickle.html
- Unit metadata is encoded in the zeroth element of third element of the
- returned tuple, itself a tuple used to restore the state of the ndarray.
+ Unit metadata is encoded in the zeroth element of third element of the
+ returned tuple, itself a tuple used to restore the state of the ndarray.
This is always defined for numpy arrays.
"""
np_ret = super(YTArray, self).__reduce__()
Repository URL: https://bitbucket.org/yt_analysis/yt/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the yt-svn
mailing list