[yt-svn] commit/yt: 11 new changesets

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Thu Aug 13 09:26:57 PDT 2015


11 new commits in yt:

https://bitbucket.org/yt_analysis/yt/commits/e1d184e66704/
Changeset:   e1d184e66704
Branch:      yt
User:        ngoldbaum
Date:        2015-08-09 18:34:18+00:00
Summary:     Add a basic test for YTArray iteration semantics
Affected #:  1 file

diff -r 57abf679467c2b46098177a6662cc507c173395b -r e1d184e667040cd13157641a52cada2e408c880f yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -554,6 +554,17 @@
     yield assert_true, a_slice.base is a
 
 
+def test_iteration():
+    """
+    Test that iterating over a YTArray returns a sequence of YTQuantity insances
+    """
+    a = np.arange(3)
+    b = YTArray(np.arange(3), 'cm')
+    for ia, ib, in zip(a, b):
+        yield assert_equal, ia, ib.value
+        yield assert_equal, ib.units, b.units
+
+
 def test_fix_length():
     """
     Test fixing the length of an array. Used in spheres and other data objects


https://bitbucket.org/yt_analysis/yt/commits/edb0a4320990/
Changeset:   edb0a4320990
Branch:      yt
User:        ngoldbaum
Date:        2015-08-09 17:01:28+00:00
Summary:     Outsource YTArray comparison operator unit validation logic to one function
Affected #:  1 file

diff -r e1d184e667040cd13157641a52cada2e408c880f -r edb0a43209903f32588fe78830bdbac18b76e697 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -134,6 +134,16 @@
             raise YTUnitOperationError(op_string, inp.units, dimensionless)
     return ret
 
+def validate_comparison_units(this, other, op_string):
+    # Check that other is a YTArray.
+    if isinstance(other, YTArray):
+        if not this.units.same_dimensions_as(other.units):
+            raise YTUnitOperationError(op_string, this.units, other.units)
+
+        return other.in_units(this.units)
+
+    return other
+
 unary_operators = (
     negative, absolute, rint, ones_like, sign, conj, exp, exp2, log, log2,
     log10, expm1, log1p, sqrt, square, reciprocal, sin, cos, tan, arcsin,
@@ -910,26 +920,13 @@
     # @todo: outsource to a single method with an op argument.
     def __lt__(self, other):
         """ Test if this is less than the object on the right. """
-        # Check that other is a YTArray.
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError('less than', self.units, other.units)
-
-            return np.array(self).__lt__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__lt__(np.array(other))
+        oth = validate_comparison_units(self, other, 'less_than')
+        return np.array(self).__lt__(np.array(oth))
 
     def __le__(self, other):
         """ Test if this is less than or equal to the object on the right. """
-        # Check that other is a YTArray.
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError('less than or equal', self.units,
-                                           other.units)
-
-            return np.array(self).__le__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__le__(np.array(other))
+        oth = validate_comparison_units(self, other, 'less_than or equal')
+        return np.array(self).__le__(np.array(oth))
 
     def __eq__(self, other):
         """ Test if this is equal to the object on the right. """
@@ -937,50 +934,28 @@
         if other is None:
             # self is a YTArray, so it can't be None.
             return False
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError("equal", self.units, other.units)
-
-            return np.array(self).__eq__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__eq__(np.array(other))
+        oth = validate_comparison_units(self, other, 'equal')
+        return np.array(self).__eq__(np.array(oth))
 
     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 is None:
             return True
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError("not equal", self.units, other.units)
-
-            return np.array(self).__ne__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__ne__(np.array(other))
+        oth = validate_comparison_units(self, other, 'not equal')
+        return np.array(self).__ne__(np.array(oth))
 
     def __ge__(self, other):
         """ Test if this is greater than or equal to other. """
         # Check that the other is a YTArray.
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError("greater than or equal",
-                                           self.units, other.units)
-
-            return np.array(self).__ge__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__ge__(np.array(other))
+        oth = validate_comparison_units(self, other, 'greater than or equal')
+        return np.array(self).__ge__(np.array(oth))
 
     def __gt__(self, other):
         """ Test if this is greater than the object on the right. """
         # Check that the other is a YTArray.
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError("greater than", self.units,
-                                           other.units)
-
-            return np.array(self).__gt__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__gt__(np.array(other))
+        oth = validate_comparison_units(self, other, 'greater than')
+        return np.array(self).__gt__(np.array(oth))
 
     #
     # End comparison operators


https://bitbucket.org/yt_analysis/yt/commits/23230f4fbd9f/
Changeset:   23230f4fbd9f
Branch:      yt
User:        ngoldbaum
Date:        2015-08-09 17:02:06+00:00
Summary:     Only do a unit conversion for comparison operators if we need it

This leads to a 5x speedup for iterating over a YTArray
Affected #:  1 file

diff -r edb0a43209903f32588fe78830bdbac18b76e697 -r 23230f4fbd9f715a93ac077b55c82670979d98a0 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -139,8 +139,8 @@
     if isinstance(other, YTArray):
         if not this.units.same_dimensions_as(other.units):
             raise YTUnitOperationError(op_string, this.units, other.units)
-
-        return other.in_units(this.units)
+        if this.units.expr != other.units.expr:
+            return other.in_units(this.units)
 
     return other
 


https://bitbucket.org/yt_analysis/yt/commits/a8eff5b3d056/
Changeset:   a8eff5b3d056
Branch:      yt
User:        ngoldbaum
Date:        2015-08-09 18:45:18+00:00
Summary:     Add a way to bypass input validation for YTArray
Affected #:  1 file

diff -r 23230f4fbd9f715a93ac077b55c82670979d98a0 -r a8eff5b3d056f5af29a889e9da89a2a557efd2ef yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -176,7 +176,13 @@
         with a unit registry and this is specified, this will be used instead of
         the registry associated with the unit object.
     dtype : string or NumPy dtype object
-        The dtype of the array data.
+        The dtype of the array data. Defaults to the dtype of the input data,
+        or, if none is found, uses np.float64
+    bypass_validation : boolean
+        If True, all input validation is skipped. Using this option may produce
+        corrupted, invalid units or array data, but can lead to significant
+        speedups in the input validation logic adds significant overhead. If set,
+        input_units *must* be a valid unit object. Defaults to False.
 
     Examples
     --------
@@ -295,9 +301,16 @@
 
     __array_priority__ = 2.0
 
-    def __new__(cls, input_array, input_units=None, registry=None, dtype=None):
+    def __new__(cls, input_array, input_units=None, registry=None, dtype=None,
+                bypass_validation=False):
         if dtype is None:
             dtype = getattr(input_array, 'dtype', np.float64)
+        if bypass_validation is True:
+            obj = np.asarray(input_array, dtype=dtype).view(cls)
+            obj.units = input_units
+            if registry is not None:
+                obj.units.registry = registry
+            return obj
         if input_array is NotImplemented:
             return input_array
         if registry is None and isinstance(input_units, (str, bytes)):
@@ -992,7 +1005,7 @@
     def __getitem__(self, item):
         ret = super(YTArray, self).__getitem__(item)
         if ret.shape == ():
-            return YTQuantity(ret, self.units)
+            return YTQuantity(ret, self.units, bypass_validation=True)
         else:
             return ret
 
@@ -1159,11 +1172,11 @@
 
     """
     def __new__(cls, input_scalar, input_units=None, registry=None,
-                dtype=np.float64):
+                dtype=np.float64, bypass_validation=False):
         if not isinstance(input_scalar, (numeric_type, np.number, np.ndarray)):
             raise RuntimeError("YTQuantity values must be numeric")
         ret = YTArray.__new__(cls, input_scalar, input_units, registry,
-                              dtype=dtype)
+                              dtype=dtype, bypass_validation=bypass_validation)
         if ret.size > 1:
             raise RuntimeError("YTQuantity instances must be scalars")
         return ret


