Skip to content

Commit

Permalink
Merge pull request #102 from SpatialHackathon/metric_lisi_kbiharie
Browse files Browse the repository at this point in the history
Metric LISI Score
  • Loading branch information
niklasmueboe authored Dec 13, 2023
2 parents 72f30cd + 16fec5b commit 9576f70
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
90 changes: 90 additions & 0 deletions metric/LISI/LISI.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env Rscript

# Author_and_contribution: Niklas Mueller-Boetticher; created template
# Author_and_contribution: Kirti Biharie; implemented LISI score

suppressPackageStartupMessages(library(optparse))

option_list <- list(
make_option(
c("-l", "--labels"),
type = "character", default = NULL,
help = "Labels from domain clustering."
),
make_option(
c("-g", "--ground_truth"),
type = "character", default = NA,
help = "Groundtruth labels."
),
make_option(
c("-e", "--embedding"),
type = "character", default = NA,
help = "Embedding of points in latent space. Potential usage for metrics without groundtruth."
),
# format should be json
make_option(
c("-c", "--config"),
type = "character", default = NA,
help = "Optional config file (json) used to pass additional parameters."
),
make_option(
c("-o", "--out_file"),
type = "character", default = NULL,
help = "Output file."
)
)

description <- "Calculate LISI Score"

opt_parser <- OptionParser(
usage = description,
option_list = option_list
)
opt <- parse_args(opt_parser)

# Use these filepaths as input
label_file <- opt$labels

if (!is.na(opt$ground_truth)) {
groundtruth_file <- opt$ground_truth
}
if (!is.na(opt$embedding)) {
embedding_file <- opt$embedding
}
if (!is.na(opt$config)) {
config_file <- opt$config
}


## Your code goes here
library(lisi)
library(rjson)

if (is.na(opt$ground_truth)) {
stop("Groundtruth labels needed to calculate the LISI Score")
}

if (is.na(opt$embedding)) {
stop("Embeddings needed to calculate the LISI Score")
}

if (is.na(opt$config)) {
stop("Config file not provided")
}

ground_truth <- read.delim(groundtruth_file, sep="\t", row.names=1)
embeddings <- read.delim(embedding_file, sep="\t", row.names=1)
config <- fromJSON(file=config_file)

common_index <- intersect(rownames(ground_truth), rownames(embeddings))
ground_truth <- ground_truth[common_index,,drop=FALSE]
embeddings <- embeddings[common_index,,drop=FALSE]

metric <- mean(compute_lisi(embeddings, ground_truth, "label", perplexity=config$perplexity)[,"label"])

## Write output
outfile <- file(opt$out_file)
dir.create(dirname(opt$out_file), showWarnings = FALSE, recursive = TRUE)

writeLines(format(metric, digits = 6, scientific = TRUE), outfile)
close(outfile)
8 changes: 8 additions & 0 deletions metric/LISI/LISI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
channels:
- conda-forge
- bioconda
dependencies:
- r-base=4.3.1
- r-optparse=1.7.3
- r-lisi=1.0
- r-rjson=0.2.21
1 change: 1 addition & 0 deletions metric/LISI/config/config_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"perplexity": 15}

0 comments on commit 9576f70

Please sign in to comment.