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

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Mar 19 05:55:17 PDT 2014


7 new commits in yt:

https://bitbucket.org/yt_analysis/yt/commits/f521ab6e5ba8/
Changeset:   f521ab6e5ba8
Branch:      yt-3.0
User:        ngoldbaum
Date:        2014-03-18 05:41:38
Summary:     Fixing minor flake8 issues in yt_array.py.
Affected #:  1 file

diff -r 4bc951ee2db7e00d453de6a3bdf95fdd03732836 -r f521ab6e5ba88f4813ee258d81121c98b27812c9 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -16,19 +16,18 @@
 import copy
 
 import numpy as np
-import sympy
 
 from functools import wraps
 from numpy import \
-     add, subtract, multiply, divide, logaddexp, logaddexp2, true_divide, \
-     floor_divide, negative, power, remainder, mod, fmod, absolute, rint, \
-     sign, conj, exp, exp2, log, log2, log10, expm1, log1p, sqrt, square, \
-     reciprocal, ones_like, sin, cos, tan, arcsin, arccos, arctan, arctan2, \
-     hypot, sinh, cosh, tanh, arcsinh, arccosh, arctanh, deg2rad, rad2deg, \
-     greater, greater_equal, less, less_equal, not_equal, equal, logical_and, \
-     logical_or, logical_xor, logical_not, maximum, minimum, isreal, iscomplex, \
-     isfinite, isinf, isnan, signbit, copysign, nextafter, modf, frexp, \
-     floor, ceil, trunc, fmax, fmin
+    add, subtract, multiply, divide, logaddexp, logaddexp2, true_divide, \
+    floor_divide, negative, power, remainder, mod, fmod, absolute, rint, \
+    sign, conj, exp, exp2, log, log2, log10, expm1, log1p, sqrt, square, \
+    reciprocal, ones_like, sin, cos, tan, arcsin, arccos, arctan, arctan2, \
+    hypot, sinh, cosh, tanh, arcsinh, arccosh, arctanh, deg2rad, rad2deg, \
+    greater, greater_equal, less, less_equal, not_equal, equal, logical_and, \
+    logical_or, logical_xor, logical_not, maximum, minimum, isreal, iscomplex, \
+    isfinite, isinf, isnan, signbit, copysign, nextafter, modf, frexp, \
+    floor, ceil, trunc, fmax, fmin
 
 from yt.units.unit_object import Unit
 from yt.units.unit_registry import UnitRegistry
@@ -49,7 +48,7 @@
     def wrapped(unit):
         if unit != Unit():
             raise RuntimeError(
-                "This operation is only defined for unitless quantities. " \
+                "This operation is only defined for unitless quantities. "
                 "Received unit (%s)" % unit
                 )
         return func(unit)


https://bitbucket.org/yt_analysis/yt/commits/becdaa3cfe12/
Changeset:   becdaa3cfe12
Branch:      yt-3.0
User:        ngoldbaum
Date:        2014-03-18 05:42:15
Summary:     Adding convenience methods for ndarray casting and YTArray and YTQuantity creation
Affected #:  2 files

diff -r f521ab6e5ba88f4813ee258d81121c98b27812c9 -r becdaa3cfe128a6e1927bf92ccd2a433cd1cf384 yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -549,3 +549,25 @@
 
         for pair in itertools.product([a,b,c,d,e], repeat=2):
             yield binary_ufunc_comparison, ufunc, pair[0], pair[1]
+
+def test_convenience():
+
+    arr = YTArray([1, 2, 3], 'cm')
+
+    yield assert_equal, arr.unit_quantity, YTQuantity(1, 'cm')
+    yield assert_equal, arr.uq, YTQuantity(1, 'cm')
+    yield assert_isinstance, arr.unit_quantity, YTQuantity
+    yield assert_isinstance, arr.uq, YTQuantity
+
+    yield assert_array_equal, arr.unit_array, YTArray(np.ones_like(arr), 'cm')
+    yield assert_array_equal, arr.ua, YTArray(np.ones_like(arr), 'cm')
+    yield assert_isinstance, arr.unit_array, YTArray
+    yield assert_isinstance, arr.ua, YTArray
+
+    yield assert_array_equal, arr.data, arr.view(np.ndarray)
+    yield assert_array_equal, arr.d, arr.view(np.ndarray)
+    yield assert_true, arr.data.base is arr.base
+    yield assert_true, arr.d.base is arr.base
+
+    yield assert_array_equal, arr.value, np.array(arr)
+    yield assert_array_equal, arr.v, np.array(arr)

