Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSD metric #4

Merged
merged 3 commits into from
Aug 26, 2024
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
33 changes: 33 additions & 0 deletions src/metrics/jsd/config.vsh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
__merge__: ../../api/comp_metric.yaml

name: jsd
info:
metrics:
- name: jensen_shannon_distance
label: Jensen-Shannon Distance
summary: "Jensen-Shannon Distance measure the similarity between to probability distributions."
description: |
The Jensen-Shannon Distance, which is the square root of Jensen-Shannon Divergence is a symmetric method for measuring the similarity between two probability distributions. The similarity between the distributions is greater when the Jensen-Shannon distance is closer to zero.
reference: 10.1109/18.61115
documentation_url: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.jensenshannon.html
repository_url: https://github.com/scipy/scipy/
rcannood marked this conversation as resolved.
Show resolved Hide resolved
min: 0
max: 1
maximize: false

resources:
- type: python_script
path: script.py

engines:
- type: docker
image: ghcr.io/openproblems-bio/base_images/python:1.1.0
setup:
- type: python
packages: [numpy, scipy]

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

## VIASH START
par = {
'input_method': 'resources_test/spatial_decomposition/cxg_mouse_pancreas_atlas/output.h5ad',
'input_solution': 'resources_test/spatial_decomposition/cxg_mouse_pancreas_atlas/solution.h5ad',
'output': 'score.h5ad'
}
meta = {
'name': 'r2'
}
## VIASH END

print('Reading input files', flush=True)
input_method = ad.read_h5ad(par['input_method'])
input_solution = ad.read_h5ad(par['input_solution'])

print('Compute metrics', flush=True)
jsd = jensenshannon(input_solution.obsm['proportions_true'], input_method.obsm['proportions_pred'], axis=0)
uns_metric_ids = [ 'jsd' ]
uns_metric_values = [ np.mean(jsd) ]

print("Write output AnnData to file", flush=True)
output = ad.AnnData(
uns={
'dataset_id': input_method.uns['dataset_id'],
'method_id': input_method.uns['method_id'],
'metric_ids': uns_metric_ids,
'metric_values': uns_metric_values
}
)
output.write_h5ad(par['output'], compression='gzip')