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

Bitbucket commits-noreply at bitbucket.org
Wed Dec 26 11:43:18 PST 2012


5 new commits in yt:


https://bitbucket.org/yt_analysis/yt/changeset/2893f026fcfd/
changeset:   2893f026fcfd
branch:      yt
user:        xarthisius
date:        2012-12-26 17:43:20
summary:     Add method for converting scalar/list to numpy array
affected #:  1 file

diff -r c176809c9e569747594281ea8b40348b4410e584 -r 2893f026fcfda7e888b867c0aa6bfea285462f80 yt/funcs.py
--- a/yt/funcs.py
+++ b/yt/funcs.py
@@ -27,6 +27,7 @@
 import time, types, signal, inspect, traceback, sys, pdb, os
 import contextlib
 import warnings, struct, subprocess
+import numpy as np
 from distutils import version
 from math import floor, ceil
 
@@ -61,6 +62,18 @@
         return [obj]
     return obj
 
+def ensure_numpy_array(obj):
+    """
+    This function ensures that *obj* is a numpy array. Typically used to
+    convert scalar or list argument passed to functions using Cython.
+    """
+    if isinstance(obj, np.ndarray):
+        return obj
+    elif isinstance(obj, types.ListType):
+        return np.asarray(obj)
+    else:
+        return np.asarray([obj])
+
 def read_struct(f, fmt):
     """
     This reads a struct, and only that struct, from an open file.
@@ -168,7 +181,7 @@
     @wraps(func)
     def check_parallel_rank(*args, **kwargs):
         if ytcfg.getint("yt","__topcomm_parallel_rank") > 0:
-            return 
+            return
         return func(*args, **kwargs)
     return check_parallel_rank
 
@@ -569,7 +582,7 @@
     if nt < 0:
         return os.environ.get("OMP_NUM_THREADS", 0)
     return nt
-        
+
 def fix_axis(axis):
     return inv_axis_names.get(axis, axis)
 



https://bitbucket.org/yt_analysis/yt/changeset/e7a395a4efb9/
changeset:   e7a395a4efb9
branch:      yt
user:        xarthisius
date:        2012-12-26 17:43:46
summary:     Import assert_raises from numpy.testing
affected #:  1 file

diff -r 2893f026fcfda7e888b867c0aa6bfea285462f80 -r e7a395a4efb952cbfad491a606dfc77f1cd3a15a yt/testing.py
--- a/yt/testing.py
+++ b/yt/testing.py
@@ -27,7 +27,7 @@
 from numpy.testing import assert_array_equal, assert_almost_equal, \
     assert_approx_equal, assert_array_almost_equal, assert_equal, \
     assert_array_less, assert_string_equal, assert_array_almost_equal_nulp,\
-    assert_allclose
+    assert_allclose, assert_raises
 
 def assert_rel_equal(a1, a2, decimals, err_msg='', verbose=True):
     # We have nan checks in here because occasionally we have fields that get
@@ -43,7 +43,7 @@
                                verbose=verbose)
 
 def amrspace(extent, levels=7, cells=8):
-    """Creates two numpy arrays representing the left and right bounds of 
+    """Creates two numpy arrays representing the left and right bounds of
     an AMR grid as well as an array for the AMR level of each cell.
 
     Parameters
@@ -57,7 +57,7 @@
         length ndims), then each dimension will be refined down to this level.
         All values in this array must be the same or zero.  A zero valued dimension
         indicates that this dim should not be refined.  Taking the 3D cylindrical
-        example above if we don't want refine theta but want r and z at 5 we would 
+        example above if we don't want refine theta but want r and z at 5 we would
         set levels=(5, 5, 0).
     cells : int, optional
         This is the number of cells per refinement level.



https://bitbucket.org/yt_analysis/yt/changeset/b047fb91d53a/
changeset:   b047fb91d53a
branch:      yt
user:        xarthisius
date:        2012-12-26 17:47:21
summary:     Find_points now accepts scalar and lists as the arguments, fixes #473
affected #:  1 file

diff -r e7a395a4efb952cbfad491a606dfc77f1cd3a15a -r b047fb91d53af9a28c917202e42d5b5557300cfc yt/data_objects/object_finding_mixin.py
--- a/yt/data_objects/object_finding_mixin.py
+++ b/yt/data_objects/object_finding_mixin.py
@@ -117,27 +117,32 @@
         """
         Returns the (objects, indices) of leaf grids containing a number of (x,y,z) points
         """
-        num_points = len(x)
+        x = ensure_numpy_array(x)
+        y = ensure_numpy_array(y)
+        z = ensure_numpy_array(z)
+        if not len(x) == len(y) == len(z):
+            raise AssertionError("Arrays of indices must be of the same size")
+
         grid_tree = self.get_grid_tree()
-        pts = MatchPointsToGrids(grid_tree,num_points,x,y,z)
-        ind = pts.find_points_in_tree() 
+        pts = MatchPointsToGrids(grid_tree, len(x), x, y, z)
+        ind = pts.find_points_in_tree()
         return self.grids[ind], ind
-    
+
     def find_field_value_at_point(self, fields, coord):
         r"""Find the value of fields at a point.