diff -r f521ab6e5ba88f4813ee258d81121c98b27812c9 -r becdaa3cfe128a6e1927bf92ccd2a433cd1cf384 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -398,6 +398,34 @@
     #
 
     #
+    # Start convenience methods
+    #
+
+    @property
+    def value(self):
+        return np.array(self)
+
+    v = value
+
+    @property
+    def data(self):
+        return self.ndarray_view()
+
+    d = data
+
+    @property
+    def unit_quantity(self):
+        return YTQuantity(1.0, self.units)
+
+    uq = unit_quantity
+
+    @property
+    def unit_array(self):
+        return np.ones_like(self)
+
+    ua = unit_array
+
+    #
     # Start operation methods
     #
 


https://bitbucket.org/yt_analysis/yt/commits/c19fa58337f1/
Changeset:   c19fa58337f1
Branch:      yt-3.0
User:        ngoldbaum
Date:        2014-03-18 05:48:03
Summary:     Removing a redundant definition of the value property of YTArray.
Affected #:  1 file

diff -r becdaa3cfe128a6e1927bf92ccd2a433cd1cf384 -r c19fa58337f18bbf2c1a69589d20757fdb825bbd yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -828,10 +828,6 @@
     def __repr__(self):
         return str(self)
 
-    @property
-    def value(self):
-        return np.array(self)
-
 def uconcatenate(arrs, *args, **kwargs):
     v = np.concatenate(arrs, *args, **kwargs)
     if not any(isinstance(a, YTArray) for a in arrs):


https://bitbucket.org/yt_analysis/yt/commits/3d57a8154158/
Changeset:   3d57a8154158
Branch:      yt-3.0
User:        ngoldbaum
Date:        2014-03-19 03:59:49
Summary:     Responding to PR comments.  Adding docstrings.
Affected #:  1 file

diff -r c19fa58337f18bbf2c1a69589d20757fdb825bbd -r 3d57a8154158d239e22f293a72d57ab67f927aba yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -403,24 +403,28 @@
 
     @property
     def value(self):
+        """Get a copy of the array data as a numpy ndarray"""
         return np.array(self)
 
     v = value
 
     @property
-    def data(self):
+    def ndview(self):
+        """Get a view of the array data."""
         return self.ndarray_view()
 
-    d = data
+    d = ndview
 
     @property
     def unit_quantity(self):
+        """Get a YTQuantity with the same unit as this array and a value of 1.0"""
         return YTQuantity(1.0, self.units)
 
     uq = unit_quantity
 
     @property
     def unit_array(self):
+        """Get a YTArray filled with ones with the same unit and shape as this array"""
         return np.ones_like(self)
 
     ua = unit_array


https://bitbucket.org/yt_analysis/yt/commits/26c7f8a4038f/
Changeset:   26c7f8a4038f
Branch:      yt-3.0
User:        ngoldbaum
Date:        2014-03-19 04:00:06
Summary:     Updating the units docs.
Affected #:  3 files