https://bitbucket.org/yt_analysis/yt/commits/2294729c569d/
Changeset:   2294729c569d
Branch:      yt
User:        ngoldbaum
Date:        2015-08-09 18:45:59+00:00
Summary:     Eplicitly overload iteration and copying to speed up iteration over a YTArray
Affected #:  1 file

diff -r a8eff5b3d056f5af29a889e9da89a2a557efd2ef -r 2294729c569dd02c422aaebc560f45aa9cc8476a yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -1009,6 +1009,17 @@
         else:
             return ret
 
+    def __iter__(self):
+        nextret = None
+        for item in self.ndarray_view():
+            if nextret is None:
+                ret = YTQuantity(item, self.units, bypass_validation=True)
+            else:
+                ret = nextret
+                np.put(ret, 0, item)
+            nextret = ret.copy()
+            yield ret
+
     def __array_wrap__(self, out_arr, context=None):
         ret = super(YTArray, self).__array_wrap__(out_arr, context)
         if isinstance(ret, YTQuantity) and ret.shape != ():
@@ -1104,6 +1115,10 @@
         registry = UnitRegistry(lut=lut, add_default_symbols=False)
         self.units = Unit(unit, registry=registry)
 
+    def __copy__(self):
+        ret = super(YTArray, self).__copy__()
+        return type(self)(ret, self.units)
+
     def __deepcopy__(self, memodict=None):
         """copy.deepcopy implementation
 


https://bitbucket.org/yt_analysis/yt/commits/1484ed821322/
Changeset:   1484ed821322
Branch:      yt
User:        ngoldbaum
Date:        2015-08-09 20:10:24+00:00
Summary:     Remove unnecessary copy function
Affected #:  1 file

diff -r 2294729c569dd02c422aaebc560f45aa9cc8476a -r 1484ed82132249a78c8508d5d331aeffbace5379 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -1115,10 +1115,6 @@
         registry = UnitRegistry(lut=lut, add_default_symbols=False)
         self.units = Unit(unit, registry=registry)
 
-    def __copy__(self):
-        ret = super(YTArray, self).__copy__()
-        return type(self)(ret, self.units)
-
     def __deepcopy__(self, memodict=None):
         """copy.deepcopy implementation
 


