[yt-svn] commit/yt: MatthewTurk: Merged in MatthewTurk/yt/yt-3.0 (pull request #898)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed May 14 05:23:26 PDT 2014


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/036f745796c1/
Changeset:   036f745796c1
Branch:      yt-3.0
User:        MatthewTurk
Date:        2014-05-14 14:23:19
Summary:     Merged in MatthewTurk/yt/yt-3.0 (pull request #898)

Adding first ragged array extension.
Affected #:  3 files

diff -r 4d1488b8fa476b1e6f5cb90c16312fce75abe34e -r 036f745796c12e66b4636835800f2b029bbe7b4b yt/utilities/lib/ragged_arrays.pyx
--- /dev/null
+++ b/yt/utilities/lib/ragged_arrays.pyx
@@ -0,0 +1,97 @@
+"""
+Some simple operations for operating on ragged arrays
+
+
+
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2014, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+import numpy as np
+cimport numpy as np
+cimport cython
+
+cdef fused numpy_dt:
+    np.float32_t
+    np.float64_t
+    np.int32_t
+    np.int64_t
+
+cdef numpy_dt r_min(numpy_dt a, numpy_dt b):
+    if a < b: return a
+    return b
+
+cdef numpy_dt r_max(numpy_dt a, numpy_dt b):
+    if a > b: return a
+    return b
+
+cdef numpy_dt r_add(numpy_dt a, numpy_dt b):
+    return a + b
+
+cdef numpy_dt r_subtract(numpy_dt a, numpy_dt b):
+    return a - b
+
+cdef numpy_dt r_multiply(numpy_dt a, numpy_dt b):
+    return a * b
+
+ at cython.cdivision(True)
+cdef numpy_dt r_divide(numpy_dt a, numpy_dt b):
+    return a / b
+
+def index_unop(np.ndarray[numpy_dt, ndim=1] values,
+              np.ndarray[np.int64_t, ndim=1] indices,
+              np.ndarray[np.int64_t, ndim=1] sizes,
+              operation):
+    cdef numpy_dt mi, ma
+    if numpy_dt == np.float32_t:
+        dt = "float32"
+        mi = np.finfo(dt).min
+        ma = np.finfo(dt).max
+    elif numpy_dt == np.float64_t:
+        dt = "float64"
+        mi = np.finfo(dt).min
+        ma = np.finfo(dt).max
+    elif numpy_dt == np.int32_t:
+        dt = "int32"
+        mi = np.iinfo(dt).min
+        ma = np.iinfo(dt).max
+    elif numpy_dt == np.int64_t:
+        dt = "int64"
+        mi = np.iinfo(dt).min
+        ma = np.iinfo(dt).max
+    cdef np.ndarray[numpy_dt] out_values = np.zeros(sizes.size, dtype=dt)
+    cdef numpy_dt (*func)(numpy_dt a, numpy_dt b)
+    # Now we figure out our function.  At present, we only allow addition and
+    # multiplication, because they are commutative and easy to bootstrap.
+    cdef numpy_dt ival, val
+    if operation == "sum":
+        ival = 0
+        func = r_add
+    elif operation == "prod":
+        ival = 1
+        func = r_multiply
+    elif operation == "max":
+        ival = mi
+        func = r_max
+    elif operation == "min":
+        ival = ma
+        func = r_min
+    else:
+        raise NotImplementedError
+    cdef np.int64_t i, j, ind_ind, ind_arr
+    ind_ind = 0
+    for i in range(sizes.size):
+        # Each entry in sizes is the size of the array
+        val = ival
+        for j in range(sizes[i]):
+            ind_arr = indices[ind_ind]
+            val = func(val, values[ind_arr])
+            ind_ind += 1
+        out_values[i] = val
+    return out_values

diff -r 4d1488b8fa476b1e6f5cb90c16312fce75abe34e -r 036f745796c12e66b4636835800f2b029bbe7b4b yt/utilities/lib/setup.py
--- a/yt/utilities/lib/setup.py
+++ b/yt/utilities/lib/setup.py
@@ -139,6 +139,8 @@
           )
     config.add_extension("write_array",
                          ["yt/utilities/lib/write_array.pyx"])
+    config.add_extension("ragged_arrays",
+                         ["yt/utilities/lib/ragged_arrays.pyx"])
     config.add_extension("GridTree", 
     ["yt/utilities/lib/GridTree.pyx"],
         libraries=["m"], depends=["yt/utilities/lib/fp_utils.pxd"])

diff -r 4d1488b8fa476b1e6f5cb90c16312fce75abe34e -r 036f745796c12e66b4636835800f2b029bbe7b4b yt/utilities/lib/tests/test_ragged_arrays.py
--- /dev/null
+++ b/yt/utilities/lib/tests/test_ragged_arrays.py
@@ -0,0 +1,36 @@
+from yt.testing import *
+import numpy as np
+from yt.utilities.lib.ragged_arrays import index_unop
+
+operations = ((np.sum, "sum"),
+              (np.prod, "prod"),
+              (np.max, "max"),
+              (np.min, "min"))
+dtypes = ((-1e8, 1e8, "float32"),
+          (-1e8, 1e8, "float64"),
+          (-10000, 10000, "int32"),
+          (-100000000, 100000000, "int64"))
+
+def test_index_unop():
+    np.random.seed(0x4d3d3d3)
+    indices = np.arange(1000)
+    np.random.shuffle(indices)
+    sizes = np.array([
+        200, 50, 50, 100, 32, 32, 32, 32, 32, 64, 376], dtype="int64")
+    for mi, ma, dtype in dtypes:
+        for op, operation in operations:
+            # Create a random set of values
+            values = np.random.random(1000)
+            if operation != "prod":
+                values = values * ma + (ma - mi)
+            if operation == "prod" and dtype.startswith("int"):
+                values = values.astype(dtype)
+                values[values != 0] = 1
+                values[values == 0] = -1
+            values = values.astype(dtype)
+            out_values = index_unop(values, indices, sizes, operation)
+            i = 0
+            for j, v in enumerate(sizes):
+                arr = values[indices[i:i+v]]
+                yield assert_equal, op(arr), out_values[j]
+                i += v

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