diff -r 3d57a8154158d239e22f293a72d57ab67f927aba -r 26c7f8a4038fd32d104587cf0d9fa915a85f43e7 doc/source/analyzing/units/1)_Symbolic_Units.ipynb
--- a/doc/source/analyzing/units/1)_Symbolic_Units.ipynb
+++ b/doc/source/analyzing/units/1)_Symbolic_Units.ipynb
@@ -1,7 +1,7 @@
 {
  "metadata": {
   "name": "",
-  "signature": "sha256:982174bfd01e41ea6510dc5cbff556dc82e6f13b8d0c6189324f3ca7a81b702a"
+  "signature": "sha256:52f186664831f5290b31ec433114927b9771e224bd79d0c82dd3d9a8d9c09bf6"
  },
  "nbformat": 3,
  "nbformat_minor": 0,
@@ -227,6 +227,119 @@
      "cell_type": "markdown",
      "metadata": {},
      "source": [
+      "When working with a YTArray with complicated units, you can use `unit_array` and `unit_quantity` to conveniently apply units to data:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "test_array = YTArray(np.random.random(20), 'erg/s')\n",
+      "\n",
+      "print test_array"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "`unit_quantity` returns a `YTQuantity` with a value of 1.0 and the same units as the array it is a attached to."
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print test_array.unit_quantity"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "`unit_array` returns a `YTArray` with the same units and shape as the array it is a attached to and with all values set to 1.0."
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print test_array.unit_array"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "These are useful when doing arithmetic:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print test_array + 1.0*test_array.unit_quantity"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print test_array + np.arange(20)*test_array.unit_array"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "For convenience, `unit_quantity` is also available via `uq` and `unit_array` is available via `ua`:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print test_array.uq\n",
+      "\n",
+      "print test_array.unit_quantity == test_array.uq"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "from numpy import array_equal\n",
+      "\n",
+      "print test_array.ua\n",
+      "\n",
+      "print array_equal(test_array.ua, test_array.unit_array)"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
       "Unit metadata is encoded in the `units` attribute that hangs off of `YTArray` or `YTQuantity` instances:"
      ]
     },
@@ -249,6 +362,14 @@
      "outputs": []
     },
     {
+     "cell_type": "heading",
+     "level": 3,
+     "metadata": {},
+     "source": [
+      "Arithmetic with `YTQuantity` and `YTArray`"
+     ]
+    },
+    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
@@ -342,10 +463,15 @@
      "cell_type": "code",
      "collapsed": false,
      "input": [
+      "from yt.utilities.exceptions import YTUnitOperationError\n",
+      "\n",
       "a = YTQuantity(3, 'm')\n",
       "b = YTQuantity(5, 'erg')\n",
       "\n",
-      "print a+b"
+      "try:\n",
+      "    print a+b\n",
+      "except YTUnitOperationError as e:\n",
+      "    print e"
      ],
      "language": "python",
      "metadata": {},