https://bitbucket.org/yt_analysis/yt/commits/ede84c9c7631/
Changeset:   ede84c9c7631
Branch:      yt
User:        ngoldbaum
Date:        2015-08-09 20:35:12+00:00
Summary:     Minor speedup from using exact object comparison to compare unit expressions
Affected #:  1 file

diff -r 1484ed82132249a78c8508d5d331aeffbace5379 -r ede84c9c76312594c3d8dd047187a93cda54d822 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -136,11 +136,12 @@
 
 def validate_comparison_units(this, other, op_string):
     # Check that other is a YTArray.
-    if isinstance(other, YTArray):
+    if hasattr(other, 'units'):
+        if this.units.expr is other.units.expr:
+            return other
         if not this.units.same_dimensions_as(other.units):
             raise YTUnitOperationError(op_string, this.units, other.units)
-        if this.units.expr != other.units.expr:
-            return other.in_units(this.units)
+        return other.in_units(this.units)
 
     return other
 


https://bitbucket.org/yt_analysis/yt/commits/97d54cddabb9/
Changeset:   97d54cddabb9
Branch:      yt
User:        ngoldbaum
Date:        2015-08-09 21:13:29+00:00
Summary:     Use [()] indexing for a small speedup compared to put
Affected #:  1 file

diff -r ede84c9c76312594c3d8dd047187a93cda54d822 -r 97d54cddabb9c057a5cec340487579c9c564c107 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -1012,7 +1012,7 @@
 
     def __iter__(self):
         nextret = None
-        for item in self.ndarray_view():
+        for item in np.nditer(self.ndarray_view()):
             if nextret is None:
                 ret = YTQuantity(item, self.units, bypass_validation=True)
             else:


https://bitbucket.org/yt_analysis/yt/commits/e04f70e0a91d/
Changeset:   e04f70e0a91d
Branch:      yt
User:        ngoldbaum
Date:        2015-08-10 16:00:08+00:00
Summary:     Allow writing to array while iterating over it
Affected #:  1 file