-        
+
         Returns the values [field1, field2,...] of the fields at the given
         (x,y,z) point. Returns a list of field values in the same order
         as the input *fields*.
-        
+
         Parameters
         ----------
         fields : string or list of strings
             The field(s) that will be returned.
-        
+
         coord : list or array of floats
             The location for which field values will be returned.
-        
+
         Examples
         --------
         >>> pf.h.find_field_value_at_point(['Density', 'Temperature'],



https://bitbucket.org/yt_analysis/yt/changeset/ae6310f3d702/
changeset:   ae6310f3d702
branch:      yt
user:        xarthisius
date:        2012-12-26 17:57:42
summary:     Update tests for GridTree, add test for new functionality introduced in previous commit
affected #:  1 file

diff -r b047fb91d53af9a28c917202e42d5b5557300cfc -r ae6310f3d702df116bc71b22ce19c714df8accf1 yt/utilities/lib/tests/test_grid_tree.py
--- a/yt/utilities/lib/tests/test_grid_tree.py
+++ b/yt/utilities/lib/tests/test_grid_tree.py
@@ -1,75 +1,128 @@
+"""
+Tests for GridTree
+
+Author: John ZuHone <jzuhone at gmail.com>
+Affiliation: NASA/Goddard Space Flight Center
+Homepage: http://yt-project.org/
+License:
+Copyright (C) 2012 John ZuHone.  All Rights Reserved.
+
+This file is part of yt.
+
+yt is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
 import numpy as np
+import random
 
-from yt.testing import *
-from yt.frontends.stream.api import load_amr_grids
+from yt.testing import \
+    assert_equal, assert_raises
+from yt.frontends.stream.api import \
+    load_amr_grids
+
 
 def setup():
+    """Prepare setup specific environment"""
+    global test_pf
 
-    global pf
-    
     grid_data = [
-        dict(left_edge = [0.0, 0.0, 0.0], right_edge = [1.0, 1.0, 1.],
-             level = 0, dimensions = [16, 16, 16]),
-        dict(left_edge = [0.25, 0.25, 0.25], right_edge = [0.75, 0.75, 0.75],
-             level = 1, dimensions = [16, 16, 16]),
-        dict(left_edge = [0.25, 0.25, 0.375], right_edge = [0.5, 0.5, 0.625],
-             level = 2, dimensions = [16, 16, 16]),
-        dict(left_edge = [0.5, 0.5, 0.375], right_edge = [0.75, 0.75, 0.625],
-             level = 2, dimensions = [16, 16, 16]),
-        dict(left_edge = [0.3125, 0.3125, 0.4375], right_edge = [0.4375, 0.4375, 0.5625],
-             level = 3, dimensions = [16, 16, 16]),
-        dict(left_edge = [0.5625, 0.5625, 0.4375], right_edge = [0.6875, 0.6875, 0.5625],
-             level = 3, dimensions = [16, 16, 16])
-        ]
+        dict(left_edge=[0.0, 0.0, 0.0], right_edge=[1.0, 1.0, 1.],
+             level=0, dimensions=[16, 16, 16]),
+        dict(left_edge=[0.25, 0.25, 0.25], right_edge=[0.75, 0.75, 0.75],
+             level=1, dimensions=[16, 16, 16]),
+        dict(left_edge=[0.25, 0.25, 0.375], right_edge=[0.5, 0.5, 0.625],
+             level=2, dimensions=[16, 16, 16]),
+        dict(left_edge=[0.5, 0.5, 0.375], right_edge=[0.75, 0.75, 0.625],
+             level=2, dimensions=[16, 16, 16]),
+        dict(left_edge=[0.3125, 0.3125, 0.4375],
+             right_edge=[0.4375, 0.4375, 0.5625],
+             level=3, dimensions=[16, 16, 16]),
+        dict(left_edge=[0.5625, 0.5625, 0.4375],
+             right_edge=[0.6875, 0.6875, 0.5625],
+             level=3, dimensions=[16, 16, 16])
+    ]
 
-    for g in grid_data: g["Density"] = np.random.random(g["dimensions"]) * 2**g["level"]
-    pf = load_amr_grids(grid_data, [16, 16, 16], 1.0)
+    for grid in grid_data:
+        grid["Density"] = \
+            np.random.random(grid["dimensions"]) * 2 ** grid["level"]
+    test_pf = load_amr_grids(grid_data, [16, 16, 16], 1.0)
 
-def test_grid_tree() :
 
-    grid_tree = pf.h.get_grid_tree()
+def test_grid_tree():
+    """Main test suite for GridTree"""
+    grid_tree = test_pf.h.get_grid_tree()
     indices, levels, nchild, children = grid_tree.return_tree_info()
 
-    grid_levels = [grid.Level for grid in pf.h.grids]
-    grid_indices = [grid.id-grid._id_offset for grid in pf.h.grids]
-    grid_nchild = [len(grid.Children) for grid in pf.h.grids]
+    grid_levels = [grid.Level for grid in test_pf.h.grids]
 
-    print levels, grid_levels
-    assert_equal(levels, grid_levels)
-    assert_equal(indices, grid_indices)
-    assert_equal(nchild, grid_nchild)
+    grid_indices = [grid.id - grid._id_offset for grid in test_pf.h.grids]
+    grid_nchild = [len(grid.Children) for grid in test_pf.h.grids]
 
-    for i, grid in enumerate(pf.h.grids) :
+    yield assert_equal, levels, grid_levels
+    yield assert_equal, indices, grid_indices
+    yield assert_equal, nchild, grid_nchild
+
+    for i, grid in enumerate(test_pf.h.grids):
         if grid_nchild[i] > 0:
-            grid_children = np.array([child.id-child._id_offset
+            grid_children = np.array([child.id - child._id_offset
                                       for child in grid.Children])
-            assert_equal(grid_children, children[i])
+            yield assert_equal, grid_children, children[i]
 
-def test_find_points() :
-    
+
+def test_find_points():
+    """Main test suite for MatchPoints"""
     num_points = 100
+    randx = np.random.uniform(low=test_pf.domain_left_edge[0],
+                              high=test_pf.domain_right_edge[0],
+                              size=num_points)
+    randy = np.random.uniform(low=test_pf.domain_left_edge[1],
+                              high=test_pf.domain_right_edge[1],
+                              size=num_points)
+    randz = np.random.uniform(low=test_pf.domain_left_edge[2],
+                              high=test_pf.domain_right_edge[2],
+                              size=num_points)
 
-    x = np.random.uniform(low=pf.domain_left_edge[0],
-                          high=pf.domain_right_edge[0], size=num_points)
-    y = np.random.uniform(low=pf.domain_left_edge[1],
-                          high=pf.domain_right_edge[1], size=num_points)
-    z = np.random.uniform(low=pf.domain_left_edge[2],
-                          high=pf.domain_right_edge[2], size=num_points)
-
-    point_grids, point_grid_inds = pf.h.find_points(x,y,z)
+    point_grids, point_grid_inds = test_pf.h.find_points(randx, randy, randz)
 
     grid_inds = np.zeros((num_points), dtype='int64')
 
-    for i, xx, yy, zz in zip(range(num_points), x, y, z) :
+    for ind, ixx, iyy, izz in zip(range(num_points), randx, randy, randz):
 
         pt_level = -1
-        
-        for grid in pf.h.grids:
 
-            if grid.is_in_grid(xx, yy, zz) :
-            
-                if grid.Level > pt_level :
+        for grid in test_pf.h.grids:
+
+            if grid.is_in_grid(ixx, iyy, izz):
+
+                if grid.Level > pt_level:
                     pt_level = grid.Level
-                    grid_inds[i] = grid.id-grid._id_offset
-                    
-    assert_equal(point_grid_inds, grid_inds)
+                    grid_inds[ind] = grid.id - grid._id_offset
+
+    yield assert_equal, point_grid_inds, grid_inds
+
+    # Test wheter find_points works for lists
+    point_grids, point_grid_inds = test_pf.h.find_points(randx.tolist(),
+                                                         randy.tolist(),
+                                                         randz.tolist())
+    yield assert_equal, point_grid_inds, grid_inds
+
+    # Test if find_points works for scalar
+    ind = random.randint(0, num_points - 1)
+    point_grids, point_grid_inds = test_pf.h.find_points(randx[ind],
+                                                         randy[ind],
+                                                         randz[ind])
+    yield assert_equal, point_grid_inds, grid_inds[ind]
+
+    # Test if find_points fails properly for non equal indices' array sizes
+    yield assert_raises, AssertionError, test_pf.h.find_points, \
+        [0], 1.0, [2, 3]



https://bitbucket.org/yt_analysis/yt/changeset/ad241a5772fc/
changeset:   ad241a5772fc
branch:      yt
user:        xarthisius
date:        2012-12-26 18:51:56
summary:     ensure_numpy_array works for now for tuples too
affected #:  1 file

diff -r ae6310f3d702df116bc71b22ce19c714df8accf1 -r ad241a5772fc214f75370fae5ba3da7f457e13c0 yt/funcs.py
--- a/yt/funcs.py
+++ b/yt/funcs.py
@@ -65,11 +65,11 @@
 def ensure_numpy_array(obj):
     """
     This function ensures that *obj* is a numpy array. Typically used to
-    convert scalar or list argument passed to functions using Cython.
+    convert scalar, list or tuple argument passed to functions using Cython.
     """
     if isinstance(obj, np.ndarray):
         return obj
-    elif isinstance(obj, types.ListType):
+    elif isinstance(obj, (types.ListType, types.TupleType)):
         return np.asarray(obj)
     else:
         return np.asarray([obj])

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