def setUp(self): self.attrs = {'attr1': 'value1', 'attr2': 2929} self.x = np.random.random((10, 20)) self.v = Variable(['x', 'y'], self.x) self.va = Variable(['x', 'y'], self.x, self.attrs) self.ds = Dataset({'foo': self.v}) self.dv = self.ds['foo']
def test_items(self): data = np.random.random((10, 11)) v = Variable(['x', 'y'], data) # test slicing self.assertVariableIdentical(v, v[:]) self.assertVariableIdentical(v, v[...]) self.assertVariableIdentical(Variable(['y'], data[0]), v[0]) self.assertVariableIdentical(Variable(['x'], data[:, 0]), v[:, 0]) self.assertVariableIdentical(Variable(['x', 'y'], data[:3, :2]), v[:3, :2]) # test array indexing x = Variable(['x'], np.arange(10)) y = Variable(['y'], np.arange(11)) self.assertVariableIdentical(v, v[x.values]) self.assertVariableIdentical(v, v[x]) self.assertVariableIdentical(v[:3], v[x < 3]) self.assertVariableIdentical(v[:, 3:], v[:, y >= 3]) self.assertVariableIdentical(v[:3, 3:], v[x < 3, y >= 3]) self.assertVariableIdentical(v[:3, :2], v[x[:3], y[:2]]) self.assertVariableIdentical(v[:3, :2], v[range(3), range(2)]) # test iteration for n, item in enumerate(v): self.assertVariableIdentical(Variable(['y'], data[n]), item) with self.assertRaisesRegexp(TypeError, 'iteration over a 0-d'): iter(Variable([], 0)) # test setting v.values[:] = 0 self.assertTrue(np.all(v.values == 0)) # test orthogonal setting v[range(10), range(11)] = 1 self.assertArrayEqual(v.values, np.ones((10, 11)))
def test_as_variable(self): data = np.arange(10) expected = Variable('x', data) self.assertVariableIdentical(expected, as_variable(expected)) ds = Dataset({'x': expected}) self.assertVariableIdentical(expected, as_variable(ds['x'])) self.assertNotIsInstance(ds['x'], Variable) self.assertIsInstance(as_variable(ds['x']), Variable) self.assertIsInstance(as_variable(ds['x'], strict=False), DataArray) FakeVariable = namedtuple('FakeVariable', 'values dims') fake_xarray = FakeVariable(expected.values, expected.dims) self.assertVariableIdentical(expected, as_variable(fake_xarray)) xarray_tuple = (expected.dims, expected.values) self.assertVariableIdentical(expected, as_variable(xarray_tuple)) with self.assertRaisesRegexp(TypeError, 'cannot convert arg'): as_variable(tuple(data)) with self.assertRaisesRegexp(TypeError, 'cannot infer .+ dimensions'): as_variable(data) actual = as_variable(data, key='x') self.assertVariableIdentical(expected, actual) actual = as_variable(0) expected = Variable([], 0) self.assertVariableIdentical(expected, actual)
def test_groupby_sum(self): array = self.make_groupby_example_array() grouped = array.groupby('abc') expected_sum_all = Dataset( {'foo': Variable(['abc'], np.array([self.x[:, :9].sum(), self.x[:, 10:].sum(), self.x[:, 9:10].sum()]).T), 'abc': Variable(['abc'], np.array(['a', 'b', 'c']))})['foo'] self.assertDataArrayAllClose(expected_sum_all, grouped.reduce(np.sum)) self.assertDataArrayAllClose(expected_sum_all, grouped.sum()) expected = DataArray([array['y'].values[idx].sum() for idx in [slice(9), slice(10, None), slice(9, 10)]], [['a', 'b', 'c']], ['abc']) actual = array['y'].groupby('abc').apply(np.sum) self.assertDataArrayAllClose(expected, actual) actual = array['y'].groupby('abc').sum() self.assertDataArrayAllClose(expected, actual) expected_sum_axis1 = Dataset( {'foo': (['x', 'abc'], np.array([self.x[:, :9].sum(1), self.x[:, 10:].sum(1), self.x[:, 9:10].sum(1)]).T), 'x': self.ds['x'], 'abc': Variable(['abc'], np.array(['a', 'b', 'c']))})['foo'] self.assertDataArrayAllClose(expected_sum_axis1, grouped.reduce(np.sum, 'y')) self.assertDataArrayAllClose(expected_sum_axis1, grouped.sum('y'))
def test_numpy_same_methods(self): v = Variable([], np.float32(0.0)) self.assertEqual(v.item(), 0) self.assertIs(type(v.item()), float) v = Coordinate('x', np.arange(5)) self.assertEqual(2, v.searchsorted(2))
def test_concat(self): u = self.eager_var v = self.lazy_var self.assertLazyAndIdentical(u, Variable.concat([v[:2], v[2:]], 'x')) self.assertLazyAndIdentical(u[:2], Variable.concat([v[0], v[1]], 'x')) self.assertLazyAndIdentical( u[:3], Variable.concat([v[[0, 2]], v[[1]]], 'x', positions=[[0, 2], [1]]))
def test_index_0d_numpy_string(self): # regression test to verify our work around for indexing 0d strings v = Variable([], np.string_('asdf')) self.assertVariableIdentical(v[()], v) v = Variable([], np.unicode_(u'asdf')) self.assertVariableIdentical(v[()], v)
def test_roll_consistency(self): v = Variable(("x", "y"), np.random.randn(5, 6)) for axis, dim in [(0, "x"), (1, "y")]: for shift in [-3, 0, 1, 7, 11]: expected = np.roll(v.values, shift, axis=axis) actual = v.roll(**{dim: shift}).values self.assertArrayEqual(expected, actual)
def test_roll_consistency(self): v = Variable(('x', 'y'), np.random.randn(5, 6)) for axis, dim in [(0, 'x'), (1, 'y')]: for shift in [-3, 0, 1, 7, 11]: expected = np.roll(v.values, shift, axis=axis) actual = v.roll(**{dim: shift}).values self.assertArrayEqual(expected, actual)
def test_0d_str(self): v = Variable([], u'foo') self.assertEqual(v.dtype, np.dtype('U3')) self.assertEqual(v.values, 'foo') v = Variable([], np.string_('foo')) self.assertEqual(v.dtype, np.dtype('S3')) self.assertEqual(v.values, bytes('foo', 'ascii') if PY3 else 'foo')
def test_decode_cf_with_multiple_missing_values(self): original = Variable(['t'], [0, 1, 2], {'missing_value': np.array([0, 1])}) expected = Variable(['t'], [np.nan, np.nan, 2], {}) with warnings.catch_warnings(record=True) as w: actual = conventions.decode_cf_variable(original) self.assertDatasetIdentical(expected, actual) self.assertIn('variable has multiple fill', str(w[0].message))
def test_broadcasting_failures(self): a = Variable(['x'], np.arange(10)) b = Variable(['x'], np.arange(5)) c = Variable(['x', 'x'], np.arange(100).reshape(10, 10)) with self.assertRaisesRegexp(ValueError, 'mismatched lengths'): a + b with self.assertRaisesRegexp(ValueError, 'duplicate dimensions'): a + c
def test_concat_number_strings(self): # regression test for #305 a = self.cls('x', ['0', '1', '2']) b = self.cls('x', ['3', '4']) actual = Variable.concat([a, b], dim='x') expected = Variable('x', np.arange(5).astype(str).astype(object)) self.assertVariableIdentical(expected, actual) self.assertEqual(expected.dtype, object) self.assertEqual(type(expected.values[0]), str)
def test_concat_fixed_len_str(self): # regression test for #217 for kind in ['S', 'U']: x = self.cls('animal', np.array(['horse'], dtype=kind)) y = self.cls('animal', np.array(['aardvark'], dtype=kind)) actual = Variable.concat([x, y], 'animal') expected = Variable('animal', np.array(['horse', 'aardvark'], dtype=kind)) self.assertVariableEqual(expected, actual)
def test_item_math(self): self.ds['x'] = ('x', np.array(list('abcdefghij'))) self.assertVariableEqual(self.dv + self.dv[0, 0], self.dv + self.dv[0, 0].values) new_data = self.x[0][None, :] + self.x[:, 0][:, None] self.assertVariableEqual(self.dv[:, 0] + self.dv[0], Variable(['x', 'y'], new_data)) self.assertVariableEqual(self.dv[0] + self.dv[:, 0], Variable(['y', 'x'], new_data.T))
def test_data(self): v = Variable(['time', 'x'], self.d) self.assertArrayEqual(v.values, self.d) self.assertIs(source_ndarray(v.values), self.d) with self.assertRaises(ValueError): # wrong size v.values = np.random.random(5) d2 = np.random.random((10, 3)) v.values = d2 self.assertIs(source_ndarray(v.values), d2)
def test_incompatible_attributes(self): invalid_vars = [ Variable(['t'], pd.date_range('2000-01-01', periods=3), {'units': 'foobar'}), Variable(['t'], [0, 1, 2], {'add_offset': 0}, {'add_offset': 2}), Variable(['t'], [0, 1, 2], {'_FillValue': 0}, {'_FillValue': 2}), ] for var in invalid_vars: with self.assertRaises(ValueError): conventions.encode_cf_variable(var)
def test_broadcasting_math(self): x = np.random.randn(2, 3) v = Variable(['a', 'b'], x) # 1d to 2d broadcasting self.assertVariableIdentical( v * v, Variable(['a', 'b'], np.einsum('ab,ab->ab', x, x))) self.assertVariableIdentical( v * v[0], Variable(['a', 'b'], np.einsum('ab,b->ab', x, x[0]))) self.assertVariableIdentical( v[0] * v, Variable(['b', 'a'], np.einsum('b,ab->ba', x[0], x))) self.assertVariableIdentical( v[0] * v[:, 0], Variable(['b', 'a'], np.einsum('b,a->ba', x[0], x[:, 0]))) # higher dim broadcasting y = np.random.randn(3, 4, 5) w = Variable(['b', 'c', 'd'], y) self.assertVariableIdentical( v * w, Variable(['a', 'b', 'c', 'd'], np.einsum('ab,bcd->abcd', x, y))) self.assertVariableIdentical( w * v, Variable(['b', 'c', 'd', 'a'], np.einsum('bcd,ab->bcda', y, x))) self.assertVariableIdentical( v * w[0], Variable(['a', 'b', 'c', 'd'], np.einsum('ab,cd->abcd', x, y[0])))
def test_concat_attrs(self): # different or conflicting attributes should be removed v = self.cls("a", np.arange(5), {"foo": "bar"}) w = self.cls("a", np.ones(5)) expected = self.cls("a", np.concatenate([np.arange(5), np.ones(5)])) self.assertVariableIdentical(expected, Variable.concat([v, w], "a")) w.attrs["foo"] = 2 self.assertVariableIdentical(expected, Variable.concat([v, w], "a")) w.attrs["foo"] = "bar" expected.attrs["foo"] = "bar" self.assertVariableIdentical(expected, Variable.concat([v, w], "a"))
def test_concat_attrs(self): # different or conflicting attributes should be removed v = self.cls('a', np.arange(5), {'foo': 'bar'}) w = self.cls('a', np.ones(5)) expected = self.cls('a', np.concatenate([np.arange(5), np.ones(5)])) self.assertVariableIdentical(expected, Variable.concat([v, w], 'a')) w.attrs['foo'] = 2 self.assertVariableIdentical(expected, Variable.concat([v, w], 'a')) w.attrs['foo'] = 'bar' expected.attrs['foo'] = 'bar' self.assertVariableIdentical(expected, Variable.concat([v, w], 'a'))
def test_concat(self): self.ds['bar'] = Variable(['x', 'y'], np.random.randn(10, 20)) foo = self.ds['foo'].select() bar = self.ds['bar'].rename('foo').select() # from dataset array: self.assertVariableEqual( Variable(['w', 'x', 'y'], np.array([foo.values, bar.values])), DataArray.concat([foo, bar], 'w')) # from iteration: grouped = [g for _, g in foo.groupby('x')] stacked = DataArray.concat(grouped, self.ds['x']) self.assertDataArrayIdentical(foo.select(), stacked)
def test_inplace_math(self): x = np.arange(5) v = Variable(['x'], x) v2 = v v2 += 1 self.assertIs(v, v2) # since we provided an ndarray for data, it is also modified in-place self.assertIs(source_ndarray(v.values), x) self.assertArrayEqual(v.values, np.arange(5) + 1) with self.assertRaisesRegexp(ValueError, 'dimensions cannot change'): v += Variable('y', np.arange(5))
def test_init(self): var1 = Variable('x', 2 * np.arange(100)) var2 = Variable('x', np.arange(1000)) var3 = Variable(['x', 'y'], np.arange(1000).reshape(100, 10)) with self.assertRaisesRegexp(ValueError, 'but already exists'): Dataset({'a': var1, 'b': var2}) with self.assertRaisesRegexp(ValueError, 'must be defined with 1-d'): Dataset({'a': var1, 'x': var3}) # verify handling of DataArrays expected = Dataset({'x': var1, 'z': var3}) actual = Dataset({'z': expected['z']}) self.assertDatasetIdentical(expected, actual)
def test_missing_values(self): values = np.array([0, 1, np.nan, 3]) data = da.from_array(values, chunks=(2,)) eager_var = Variable('x', values) lazy_var = Variable('x', data) self.assertLazyAndIdentical(eager_var, lazy_var.fillna(lazy_var)) self.assertLazyAndIdentical(Variable('x', range(4)), lazy_var.fillna(2)) self.assertLazyAndIdentical(eager_var.count(), lazy_var.count())
def test_transpose(self): v = Variable(['time', 'x'], self.d) v2 = Variable(['x', 'time'], self.d.T) self.assertVariableIdentical(v, v2.transpose()) self.assertVariableIdentical(v.transpose(), v.T) x = np.random.randn(2, 3, 4, 5) w = Variable(['a', 'b', 'c', 'd'], x) w2 = Variable(['d', 'b', 'c', 'a'], np.einsum('abcd->dbca', x)) self.assertEqual(w2.shape, (5, 3, 4, 2)) self.assertVariableIdentical(w2, w.transpose('d', 'b', 'c', 'a')) self.assertVariableIdentical(w, w2.transpose('a', 'b', 'c', 'd')) w3 = Variable(['b', 'c', 'd', 'a'], np.einsum('abcd->bcda', x)) self.assertVariableIdentical(w, w3.transpose('a', 'b', 'c', 'd'))
def test_transpose(self): v = Variable(["time", "x"], self.d) v2 = Variable(["x", "time"], self.d.T) self.assertVariableIdentical(v, v2.transpose()) self.assertVariableIdentical(v.transpose(), v.T) x = np.random.randn(2, 3, 4, 5) w = Variable(["a", "b", "c", "d"], x) w2 = Variable(["d", "b", "c", "a"], np.einsum("abcd->dbca", x)) self.assertEqual(w2.shape, (5, 3, 4, 2)) self.assertVariableIdentical(w2, w.transpose("d", "b", "c", "a")) self.assertVariableIdentical(w, w2.transpose("a", "b", "c", "d")) w3 = Variable(["b", "c", "d", "a"], np.einsum("abcd->bcda", x)) self.assertVariableIdentical(w, w3.transpose("a", "b", "c", "d"))
def test_reduce_keep_attrs(self): _attrs = {'units': 'test', 'long_name': 'testing'} v = Variable(['x', 'y'], self.d, _attrs) # Test dropped attrs vm = v.mean() self.assertEqual(len(vm.attrs), 0) self.assertEqual(vm.attrs, OrderedDict()) # Test kept attrs vm = v.mean(keep_attrs=True) self.assertEqual(len(vm.attrs), len(_attrs)) self.assertEqual(vm.attrs, _attrs)
def test_reduce_keep_attrs(self): _attrs = {"units": "test", "long_name": "testing"} v = Variable(["x", "y"], self.d, _attrs) # Test dropped attrs vm = v.mean() self.assertEqual(len(vm.attrs), 0) self.assertEqual(vm.attrs, OrderedDict()) # Test kept attrs vm = v.mean(keep_attrs=True) self.assertEqual(len(vm.attrs), len(_attrs)) self.assertEqual(vm.attrs, _attrs)
def test_data_and_values(self): v = Variable(["time", "x"], self.d) self.assertArrayEqual(v.data, self.d) self.assertArrayEqual(v.values, self.d) self.assertIs(source_ndarray(v.values), self.d) with self.assertRaises(ValueError): # wrong size v.values = np.random.random(5) d2 = np.random.random((10, 3)) v.values = d2 self.assertIs(source_ndarray(v.values), d2) d3 = np.random.random((10, 3)) v.data = d3 self.assertIs(source_ndarray(v.data), d3)
def test_isel(self): v = Variable(['time', 'x'], self.d) self.assertVariableIdentical(v.isel(time=slice(None)), v) self.assertVariableIdentical(v.isel(time=0), v[0]) self.assertVariableIdentical(v.isel(time=slice(0, 3)), v[:3]) self.assertVariableIdentical(v.isel(x=0), v[:, 0]) with self.assertRaisesRegexp(ValueError, 'do not exist'): v.isel(not_a_dim=0)
def test_get_axis_num(self): v = Variable(['x', 'y', 'z'], np.random.randn(2, 3, 4)) self.assertEqual(v.get_axis_num('x'), 0) self.assertEqual(v.get_axis_num(['x']), (0, )) self.assertEqual(v.get_axis_num(['x', 'y']), (0, 1)) self.assertEqual(v.get_axis_num(['z', 'y', 'x']), (2, 1, 0)) with self.assertRaisesRegexp(ValueError, 'not found in array dim'): v.get_axis_num('foobar')
def test_rename(self): data = create_test_data() newnames = {'var1': 'renamed_var1', 'dim2': 'renamed_dim2'} renamed = data.rename(newnames) variables = OrderedDict(data.variables) for k, v in iteritems(newnames): variables[v] = variables.pop(k) for k, v in iteritems(variables): dims = list(v.dimensions) for name, newname in iteritems(newnames): if name in dims: dims[dims.index(name)] = newname self.assertVariableEqual(Variable(dims, v.values, v.attrs), renamed.variables[k]) self.assertEqual(v.encoding, renamed.variables[k].encoding) self.assertEqual(type(v), type(renamed.variables[k])) self.assertTrue('var1' not in renamed.variables) self.assertTrue('dim2' not in renamed.variables) with self.assertRaisesRegexp(ValueError, "cannot rename 'not_a_var'"): data.rename({'not_a_var': 'nada'}) # verify that we can rename a variable without accessing the data var1 = data['var1'] data['var1'] = (var1.dimensions, InaccessibleArray(var1.values)) renamed = data.rename(newnames) with self.assertRaises(UnexpectedDataAccess): renamed['renamed_var1'].values
def test_broadcast_equals(self): v1 = Variable((), np.nan) v2 = Variable(('x'), [np.nan, np.nan]) self.assertTrue(v1.broadcast_equals(v2)) self.assertFalse(v1.equals(v2)) self.assertFalse(v1.identical(v2)) v3 = Variable(('x'), [np.nan]) self.assertTrue(v1.broadcast_equals(v3)) self.assertFalse(v1.equals(v3)) self.assertFalse(v1.identical(v3)) self.assertFalse(v1.broadcast_equals(None)) v4 = Variable(('x'), [np.nan] * 3) self.assertFalse(v2.broadcast_equals(v4))
def test_1d_reduce(self): x = np.arange(5) v = self.cls(['x'], x) actual = v.sum() expected = Variable((), 10) self.assertVariableIdentical(expected, actual) self.assertIs(type(actual), Variable)
def test_remap_label_indexers(self): # TODO: fill in more tests! data = Dataset({'x': ('x', [1, 2, 3])}) test_indexer = lambda x: indexing.remap_label_indexers(data, {'x': x}) self.assertEqual({'x': 0}, test_indexer(1)) self.assertEqual({'x': 0}, test_indexer(np.int32(1))) self.assertEqual({'x': 0}, test_indexer(Variable([], 1)))
def test_count(self): expected = Variable([], 3) actual = Variable(["x"], [1, 2, 3, np.nan]).count() self.assertVariableIdentical(expected, actual) v = Variable(["x"], np.array(["1", "2", "3", np.nan], dtype=object)) actual = v.count() self.assertVariableIdentical(expected, actual) actual = Variable(["x"], [True, False, True]).count() self.assertVariableIdentical(expected, actual) self.assertEqual(actual.dtype, int) expected = Variable(["x"], [2, 3]) actual = Variable(["x", "y"], [[1, 0, np.nan], [1, 1, 1]]).count("y") self.assertVariableIdentical(expected, actual)
def test_concat(self): self.ds['bar'] = Variable(['x', 'y'], np.random.randn(10, 20)) foo = self.ds['foo'] bar = self.ds['bar'] # from dataset array: expected = DataArray(np.array([foo.values, bar.values]), dims=['w', 'x', 'y']) actual = concat([foo, bar], 'w') self.assertDataArrayEqual(expected, actual) # from iteration: grouped = [g for _, g in foo.groupby('x')] stacked = concat(grouped, self.ds['x']) self.assertDataArrayIdentical(foo, stacked) # with an index as the 'dim' argument stacked = concat(grouped, self.ds.indexes['x']) self.assertDataArrayIdentical(foo, stacked) actual = concat([foo[0], foo[1]], pd.Index([0, 1])).reset_coords(drop=True) expected = foo[:2].rename({'x': 'concat_dim'}) self.assertDataArrayIdentical(expected, actual) actual = concat([foo[0], foo[1]], [0, 1]).reset_coords(drop=True) expected = foo[:2].rename({'x': 'concat_dim'}) self.assertDataArrayIdentical(expected, actual) with self.assertRaisesRegexp(ValueError, 'not identical'): concat([foo, bar], compat='identical')
def test_count(self): expected = Variable([], 3) actual = Variable(['x'], [1, 2, 3, np.nan]).count() self.assertVariableIdentical(expected, actual) v = Variable(['x'], np.array(['1', '2', '3', np.nan], dtype=object)) actual = v.count() self.assertVariableIdentical(expected, actual) actual = Variable(['x'], [True, False, True]).count() self.assertVariableIdentical(expected, actual) self.assertEqual(actual.dtype, int) expected = Variable(['x'], [2, 3]) actual = Variable(['x', 'y'], [[1, 0, np.nan], [1, 1, 1]]).count('y') self.assertVariableIdentical(expected, actual)
def test_roll(self): v = Variable("x", [1, 2, 3, 4, 5]) self.assertVariableIdentical(v, v.roll(x=0)) self.assertIsNot(v, v.roll(x=0)) expected = Variable("x", [5, 1, 2, 3, 4]) self.assertVariableIdentical(expected, v.roll(x=1)) self.assertVariableIdentical(expected, v.roll(x=-4)) self.assertVariableIdentical(expected, v.roll(x=6)) expected = Variable("x", [4, 5, 1, 2, 3]) self.assertVariableIdentical(expected, v.roll(x=2)) self.assertVariableIdentical(expected, v.roll(x=-3)) with self.assertRaisesRegexp(ValueError, "dimension"): v.roll(z=0)
def null_wrap(ds): """ Given a data store this wraps each variable in a NullWrapper so that it appears to be out of memory. """ variables = dict((k, Variable(v.dims, NullWrapper(v.values), v.attrs)) for k, v in iteritems(ds)) return InMemoryDataStore(variables=variables, attributes=ds.attrs)
def test_decode_cf_with_conflicting_fill_missing_value(self): var = Variable(['t'], np.arange(10), { 'units': 'foobar', 'missing_value': 0, '_FillValue': 1 }) self.assertRaisesRegexp(ValueError, "_FillValue and missing_value", lambda: conventions.decode_cf_variable(var))
def test_concat_fixed_len_str(self): # regression test for #217 for kind in ["S", "U"]: x = self.cls("animal", np.array(["horse"], dtype=kind)) y = self.cls("animal", np.array(["aardvark"], dtype=kind)) actual = Variable.concat([x, y], "animal") expected = Variable("animal", np.array(["horse", "aardvark"], dtype=kind)) self.assertVariableEqual(expected, actual)
def test_concat_number_strings(self): # regression test for #305 a = self.cls("x", ["0", "1", "2"]) b = self.cls("x", ["3", "4"]) actual = Variable.concat([a, b], dim="x") expected = Variable("x", np.arange(5).astype(str).astype(object)) self.assertVariableIdentical(expected, actual) self.assertEqual(expected.dtype, object) self.assertEqual(type(expected.values[0]), str)
def test_concat_fixed_len_str(self): # regression test for #217 for kind in ['S', 'U']: x = self.cls('animal', np.array(['horse'], dtype=kind)) y = self.cls('animal', np.array(['aardvark'], dtype=kind)) actual = Variable.concat([x, y], 'animal') expected = Variable( 'animal', np.array(['horse', 'aardvark'], dtype=kind)) self.assertVariableEqual(expected, actual)
def test_get_axis_num(self): v = Variable(["x", "y", "z"], np.random.randn(2, 3, 4)) self.assertEqual(v.get_axis_num("x"), 0) self.assertEqual(v.get_axis_num(["x"]), (0,)) self.assertEqual(v.get_axis_num(["x", "y"]), (0, 1)) self.assertEqual(v.get_axis_num(["z", "y", "x"]), (2, 1, 0)) with self.assertRaisesRegexp(ValueError, "not found in array dim"): v.get_axis_num("foobar")
def test_get_axis_num(self): v = Variable(['x', 'y', 'z'], np.random.randn(2, 3, 4)) self.assertEqual(v.get_axis_num('x'), 0) self.assertEqual(v.get_axis_num(['x']), (0,)) self.assertEqual(v.get_axis_num(['x', 'y']), (0, 1)) self.assertEqual(v.get_axis_num(['z', 'y', 'x']), (2, 1, 0)) with self.assertRaisesRegexp(ValueError, 'not found in array dim'): v.get_axis_num('foobar')
def test_index_and_concat_datetime(self): # regression test for #125 date_range = pd.date_range('2011-09-01', periods=10) for dates in [date_range, date_range.values, date_range.to_pydatetime()]: expected = self.cls('t', dates) for times in [[expected[i] for i in range(10)], [expected[i:(i + 1)] for i in range(10)], [expected[[i]] for i in range(10)]]: actual = Variable.concat(times, 't') self.assertEqual(expected.dtype, actual.dtype) self.assertArrayEqual(expected, actual)
def test_missing_values(self): values = np.array([0, 1, np.nan, 3]) data = da.from_array(values, chunks=(2,)) eager_var = Variable("x", values) lazy_var = Variable("x", data) self.assertLazyAndIdentical(eager_var, lazy_var.fillna(lazy_var)) self.assertLazyAndIdentical(Variable("x", range(4)), lazy_var.fillna(2)) self.assertLazyAndIdentical(eager_var.count(), lazy_var.count())
def test_expand_dims(self): v = Variable(["x"], [0, 1]) actual = v.expand_dims(["x", "y"]) expected = Variable(["x", "y"], [[0], [1]]) self.assertVariableIdentical(actual, expected) actual = v.expand_dims(["y", "x"]) self.assertVariableIdentical(actual, expected.T) actual = v.expand_dims(OrderedDict([("x", 2), ("y", 2)])) expected = Variable(["x", "y"], [[0, 0], [1, 1]]) self.assertVariableIdentical(actual, expected) v = Variable(["foo"], [0, 1]) actual = v.expand_dims("foo") expected = v self.assertVariableIdentical(actual, expected) with self.assertRaisesRegexp(ValueError, "must be a superset"): v.expand_dims(["z"])
def test_expand_dims(self): v = Variable(['x'], [0, 1]) actual = v.expand_dims(['x', 'y']) expected = Variable(['x', 'y'], [[0], [1]]) self.assertVariableIdentical(actual, expected) actual = v.expand_dims(['y', 'x']) self.assertVariableIdentical(actual, expected.T) actual = v.expand_dims(OrderedDict([('x', 2), ('y', 2)])) expected = Variable(['x', 'y'], [[0, 0], [1, 1]]) self.assertVariableIdentical(actual, expected) v = Variable(['foo'], [0, 1]) actual = v.expand_dims('foo') expected = v self.assertVariableIdentical(actual, expected) with self.assertRaisesRegexp(ValueError, 'must be a superset'): v.expand_dims(['z'])
def test_missing_fillvalue(self): v = Variable(['x'], np.array([np.nan, 1, 2, 3])) v.encoding = {'dtype': 'int16'} with self.assertWarns('floating point data as an integer'): conventions.encode_cf_variable(v)
def test_reduce_funcs(self): v = Variable('x', np.array([1, np.nan, 2, 3])) self.assertVariableIdentical(v.mean(), Variable([], 2)) self.assertVariableIdentical(v.mean(skipna=True), Variable([], 2)) self.assertVariableIdentical(v.mean(skipna=False), Variable([], np.nan)) self.assertVariableIdentical(np.mean(v), Variable([], 2)) self.assertVariableIdentical(v.prod(), Variable([], 6)) self.assertVariableIdentical(v.var(), Variable([], 2.0 / 3)) if LooseVersion(np.__version__) < '1.9': with self.assertRaises(NotImplementedError): v.median() else: self.assertVariableIdentical(v.median(), Variable([], 2)) v = Variable('x', [True, False, False]) self.assertVariableIdentical(v.any(), Variable([], True)) self.assertVariableIdentical(v.all(dim='x'), Variable([], False)) v = Variable('t', pd.date_range('2000-01-01', periods=3)) with self.assertRaises(NotImplementedError): v.max(skipna=True) self.assertVariableIdentical( v.max(), Variable([], pd.Timestamp('2000-01-03')))