def test_non_unique_names(self): with pytest.raises(ValueError): _ = Lineage( np.random.random((10, 3, 1)), names=["foo", "bar", "baz"], colors=[(0, 0, 0), "#ffffff", "#ff00FF"], )
def test_correct_names_to_ixs(self): x = np.random.random((10, 3)) l = Lineage(x, names=["foo", "bar", "baz"]) y = l[["baz", "bar"]] assert y._names_to_ixs == {"baz": 0, "bar": 1}
def test_automatic_color_assignment(self): x = np.random.random((10, 3)) l = Lineage(x, names=["foo", "bar", "baz"]) gt_colors = [colors.to_hex(c) for c in _create_categorical_colors(3)] np.testing.assert_array_equal(l.colors, gt_colors)
def test_wrong_colors(self): with pytest.raises(ValueError): _ = Lineage( np.random.random((10, 3)), names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), "foobar"], )
def test_rest_all(self): x = Lineage(np.random.random((10, 4)), names=["foo", "bar", "baz", "quux"]) y = x[[Lin.REST]] assert y.shape == (10, 1) np.testing.assert_array_equal(y.X[:, 0], np.sum(x.X, axis=1)) np.testing.assert_array_equal(y.names, ["rest"])
def test_wrong_number_of_dimensions(self): with pytest.raises(ValueError): _ = Lineage( np.random.random((10, 3, 1)), names=["foo", "bar", "baz"], colors=[(0, 0, 0), "#ffffff", "#ff00FF"], )
def test_colors_length_mismatch(self): with pytest.raises(ValueError): _ = Lineage( np.random.random((10, 3)), names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5)], )
def test_creation(self): x = np.random.random((10, 3)) names = ["foo", "bar", "baz"] colors = ["#000000", "#ababab", "#ffffff"] l = Lineage(x, names=names, colors=colors) np.testing.assert_array_equal(l, x) np.testing.assert_array_equal(l.names, np.array(names)) np.testing.assert_array_equal(l.colors, np.array(colors))
def test_names_setter_wrong_type(self): l = Lineage( np.random.random((10, 3)), names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) with pytest.raises(TypeError): l.names = ["foo1", "bar1", 3]
def test_color_setter_wrong_colors(self): l = Lineage( np.random.random((10, 3)), names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) with pytest.raises(ValueError): l.colors = ["#ffffff", "#ffffff", "foo"]
def test_others_no_effect(self): names = ["foo", "bar", "baz", "quux"] x = Lineage(np.random.random((10, 4)), names=names) y = x[names + [Lin.OTHERS]] np.testing.assert_array_equal(x.X, y.X) np.testing.assert_array_equal(x.names, y.names) np.testing.assert_array_equal(x.colors, y.colors)
def test_names_setter_non_unique(self): l = Lineage( np.random.random((10, 3)), names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) with pytest.raises(ValueError): l.names = ["foo1", "bar1", "bar1"]
def test_others(self): names = ["foo", "bar", "baz", "quux"] x = Lineage(np.random.random((10, 4)), names=names) y = x[["foo, baz"] + [Lin.OTHERS]] expected = x[["foo, baz", "bar", "quux"]] np.testing.assert_array_equal(y.X, expected.X) np.testing.assert_array_equal(y.names, expected.names) np.testing.assert_array_equal(y.colors, expected.colors)
def test_row_subset(self): x = Lineage(np.random.random((10, 4)), names=["foo", "bar", "baz", "quux"]) y = x[:5, ["foo, bar"]] expected = np.sum(x.X[:5, [0, 1]], axis=1)[..., np.newaxis] assert y.shape == (5, 1) np.testing.assert_array_equal(y.X, expected) np.testing.assert_array_equal(y.names, ["bar or foo"]) np.testing.assert_array_equal(y.colors, [_compute_mean_color(x.colors[:2])])
def test_column_subset_boolean_invalid_dim(self): x = np.random.random((10, 3)) l = Lineage( x, names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) with pytest.raises(IndexError): y = l[:, [True]]
def test_same_names(self): x = Lineage(np.random.random((10, 4)), names=["foo", "bar", "baz", "quux"]) y = x[np.arange(len(x)), ["foo"] * len(x)] expected = x["foo"] assert y.shape == (10, 1) np.testing.assert_array_equal(y.X, expected.X) np.testing.assert_array_equal(y.names, ["mixture"]) np.testing.assert_array_equal(y.colors, ["#000000"])
def test_column_invalid_name(self): x = np.random.random((10, 3)) l = Lineage( x, names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) with pytest.raises(KeyError): y = l["quux"]
def test_reordering(self): x = np.random.random((10, 3)) l = Lineage( x, names=["foo", "bar", "baz"], colors=["#ff0000", "#00ff00", "#0000ff"] ) y = l[["baz", "bar", "foo"]] np.testing.assert_array_equal(y.names, ["baz", "bar", "foo"]) np.testing.assert_array_equal(y.colors, ["#0000ff", "#00ff00", "#ff0000"])
def test_colors_setter(self): l = Lineage( np.random.random((10, 3)), names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) colors = ["#ffffff", "#ffffff", "#ffffff"] l.colors = colors np.testing.assert_array_equal(l.colors, np.array(colors))
def test_subset_same_instance(self): x = np.random.random((10, 3)) l = Lineage( x, names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) y = l[0, 0] assert isinstance(y, Lineage)
def test_singleton_column_name(self): x = np.random.random((10, 3)) l = Lineage( x, names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) y = l[:, "foo"] np.testing.assert_array_equal(x[:, 0], np.array(y)[:, 0])
def test_remove_duplicates(self): x = np.random.random((10, 3)) l = Lineage( x, names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) y = l[0, ["foo", 2, "bar", 0, 0, "foo"]] np.testing.assert_array_equal(x[[[0]], [0, 2, 1]], np.array(y))
def test_comb_row_int_col_names(self): x = np.random.random((10, 3)) l = Lineage( x, names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) y = l[[0, 1], "baz"] np.testing.assert_array_equal(x[[0, 1], :][:, [2]], np.array(y))
def test_column_subset_with_names(self): x = np.random.random((10, 3)) l = Lineage( x, names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) y = l[:, ["foo", "bar"]] np.testing.assert_array_equal(x[:, [0, 1]], np.array(y))
def test_row_subset_with_ints(self): x = np.random.random((10, 3)) l = Lineage( x, names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) y = l[[1, 2, 3], :] np.testing.assert_array_equal(x[[1, 2, 3], :], np.array(y))
def test_column_subset_boolean(self): x = np.random.random((10, 3)) l = Lineage( x, names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) y = l[:, [False, False, True]] np.testing.assert_array_equal(x[:, -1], y.X.squeeze())
def test_same_indices(self): x = Lineage(np.random.random((10, 4)), names=["foo", "bar", "baz", "quux"]) half = len(x) // 2 y = x[[0] * len(x), ["foo"] * half + ["bar"] * half] expected = np.array([x[0, "foo"].X[0, 0]] * half + [x[0, "bar"].X[0, 0]] * half) assert y.shape == (10, 1) np.testing.assert_array_equal(y.X.squeeze(), expected) np.testing.assert_array_equal(y.names, ["mixture"]) np.testing.assert_array_equal(y.colors, ["#000000"])
def test_names_setter(self): l = Lineage( np.random.random((10, 3)), names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) names = ["foo1", "bar1", "baz1"] l.names = names np.testing.assert_array_equal(l.names, np.array(names))
def test_no_mixing(self): x = Lineage(np.random.random((10, 4)), names=["foo", "bar", "baz", "quux"]) y = x[["foo", Lin.REST]] expected = np.c_[np.sum(x.X[:, [0]], axis=1), np.sum(x.X[:, [1, 2, 3]], axis=1)] assert y.shape == (10, 2) np.testing.assert_array_equal(y.X, expected) np.testing.assert_array_equal(y.names, ["foo", "rest"]) np.testing.assert_array_equal( y.colors, [x.colors[0], _compute_mean_color(x.colors[1:])] )
def test_singleton_column_first_index_assignment(self): x = np.random.random((10, 3)) l = Lineage( x, names=["foo", "bar", "baz"], colors=[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)], ) y = l["baz"] np.testing.assert_array_equal(x[:, 2], np.array(y)[:, 0]) np.testing.assert_array_equal(y.names, ["baz"])