예제 #1
0
    def test_mapping_colors_invalid_colors(self):
        query = pd.Series(["foo", "bar", "baz"], dtype="category")
        reference = pd.Series(["foo", "bar", "baz"], dtype="category")

        with pytest.raises(ValueError):
            _map_names_and_colors(
                reference, query, colors_reference=["red", "green", "foo"]
            )
예제 #2
0
    def test_mapping_colors_different_index(self):
        query = pd.Series(["foo", "bar", "baz"],
                          dtype="category",
                          index=[2, 3, 4])
        reference = pd.Series(["foo", "bar", "baz"],
                              dtype="category",
                              index=[1, 2, 3])

        with pytest.raises(ValueError):
            _map_names_and_colors(reference, query)
예제 #3
0
    def test_simple_wrong_index(self):
        x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category")
        y = pd.Series(["b", np.nan, np.nan, "d", "a"],
                      index=["foo", "bar", "baz", "quux", "quas"])

        with pytest.raises(TypeError):
            _ = _map_names_and_colors(x, y)
예제 #4
0
    def test_mapping_colors_empty(self):
        query = pd.Series([], dtype="category")
        reference = pd.Series([], dtype="category")

        r = _map_names_and_colors(reference, query)

        assert isinstance(r, pd.Series)
        assert is_categorical_dtype(r)
예제 #5
0
    def test_mapping_colors_simple_2(self):
        query = pd.Series(["foo", "bar", "baz"], dtype="category")
        reference = pd.Series(["foo", "bar", "baz"], dtype="category")

        res = _map_names_and_colors(reference, query)

        assert isinstance(res, pd.Series)
        assert len(res) == 3
        assert is_categorical_dtype(res)
예제 #6
0
    def test_mapping_colors_merging(self):
        x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category")
        y = pd.Series(["b", np.nan, np.nan, "d", "a"]).astype("category")

        res, colors = _map_names_and_colors(x, y, colors_reference=["red", "green"])

        assert isinstance(res, pd.Series)
        assert isinstance(colors, list)
        np.testing.assert_array_equal(colors, ["#b20000", "#e65c00", "#008000"])
예제 #7
0
    def test_mapping_colors_empty_with_color(self):
        query = pd.Series([], dtype="category")
        reference = pd.Series([], dtype="category")

        r, c = _map_names_and_colors(reference, query, colors_reference=[])

        assert isinstance(r, pd.Series)
        assert is_categorical_dtype(r)
        assert isinstance(c, list)
        assert len(c) == 0
예제 #8
0
    def test_mapping_colors_0_en_cutoff(self):
        query = pd.Series(["bar", "bar", "bar"], dtype="category")
        reference = pd.Series(["bar", "bar", "bar"], dtype="category")

        # TODO: somehow extract the custom logger and check for logs
        r = _map_names_and_colors(reference, query, en_cutoff=0)

        assert isinstance(r, pd.Series)
        assert is_categorical_dtype(r)
        assert list(r.index) == ["bar"]
        assert list(r.values) == ["bar"]
예제 #9
0
    def test_mapping_colors_diff_query_reference(self):
        query = pd.Series(["bar", "bar", "bar"], dtype="category")
        reference = pd.Series(["foo", "foo", "foo"], dtype="category")

        r, c = _map_names_and_colors(
            reference, query, colors_reference=["red", "red", "red"]
        )

        assert list(r.index) == ["bar"]
        assert list(r.values) == ["foo"]
        assert c == ["#ff0000"]
예제 #10
0
    def test_mapping_colors_same_reference(self):
        query = pd.Series(["foo", "bar", "baz"], dtype="category")
        reference = pd.Series(["foo", "foo", "foo"], dtype="category")

        r, c = _map_names_and_colors(
            reference, query, colors_reference=["red", "red", "red"]
        )

        assert list(r.index) == ["bar", "baz", "foo"]
        assert list(r.values) == ["foo_1", "foo_2", "foo_3"]
        assert c == ["#b20000", "#d13200", "#f07300"]
예제 #11
0
    def test_more_colors_than_reference_categories(self):
        x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category")
        y = pd.Series(["b", np.nan, np.nan, "d", "a"]).astype("category")

        res, colors = _map_names_and_colors(
            x, y, colors_reference=["red", "green", "blue", "yellow"])

        assert isinstance(res, pd.Series)
        assert isinstance(colors, list)
        np.testing.assert_array_equal(colors,
                                      ["#bb2200", "#ee6655", "#008000"])
예제 #12
0
    def test_simple_run(self):
        x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category")
        y = pd.Series(["b", np.nan, np.nan, "d", "a"]).astype("category")
        expected = pd.Series(["a_1", "a_2", "b"])
        expected_index = pd.Index(["a", "b", "d"])

        res = _map_names_and_colors(x, y)

        assert isinstance(res, pd.Series)
        np.testing.assert_array_equal(res.values, expected.values)
        np.testing.assert_array_equal(res.index.values, expected_index.values)
예제 #13
0
    def test_mapping_colors_simple_colors(self):
        query = pd.Series(["foo", "bar", "baz"], dtype="category")
        reference = pd.Series(["foo", "bar", "baz"], dtype="category")

        res, c = _map_names_and_colors(
            reference, query, colors_reference=["red", "green", "blue"])

        assert isinstance(res, pd.Series)
        assert len(res) == 3
        assert is_categorical_dtype(res)

        assert isinstance(c, list)
        assert c == ["#ff0000", "#008000", "#0000ff"]
