def test_merge_colors_wrong_dict(self): with pytest.raises(ValueError): x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category") y = pd.Series(["b", np.nan, "a", "d", "a"]).astype("category") colors_x = {"a": "red", "foo": "blue"} _ = _merge_categorical_series(x, y, colors_old=colors_x, inplace=True)
def test_merge_different_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=[5, 4, 3, 2, 1]).astype( "category" ) with pytest.raises(ValueError): _ = _merge_categorical_series(x, y)
def test_merge_colors_wrong_number_of_colors(self): with pytest.raises(ValueError): x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category") y = pd.Series(["b", np.nan, "a", "d", "a"]).astype("category") colors_x = ["red"] _ = _merge_categorical_series(x, y, colors_old=colors_x, inplace=True)
def test_merge_normal_run(self): x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category") y = pd.Series(["b", np.nan, "a", "d", "a"]).astype("category") expected = pd.Series(["b", "b", "a", "d", "a"]).astype("category") res = _merge_categorical_series(x, y, inplace=False) np.testing.assert_array_equal(res.values, expected.values)
def test_merge_normal_run_completely_different_categories(self): x = pd.Series(["a", "a", "a"]).astype("category") y = pd.Series(["b", "b", "b"]).astype("category") expected = pd.Series(["b", "b", "b"]).astype("category") res = _merge_categorical_series(x, y, inplace=False) np.testing.assert_array_equal(res.values, expected.values) np.testing.assert_array_equal(res.cat.categories.values, ["b"])
def test_merge_normal_run_inplace(self): x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category") y = pd.Series(["b", np.nan, "a", "d", "a"]).astype("category") expected = pd.Series(["b", "b", "a", "d", "a"]).astype("category") _ = _merge_categorical_series(x, y, inplace=True) assert _ is None np.testing.assert_array_equal(x.values, expected.values)
def test_merge_colors_simple_old(self): x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category") y = pd.Series(["b", np.nan, "a", "d", "a"]).astype("category") colors_x = ["red", "blue"] colors_merged = _merge_categorical_series( x, y, colors_old=colors_x, inplace=True ) np.testing.assert_array_equal(colors_merged, ["red", "blue", "#4daf4a"])
def test_merge_colors_simple_new(self): x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category") y = pd.Series(["b", np.nan, "a", "d", "a"]).astype("category") colors_y = ["red", "blue", "green"] colors_merged = _merge_categorical_series( x, y, colors_new=colors_y, inplace=True ) np.testing.assert_array_equal(colors_merged, ["#e41a1c", "#377eb8", "green"])
def test_merge_colors_both(self): x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category") y = pd.Series(["b", np.nan, "a", "d", "a"]).astype("category") colors_x = ["red", "blue"] colors_y = ["green", "yellow", "black"] colors_merged = _merge_categorical_series( x, y, colors_old=colors_x, colors_new=colors_y, inplace=True ) np.testing.assert_array_equal(colors_merged, ["red", "blue", "black"])
def test_merge_colors_simple_old_no_inplace(self): x = pd.Series(["a", "b", np.nan, "b", np.nan]).astype("category") y = pd.Series(["b", np.nan, "a", "d", "a"]).astype("category") expected = pd.Series(["b", "b", "a", "d", "a"]).astype("category") colors_x = ["red", "blue"] merged, colors_merged = _merge_categorical_series( x, y, colors_old=colors_x, inplace=False ) np.testing.assert_array_equal(merged.values, expected.values) np.testing.assert_array_equal(colors_merged, ["red", "blue", "#4daf4a"])
def test_merge_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): _ = _merge_categorical_series(x, y)
def _set_categorical_labels( self, attr_key: str, pretty_attr_key: str, cat_key: str, add_to_existing_error_msg: str, categories: Union[Series, Dict[Any, Any]], cluster_key: Optional[str] = None, en_cutoff: Optional[float] = None, p_thresh: Optional[float] = None, add_to_existing: bool = False, ): 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"DEBUG: 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, f"{attr_key}_colors", colors) categories.cat.categories = approx_rcs_names else: setattr( self, f"{attr_key}_colors", _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"DEBUG: Overwriting `.{pretty_attr_key}`") setattr(self, attr_key, categories) self._adata.obs[cat_key] = categories.values self._adata.uns[_colors(cat_key)] = getattr(self, f"{attr_key}_colors")