diff -r 97d54cddabb9c057a5cec340487579c9c564c107 -r e04f70e0a91d80537bc75029259d5beecaba0a9e yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -1012,7 +1012,7 @@
 
     def __iter__(self):
         nextret = None
-        for item in np.nditer(self.ndarray_view()):
+        for item in np.nditer(self.ndarray_view(), op_flags=["readwrite"]):
             if nextret is None:
                 ret = YTQuantity(item, self.units, bypass_validation=True)
             else:


https://bitbucket.org/yt_analysis/yt/commits/60c55599d460/
Changeset:   60c55599d460
Branch:      yt
User:        ngoldbaum
Date:        2015-08-10 16:31:48+00:00
Summary:     Remove __iter__ method for now since it breaks unpacking in some situations
Affected #:  1 file

diff -r e04f70e0a91d80537bc75029259d5beecaba0a9e -r 60c55599d460e7ef93a8eb83a4a4624d1760d1f6 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -1010,17 +1010,6 @@
         else:
             return ret
 
-    def __iter__(self):
-        nextret = None
-        for item in np.nditer(self.ndarray_view(), op_flags=["readwrite"]):
-            if nextret is None:
-                ret = YTQuantity(item, self.units, bypass_validation=True)
-            else:
-                ret = nextret
-                np.put(ret, 0, item)
-            nextret = ret.copy()
-            yield ret
-
     def __array_wrap__(self, out_arr, context=None):
         ret = super(YTArray, self).__array_wrap__(out_arr, context)
         if isinstance(ret, YTQuantity) and ret.shape != ():