예제 #14
0
    def test_mapping_colors_name_order_same_as_cat_order(self):
        x = pd.Series(["b", "a", np.nan, "a", np.nan]).astype("category")
        y = pd.Series(["a", np.nan, np.nan, "d", "b"]).astype("category")
        expected = pd.Series(["b", "a_1", "a_2"])
        expected_index = pd.Index(["a", "b", "d"])

        res = _map_names_and_colors(x, y)

        assert isinstance(res, pd.Series)
        assert is_categorical_dtype(res)
        np.testing.assert_array_equal(res.values, expected.values)
        np.testing.assert_array_equal(res.index.values, expected_index.values)
        np.testing.assert_array_equal(res.cat.categories.values, res.values)
예제 #15
0
    def test_mapping_colors_different_color_representation(self):
        query = pd.Series(["foo", "bar", "baz"], dtype="category")
        reference = pd.Series(["foo", "bar", "baz"], dtype="category")

        res, c = _map_names_and_colors(
            reference, query, colors_reference=[(1, 0, 0), "green", (0, 0, 1, 0)]
        )

        assert isinstance(res, pd.Series)
        assert len(res) == 3
        assert is_categorical_dtype(res)

        assert isinstance(c, list)
        assert c == ["#ff0000", "#008000", "#0000ff"]
예제 #16
0
    def test_simple_not_invalid_color_length(self):
        x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category")
        y = pd.Series(["b", np.nan, np.nan, "d", "a"]).astype("category")

        with pytest.raises(ValueError):
            _ = _map_names_and_colors(x, y, colors_reference=["red"])
예제 #17
0
    def _set_categorical_labels(
        self,
        attr_key: str,
        color_key: str,
        pretty_attr_key: str,
        categories: Union[Series, Dict[Any, Any]],
        add_to_existing_error_msg: Optional[str] = None,
        cluster_key: Optional[str] = None,
        en_cutoff: Optional[float] = None,
        p_thresh: Optional[float] = None,
        add_to_existing: bool = False,
    ) -> None:
        if isinstance(categories, dict):
            categories = _convert_to_categorical_series(
                categories, list(self.adata.obs_names)
            )
        if not is_categorical_dtype(categories):
            raise TypeError(
                f"Object must be `categorical`, found `{infer_dtype(categories)}`."
            )

        if add_to_existing:
            if getattr(self, attr_key) is None:
                raise RuntimeError(add_to_existing_error_msg)
            categories = _merge_categorical_series(
                getattr(self, attr_key), categories, inplace=False
            )

        if cluster_key is not None:
            logg.debug(f"Creating colors based on `{cluster_key}`")

            # check that we can load the reference series from adata
            if cluster_key not in self.adata.obs:
                raise KeyError(
                    f"Cluster key `{cluster_key!r}` not found in `adata.obs`."
                )
            series_query, series_reference = categories, self.adata.obs[cluster_key]

            # load the reference colors if they exist
            if _colors(cluster_key) in self.adata.uns.keys():
                colors_reference = _convert_to_hex_colors(
                    self.adata.uns[_colors(cluster_key)]
                )
            else:
                colors_reference = _create_categorical_colors(
                    len(series_reference.cat.categories)
                )

            approx_rcs_names, colors = _map_names_and_colors(
                series_reference=series_reference,
                series_query=series_query,
                colors_reference=colors_reference,
                en_cutoff=en_cutoff,
            )
            setattr(self, color_key, colors)
            # if approx_rcs_names is categorical, the info is take from .cat.categories
            categories.cat.categories = approx_rcs_names
        else:
            setattr(
                self,
                color_key,
                _create_categorical_colors(len(categories.cat.categories)),
            )

        if p_thresh is not None:
            self._detect_cc_stages(categories, p_thresh=p_thresh)

        # write to class and adata
        if getattr(self, attr_key) is not None:
            logg.debug(f"Overwriting `.{pretty_attr_key}`")

        setattr(self, attr_key, categories)
예제 #18
0
    def test_simple_not_categorical(self):
        x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category")
        y = pd.Series(["b", np.nan, np.nan, "d", "a"])

        with pytest.raises(TypeError):
            _ = _map_names_and_colors(x, y)
예제 #19
0
    def test_mapping_colors_negative_en_cutoff(self):
        query = pd.Series(["foo", "bar", "baz"], dtype="category")
        reference = pd.Series(["foo", "bar", "baz"], dtype="category")

        with pytest.raises(ValueError):
            _map_names_and_colors(reference, query, en_cutoff=-1)
예제 #20
0
    def test_mapping_colors_invalid_size(self):
        query = pd.Series(["foo", "bar", "baz"], dtype="category")
        reference = pd.Series(["foo", np.nan, "bar", "baz"], dtype="category")

        with pytest.raises(ValueError):
            _map_names_and_colors(reference, query)
예제 #21
0
    def test_mapping_colors_not_categorical(self):
        query = pd.Series(["foo", "bar", "baz"], dtype="str")
        reference = pd.Series(["foo", np.nan, "bar", "baz"], dtype="category")

        with pytest.raises(TypeError):
            _map_names_and_colors(reference, query)