Skip to content

Commit

Permalink
Prevents removing only common subsumer with resnik_similarity
Browse files Browse the repository at this point in the history
Fixes #239
  • Loading branch information
johnbradley committed Jan 13, 2023
1 parent 81cc6fd commit 90eeb07
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions R/semsim.R
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ resnik_similarity <- function(subsumer_mat = NA, terms = NULL, ...,
if (any(rowsToRemove)) {
wt <- wt[! rowsToRemove]
subsumer_mat <- subsumer_mat[! rowsToRemove,]
jaccard_sim <- jaccard_similarity(subsumer_mat)
if (any(is.nan(jaccard_sim) || jaccard_sim == 0)) {
stop("Subsumer matrix contained an only common subsumer between two terms with frequency of zero.", call. = FALSE)
}
}
# we assume we got frequencies, turn into IC
wt <- -log(wt, base = base)
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-semsim.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ test_that("Resnik similarity", {
testthat::expect_equivalent(diag(sm.ic), termICs)
})

test_that("Resnik similarity prevents removing only common subsumers", {
# term frequency function that results in 0 frequencing for all terms removing common subsumers
term_freq_zero <- function(x) { rep(0, length(x))}
phens <- get_phenotypes("maxilla", taxon = "Cyprinidae")
subs.mat <- subsumer_matrix(phens$id, .colnames = "label", .labels = phens$label,
preserveOrder = TRUE)
expect_error(resnik_similarity(subs.mat, wt=term_freq_zero),
"Subsumer matrix contained an only common subsumer between two terms with frequency of zero.")
})

test_that("profile similarity with Jaccard", {
tl <- c("pelvic fin", "pectoral fin", "forelimb", "hindlimb", "dorsal fin", "caudal fin")
tt <- sapply(tl, get_term_iri, as = "anatomy", exactOnly = TRUE)
Expand Down

0 comments on commit 90eeb07

Please sign in to comment.