Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielaCorbetta committed Apr 25, 2024
1 parent 1e432ea commit ac8ca79
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 34 deletions.
Binary file added .DS_Store
Binary file not shown.
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Description: Builds prediction interval for cell type annotation using conformal
License: Artistic-2.0
LazyData: true
Depends:
R (>= 4.2.0),
SingleCellExperiment,
R (>= 4.2.0)
Suggests:
scRNAseq,
knitr,
Matrix,
rmarkdown,
BiocStyle
rmarkdown
Imports:
foreach,
igraph
VignetteBuilder: knitr
RoxygenNote: 7.3.1
Encoding: UTF-8
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
export(getConformalPredSets)
export(getHierarchicalPredSets)
importFrom(foreach,"%dopar%")
importFrom(foreach,foreach)
importFrom(igraph,V)
importFrom(igraph,degree)
importFrom(igraph,distances)
importFrom(stats,quantile)
7 changes: 4 additions & 3 deletions R/getConformalPredSets.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' @title Get prediction sets with split conformal inference
#' @description Let K be the total number of distinct cell type labels and n, m
#' the number of cells in the calibration and in the test data, respectively.
#' This function takes as input two matrices: a matrix \code{n \times K} and
#' a matrix \code{m \times K} with the estimated
#' This function takes as input two matrices: a matrix \code{n x K} and
#' a matrix \code{m x K} with the estimated
#' probabilities for each cell in the calibration and in the test data, respectively.
#' It returns a list with the prediction sets for each cell in the test data.
#'
Expand All @@ -14,6 +14,7 @@
#' @author Daniela Corbetta
#' @return The function \code{getConformalPredSets} returns a list of length equal to the number of cells in the test data.
#' Each element of the list contains the prediction set for that cell.
#' @importFrom stats quantile
#' @references For reference on split conformal prediction, refer to section 1 of
#' Angelopoulos, Anastasios N., and Stephen Bates. "A gentle introduction to conformal prediction and distribution-free uncertainty quantification." arXiv preprint arXiv:2107.07511 (2021).
#' @export
Expand All @@ -25,7 +26,7 @@ getConformalPredSets <- function(p.cal, p.test, y.cal, alpha){
# Get adjusted quantile
n <- nrow(p.cal)
q_level <- ceiling((n+1)*(1-alpha))/n
qhat <- quantile(s, q.level)
qhat <- quantile(s, q_level)

# Get prediction sets
prediction_sets <- p.test >= 1-qhat
Expand Down
12 changes: 8 additions & 4 deletions R/getHierarchicalPredSets.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,30 @@
#' @param y.cal a vector of length n with the labels of the cells in the calibration data
#' @param onto the considered section of the cell ontology
#' @param alpha a number between 0 and 1 that indicates the allowed miscoverage
#' @param lambda a vector of possible lambda values to be considered
#' @param lambdas a vector of possible lambda values to be considered
#' @author Daniela Corbetta
#' @return The function \code{getHierarchicalPredSets} returns a list of length equal to the number of cells in the test data.
#' Each element of the list contains the prediction set for that cell.
#' @references For reference on conformal risk control, see
#' Angelopoulos, Anastasios N., et al. "Conformal risk control." arXiv preprint arXiv:2208.02814 (2022).
#' @importFrom foreach %dopar%
#' @importFrom foreach foreach
#' @export


getHierarchicalPredSets <- function(p.cal, p.test, y.cal, onto, alpha, lambdas){
y.cal <- as.character(y.cal)
# Get prediction sets for each value of lambda for all the calibration data
sets <- foreach(lambda = lambdas) %dopar% {
j <- NULL
sets <- foreach(j = lambdas) %dopar% {
lapply(1:nrow(p.cal),
function(i) .predSets(lambda=lambda, pred=p.cal[i, ], onto=onto))}
function(i) .predSets(lambda=j, pred=p.cal[i, ], onto=onto))
}

# Get the loss table (ncal x length(lambda) table with TRUE\FALSE)
loss <- sapply(1:length(lambdas), function(lambda) {
sapply(seq_along(y.cal), function(i) {
!(y.cal[i] %in% prediction[[lambda]][[i]])
!(y.cal[i] %in% sets[[lambda]][[i]])
})
})

Expand Down
4 changes: 2 additions & 2 deletions R/predSets.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
# Select the most external node (i.e. the one with smallest score)
sel_node <- names(sel_scores)[length(sel_scores)]
# Add also the subgraphs we would have obtained with smaller lambda
selected <- c(lapply(anc[round(scores, 15) <= lambda], function(x) children(node = x, onto = onto)),
list(children(sel_node, onto)))
selected <- c(lapply(anc[round(scores, 15) <= lambda], function(x) .children(node = x, onto = onto)),
list(.children(sel_node, onto)))

return(Reduce(union, selected))
}
8 changes: 4 additions & 4 deletions man/dot-pred_sets.Rd → man/dot-predSets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/getConformalPredSets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/getHierarchicalPredSets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions man/hello.Rd

This file was deleted.

0 comments on commit ac8ca79

Please sign in to comment.