def testMinMax(self): # Scalar xouter = MV2.outerproduct(MV2.arange(10), MV2.arange(10)) maxval = MV2.maximum(xouter) minval = MV2.minimum(xouter) self.assertEqual(maxval, 81) self.assertEqual(minval, 0) # Do an elementwise maximum xmax = MV2.maximum(self.u_file, self.v_file) xmin = MV2.minimum(self.u_file, self.v_file) self.assertTrue(((xmax - self.u_file) >= 0).all()) self.assertTrue(((xmax - self.v_file) >= 0).all()) self.assertTrue(((xmin - self.u_file) <= 0).all()) self.assertTrue(((xmin - self.v_file) <= 0).all()) # Reduce along axes slicer = [Ellipsis for i in range(len(self.u_file.shape))] for axis_index, axis_length in enumerate(self.u_file.shape): amax = MV2.maximum.reduce(self.u_file, axis=axis_index) amin = MV2.minimum.reduce(self.u_file, axis=axis_index) s = list(slicer) for i in range(axis_length): s[axis_index] = i ind_slice = self.u_file.subSlice(*s, squeeze=True) self.assertTrue(((ind_slice - amax) <= 0).all()) self.assertTrue(((ind_slice - amin) >= 0).all()) t1 = MV2.TransientVariable([1., 2.]) t2 = MV2.TransientVariable([1., 10.]) t3 = MV2.minimum.outer(t1, t2) self.assertTrue(MV2.allequal(t3, [[1, 1], [1, 2]])) t3 = MV2.maximum.outer(t1, t2) self.assertTrue(MV2.allequal(t3, [[1, 10], [2, 10]]))
def testChoose(self): ct1 = MV2.TransientVariable([1, 1, 2, 0, 1]) ctr = MV2.choose(ct1, [numpy.ma.masked, 10, 20, 30, 40]) self.assertTrue(MV2.allclose(ctr, [10, 10, 20, 100, 10])) ctx = MV2.TransientVariable([1, 2, 3, 150, 4]) cty = -MV2.TransientVariable([1, 2, 3, 150, 4]) ctr = MV2.choose(MV2.greater(ctx, 100), (ctx, 100)) self.assertTrue(MV2.allclose(ctr, [1, 2, 3, 100, 4])) ctr = MV2.choose(MV2.greater(ctx, 100), (ctx, cty)) self.assertTrue(MV2.allclose(ctr, [1, 2, 3, -150, 4]))
def testTranspose(self): ## transpose(a, axes=None) # transpose(a, axes=None) reorder dimensions per tuple axes xtr = MV2.transpose(self.u_file) xtr = numpy.arange(24) xtr.shape = (4, 6) xtr1 = MV2.transpose(xtr) xtr2 = MV2.transpose(MV2.TransientVariable(xtr)) self.assertTrue(xtr2.shape == (6, 4)) xtr3 = numpy.transpose(xtr) self.assertTrue(MV2.allclose(xtr1, xtr3)) self.assertTrue(MV2.allclose(xtr2, xtr3))
## If weights are given, result is sum(a*weights)/sum(weights), with ## all elements masked in a or in weights ignored. ## weights if given must have a's shape. ## Denominator is multiplied by 1.0 to prevent truncation for integers. ## returned governs return of second quantity, the weights. xav = MV2.average(xones, axis=1) xav2 = MV2.average(ud) xav3 = MV2.average(udat) xav4, wav4 = MV2.average(udat, weights=MV2.ones(udat.shape, numpy.float), returned=1) ## choose(indices, t) ## Shaped like indices, values t[i] where at indices[i] ## If t[j] is masked, special treatment to preserve type. ct1 = MV2.TransientVariable([1, 1, 2, 0, 1]) ctr = MV2.choose(ct1, [numpy.ma.masked, 10, 20, 30, 40]) if not MV2.allclose(ctr, [10, 10, 20, 100, 10]): markError('choose error 1') ctx = MV2.TransientVariable([1, 2, 3, 150, 4]) cty = -MV2.TransientVariable([1, 2, 3, 150, 4]) ctr = MV2.choose(MV2.greater(ctx, 100), (ctx, 100)) if not MV2.allclose(ctr, [1, 2, 3, 100, 4]): markError('choose error 2') ctr = MV2.choose(MV2.greater(ctx, 100), (ctx, cty)) if not MV2.allclose(ctr, [1, 2, 3, -150, 4]): markError('choose error 3') ## concatenate(arrays, axis=0, axisid=None, axisattributes=None) ## Concatenate the arrays along the given axis. Give the extended axis the id and ## attributes provided - by default, those of the first array. try: xcon = MV2.concatenate((ud, vd))
def testDiagnoal(self): xdiag = MV2.TransientVariable([[1, 2, 3], [4, 5, 6]]) self.assertTrue(MV2.allclose(MV2.diagonal(xdiag, 1), [2, 6]))