diff -r 3d57a8154158d239e22f293a72d57ab67f927aba -r 26c7f8a4038fd32d104587cf0d9fa915a85f43e7 doc/source/analyzing/units/2)_Data_Selection_and_fields.ipynb
--- a/doc/source/analyzing/units/2)_Data_Selection_and_fields.ipynb
+++ b/doc/source/analyzing/units/2)_Data_Selection_and_fields.ipynb
@@ -1,7 +1,7 @@
 {
  "metadata": {
   "name": "",
-  "signature": "sha256:b22680b89964ce22188c795c6d0e498dc5654851919949ec3c5ac8822002a43d"
+  "signature": "sha256:8e1a5db9e3869bcf761ff39c5a95d21458b7c4205f00da3d3f973d398422a466"
  },
  "nbformat": 3,
  "nbformat_minor": 0,
@@ -105,23 +105,60 @@
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "YTArray defines several user-visible member functions: \n",
+      "YTArray defines several user-visible member functions that allow data to be converted from one unit system to another:\n",
       "\n",
-      "* `convert_to_units`\n",
-      "* `convert_to_cgs`\n",
       "* `in_units`\n",
       "* `in_cgs`\n",
-      "* `to_ndarray`\n",
-      "\n",
-      "The first two functions do in-place operations while the second two return copies of the original array in the new unit:"
+      "* `convert_to_units`\n",
+      "* `convert_to_cgs`"
+     ]
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "The first method, `in_units`, returns a copy of the array in the units denoted by a string argument:"
      ]
     },
     {
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "print dd['density'].in_units('Msun/pc**3')\n",
-      "\n",
+      "print dd['density'].in_units('Msun/pc**3')"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "The second, `in_cgs`, returns a copy of the array converted into the base units of yt's CGS unit system:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print (dd['pressure']/dd['density'])\n",
+      "print (dd['pressure']/dd['density']).in_cgs()"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "The next two methods do in-place conversions:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
       "dens = dd['density']\n",
       "print dens\n",
       "\n",
@@ -136,14 +173,14 @@
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "One possibly confusing wrinkle in this is that the unit conversions are always done 'in place' so if you try to query `dd['density']` again, you'll find that it has been converted to solar masses per cubic parsec:"
+      "One possibly confusing wrinkle when using in-place conversions is if you try to query `dd['density']` again, you'll find that it has been converted to solar masses per cubic parsec:"
      ]
     },
     {
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "print dens\n",
+      "print dd['density']\n",
       "\n",
       "dens.convert_to_units('g/cm**3')\n",
       "\n",
@@ -176,10 +213,18 @@
      "outputs": []
     },
     {
+     "cell_type": "heading",
+     "level": 3,
+     "metadata": {},
+     "source": [
+      "Working with views and converting to ndarray"
+     ]
+    },
+    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "Of course, the data is always available as a raw NumPy array.  There are two ways to copy the contents of a `YTArray` into an ndarray:"
+      "There are two ways to convert the data into a numpy array.  The most straightforward and safe way to do this is to create a copy of the array data.  The following cell demonstrates four equivalent ways of doing this, in increasing degree of terseness."
      ]
     },
     {
@@ -188,8 +233,12 @@
      "input": [
       "import numpy as np\n",
       "\n",
-      "print dd['cell_mass'].to_ndarray()\n",
-      "print np.array(dd['cell_mass'])"
+      "dens = dd['cell_mass']\n",
+      "\n",
+      "print dens.to_ndarray()\n",
+      "print np.array(dens)\n",
+      "print dens.value\n",
+      "print dens.v"
      ],
      "language": "python",
      "metadata": {},
@@ -199,7 +248,14 @@
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "Similarly, one can get a view into the underlying array data:"
+      "Since we have a copy of the data, we can mess with it however we wish without disturbing the original data returned by the yt data object."
+     ]
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "Another way to touch the raw array data is to get a _view_.  A numpy view is a lightweight array interface to a memory buffer. There are four ways to create views of YTArray instances:"
      ]
     },
     {
@@ -207,7 +263,9 @@
      "collapsed": false,
      "input": [
       "print dd['cell_mass'].ndarray_view()\n",
-      "print dd['cell_mass'].view(np.ndarray)"
+      "print dd['cell_mass'].view(np.ndarray)\n",
+      "print dd['cell_mass'].ndview\n",
+      "print dd['cell_mass'].d"
      ],
      "language": "python",
      "metadata": {},
@@ -217,15 +275,17 @@
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "When working with views, rememeber that you are touching the raw array data and no longer have any of the unit checking provided by the unit system."
+      "When working with views, rememeber that you are touching the raw array data and no longer have any of the unit checking provided by the unit system.  This can be useful where it might be more straightforward to treat the array as if it didn't have units but without copying the data."
      ]
     },
     {
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "density = dd['density'].ndarray_view()\n",
-      "density[0:10] = 0\n",
+      "density_values = dd['density'].d\n",
+      "density_values[0:10] = 0\n",
+      "\n",
+      "# The original array was updated\n",
       "print dd['density']"
      ],
      "language": "python",

diff -r 3d57a8154158d239e22f293a72d57ab67f927aba -r 26c7f8a4038fd32d104587cf0d9fa915a85f43e7 doc/source/analyzing/units/5)_Units_and_plotting.ipynb
--- a/doc/source/analyzing/units/5)_Units_and_plotting.ipynb
+++ b/doc/source/analyzing/units/5)_Units_and_plotting.ipynb
@@ -1,7 +1,7 @@
 {
  "metadata": {
   "name": "",
-  "signature": "sha256:e8ff0337ab94f14a8a4edb2b40ea7daaa7afe6b6b29c602207406700afc8157b"
+  "signature": "sha256:981baca6958c75f0d84bbc24be7d2b75af5957d36aa3eb4ba725d9e47a85f80d"
  },
  "nbformat": 3,
  "nbformat_minor": 0,
@@ -82,7 +82,12 @@
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "slc.set_unit('density', 'Msun')"
+      "from yt.utilities.exceptions import YTUnitConversionError\n",
+      "\n",
+      "try:\n",
+      "    slc.set_unit('density', 'Msun')\n",
+      "except YTUnitConversionError as e:\n",
+      "    print e"
      ],
      "language": "python",
      "metadata": {},


