Skip to content

Commit

Permalink
Fix pandas and plotting warning (#1065)
Browse files Browse the repository at this point in the history
* Fix setting categories

* Fix using `inplace` in `reorder_categories`

* Remove `Colorbar.draw()`

* Remove setting `pandas.DataFrame` attributes in `get_df`

---------

Co-authored-by: Philipp Weiler <weiler.philipp@gmail.com>
  • Loading branch information
michalk8 and WeilerP committed May 31, 2023
1 parent 1ae1cdf commit e8fa7a1
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencies = [
"umap-learn>=0.3.10",
"numba>=0.41.0",
"numpy>=1.17",
"pandas>=0.23, !=1.4.0",
"pandas>=1.1.1, !=1.4.0",
"scipy>=1.4.1",
"scikit-learn>=0.21.2, <1.2.0",
"scvi-tools>=0.20.1",
Expand Down
11 changes: 0 additions & 11 deletions scvelo/core/_anndata.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,6 @@ def get_df(
)
df = df.sort_values(by=sort_by, ascending=False)

if hasattr(data, "var_names"):
if df.index[0] in data.var_names:
df.var_names = df.index
elif df.columns[0] in data.var_names:
df.var_names = df.columns
if hasattr(data, "obs_names"):
if df.index[0] in data.obs_names:
df.obs_names = df.index
elif df.columns[0] in data.obs_names:
df.obs_names = df.columns

return df


Expand Down
4 changes: 2 additions & 2 deletions scvelo/plotting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@ def set_legend(
if legend_fontoutline is None:
legend_fontoutline = 1
obs_vals = adata.obs[value_to_plot]
obs_vals.cat.categories = obs_vals.cat.categories.astype(str)
str_cats = obs_vals.cat.categories.astype(str)
obs_vals = obs_vals.cat.set_categories(str_cats, rename=True)
color_keys = adata.uns[f"{value_to_plot}_colors"]
if isinstance(color_keys, dict):
color_keys = np.array([color_keys[c] for c in obs_vals.cat.categories])
Expand Down Expand Up @@ -964,7 +965,6 @@ def set_colorbar(smp, ax, orientation="vertical", labelsize=None):
cb = pl.colorbar(smp, orientation=orientation, cax=cax)
cb.set_alpha(1)
cb.ax.tick_params(labelsize=labelsize)
cb.draw_all()
cb.locator = MaxNLocator(nbins=3, integer=True)
cb.update_ticks()

Expand Down
5 changes: 3 additions & 2 deletions scvelo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from cycler import cycler
from packaging.version import parse

from matplotlib import cbook, cm, colors, rcParams
import matplotlib as mpl
from matplotlib import cm, colors, rcParams

"""Settings
"""
Expand Down Expand Up @@ -88,7 +89,7 @@
# Functions
# --------------------------------------------------------------------------------

warnings.filterwarnings("ignore", category=cbook.mplDeprecation)
warnings.filterwarnings("ignore", category=mpl.MatplotlibDeprecationWarning)


# default matplotlib 2.0 palette slightly modified.
Expand Down
4 changes: 2 additions & 2 deletions scvelo/tools/rank_velocity_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ def velocity_clusters(
vc = vdata.obs["louvain"]
vc_cats = vc.cat.categories
mean_times = [np.mean(vdata.obs[sort_by][vc == cat]) for cat in vc_cats]
vdata.obs["louvain"].cat.reorder_categories(
vc_cats[np.argsort(mean_times)], inplace=True
vdata.obs["louvain"] = vdata.obs["louvain"].cat.reorder_categories(
vc_cats[np.argsort(mean_times)]
)

if isinstance(match_with, str) and match_with in adata.obs.keys():
Expand Down
11 changes: 8 additions & 3 deletions scvelo/tools/velocity_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,15 @@ def __init__(
self.graph_neg = adata.uns[gkey_] if gkey_ in adata.uns.keys() else []

if tkey in adata.obs.keys():
self.t0 = adata.obs[tkey].copy()
self.t0 = adata.obs[tkey].astype("category").copy()
init = min(self.t0) if isinstance(min(self.t0), int) else 0
self.t0.cat.categories = np.arange(init, len(self.t0.cat.categories))
self.t0 = self.t0.cat.set_categories(
np.arange(init, len(self.t0.cat.categories)), rename=True
)
self.t1 = self.t0.copy()
self.t1.cat.categories = self.t0.cat.categories + 1
self.t1 = self.t1.cat.set_categories(
self.t0.cat.categories + 1, rename=True
)
else:
self.t0 = None

Expand All @@ -184,6 +188,7 @@ def compute_cosines(self, n_jobs=None, backend="loky"):
n_jobs=n_jobs,
unit="cells",
backend=backend,
as_array=False,
)()
uncertainties, vals, rows, cols = map(_flatten, zip(*res))

Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import pytest
from hypothesis import settings

import matplotlib as mpl

import scanpy as sc
from anndata import AnnData

Expand All @@ -25,6 +27,11 @@
_pancreas_100obs_preprocessed = sc.read("tests/_data/pancreas_100obs_preprocessed.h5ad")


def pytest_sessionstart(session: pytest.Session) -> None:
del session
mpl.use("Agg")


@pytest.fixture
def dentategyrus_50obs() -> AnnData:
return _dentategyrus_50obs.copy()
Expand Down

0 comments on commit e8fa7a1

Please sign in to comment.