Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## NEW FUNCTIONALITY

* Added `spaTrack` method (PR #4).
* Added `Spearman's correlation` metric (PR #5).

## MAJOR CHANGES

Expand Down
62 changes: 62 additions & 0 deletions src/metrics/spearman_corr/config.vsh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
__merge__: ../../api/comp_metric.yaml

name: spearman_corr

info:
metrics:
- name: spearman_corr
label: Spearman's Corr
summary: "Computes the Spearman rank correlation coefficient between inferred and ground-truth pseudotime."
description: |
Calculates the Spearman rank correlation coefficient (rho) between predicted cell pseudotime
values and the true ground-truth pseudotime. Measures monotonic relationships regardless of linearity.
references:
doi:
- 10.1038/s41592-020-0772-5
bibtex: |
@article{Virtanen_2020,
author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Jarrod Millman, K. and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C. J. and Polat, {\dot{I}}lhan and Feng, Yu and Moore, Eric W. and VanderPlas, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},
title = {Author Correction: SciPy 1.0: fundamental algorithms for scientific computing in Python},
journal = {Nature Methods},
volume = {17},
number = {3},
pages = {352},
year = {2020},
doi = {10.1038/s41592-020-0772-5}
}
links:
documentation: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html
repository: https://github.com/scipy/scipy
min: -1
max: 1
maximize: true

resources:
- type: python_script
path: script.py

engines:
- type: docker
image: python:3.11-slim
setup:
- type: apt
packages:
- procps # required by Nextflow
- git # pip needs it to install openproblems core from git+https
- type: python
packages:
- anndata~=0.10.0
- scanpy~=1.10.0
- scipy
- pandas
- pyyaml
- requests
- jsonschema
github:
- "openproblems-bio/core#subdirectory=packages/python/openproblems"

runners:
- type: executable
- type: nextflow
directives:
label: [midtime,midmem,midcpu]
74 changes: 74 additions & 0 deletions src/metrics/spearman_corr/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import anndata as ad
import numpy as np
import pandas as pd
from scipy import stats

## VIASH START
# Note: this section is auto-generated by viash at runtime. To edit it, make changes
# in config.vsh.yaml and then run `viash config inject config.vsh.yaml`.
par = {
'input_solution': 'resources_test/task_spatial_trajectory_inference/cxg_mouse_pancreas_atlas/solution.h5ad',
'input_prediction': 'resources_test/task_spatial_trajectory_inference/cxg_mouse_pancreas_atlas/prediction.h5ad',
'output': 'output.h5ad',
}
meta = {
'name': 'spearman_corr'
}
## VIASH END


def compute_spearman(true_values, inferred_values):
"""Calculates Spearman rank correlation on finite overlapping values."""
mask = np.isfinite(true_values) & np.isfinite(inferred_values)

if mask.sum() < 2:
return 0.0

rho, _ = stats.spearmanr(true_values[mask], inferred_values[mask])

if np.isnan(rho):
return 0.0

return float(rho)

# read input data
print('Reading input files', flush=True)
input_solution = ad.read_h5ad(par['input_solution'])
input_prediction = ad.read_h5ad(par['input_prediction'])

assert (input_prediction.obs_names == input_solution.obs_names).all(), "obs_names not the same in prediction and solution inputs"


# ground truth and predicted pseudotime
TRUE_COL = "pseudotime_true"
INFERRED_COL = "pseudotime_inferred"

true_vals = pd.to_numeric(input_solution.obs[TRUE_COL], errors='coerce').values
inferred_vals = pd.to_numeric(input_prediction.obs[INFERRED_COL], errors='coerce').values

# generate results
print('Compute metrics', flush=True)
# metric_ids and metric_values can have length > 1
# but should be of equal length

score = compute_spearman(true_vals, inferred_vals)

uns_metric_ids = [ 'spearman_corr' ]
uns_metric_values = [ score ]

# Write output data to file
print("Write output AnnData to file...", flush=True)

output = ad.AnnData(
obs=pd.DataFrame(index=pd.Index(np.array([], dtype=str))),
var=pd.DataFrame(index=pd.Index(np.array([], dtype=str))),
uns={
'dataset_id': input_solution.uns.get('dataset_id', 'unknown'),
'normalization_id': input_solution.uns.get('normalization_id', 'unknown'),
'method_id': input_prediction.uns.get('method_id', 'unknown'),
'metric_ids': uns_metric_ids,
'metric_values': uns_metric_values,
}
)

output.write_h5ad(par['output'], compression='gzip')
Loading