https://bitbucket.org/yt_analysis/yt/commits/fbeae9ead817/
Changeset:   fbeae9ead817
Branch:      yt-3.0
User:        ngoldbaum
Date:        2014-03-19 05:22:17
Summary:     Forgot to update the test case.
Affected #:  1 file

diff -r 26c7f8a4038fd32d104587cf0d9fa915a85f43e7 -r fbeae9ead81703c4436f6fb4f7331c2cd849d3ac yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -564,9 +564,9 @@
     yield assert_isinstance, arr.unit_array, YTArray
     yield assert_isinstance, arr.ua, YTArray
 
-    yield assert_array_equal, arr.data, arr.view(np.ndarray)
+    yield assert_array_equal, arr.ndview, arr.view(np.ndarray)
     yield assert_array_equal, arr.d, arr.view(np.ndarray)
-    yield assert_true, arr.data.base is arr.base
+    yield assert_true, arr.ndview.base is arr.base
     yield assert_true, arr.d.base is arr.base
 
     yield assert_array_equal, arr.value, np.array(arr)


https://bitbucket.org/yt_analysis/yt/commits/b67bd51073b2/
Changeset:   b67bd51073b2
Branch:      yt-3.0
User:        MatthewTurk
Date:        2014-03-19 13:55:10
Summary:     Merged in ngoldbaum/yt/yt-3.0 (pull request #728)

Adding convenience methods for unit YTArray and YTQuantity creation
Affected #:  5 files

diff -r 0822a77df2d236b6ee843883ae68c976d66afb2a -r b67bd51073b20a9a851e73e3e53eb2b2fbb5cc63 doc/source/analyzing/units/1)_Symbolic_Units.ipynb
--- a/doc/source/analyzing/units/1)_Symbolic_Units.ipynb
+++ b/doc/source/analyzing/units/1)_Symbolic_Units.ipynb
@@ -1,7 +1,7 @@
 {
  "metadata": {
   "name": "",
-  "signature": "sha256:982174bfd01e41ea6510dc5cbff556dc82e6f13b8d0c6189324f3ca7a81b702a"
+  "signature": "sha256:52f186664831f5290b31ec433114927b9771e224bd79d0c82dd3d9a8d9c09bf6"
  },
  "nbformat": 3,
  "nbformat_minor": 0,
@@ -227,6 +227,119 @@
      "cell_type": "markdown",
      "metadata": {},
      "source": [
+      "When working with a YTArray with complicated units, you can use `unit_array` and `unit_quantity` to conveniently apply units to data:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "test_array = YTArray(np.random.random(20), 'erg/s')\n",
+      "\n",
+      "print test_array"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "`unit_quantity` returns a `YTQuantity` with a value of 1.0 and the same units as the array it is a attached to."
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print test_array.unit_quantity"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "`unit_array` returns a `YTArray` with the same units and shape as the array it is a attached to and with all values set to 1.0."
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print test_array.unit_array"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "These are useful when doing arithmetic:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print test_array + 1.0*test_array.unit_quantity"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print test_array + np.arange(20)*test_array.unit_array"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "For convenience, `unit_quantity` is also available via `uq` and `unit_array` is available via `ua`:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print test_array.uq\n",
+      "\n",
+      "print test_array.unit_quantity == test_array.uq"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "from numpy import array_equal\n",
+      "\n",
+      "print test_array.ua\n",
+      "\n",
+      "print array_equal(test_array.ua, test_array.unit_array)"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
       "Unit metadata is encoded in the `units` attribute that hangs off of `YTArray` or `YTQuantity` instances:"
      ]
     },
