Skip to content

Commit 6b25bf9

Browse files
authored
Merge pull request #4 from openproblems-bio/add_jsd
Add JSD metric
2 parents 3f5442c + e1bb94a commit 6b25bf9

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

src/metrics/jsd/config.vsh.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
__merge__: ../../api/comp_metric.yaml
2+
3+
name: jsd
4+
info:
5+
metrics:
6+
- name: jensen_shannon_distance
7+
label: Jensen-Shannon Distance
8+
summary: "Jensen-Shannon Distance measure the similarity between to probability distributions."
9+
description: |
10+
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.
11+
reference: 10.1109/18.61115
12+
documentation_url: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.jensenshannon.html
13+
repository_url: https://github.com/scipy/scipy/
14+
min: 0
15+
max: 1
16+
maximize: false
17+
18+
resources:
19+
- type: python_script
20+
path: script.py
21+
22+
engines:
23+
- type: docker
24+
image: ghcr.io/openproblems-bio/base_images/python:1.1.0
25+
setup:
26+
- type: python
27+
packages: [numpy, scipy]
28+
29+
runners:
30+
- type: executable
31+
- type: nextflow
32+
directives:
33+
label: [midtime, midmem, midcpu]

src/metrics/jsd/script.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import anndata as ad
2+
import numpy as np
3+
from scipy.spatial.distance import jensenshannon
4+
5+
## VIASH START
6+
par = {
7+
'input_method': 'resources_test/spatial_decomposition/cxg_mouse_pancreas_atlas/output.h5ad',
8+
'input_solution': 'resources_test/spatial_decomposition/cxg_mouse_pancreas_atlas/solution.h5ad',
9+
'output': 'score.h5ad'
10+
}
11+
meta = {
12+
'name': 'r2'
13+
}
14+
## VIASH END
15+
16+
print('Reading input files', flush=True)
17+
input_method = ad.read_h5ad(par['input_method'])
18+
input_solution = ad.read_h5ad(par['input_solution'])
19+
20+
print('Compute metrics', flush=True)
21+
jsd = jensenshannon(input_solution.obsm['proportions_true'], input_method.obsm['proportions_pred'], axis=0)
22+
uns_metric_ids = [ 'jsd' ]
23+
uns_metric_values = [ np.mean(jsd) ]
24+
25+
print("Write output AnnData to file", flush=True)
26+
output = ad.AnnData(
27+
uns={
28+
'dataset_id': input_method.uns['dataset_id'],
29+
'method_id': input_method.uns['method_id'],
30+
'metric_ids': uns_metric_ids,
31+
'metric_values': uns_metric_values
32+
}
33+
)
34+
output.write_h5ad(par['output'], compression='gzip')

0 commit comments

Comments
 (0)