Skip to content

Commit

Permalink
Remove unused imports and print statements
Browse files Browse the repository at this point in the history
  • Loading branch information
LLehner committed Oct 9, 2024
1 parent e0a67f5 commit ae3b4db
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/squidpy/gr/_niche.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import itertools
from collections.abc import Iterator
from typing import Any

Expand All @@ -10,12 +9,10 @@
import scanpy as sc
import scipy.sparse as sps
from anndata import AnnData
from scipy.sparse import csr_matrix, hstack, issparse, spdiags
from scipy.stats import ranksums
from scipy.sparse import hstack, issparse, spdiags
from sklearn import metrics
from sklearn.metrics import adjusted_rand_score, fowlkes_mallows_score, normalized_mutual_info_score
from sklearn.mixture import GaussianMixture
from sklearn.preprocessing import StandardScaler, normalize
from sklearn.preprocessing import normalize
from spatialdata import SpatialData

from squidpy._utils import NDArrayA
Expand All @@ -26,7 +23,7 @@
def calculate_niche(
adata: AnnData | SpatialData,
flavor: str = "neighborhood",
library_key: str | None = None,
library_key: str | None = None, # TODO: calculate niches on a per-slide basis
table_key: str | None = None,
mask: pd.core.series.Series = None,
groups: str | None = None,
Expand Down Expand Up @@ -132,9 +129,7 @@ def calculate_niche(
adata_neighborhood = adata_neighborhood[mask]

# required for leiden clustering (note: no dim reduction performed in original implementation)
print("calculating neighbors...")
sc.pp.neighbors(adata_neighborhood, n_neighbors=n_neighbors, use_rep="X")
print("finished calculating neighbors")

if resolutions is not None:
if not isinstance(resolutions, list):
Expand All @@ -143,13 +138,11 @@ def calculate_niche(
raise ValueError("Please provide resolutions for leiden clustering.")

# For each resolution, apply leiden on neighborhood profile. Each cluster label equals to a niche label
print("starting clustering...")
for res in resolutions:
sc.tl.leiden(adata_neighborhood, resolution=res, key_added=f"neighborhood_niche_res={res}")
adata.obs[f"neighborhood_niche_res={res}"] = adata.obs.index.map(
adata_neighborhood.obs[f"neighborhood_niche_res={res}"]
).fillna("not_a_niche")
print(f"finished clustering at resolution {res}")

# filter niches with n_cells < min_niche_size
if min_niche_size is not None:
Expand Down Expand Up @@ -337,13 +330,9 @@ def _get_GMM_clusters(A: np.ndarray[np.float64, Any], n_components: int, random_
"""Returns niche labels generated by GMM clustering.
Compared to cellcharter this approach is simplified by using sklearn's GaussianMixture model without stability analysis."""

print("initializing GMM...")
gmm = GaussianMixture(n_components=n_components, random_state=random_state, init_params="random_from_data")
print("fitting GMM...")
gmm.fit(A)
print("predicting labels...")
labels = gmm.predict(A)
print("done")

return labels

Expand Down

0 comments on commit ae3b4db

Please sign in to comment.