@@ -249,6 +362,14 @@
      "outputs": []
     },
     {
+     "cell_type": "heading",
+     "level": 3,
+     "metadata": {},
+     "source": [
+      "Arithmetic with `YTQuantity` and `YTArray`"
+     ]
+    },
+    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
@@ -342,10 +463,15 @@
      "cell_type": "code",
      "collapsed": false,
      "input": [
+      "from yt.utilities.exceptions import YTUnitOperationError\n",
+      "\n",
       "a = YTQuantity(3, 'm')\n",
       "b = YTQuantity(5, 'erg')\n",
       "\n",
-      "print a+b"
+      "try:\n",
+      "    print a+b\n",
+      "except YTUnitOperationError as e:\n",
+      "    print e"
      ],
      "language": "python",
      "metadata": {},

diff -r 0822a77df2d236b6ee843883ae68c976d66afb2a -r b67bd51073b20a9a851e73e3e53eb2b2fbb5cc63 doc/source/analyzing/units/2)_Data_Selection_and_fields.ipynb
--- a/doc/source/analyzing/units/2)_Data_Selection_and_fields.ipynb
+++ b/doc/source/analyzing/units/2)_Data_Selection_and_fields.ipynb
@@ -1,7 +1,7 @@
 {
  "metadata": {
   "name": "",
-  "signature": "sha256:b22680b89964ce22188c795c6d0e498dc5654851919949ec3c5ac8822002a43d"
+  "signature": "sha256:8e1a5db9e3869bcf761ff39c5a95d21458b7c4205f00da3d3f973d398422a466"
  },
  "nbformat": 3,
  "nbformat_minor": 0,
@@ -105,23 +105,60 @@
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "YTArray defines several user-visible member functions: \n",
+      "YTArray defines several user-visible member functions that allow data to be converted from one unit system to another:\n",
       "\n",
-      "* `convert_to_units`\n",
-      "* `convert_to_cgs`\n",
       "* `in_units`\n",
       "* `in_cgs`\n",
-      "* `to_ndarray`\n",
-      "\n",
-      "The first two functions do in-place operations while the second two return copies of the original array in the new unit:"
+      "* `convert_to_units`\n",
+      "* `convert_to_cgs`"
+     ]
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "The first method, `in_units`, returns a copy of the array in the units denoted by a string argument:"
      ]
     },
     {
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "print dd['density'].in_units('Msun/pc**3')\n",
-      "\n",
+      "print dd['density'].in_units('Msun/pc**3')"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "The second, `in_cgs`, returns a copy of the array converted into the base units of yt's CGS unit system:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "print (dd['pressure']/dd['density'])\n",
+      "print (dd['pressure']/dd['density']).in_cgs()"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "The next two methods do in-place conversions:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
       "dens = dd['density']\n",
       "print dens\n",
       "\n",
@@ -136,14 +173,14 @@
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "One possibly confusing wrinkle in this is that the unit conversions are always done 'in place' so if you try to query `dd['density']` again, you'll find that it has been converted to solar masses per cubic parsec:"
+      "One possibly confusing wrinkle when using in-place conversions is if you try to query `dd['density']` again, you'll find that it has been converted to solar masses per cubic parsec:"
      ]
     },
     {
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "print dens\n",
+      "print dd['density']\n",
       "\n",
       "dens.convert_to_units('g/cm**3')\n",
       "\n",
@@ -176,10 +213,18 @@
      "outputs": []
     },
     {
+     "cell_type": "heading",
+     "level": 3,
+     "metadata": {},
+     "source": [
+      "Working with views and converting to ndarray"
+     ]
+    },
+    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "Of course, the data is always available as a raw NumPy array.  There are two ways to copy the contents of a `YTArray` into an ndarray:"
+      "There are two ways to convert the data into a numpy array.  The most straightforward and safe way to do this is to create a copy of the array data.  The following cell demonstrates four equivalent ways of doing this, in increasing degree of terseness."
      ]
     },
     {
@@ -188,8 +233,12 @@
      "input": [
       "import numpy as np\n",
       "\n",
-      "print dd['cell_mass'].to_ndarray()\n",
-      "print np.array(dd['cell_mass'])"
+      "dens = dd['cell_mass']\n",
+      "\n",
+      "print dens.to_ndarray()\n",
+      "print np.array(dens)\n",
+      "print dens.value\n",
+      "print dens.v"
      ],
      "language": "python",
      "metadata": {},
@@ -199,7 +248,14 @@
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "Similarly, one can get a view into the underlying array data:"
+      "Since we have a copy of the data, we can mess with it however we wish without disturbing the original data returned by the yt data object."
+     ]
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "Another way to touch the raw array data is to get a _view_.  A numpy view is a lightweight array interface to a memory buffer. There are four ways to create views of YTArray instances:"
      ]
     },
     {
@@ -207,7 +263,9 @@
      "collapsed": false,
      "input": [
       "print dd['cell_mass'].ndarray_view()\n",
-      "print dd['cell_mass'].view(np.ndarray)"
+      "print dd['cell_mass'].view(np.ndarray)\n",
+      "print dd['cell_mass'].ndview\n",
+      "print dd['cell_mass'].d"
      ],
      "language": "python",
      "metadata": {},
@@ -217,15 +275,17 @@
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "When working with views, rememeber that you are touching the raw array data and no longer have any of the unit checking provided by the unit system."
+      "When working with views, rememeber that you are touching the raw array data and no longer have any of the unit checking provided by the unit system.  This can be useful where it might be more straightforward to treat the array as if it didn't have units but without copying the data."
      ]
     },
     {
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "density = dd['density'].ndarray_view()\n",
-      "density[0:10] = 0\n",
+      "density_values = dd['density'].d\n",
+      "density_values[0:10] = 0\n",
+      "\n",
+      "# The original array was updated\n",
       "print dd['density']"
      ],
      "language": "python",