https://bitbucket.org/yt_analysis/yt/commits/a46e96ef9fcb/
Changeset:   a46e96ef9fcb
Branch:      yt
User:        MatthewTurk
Date:        2015-08-13 16:26:47+00:00
Summary:     Merged in ngoldbaum/yt (pull request #1687)

[perf] [enhancement] Speedups for iterating over YTArray instances
Affected #:  2 files

diff -r 475280b53ae10379ad79ac02519763085ec634c8 -r a46e96ef9fcbbcd1d4ebccae406938d166212f2a yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -554,6 +554,17 @@
     yield assert_true, a_slice.base is a
 
 
+def test_iteration():
+    """
+    Test that iterating over a YTArray returns a sequence of YTQuantity insances
+    """
+    a = np.arange(3)
+    b = YTArray(np.arange(3), 'cm')
+    for ia, ib, in zip(a, b):
+        yield assert_equal, ia, ib.value
+        yield assert_equal, ib.units, b.units
+
+
 def test_fix_length():
     """
     Test fixing the length of an array. Used in spheres and other data objects

diff -r 475280b53ae10379ad79ac02519763085ec634c8 -r a46e96ef9fcbbcd1d4ebccae406938d166212f2a yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -135,6 +135,17 @@
             raise YTUnitOperationError(op_string, inp.units, dimensionless)
     return ret
 
+def validate_comparison_units(this, other, op_string):
+    # Check that other is a YTArray.
+    if hasattr(other, 'units'):
+        if this.units.expr is other.units.expr:
+            return other
+        if not this.units.same_dimensions_as(other.units):
+            raise YTUnitOperationError(op_string, this.units, other.units)
+        return other.in_units(this.units)
+
+    return other
+
 unary_operators = (
     negative, absolute, rint, ones_like, sign, conj, exp, exp2, log, log2,
     log10, expm1, log1p, sqrt, square, reciprocal, sin, cos, tan, arcsin,
@@ -167,7 +178,13 @@
         with a unit registry and this is specified, this will be used instead of
         the registry associated with the unit object.
     dtype : string or NumPy dtype object
-        The dtype of the array data.
+        The dtype of the array data. Defaults to the dtype of the input data,
+        or, if none is found, uses np.float64
+    bypass_validation : boolean
+        If True, all input validation is skipped. Using this option may produce
+        corrupted, invalid units or array data, but can lead to significant
+        speedups in the input validation logic adds significant overhead. If set,
+        input_units *must* be a valid unit object. Defaults to False.
 
     Examples
     --------
@@ -286,9 +303,16 @@
 
     __array_priority__ = 2.0
 
-    def __new__(cls, input_array, input_units=None, registry=None, dtype=None):
+    def __new__(cls, input_array, input_units=None, registry=None, dtype=None,
+                bypass_validation=False):
         if dtype is None:
             dtype = getattr(input_array, 'dtype', np.float64)
+        if bypass_validation is True:
+            obj = np.asarray(input_array, dtype=dtype).view(cls)
+            obj.units = input_units
+            if registry is not None:
+                obj.units.registry = registry
+            return obj
         if input_array is NotImplemented:
             return input_array
         if registry is None and isinstance(input_units, (str, bytes)):
@@ -911,26 +935,13 @@
     # @todo: outsource to a single method with an op argument.
     def __lt__(self, other):
         """ Test if this is less than the object on the right. """
-        # Check that other is a YTArray.
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError('less than', self.units, other.units)
-
-            return np.array(self).__lt__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__lt__(np.array(other))
+        oth = validate_comparison_units(self, other, 'less_than')
+        return np.array(self).__lt__(np.array(oth))
 
     def __le__(self, other):
         """ Test if this is less than or equal to the object on the right. """
-        # Check that other is a YTArray.
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError('less than or equal', self.units,
-                                           other.units)
-
-            return np.array(self).__le__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__le__(np.array(other))
+        oth = validate_comparison_units(self, other, 'less_than or equal')
+        return np.array(self).__le__(np.array(oth))
 
     def __eq__(self, other):
         """ Test if this is equal to the object on the right. """
@@ -938,50 +949,28 @@
         if other is None:
             # self is a YTArray, so it can't be None.
             return False
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError("equal", self.units, other.units)
-
-            return np.array(self).__eq__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__eq__(np.array(other))
+        oth = validate_comparison_units(self, other, 'equal')
+        return np.array(self).__eq__(np.array(oth))
 
     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 is None:
             return True
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError("not equal", self.units, other.units)
-
-            return np.array(self).__ne__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__ne__(np.array(other))
+        oth = validate_comparison_units(self, other, 'not equal')
+        return np.array(self).__ne__(np.array(oth))
 
     def __ge__(self, other):
         """ Test if this is greater than or equal to other. """
         # Check that the other is a YTArray.
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError("greater than or equal",
-                                           self.units, other.units)
-
-            return np.array(self).__ge__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__ge__(np.array(other))
+        oth = validate_comparison_units(self, other, 'greater than or equal')
+        return np.array(self).__ge__(np.array(oth))
 
     def __gt__(self, other):
         """ Test if this is greater than the object on the right. """
         # Check that the other is a YTArray.
-        if isinstance(other, YTArray):
-            if not self.units.same_dimensions_as(other.units):
-                raise YTUnitOperationError("greater than", self.units,
-                                           other.units)
-
-            return np.array(self).__gt__(np.array(other.in_units(self.units)))
-
-        return np.array(self).__gt__(np.array(other))
+        oth = validate_comparison_units(self, other, 'greater than')
+        return np.array(self).__gt__(np.array(oth))
 
     #
     # End comparison operators
@@ -1018,7 +1007,7 @@
     def __getitem__(self, item):
         ret = super(YTArray, self).__getitem__(item)
         if ret.shape == ():
-            return YTQuantity(ret, self.units)
+            return YTQuantity(ret, self.units, bypass_validation=True)
         else:
             return ret
 
@@ -1185,11 +1174,11 @@
 
     """
     def __new__(cls, input_scalar, input_units=None, registry=None,
-                dtype=np.float64):
+                dtype=np.float64, bypass_validation=False):
         if not isinstance(input_scalar, (numeric_type, np.number, np.ndarray)):
             raise RuntimeError("YTQuantity values must be numeric")
         ret = YTArray.__new__(cls, input_scalar, input_units, registry,
-                              dtype=dtype)
+                              dtype=dtype, bypass_validation=bypass_validation)
         if ret.size > 1:
             raise RuntimeError("YTQuantity instances must be scalars")
         return ret

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