Skip to content

Commit ab0064c

Browse files
authored
Adding a new metric BRAS(Batch Removal Adapted Silhouette) (#62)
* working BRAS metric * use scib-metrics instead * remove comments
1 parent 5407bcf commit ab0064c

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/metrics/bras/config.vsh.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
__merge__: ../../api/comp_metric.yaml
2+
name: bras
3+
info:
4+
metrics:
5+
- name: bras
6+
label: BRAS
7+
summary: "Modified ASW metric for batch removal"
8+
description: |
9+
The BRAS (Batch Removal Adapted Silhouette) metric modifies the standard silhouette score to account for batch effects in single-cell data integration benchmarking.
10+
Instead of measuring how well a cell matches its biological label cluster compared to other clusters (as in regular silhouette), BRAS compares how well it matches its biological cluster in its own batch versus the same biological cluster in other batches.
11+
For each cells, BRAS computes the ai = average distance to cells with the same label in the same batch, and bi = the average distance to cells with the same label in different batches.
12+
It then uses ai and bi for the standard silhoueette formula.
13+
references:
14+
doi:
15+
- 10.1101/2025.01.21.634098
16+
links:
17+
documentation: https://github.com/ohlerlab/metrics_matter_manuscript_reproducibility/tree/master
18+
repository: https://github.com/ohlerlab/metrics_matter_manuscript_reproducibility/tree/master
19+
min: 0
20+
max: 1
21+
maximize: true
22+
resources:
23+
- type: python_script
24+
path: script.py
25+
- path: /src/utils/read_anndata_partial.py
26+
27+
engines:
28+
- type: docker
29+
image: openproblems/base_python:1.0.0
30+
setup:
31+
- type: python
32+
pypi:
33+
- scib-metrics==0.5.5
34+
35+
runners:
36+
- type: executable
37+
- type: nextflow
38+
directives:
39+
label: [midtime,midmem,midcpu]

src/metrics/bras/script.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import anndata as ad
2+
import sys
3+
import numpy as np
4+
import pandas as pd
5+
from scib_metrics import bras
6+
7+
## VIASH START
8+
par = {
9+
'input_integrated': 'resources_test/.../integrated.h5ad',
10+
'input_solution': 'resources_test/.../solution.h5ad',
11+
'output': 'output.h5ad',
12+
'input_integrated': 'resources_test/task_batch_integration/cxg_immune_cell_atlas/integrated_full.h5ad',
13+
'output': 'output.h5ad',
14+
}
15+
meta = {
16+
'name': 'bras'
17+
}
18+
## VIASH END
19+
20+
sys.path.append(meta["resources_dir"])
21+
from read_anndata_partial import read_anndata
22+
23+
print('Reading input files', flush=True)
24+
adata = read_anndata(par['input_integrated'], obs='obs', obsm='obsm', uns='uns')
25+
adata.obs = read_anndata(par['input_solution'], obs='obs').obs
26+
adata.uns |= read_anndata(par['input_solution'], uns='uns').uns
27+
28+
print('Compute metrics', flush=True)
29+
score = bras(
30+
X=adata.obsm['X_emb'],
31+
labels=adata.obs['cell_type'].to_numpy(),
32+
batch=adata.obs['batch'].to_numpy()
33+
)
34+
35+
print('Create output AnnData object', flush=True)
36+
output = ad.AnnData(
37+
uns={
38+
'dataset_id': adata.uns['dataset_id'],
39+
'normalization_id': adata.uns['normalization_id'],
40+
'method_id': adata.uns['method_id'],
41+
'metric_ids': [ meta['name'] ],
42+
'metric_values': [ score ]
43+
}
44+
)
45+
46+
print("Write output AnnData to file", flush=True)
47+
output.write_h5ad(par['output'], compression='gzip')

0 commit comments

Comments
 (0)