def test_calculate(self): ma = MovingWindow() np.random.seed(1) values = np.ma.array(np.random.rand(10), dtype=float).reshape(1, -1, 1, 1, 1) k = 5 ret = ma.calculate(values, k=k, mode='same', operation='mean') self.assertEqual(ret.shape, values.shape) actual = pickle.loads( 'cnumpy.ma.core\n_mareconstruct\np0\n(cnumpy.ma.core\nMaskedArray\np1\ncnumpy\nndarray\np2\n(I0\ntp3\nS\'b\'\np4\ntp5\nRp6\n(I1\n(I1\nI10\nI1\nI1\nI1\ntp7\ncnumpy\ndtype\np8\n(S\'f8\'\np9\nI0\nI1\ntp10\nRp11\n(I3\nS\'<\'\np12\nNNNI-1\nI-1\nI0\ntp13\nbI00\nS\'\\xec\\x80\\\'\\x90\\rD\\xd8?\\xee"\\x1d\\xdad\\t\\xd7?\\x126\\xab\\x0b\\xceN\\xd4?\\xc4\\xcdN\\xdc\\xe1&\\xd0?\\x0e\\xa2\\x12\\x8a\\xb8\\xa1\\xc2?#!\\x9bX\\xa3y\\xcb?\\x83\\xb5\\x00\\xd2\\x86\\xe4\\xcd?*M\\xfa\\xe1\\xf7\\xf6\\xd3?\\xd4\\xd3\\xad\\xd1}z\\xd7? J4q\\xc2T\\xdb?\'\np14\nS\'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\'\np15\ncnumpy.core.multiarray\n_reconstruct\np16\n(g2\n(I0\ntp17\ng4\ntp18\nRp19\n(I1\n(tg11\nI00\nS\'@\\x8c\\xb5x\\x1d\\xaf\\x15D\'\np20\ntp21\nbtp22\nb.') self.assertNumpyAllClose(ret, actual) ret = ret.squeeze() values = values.squeeze() self.assertEqual(ret[4], np.mean(values[2:7]))
def test_execute(self): field = self.get_field(month_count=1, with_value=True) field = field[:, 0:4, :, :, :] field.variables['tmax'].value[:] = 1 field.variables['tmax'].value.mask[:, :, :, 1, 1] = True for mode in ['same', 'valid']: for operation in ('mean', 'min', 'max', 'median', 'var', 'std'): parms = {'k': 3, 'mode': mode, 'operation': operation} ma = MovingWindow(field=field, parms=parms) vc = ma.execute() if mode == 'same': self.assertEqual(vc['moving_window'].value.shape, field.shape) else: self.assertEqual(vc['moving_window'].value.shape, (2, 2, 2, 3, 4)) self.assertEqual(ma.field.shape, (2, 2, 2, 3, 4))
def test_iter_kernel_values_asserts(self): """Test assert statements.""" k = [1, 2, 3, 4] values = [ np.array([[2, 3], [4, 5]]), np.arange(0, 13).reshape(-1, 1, 1) ] mode = ['same', 'valid', 'foo'] for kwds in itr_products_keywords( { 'k': k, 'values': values, 'mode': mode }, as_namedtuple=True): try: list(MovingWindow._iter_kernel_values_(kwds.values, kwds.k)) except AssertionError: if kwds.k == 3: if kwds.values.shape == (2, 2): continue else: raise else: continue except NotImplementedError: if kwds.mode == 'foo': continue else: raise
def test_calculate(self): ma = MovingWindow() np.random.seed(1) values = np.ma.array(np.random.rand(10), dtype=float).reshape(1, -1, 1, 1, 1) k = 5 ret = ma.calculate(values, k=k, mode='same', operation='mean') self.assertEqual(ret.shape, values.shape) desired = [ [[[[0.37915362432069233]]], [[[0.3599483613984792]]], [[[0.317309867282206]]], [[[0.2523731852954507]]], [[[0.14556032888255327]]], [[[0.21464959932769387]]], [[[0.23353657964745986]]], [[[0.31194874828470864]]], [[[0.3668512866636864]]], [[[0.4270483117590249]]]]] desired = np.array(desired) self.assertNumpyAllClose(ret, desired) ret = ret.squeeze() values = values.squeeze() self.assertEqual(ret[4], np.mean(values[2:7]))
def test_execute(self): field = self.get_field(month_count=1, with_value=True) field = field.get_field_slice({'time': slice(0, 4)}) field['tmax'].get_value()[:] = 1 mask = field['tmax'].get_mask(create=True) mask[:, :, :, 1, 1] = True field['tmax'].set_mask(mask) for mode in ['same', 'valid']: for operation in ('mean', 'min', 'max', 'median', 'var', 'std'): parms = {'k': 3, 'mode': mode, 'operation': operation} ma = MovingWindow(field=field, parms=parms) vc = ma.execute() if mode == 'same': self.assertEqual(vc['moving_window'].get_value().shape, field['tmax'].shape) else: actual_mask = vc['moving_window'].get_mask() self.assertTrue(np.all(actual_mask[:, 0, :, :, :])) self.assertTrue(np.all(actual_mask[:, -1, :, :, :])) self.assertEqual(vc['moving_window'].get_value().shape, (2, 4, 2, 3, 4))
def test_iter_kernel_values_valid(self): """Test returning kernel values with the 'valid' mode.""" values = np.arange(2, 11).reshape(-1, 1, 1) k = 5 mode = 'valid' itr = MovingWindow._iter_kernel_values_(values, k, mode=mode) to_test = list(itr) actual = pickle.loads( "(lp0\n(I2\ncnumpy.core.multiarray\n_reconstruct\np1\n(cnumpy\nndarray\np2\n(I0\ntp3\nS'b'\np4\ntp5\nRp6\n(I1\n(I5\nI1\nI1\ntp7\ncnumpy\ndtype\np8\n(S'i8'\np9\nI0\nI1\ntp10\nRp11\n(I3\nS'<'\np12\nNNNI-1\nI-1\nI0\ntp13\nbI00\nS'\\x02\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x06\\x00\\x00\\x00\\x00\\x00\\x00\\x00'\np14\ntp15\nbtp16\na(I3\ng1\n(g2\n(I0\ntp17\ng4\ntp18\nRp19\n(I1\n(I5\nI1\nI1\ntp20\ng11\nI00\nS'\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x06\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\x00\\x00\\x00\\x00'\np21\ntp22\nbtp23\na(I4\ng1\n(g2\n(I0\ntp24\ng4\ntp25\nRp26\n(I1\n(I5\nI1\nI1\ntp27\ng11\nI00\nS'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x06\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x08\\x00\\x00\\x00\\x00\\x00\\x00\\x00'\np28\ntp29\nbtp30\na(I5\ng1\n(g2\n(I0\ntp31\ng4\ntp32\nRp33\n(I1\n(I5\nI1\nI1\ntp34\ng11\nI00\nS'\\x05\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x06\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x08\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\t\\x00\\x00\\x00\\x00\\x00\\x00\\x00'\np35\ntp36\nbtp37\na(I6\ng1\n(g2\n(I0\ntp38\ng4\ntp39\nRp40\n(I1\n(I5\nI1\nI1\ntp41\ng11\nI00\nS'\\x06\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x08\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\t\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\n\\x00\\x00\\x00\\x00\\x00\\x00\\x00'\np42\ntp43\nbtp44\na.") for idx in range(len(to_test)): self.assertEqual(to_test[idx][1].ndim, 3) self.assertEqual(to_test[idx][0], actual[idx][0]) self.assertNumpyAll(to_test[idx][1], actual[idx][1])
def test_iter_kernel_values_valid(self): """Test returning kernel values with the 'valid' mode.""" values = np.arange(2, 11).reshape(-1, 1, 1) k = 5 mode = 'valid' itr = MovingWindow._iter_kernel_values_(values, k, mode=mode) to_test = list(itr) desired = [[2, [[[2]], [[3]], [[4]], [[5]], [[6]]]], [3, [[[3]], [[4]], [[5]], [[6]], [[7]]]], [4, [[[4]], [[5]], [[6]], [[7]], [[8]]]], [5, [[[5]], [[6]], [[7]], [[8]], [[9]]]], [6, [[[6]], [[7]], [[8]], [[9]], [[10]]]]] for idx in range(len(to_test)): self.assertEqual(to_test[idx][1].ndim, 3) self.assertEqual(to_test[idx][0], desired[idx][0]) self.assertEqual(to_test[idx][1].tolist(), desired[idx][1])
def test_iter_kernel_values_asserts(self): """Test assert statements.""" k = [1, 2, 3, 4] values = [np.array([[2, 3], [4, 5]]), np.arange(0, 13).reshape(-1, 1, 1)] mode = ['same', 'valid', 'foo'] for kwds in itr_products_keywords({'k': k, 'values': values, 'mode': mode}, as_namedtuple=True): try: list(MovingWindow._iter_kernel_values_(kwds.values, kwds.k)) except AssertionError: if kwds.k == 3: if kwds.values.shape == (2, 2): continue else: raise else: continue except NotImplementedError: if kwds.mode == 'foo': continue else: raise