def do_concat (shape1, shape2, iaxis, count=[0]): from pygeode.axis import XAxis, YAxis, ZAxis, TAxis from pygeode.var import Var from pygeode.concat import concat from var_test import varTest import numpy as np # Increment test counter (and generate a unique test name) count[0] += 1 testname = "concattest%05d"%(count[0]) # Create some test data np.random.seed(count[0]) array1 = np.random.randn(*shape1) array2 = np.random.randn(*shape2) # Get the # of dimensions, and assign a unique axis class for each dim assert array1.ndim == array2.ndim ndim = array1.ndim axis_classes = (XAxis, YAxis, ZAxis, TAxis)[:ndim] # Construct the first var axes = [cls(n) for cls,n in zip(axis_classes,array1.shape)] var1 = Var(axes = axes, values = array1, name = "myvar", atts={'a':1, 'b':2, 'c':3}) # The second var should have the same axes, except for the concatenation one n1 = array1.shape[iaxis] n2 = array2.shape[iaxis] axes[iaxis] = axis_classes[iaxis](np.arange(n1, n1+n2)) var2 = Var(axes = axes, values = array2, name = "myvar", atts={'a':1, 'b':3, 'd':4}) # Try concatenating var = concat(var1,var2) # The expected result expected = np.concatenate ( (array1, array2), iaxis) # Test this test = varTest(testname=testname, var=var, values=expected) # Store this test globals()[testname] = test
sl = tuple([i] if isinstance(i,int) else i for i in sl) # Apply the slices one dimension at a time. # Avoids a 'feature' in numpy slicing when there are advanced integer # indices. If there are 2 such things, numpy goes into a special # mode where it zips the arrays to get a list of specific coordinates, # then picks out those individual elements. # E.g., you would think that x[ [0,1], [0,1] ] would give you a 2x2 # square array of the first 2 rows and first 2 columns of x, but noooo... # what it does is give you a 1D array, consisting of elements (0,0) and # (1,1)!!! expected = values for dim in range(naxes): current_sl = [slice(None)]*dim + [sl[dim]] + [slice(None)]*(naxes-1-dim) expected = expected[current_sl] # Things are just about to start getting crazy-go-nuts. # Pass the var and expected values to 'varTest', which in turn # dynamically defines a test class (subclass of unittest.TestCase) # to check the var for consistency. # We then have to take this test class, and assign it to a variable # (this has to be done dynamically, since we're looping over many tests). # 'nosetests' will then find this file, import it, and look for any # global variables that represent a subclass of unittest.TestCase(?), # then invoke the corresponding tests. testname = 'slicetest%05d'%count globals()[testname] = varTest(testname=testname, var=slicedvar, values=expected) count += 1