diff -r 0822a77df2d236b6ee843883ae68c976d66afb2a -r b67bd51073b20a9a851e73e3e53eb2b2fbb5cc63 doc/source/analyzing/units/5)_Units_and_plotting.ipynb
--- a/doc/source/analyzing/units/5)_Units_and_plotting.ipynb
+++ b/doc/source/analyzing/units/5)_Units_and_plotting.ipynb
@@ -1,7 +1,7 @@
 {
  "metadata": {
   "name": "",
-  "signature": "sha256:e8ff0337ab94f14a8a4edb2b40ea7daaa7afe6b6b29c602207406700afc8157b"
+  "signature": "sha256:981baca6958c75f0d84bbc24be7d2b75af5957d36aa3eb4ba725d9e47a85f80d"
  },
  "nbformat": 3,
  "nbformat_minor": 0,
@@ -82,7 +82,12 @@
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "slc.set_unit('density', 'Msun')"
+      "from yt.utilities.exceptions import YTUnitConversionError\n",
+      "\n",
+      "try:\n",
+      "    slc.set_unit('density', 'Msun')\n",
+      "except YTUnitConversionError as e:\n",
+      "    print e"
      ],
      "language": "python",
      "metadata": {},

diff -r 0822a77df2d236b6ee843883ae68c976d66afb2a -r b67bd51073b20a9a851e73e3e53eb2b2fbb5cc63 yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -549,3 +549,25 @@
 
         for pair in itertools.product([a,b,c,d,e], repeat=2):
             yield binary_ufunc_comparison, ufunc, pair[0], pair[1]
+
+def test_convenience():
+
+    arr = YTArray([1, 2, 3], 'cm')
+
+    yield assert_equal, arr.unit_quantity, YTQuantity(1, 'cm')
+    yield assert_equal, arr.uq, YTQuantity(1, 'cm')
+    yield assert_isinstance, arr.unit_quantity, YTQuantity
+    yield assert_isinstance, arr.uq, YTQuantity
+
+    yield assert_array_equal, arr.unit_array, YTArray(np.ones_like(arr), 'cm')
+    yield assert_array_equal, arr.ua, YTArray(np.ones_like(arr), 'cm')
+    yield assert_isinstance, arr.unit_array, YTArray
+    yield assert_isinstance, arr.ua, YTArray
+
+    yield assert_array_equal, arr.ndview, arr.view(np.ndarray)
+    yield assert_array_equal, arr.d, arr.view(np.ndarray)
+    yield assert_true, arr.ndview.base is arr.base
+    yield assert_true, arr.d.base is arr.base
+
+    yield assert_array_equal, arr.value, np.array(arr)
+    yield assert_array_equal, arr.v, np.array(arr)

