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_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_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_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_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_concat(self): x = np.arange(5) y = np.arange(5, 10) v = self.cls(["a"], x) w = self.cls(["a"], y) self.assertVariableIdentical(Variable(["b", "a"], np.array([x, y])), Variable.concat([v, w], "b")) self.assertVariableIdentical(Variable(["b", "a"], np.array([x, y])), Variable.concat((v, w), "b")) self.assertVariableIdentical(Variable(["b", "a"], np.array([x, y])), Variable.concat((v, w), "b")) with self.assertRaisesRegexp(ValueError, "inconsistent dimensions"): Variable.concat([v, Variable(["c"], y)], "b") # test indexers actual = Variable.concat([v, w], positions=[range(0, 10, 2), range(1, 10, 2)], dim="a") expected = Variable("a", np.array([x, y]).ravel(order="F")) self.assertVariableIdentical(expected, actual) # test concatenating along a dimension v = Variable(["time", "x"], np.random.random((10, 8))) self.assertVariableIdentical(v, Variable.concat([v[:5], v[5:]], "time")) self.assertVariableIdentical(v, Variable.concat([v[:5], v[5:6], v[6:]], "time")) self.assertVariableIdentical(v, Variable.concat([v[:1], v[1:]], "time")) # test dimension order self.assertVariableIdentical(v, Variable.concat([v[:, :5], v[:, 5:]], "x")) with self.assertRaisesRegexp(ValueError, "all input arrays must have"): Variable.concat([v[:, 0], v[:, 1:]], "x")
def test_concat(self): x = np.arange(5) y = np.arange(5, 10) v = self.cls(['a'], x) w = self.cls(['a'], y) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat([v, w], 'b')) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b')) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b')) with self.assertRaisesRegexp(ValueError, 'inconsistent dimensions'): Variable.concat([v, Variable(['c'], y)], 'b') # test indexers actual = Variable.concat([v, w], indexers=[range(0, 10, 2), range(1, 10, 2)], dim='a') expected = Variable('a', np.array([x, y]).ravel(order='F')) self.assertVariableIdentical(expected, actual) # test concatenating along a dimension v = Variable(['time', 'x'], np.random.random((10, 8))) self.assertVariableIdentical(v, Variable.concat([v[:5], v[5:]], 'time')) self.assertVariableIdentical(v, Variable.concat([v[:5], v[5:6], v[6:]], 'time')) self.assertVariableIdentical(v, Variable.concat([v[:1], v[1:]], 'time')) # test dimension order self.assertVariableIdentical(v, Variable.concat([v[:, :5], v[:, 5:]], 'x')) with self.assertRaisesRegexp(ValueError, 'same number of dimensions'): Variable.concat([v[:, 0], v[:, 1:]], 'x')
def test_concat(self): x = np.arange(5) y = np.ones(5) v = self.cls(['a'], x) w = self.cls(['a'], y) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat([v, w], 'b')) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b')) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b', length=2)) with self.assertRaisesRegexp(ValueError, 'actual length'): Variable.concat([v, w], 'b', length=1) with self.assertRaisesRegexp(ValueError, 'actual length'): Variable.concat([v, w, w], 'b', length=4) with self.assertRaisesRegexp(ValueError, 'inconsistent dimensions'): Variable.concat([v, Variable(['c'], y)], 'b') # test concatenating along a dimension v = Variable(['time', 'x'], np.random.random((10, 8))) self.assertVariableIdentical(v, Variable.concat([v[:5], v[5:]], 'time')) self.assertVariableIdentical(v, Variable.concat([v[:5], v[5], v[6:]], 'time')) self.assertVariableIdentical(v, Variable.concat([v[0], v[1:]], 'time')) # test dimension order self.assertVariableIdentical(v, Variable.concat([v[:, :5], v[:, 5:]], 'x')) self.assertVariableIdentical(v.transpose(), Variable.concat([v[:, 0], v[:, 1:]], 'x'))
def test_concat(self): x = np.arange(5) y = np.arange(5, 10) v = self.cls(['a'], x) w = self.cls(['a'], y) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat([v, w], 'b')) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b')) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b')) with self.assertRaisesRegexp(ValueError, 'inconsistent dimensions'): Variable.concat([v, Variable(['c'], y)], 'b') # test indexers actual = Variable.concat([v, w], positions=[range(0, 10, 2), range(1, 10, 2)], dim='a') expected = Variable('a', np.array([x, y]).ravel(order='F')) self.assertVariableIdentical(expected, actual) # test concatenating along a dimension v = Variable(['time', 'x'], np.random.random((10, 8))) self.assertVariableIdentical(v, Variable.concat([v[:5], v[5:]], 'time')) self.assertVariableIdentical(v, Variable.concat([v[:5], v[5:6], v[6:]], 'time')) self.assertVariableIdentical(v, Variable.concat([v[:1], v[1:]], 'time')) # test dimension order self.assertVariableIdentical(v, Variable.concat([v[:, :5], v[:, 5:]], 'x')) with self.assertRaisesRegexp(ValueError, 'same number of dimensions'): Variable.concat([v[:, 0], v[:, 1:]], 'x')
def test_concat(self): x = np.arange(5) y = np.ones(5) v = self.cls(['a'], x) w = self.cls(['a'], y) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat([v, w], 'b')) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b')) self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b', length=2)) with self.assertRaisesRegexp(ValueError, 'actual length'): Variable.concat([v, w], 'b', length=1) with self.assertRaisesRegexp(ValueError, 'actual length'): Variable.concat([v, w, w], 'b', length=4) with self.assertRaisesRegexp(ValueError, 'inconsistent dimensions'): Variable.concat([v, Variable(['c'], y)], 'b') # test concatenating along a dimension v = Variable(['time', 'x'], np.random.random((10, 8))) self.assertVariableIdentical(v, Variable.concat([v[:5], v[5:]], 'time')) self.assertVariableIdentical( v, Variable.concat([v[:5], v[5], v[6:]], 'time')) self.assertVariableIdentical(v, Variable.concat([v[0], v[1:]], 'time')) # test dimension order self.assertVariableIdentical( v, Variable.concat([v[:, :5], v[:, 5:]], 'x')) self.assertVariableIdentical(v.transpose(), Variable.concat([v[:, 0], v[:, 1:]], 'x'))