Skip to content

Commit

Permalink
add scaling option for isolated label ASW (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
mumichae authored Feb 2, 2023
1 parent 98ee9a2 commit c42935b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
12 changes: 10 additions & 2 deletions scib/metrics/isolated_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def isolated_labels_asw(
batch_key,
embed,
iso_threshold=None,
scale=True,
verbose=True,
):
"""Isolated label score ASW
Expand All @@ -81,6 +82,7 @@ def isolated_labels_asw(
:param iso_threshold: max number of batches per label for label to be considered as
isolated, if iso_threshold is integer.
If ``iso_threshold=None``, consider minimum number of batches that labels are present in
:param scale: Whether to scale the score between 0 and 1. Only relevant for ASW scores.
:param verbose:
:return: Mean of ASW over all isolated labels
Expand All @@ -105,14 +107,14 @@ def isolated_labels_asw(
)
"""

return isolated_labels(
adata,
label_key=label_key,
batch_key=batch_key,
embed=embed,
cluster=False,
iso_threshold=iso_threshold,
scale=scale,
verbose=verbose,
)

Expand All @@ -124,6 +126,7 @@ def isolated_labels(
embed,
cluster=True,
iso_threshold=None,
scale=True,
return_all=False,
verbose=True,
):
Expand All @@ -143,6 +146,7 @@ def isolated_labels(
:param iso_threshold: max number of batches per label for label to be considered as
isolated, if iso_threshold is integer.
If iso_threshold=None, consider minimum number of batches that labels are present in
:param scale: Whether to scale the score between 0 and 1. Only relevant for ASW scores.
:param return_all: return scores for all isolated labels instead of aggregated mean
:param verbose:
:return:
Expand All @@ -168,6 +172,7 @@ def isolated_labels(
label,
embed,
cluster,
scale=scale,
verbose=verbose,
)
scores[label] = score
Expand All @@ -186,6 +191,7 @@ def score_isolated_label(
embed,
cluster=True,
iso_label_key="iso_label",
scale=True,
verbose=False,
):
"""
Expand All @@ -199,6 +205,7 @@ def score_isolated_label(
silhouette score on grouping of isolated label vs all other remaining labels
:param iso_label_key: name of key to use for cluster assignment for F1 score or
isolated-vs-rest assignment for silhouette score
:param scale: Whether to scale the score between 0 and 1. Only relevant for ASW scores.
:param verbose:
:return:
Isolated label score
Expand Down Expand Up @@ -235,13 +242,14 @@ def max_f1(adata, label_key, cluster_key, label, argmax=False):
score = max_f1(adata, label_key, iso_label_key, isolated_label, argmax=False)
else:
# AWS score between isolated label vs rest

if "silhouette_temp" not in adata.obs:
adata.obs["silhouette_temp"] = silhouette_samples(
adata.obsm[embed], adata.obs[label_key]
)
# aggregate silhouette scores for isolated label only
score = adata.obs[adata.obs[label_key] == isolated_label].silhouette_temp.mean()
if scale:
score = (score + 1) / 2

if verbose:
print(f"{isolated_label}: {score}")
Expand Down
2 changes: 1 addition & 1 deletion tests/metrics/test_isolated_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_isolated_labels_ASW(adata_pca):
verbose=True,
)
LOGGER.info(f"score: {score}")
assert_near_exact(score, 0.14066337049007416, diff=1e-3)
assert_near_exact(score, 0.5703811198472977, diff=1e-3)


def test_isolated_labels_f1_perfect(adata_pca):
Expand Down
13 changes: 0 additions & 13 deletions tests/metrics/test_silhouette_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,3 @@ def test_silhouette_batch_empty(adata_pca):
assert np.isnan(sil_means)
assert sil_df.shape[0] == 0
LOGGER.info(sil_df)


def test_isolated_labels_silhouette(adata_pca):
score = scib.me.isolated_labels(
adata_pca,
label_key="celltype",
batch_key="batch",
embed="X_pca",
cluster=False,
verbose=True,
)
LOGGER.info(f"score: {score}")
assert_near_exact(score, 0.14066337049007416, diff=1e-3)

0 comments on commit c42935b

Please sign in to comment.