diff -r 0822a77df2d236b6ee843883ae68c976d66afb2a -r b67bd51073b20a9a851e73e3e53eb2b2fbb5cc63 yt/units/yt_array.py
--- a/yt/units/yt_array.py
+++ b/yt/units/yt_array.py
@@ -16,19 +16,18 @@
 import copy
 
 import numpy as np
-import sympy
 
 from functools import wraps
 from numpy import \
-     add, subtract, multiply, divide, logaddexp, logaddexp2, true_divide, \
-     floor_divide, negative, power, remainder, mod, fmod, absolute, rint, \
-     sign, conj, exp, exp2, log, log2, log10, expm1, log1p, sqrt, square, \
-     reciprocal, ones_like, sin, cos, tan, arcsin, arccos, arctan, arctan2, \
-     hypot, sinh, cosh, tanh, arcsinh, arccosh, arctanh, deg2rad, rad2deg, \
-     greater, greater_equal, less, less_equal, not_equal, equal, logical_and, \
-     logical_or, logical_xor, logical_not, maximum, minimum, isreal, iscomplex, \
-     isfinite, isinf, isnan, signbit, copysign, nextafter, modf, frexp, \
-     floor, ceil, trunc, fmax, fmin
+    add, subtract, multiply, divide, logaddexp, logaddexp2, true_divide, \
+    floor_divide, negative, power, remainder, mod, fmod, absolute, rint, \
+    sign, conj, exp, exp2, log, log2, log10, expm1, log1p, sqrt, square, \
+    reciprocal, ones_like, sin, cos, tan, arcsin, arccos, arctan, arctan2, \
+    hypot, sinh, cosh, tanh, arcsinh, arccosh, arctanh, deg2rad, rad2deg, \
+    greater, greater_equal, less, less_equal, not_equal, equal, logical_and, \
+    logical_or, logical_xor, logical_not, maximum, minimum, isreal, iscomplex, \
+    isfinite, isinf, isnan, signbit, copysign, nextafter, modf, frexp, \
+    floor, ceil, trunc, fmax, fmin
 
 from yt.units.unit_object import Unit
 from yt.units.unit_registry import UnitRegistry
@@ -49,7 +48,7 @@
     def wrapped(unit):
         if unit != Unit():
             raise RuntimeError(
-                "This operation is only defined for unitless quantities. " \
+                "This operation is only defined for unitless quantities. "
                 "Received unit (%s)" % unit
                 )
         return func(unit)
@@ -399,6 +398,38 @@
     #
 
     #
+    # Start convenience methods
+    #
+
+    @property
+    def value(self):
+        """Get a copy of the array data as a numpy ndarray"""
+        return np.array(self)
+
+    v = value
+
+    @property
+    def ndview(self):
+        """Get a view of the array data."""
+        return self.ndarray_view()
+
+    d = ndview
+
+    @property
+    def unit_quantity(self):
+        """Get a YTQuantity with the same unit as this array and a value of 1.0"""
+        return YTQuantity(1.0, self.units)
+
+    uq = unit_quantity
+
+    @property
+    def unit_array(self):
+        """Get a YTArray filled with ones with the same unit and shape as this array"""
+        return np.ones_like(self)
+
+    ua = unit_array
+
+    #
     # Start operation methods
     #
 
@@ -801,10 +832,6 @@
     def __repr__(self):
         return str(self)
 
-    @property
-    def value(self):
-        return np.array(self)
-
 def uconcatenate(arrs, *args, **kwargs):
     v = np.concatenate(arrs, *args, **kwargs)
     if not any(isinstance(a, YTArray) for a in arrs):

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