Skip to content

Commit

Permalink
Maint/fix warnings (#798)
Browse files Browse the repository at this point in the history
* Remove `applymap`

* Remove `dtype` arg from `AnnData`

* Fix `ChainedAssignmentError`

* Use `plt.colormaps` in tests

* Use `errorbar="sd"`

* Remove `is_categorical_dtype`

* Fix `axis` in `DataFrame.groupby`

* Fix docs in `var_by_distance`

* Fix inplace operation in `var_by_distance`

* Use `.iloc` for `row_order` in `_heatmap`

* Convert library ids to list

* Use `Dataset.sizes` in appropriate places

* Set `observed=True` in `groupby`

* Use `na_action=None` in `Categorical.map`

* Ignore `pkg_resources` warnings

* Remove `get_cmap` usage

* Remove old warning filters
  • Loading branch information
michalk8 authored Feb 9, 2024
1 parent ec0f57e commit 8f82046
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 111 deletions.
2 changes: 1 addition & 1 deletion src/squidpy/gr/_ligrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ def extractor(res: Sequence[TempResult]) -> TempResult:

return TempResult(means=means, pvalues=pvalues)

groups = data.groupby("clusters")
groups = data.groupby("clusters", observed=True)
clustering = np.array(data["clusters"].values, dtype=np.int32)

mean = groups.mean().values.T # (n_genes, n_clusters)
Expand Down
2 changes: 1 addition & 1 deletion src/squidpy/gr/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from anndata._core.views import ArrayView, SparseCSCView, SparseCSRView
from anndata.utils import make_index_unique
from pandas import CategoricalDtype
from pandas.api.types import infer_dtype, is_categorical_dtype
from pandas.api.types import infer_dtype
from scanpy import logging as logg
from scipy.sparse import csc_matrix, csr_matrix, issparse, spmatrix

Expand Down
13 changes: 6 additions & 7 deletions src/squidpy/im/_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def concat(
# check that imgs are not already 3d
imgs = list(imgs)
for img in imgs:
if img.data.dims["z"] > 1:
if img.data.sizes["z"] > 1:
raise ValueError(
f"Currently, can concatenate only images with 1 Z-dimension, found `{img.data.dims['z']}`."
)
Expand Down Expand Up @@ -605,10 +605,10 @@ def _rescale(arr: xr.DataArray) -> xr.DataArray:
data.attrs = _update_attrs_scale(attrs, scale)

if mask_circle:
if data.dims["y"] != data.dims["x"]:
if data.sizes["y"] != data.sizes["x"]:
raise ValueError(
f"Masking circle is only available for square crops, "
f"found crop of shape `{(data.dims['y'], data.dims['x'])}`."
f"found crop of shape `{(data.sizes['y'], data.sizes['x'])}`."
)
c = data.x.shape[0] // 2
# manually reassign coordinates
Expand Down Expand Up @@ -789,7 +789,7 @@ def generate_spot_crops(
raise e
else:
try:
obs_library_ids = adata.obs[library_id]
obs_library_ids = list(adata.obs[library_id])
except KeyError:
logg.debug(
f"Unable to find library ids in `adata.obs[{library_id!r}]`. "
Expand Down Expand Up @@ -1368,7 +1368,7 @@ def shape(self) -> tuple[int, int]:
"""Image shape ``(y, x)``."""
if not len(self):
return 0, 0
return self.data.dims["y"], self.data.dims["x"]
return self.data.sizes["y"], self.data.sizes["x"]

def copy(self, deep: bool = False) -> ImageContainer:
"""
Expand Down Expand Up @@ -1596,8 +1596,7 @@ def _repr_html_(self) -> str:
for i, layer in enumerate(self.data.keys()):
s += f"<p style={style!r}><strong>{html.escape(str(layer))}</strong>: "
s += ", ".join(
f"<em>{html.escape(str(dim))}</em> ({shape})"
for dim, shape in zip(self.data[layer].dims, self.data[layer].shape)
f"<em>{html.escape(str(dim))}</em> ({shape})" for dim, shape in self.data[layer].sizes.items()
)
s += "</p>"
if i == 9 and i < len(self) - 1: # show only first 10 layers
Expand Down
8 changes: 4 additions & 4 deletions src/squidpy/im/_feature_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ def convert_to_full_image_coordinates(x: NDArrayA, y: NDArrayA) -> NDArrayA:
return np.array([[]], dtype=np.float64)

if self.data.attrs.get("mask_circle", False):
if self.data.dims["y"] != self.data.dims["x"]:
raise ValueError(f"Crop is not a square: `{self.data.dims}`.")
c = self.data.dims["x"] // 2 # center
if self.data.sizes["y"] != self.data.sizes["x"]:
raise ValueError(f"Crop is not a square: `{self.data.sizes}`.")
c = self.data.sizes["x"] // 2 # center
mask = (x - c) ** 2 + (y - c) ** 2 <= c**2
y = y[mask]
x = x[mask]
Expand All @@ -341,7 +341,7 @@ def convert_to_full_image_coordinates(x: NDArrayA, y: NDArrayA) -> NDArrayA:
return np.array([[]], dtype=np.float64) # because of masking, should not happen

coord = self.data.attrs.get(
Key.img.coords, CropCoords(x0=0, y0=0, x1=self.data.dims["x"], y1=self.data.dims["y"])
Key.img.coords, CropCoords(x0=0, y0=0, x1=self.data.sizes["x"], y1=self.data.sizes["y"])
) # fall back to default (i.e no crop) coordinates
padding = self.data.attrs.get(Key.img.padding, _NULL_PADDING) # fallback to no padding
y_slc, x_slc = coord.to_image_coordinates(padding).slice
Expand Down
2 changes: 1 addition & 1 deletion src/squidpy/pl/_color_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _get_palette(

len_cat = len(categories)
if isinstance(palette, str):
cmap = plt.get_cmap(palette)
cmap = plt.colormaps[palette]
palette = [to_hex(x, keep_alpha=True) for x in cmap(np.linspace(0, 1, len_cat), alpha=alpha)]
elif isinstance(palette, ListedColormap):
palette = [to_hex(x, keep_alpha=True) for x in palette(np.linspace(0, 1, len_cat), alpha=alpha)]
Expand Down
6 changes: 3 additions & 3 deletions src/squidpy/pl/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def interaction_matrix(
_assert_categorical_obs(adata, key=cluster_key)
array = _get_data(adata, cluster_key=cluster_key, func_name="interaction_matrix")

ad = AnnData(X=array, obs={cluster_key: pd.Categorical(adata.obs[cluster_key].cat.categories)}, dtype=array.dtype)
ad = AnnData(X=array, obs={cluster_key: pd.Categorical(adata.obs[cluster_key].cat.categories)})
_maybe_set_colors(source=adata, target=ad, key=cluster_key, palette=palette)
if title is None:
title = "Interaction matrix"
Expand Down Expand Up @@ -224,7 +224,7 @@ def nhood_enrichment(
_assert_categorical_obs(adata, key=cluster_key)
array = _get_data(adata, cluster_key=cluster_key, func_name="nhood_enrichment")[mode]

ad = AnnData(X=array, obs={cluster_key: pd.Categorical(adata.obs[cluster_key].cat.categories)}, dtype=array.dtype)
ad = AnnData(X=array, obs={cluster_key: pd.Categorical(adata.obs[cluster_key].cat.categories)})
_maybe_set_colors(source=adata, target=ad, key=cluster_key, palette=palette)
if title is None:
title = "Neighborhood enrichment"
Expand Down Expand Up @@ -315,7 +315,7 @@ def ripley(
**kwargs,
)
if plot_sims:
sns.lineplot(y="stats", x="bins", ci="sd", alpha=0.01, color="gray", data=res["sims_stat"], ax=ax)
sns.lineplot(y="stats", x="bins", errorbar="sd", alpha=0.01, color="gray", data=res["sims_stat"], ax=ax)
ax.legend(**legend_kwargs)
ax.set_ylabel("value")
ax.set_title(f"Ripley's {mode.s}")
Expand Down
1 change: 0 additions & 1 deletion src/squidpy/pl/_interactive/_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from napari import Viewer
from napari.layers import Points, Shapes
from pandas import CategoricalDtype
from pandas.core.dtypes.common import is_categorical_dtype
from PyQt5.QtWidgets import QGridLayout, QLabel, QWidget
from scanpy import logging as logg

Expand Down
1 change: 0 additions & 1 deletion src/squidpy/pl/_interactive/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from numba import njit
from pandas import CategoricalDtype
from pandas._libs.lib import infer_dtype
from pandas.core.dtypes.common import is_categorical_dtype
from scanpy import logging as logg
from scanpy.plotting._utils import add_colors_for_categorical_sample_annotation
from scipy.spatial import KDTree
Expand Down
6 changes: 3 additions & 3 deletions src/squidpy/pl/_ligrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _plot_size_legend(self, size_legend_ax: Axes) -> None:
ax.set_position([l + w, b, w, h])

def _plot_colorbar(self, color_legend_ax: Axes, normalize: bool) -> None:
cmap = plt.get_cmap(self.cmap)
cmap = plt.colormaps[self.cmap]

ColorbarBase(
color_legend_ax,
Expand Down Expand Up @@ -303,7 +303,7 @@ def get_dendrogram(adata: AnnData, linkage: str = "complete") -> Mapping[str, An
pvals = pvals.T
means = means.T

for cls, size in (pvals.groupby(level=0, axis=1)).size().to_dict().items():
for cls, size in (pvals.T.groupby(level=0)).size().to_dict().items():
label_ranges[cls] = (start, start + size - 1)
start += size
label_ranges = {k: label_ranges[k] for k in sorted(label_ranges.keys())}
Expand All @@ -322,7 +322,7 @@ def get_dendrogram(adata: AnnData, linkage: str = "complete") -> Mapping[str, An
var = pd.DataFrame(pvals.columns)
var = var.set_index(var.columns[0])

adata = AnnData(pvals.values, obs={"groups": pd.Categorical(pvals.index)}, var=var, dtype=pvals.values.dtype)
adata = AnnData(pvals.values, obs={"groups": pd.Categorical(pvals.index)}, var=var)
adata.obs_names = pvals.index
minn = np.nanmin(adata.X)
delta = np.nanmax(adata.X) - minn
Expand Down
19 changes: 10 additions & 9 deletions src/squidpy/pl/_spatial_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from matplotlib import colors, patheffects, rcParams
from matplotlib import pyplot as plt
from matplotlib.axes import Axes
from matplotlib.cm import get_cmap
from matplotlib.collections import Collection, PatchCollection
from matplotlib.colors import (
ColorConverter,
Expand All @@ -40,7 +39,6 @@
from matplotlib.patches import Circle, Polygon, Rectangle
from matplotlib_scalebar.scalebar import ScaleBar
from pandas import CategoricalDtype
from pandas.core.dtypes.common import is_categorical_dtype
from scanpy import logging as logg
from scanpy._settings import settings as sc_settings
from scanpy.plotting._tools.scatterplots import _add_categorical_legend
Expand Down Expand Up @@ -385,7 +383,7 @@ def subset_by_key(
) -> tuple[AnnData, NDArrayA]:
if key is None or values is None:
return adata, coords
if key not in adata.obs or not is_categorical_dtype(adata.obs[key]):
if key not in adata.obs or not isinstance(adata.obs[key].dtype, CategoricalDtype):
return adata, coords
try:
mask = adata.obs[key].isin(values).values
Expand Down Expand Up @@ -468,7 +466,7 @@ def _set_color_source_vec(
else:
color_source_vector = adata.obs_vector(value_to_plot, layer=layer)

if not is_categorical_dtype(color_source_vector):
if not isinstance(color_source_vector.dtype, CategoricalDtype):
return None, color_source_vector, False

color_source_vector = pd.Categorical(color_source_vector) # convert, e.g., `pd.Series`
Expand All @@ -480,7 +478,7 @@ def _set_color_source_vec(
if color_map is None:
raise ValueError("Unable to create color palette.")
# do not rename categories, as colors need not be unique
color_vector = color_source_vector.map(color_map)
color_vector = color_source_vector.map(color_map, na_action=None)
if color_vector.isna().any():
color_vector = color_vector.add_categories([to_hex(na_color)])
color_vector = color_vector.fillna(to_hex(na_color))
Expand Down Expand Up @@ -613,7 +611,7 @@ def _decorate_axs(
adata: AnnData,
coords: NDArrayA,
value_to_plot: str,
color_source_vector: pd.Series[CategoricalDtype],
color_source_vector: pd.Series[CategoricalDtype] | None,
img: NDArrayA | None = None,
img_cmap: str | None = None,
img_alpha: float | None = None,
Expand Down Expand Up @@ -646,7 +644,7 @@ def _decorate_axs(
path_effect = []

# Adding legends
if is_categorical_dtype(color_source_vector):
if color_source_vector is not None and isinstance(color_source_vector.dtype, CategoricalDtype):
clusters = color_source_vector.categories
palette = _get_palette(adata, cluster_key=value_to_plot, categories=clusters, palette=palette, alpha=alpha)
_add_categorical_legend(
Expand Down Expand Up @@ -691,7 +689,7 @@ def _map_color_seg(
) -> NDArrayA:
cell_id = np.array(cell_id)

if is_categorical_dtype(color_vector):
if isinstance(color_vector.dtype, CategoricalDtype):
if isinstance(na_color, tuple) and len(na_color) == 4 and np.any(color_source_vector.isna()):
cell_id[color_source_vector.isna()] = 0
val_im: NDArrayA = map_array(seg, cell_id, color_vector.codes + 1) # type: ignore[union-attr]
Expand Down Expand Up @@ -822,7 +820,10 @@ def _prepare_params_plot(
fig, ax = plt.subplots(figsize=figsize, dpi=dpi, constrained_layout=True)

# set cmap and norm
cmap = copy(get_cmap(cmap))
if cmap is None:
cmap = plt.rcParams["image.cmap"]
if isinstance(cmap, str):
cmap = plt.colormaps[cmap]
cmap.set_bad("lightgray" if na_color is None else na_color)

if isinstance(norm, Normalize):
Expand Down
9 changes: 4 additions & 5 deletions src/squidpy/pl/_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import os
from copy import copy
from functools import wraps
from inspect import signature
from pathlib import Path
Expand Down Expand Up @@ -34,7 +33,6 @@
from pandas._libs.lib import infer_dtype
from pandas.core.dtypes.common import (
is_bool_dtype,
is_categorical_dtype,
is_integer_dtype,
is_numeric_dtype,
is_object_dtype,
Expand Down Expand Up @@ -482,7 +480,7 @@ def _annotate_heatmap(
) -> None:
# modified from matplotlib's site
if isinstance(cmap, str):
cmap = plt.get_cmap(cmap)
cmap = plt.colormaps[cmap]

data = im.get_array()
kw = {"ha": "center", "va": "center"}
Expand Down Expand Up @@ -554,7 +552,7 @@ def _heatmap(
row_order = col_order = np.arange(len(adata.uns[Key.uns.colors(key)])).tolist()

row_order = row_order[::-1]
row_labels = adata.obs[key][row_order]
row_labels = adata.obs[key].iloc[row_order]
data = adata[row_order, col_order].X

row_cmap, col_cmap, row_norm, col_norm, n_cls = _get_cmap_norm(adata, key, order=(row_order, col_order))
Expand All @@ -563,7 +561,8 @@ def _heatmap(
col_sm = mpl.cm.ScalarMappable(cmap=col_cmap, norm=col_norm)

norm = mpl.colors.Normalize(vmin=kwargs.pop("vmin", np.nanmin(data)), vmax=kwargs.pop("vmax", np.nanmax(data)))
cont_cmap = copy(plt.get_cmap(cont_cmap))
if isinstance(cont_cmap, str):
cont_cmap = plt.colormaps[cont_cmap]
cont_cmap.set_bad(color="grey")

im = ax.imshow(data[::-1], cmap=cont_cmap, norm=norm)
Expand Down
17 changes: 12 additions & 5 deletions src/squidpy/pl/_var_by_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,31 @@ def var_by_distance(
line_palette
Categorical color palette used in case a covariate is specified.
scatter_palette
Color palette for the scatter plot underlying the `sns.regplot`
%(plotting_save)s
Color palette for the scatter plot underlying the :func:`seaborn.regplot`.
dpi
Dots per inch.
figsize
Size of the figure in inches.
save
Whether to save the plot.
title
Panel titles.
axis_label
Panel axis labels.
return_ax
Whether to return :class:`matplotlib.axes.Axes` object(s).
regplot_kwargs
Kwargs for `sns.regplot`
Kwargs for :func:`seaborn.regplot`.
scatterplot_kwargs
Kwargs for `sns.scatter`
Kwargs for :func:`matplotlib.pyplot.scatter`.
Returns
-------
%(plotting_returns)s
"""
dpi = rcParams["figure.dpi"] if dpi is None else dpi
regplot_kwargs = dict(regplot_kwargs)
scatterplot_kwargs = dict(regplot_kwargs)
scatterplot_kwargs = dict(scatterplot_kwargs)

df = adata.obsm[design_matrix_key] # get design matrix
df[var] = np.array(adata[:, var].X.A) if issparse(adata[:, var].X) else np.array(adata[:, var].X) # add var column
Expand Down
1 change: 0 additions & 1 deletion src/squidpy/read/_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ def nanostring(

adata = AnnData(
csr_matrix(counts.loc[common_index, :].values),
dtype=counts.values.dtype,
obs=obs.loc[common_index, :],
uns={Key.uns.spatial: {}},
)
Expand Down
Loading

0 comments on commit 8f82046

Please sign in to comment.