Skip to content

Commit

Permalink
art: propagate callAgg function
Browse files Browse the repository at this point in the history
  • Loading branch information
naudinlo committed Aug 30, 2022
1 parent 43ed3eb commit 85a17f4
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 64 deletions.
14 changes: 10 additions & 4 deletions R/ds.boundedCovarianceDP.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source("R/utils.R")

#' @title Differentially private covariance
#'
#' @param x First input to the covariance
Expand All @@ -14,14 +16,18 @@
#' @return \code{ds.boundedCovarianceDP} returns a differentially private covariance
#' @export

ds.boundedCovarianceDP <- function(x, y, epsilon, x_min, x_max, y_min, y_max, datasources) {
ds.boundedCovarianceDP <- function(x, y, epsilon, x_min, x_max, y_min, y_max, type="split", datasources=NULL) {

if (is.null(datasources)) {
datasources <- DSI::datashield.connections_find()
}
if (!type %in% c("both", "split", "combine")) {
stop("Type must be one of 'both', 'split' or 'combine'")
}

cally <- paste0("boundedCovarianceDP(", x, ", ", y, ", ", epsilon, ", ", x_min, ", ", x_max, ", ", y_min, ", ", y_max, ")")
result <- DSI::datashield.aggregate(datasources, as.symbol(cally))
covariance.split <- callAggregationMethod(datasources, paste0("boundedCovarianceDP(", x, ", ", y, ", ", epsilon, ", ", x_min, ", ", x_max, ", ", y_min, ", ", y_max, ")"))

return(result)
if (type == "both") stop("Combine type not implemented")
if (type == "combine") stop("NotImplemented")
if (type == "split") return(list(Covariance.by.Study=covariance.split,Nstudies=Nstudies))
}
20 changes: 12 additions & 8 deletions R/ds.boundedMeanDP.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source("R/utils.R")

#' @title Differentially private mean
#'
#' @param input_data the input vector
Expand All @@ -24,16 +26,18 @@ ds.boundedMeanDP <- function(input_data, epsilon, lower_bound, upper_bound, type
if (is.null(datasources)) {
datasources <- DSI::datashield.connections_find()
}
if (!type %in% c("both", "split", "combine")) {
stop("Type must be one of 'both', 'split' or 'combine'")
}

cally <- paste0("boundedMeanDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")")
res.obj <- DSI::datashield.aggregate(datasources, as.symbol(cally))
mean.data <- callAggregationMethod(datasources, paste0("boundedMeanDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")"))

Nstudies <- length(datasources)
res.mat <- matrix(as.numeric(matrix(unlist(res.obj),nrow=Nstudies,byrow=TRUE)[,1:2]),nrow=Nstudies)

res.mat.combined <- (t(matrix(res.mat[,2]))%*%res.mat[,1])/sum(res.mat[,2])
mean.mat <- matrix(as.numeric(unlist(mean.data)),nrow=Nstudies,byrow=TRUE)
mean.split <- mean.mat[,1]
mean.combine <- ((t(matrix(mean.mat[,2]))%*%mean.mat[,1])/sum(mean.mat[,2]))[[1]]

if (type=="combine") return(list(Mean.by.Study=res.mat[,1],Nstudies=Nstudies))
if (type=="split") return(list(Global.Mean=res.mat.combined,Nstudies=Nstudies))
if (type=="both") return(list(Mean.by.Study=res.mat[,1],Global.Mean=res.mat.combined,Nstudies=Nstudies))
if (type=="combine") return(list(Global.Mean=mean.combine,Nstudies=Nstudies))
if (type=="split") return(list(Mean.by.Study=mean.split,Nstudies=Nstudies))
if (type=="both") return(list(Mean.by.Study=mean.split,Global.Mean=mean.combine,Nstudies=Nstudies))
}
15 changes: 11 additions & 4 deletions R/ds.boundedStandardDeviationDP.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source("R/utils.R")

#' @title Differentially private standard deviation
#'
#' @param input_data the input vector
Expand All @@ -11,14 +13,19 @@
#' @return \code{ds.boundedStandardDeviationDP} returns a differentially private standard deviation
#' @export

ds.boundedStandardDeviationDP <- function(input_data, epsilon, lower_bound, upper_bound, datasources=NULL) {
ds.boundedStandardDeviationDP <- function(input_data, epsilon, lower_bound, upper_bound, type="split", datasources=NULL) {

if (is.null(datasources)) {
datasources <- DSI::datashield.connections_find()
}
if (!type %in% c("both", "split", "combine")) {
stop("Type must be one of 'both', 'split' or 'combine'")
}

cally <- paste0("boundedStandardDeviationDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")")
result <- DSI::datashield.aggregate(datasources, as.symbol(cally))
standardDeviation.split <- callAggregationMethod(datasources, paste0("boundedStandardDeviationDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")"))

return(result)
return(standardDeviation.split)
if (type=="combine") stop("NotImplemented")
if (type=="split") return(list(StandardDeviation.by.Study=standardDeviation.split,Nstudies=Nstudies))
if (type=="both") stop("Combine type not implemented")
}
17 changes: 10 additions & 7 deletions R/ds.boundedSumDP.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source("R/utils.R")

#' @title Differentially private sum
#'
#' @param input_data the input vector
Expand All @@ -20,13 +22,14 @@ ds.boundedSumDP <- function(input_data, epsilon, lower_bound, upper_bound, type=
if (is.null(datasources)) {
datasources <- DSI::datashield.connections_find()
}
if (!type %in% c("both", "split", "combine")) {
stop("Type must be one of 'both', 'split' or 'combine'")
}

cally <- paste0("boundedSumDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")")
res <- DSI::datashield.aggregate(datasources, as.symbol(cally))

combined <- sum(unlist(res))
sum.split <- callAggregationMethod(datasources, paste0("boundedSumDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")"))
sum.combine <- sum(unlist(sum.split))

if (type=="combine") return(list(Sum.by.Study=res))
if (type=="split") return(list(Global.Sum=combined))
if (type=="both") return(list(Sum.by.Study=res,Global.Sum=combined))
if (type=="combine") return(list(Global.Sum=sum.combine))
if (type=="split") return(list(Sum.by.Study=sum.split))
if (type=="both") return(list(Sum.by.Study=sum.split,Global.Sum=sum.combine))
}
12 changes: 4 additions & 8 deletions R/ds.boundedVarianceDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ source("R/utils.R")
#' @return \code{ds.boundedVarianceDP} returns a differentially private covariance
#' @export

ds.boundedVarianceDP <- function(input_data, epsilon, lower_bound, upper_bound, datasources=NULL, type="combine") {
ds.boundedVarianceDP <- function(input_data, epsilon, lower_bound, upper_bound, type="combine", datasources=NULL) {

if (is.null(datasources)) {
datasources <- DSI::datashield.connections_find()
Expand All @@ -35,13 +35,9 @@ ds.boundedVarianceDP <- function(input_data, epsilon, lower_bound, upper_bound,
variance.combine <- computeVarCombine(Nstudies, Sum, SumSquares, Nvalid)
variance.split <- computeVarSplit(Nstudies, Sum, SumSquares, Nvalid)

if (type == "both") {
return(list(Variance.by.Study=variance.split,Global.Variance=variance.combine,Nstudies=Nstudies))
} else if (type == "combine") {
return(list(Global.Variance=variance.combine,Nstudies=Nstudies))
} else if (type == "split") {
return(list(Variance.by.Study=variance.split,Nstudies=Nstudies))
}
if (type == "both") return(list(Variance.by.Study=variance.split,Global.Variance=variance.combine,Nstudies=Nstudies))
if (type == "combine") return(list(Global.Variance=variance.combine,Nstudies=Nstudies))
if (type == "split") return(list(Variance.by.Study=variance.split,Nstudies=Nstudies))
}

computeVarCombine <- function(Nstudies, Sum, SumSquares, Nvalid) {
Expand Down
14 changes: 7 additions & 7 deletions R/ds.countDP.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source("R/utils.R")

#' @title Differentially private count
#'
#' @param input_data the input vector
Expand All @@ -19,12 +21,10 @@ ds.countDP <- function(input_data, epsilon, type="combine", datasources=NULL) {
datasources <- DSI::datashield.connections_find()
}

cally <- paste0("countDP(", input_data, ", ", epsilon, ")")
res <- DSI::datashield.aggregate(datasources, as.symbol(cally))

combined <- sum(unlist(res))
count.split <- callAggregationMethod(datasources, paste0("countDP(", input_data, ", ", epsilon, ")"))
count.combine <- sum(unlist(count.split))

if (type=="combine") return(list(Count.by.Study=res))
if (type=="split") return(list(Global.Count=combined))
if (type=="both") return(list(Count.by.Study=res,Global.Count=combined))
if (type=="combine") return(list(Global.Count=count.combine))
if (type=="split") return(list(Count.by.Study=count.split))
if (type=="both") return(list(Count.by.Study=count.split,Global.Count=count.combine))
}
17 changes: 10 additions & 7 deletions R/ds.maxDP.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source("R/utils.R")

#' @title Differentially private max
#'
#' @param input_data the input vector
Expand All @@ -20,13 +22,14 @@ ds.maxDP <- function(input_data, epsilon, lower_bound, upper_bound, type="combin
if (is.null(datasources)) {
datasources <- DSI::datashield.connections_find()
}
if (!type %in% c("both", "split", "combine")) {
stop("Type must be one of 'both', 'split' or 'combine'")
}

cally <- paste0("maxDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")")
res <- DSI::datashield.aggregate(datasources, as.symbol(cally))

combined <- max(unlist(res))
max.split <- callAggregationMethod(datasources, paste0("maxDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")"))
max.combine <- max(unlist(max.split))

if (type=="combine") return(list(Max.by.Study=res))
if (type=="split") return(list(Global.Max=combined))
if (type=="both") return(list(Max.by.Study=res,Global.Max=combined))
if (type=="combine") return(list(Global.Max=max.combine))
if (type=="split") return(list(Max.by.Study=max.split))
if (type=="both") return(list(Max.by.Study=max.split,Global.Max=max.combine))
}
14 changes: 10 additions & 4 deletions R/ds.medianDP.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source("R/utils.R")

#' @title Differentially private median
#'
#' @param input_data the input vector
Expand All @@ -11,14 +13,18 @@
#' @return \code{ds.medianDP} returns a differentially private median
#' @export

ds.medianDP <- function(input_data, epsilon, lower_bound, upper_bound, datasources=NULL) {
ds.medianDP <- function(input_data, epsilon, lower_bound, upper_bound, type="split", datasources=NULL) {

if (is.null(datasources)) {
datasources <- DSI::datashield.connections_find()
}
if (!type %in% c("both", "split", "combine")) {
stop("Type must be one of 'both', 'split' or 'combine'")
}

cally <- paste0("medianDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")")
result <- DSI::datashield.aggregate(datasources, as.symbol(cally))
median.split <- callAggregationMethod(datasources, paste0("medianDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")"))

return(result)
if (type == "both") stop("Combine type not implemented")
if (type == "combine") stop("NotImplemented")
if (type == "split") return(list(Median.by.Study=median.split,Nstudies=Nstudies))
}
18 changes: 11 additions & 7 deletions R/ds.minDP.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source("R/utils.R")

#' @title Differentially private min
#'
#' @param input_data the input vector
Expand All @@ -20,12 +22,14 @@ ds.minDP <- function(input_data, epsilon, lower_bound, upper_bound, type="combin
if (is.null(datasources)) {
datasources <- DSI::datashield.connections_find()
}
if (!type %in% c("both", "split", "combine")) {
stop("Type must be one of 'both', 'split' or 'combine'")
}

cally <- paste0("minDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")")
res <- DSI::datashield.aggregate(datasources, as.symbol(cally))

combined <- min(unlist(res))
min.split <- callAggregationMethod(datasources, paste0("minDP(", input_data, ", ", epsilon, ", ", lower_bound, ", ", upper_bound, ")"))
min.combine <- min(unlist(min.split))

if (type=="combine") return(list(Min.by.Study=res))
if (type=="split") return(list(Global.Min=combined))
if (type=="both") return(list(Min.by.Study=res,Global.Min=combined))}
if (type=="combine") return(list(Global.Min=min.combine))
if (type=="split") return(list(Min.by.Study=min.split))
if (type=="both") return(list(Min.by.Study=min.split,Global.Min=min.combine))
}
12 changes: 4 additions & 8 deletions R/ds.studentTestDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ source("R/utils.R")
#' @return \code{ds.studentTestDP} returns a differentially private student-test
#' @export

ds.studentTestDP <- function(datasources, x, y, epsilon, x_min, x_max, y_min, y_max, type="combine") {
ds.studentTestDP <- function(x, y, epsilon, x_min, x_max, y_min, y_max, type="combine", datasources=NULL) {
if (is.null(datasources)) {
datasources <- DSI::datashield.connections_find()
}
Expand All @@ -35,13 +35,9 @@ ds.studentTestDP <- function(datasources, x, y, epsilon, x_min, x_max, y_min, y_
studentTest.combine <- computeStudentTestCombine(Nstudies, res_x, res_y)
studentTest.split <- computeStudentTestSplit(Nstudies, res_x, res_y)

if (type == "both") {
return(list(StudentTest.by.Study=studentTest.split,Global.StudentTest=studentTest.combine,Nstudies=Nstudies))
} else if (type == "combine") {
return(list(Global.StudentTest=studentTest.combine,Nstudies=Nstudies))
} else if (type == "split") {
return(list(StudentTest.by.Study=studentTest.split,Nstudies=Nstudies))
}
if (type == "both") return(list(StudentTest.by.Study=studentTest.split,Global.StudentTest=studentTest.combine,Nstudies=Nstudies))
if (type == "combine") return(list(Global.StudentTest=studentTest.combine,Nstudies=Nstudies))
if (type == "split") return(list(StudentTest.by.Study=studentTest.split,Nstudies=Nstudies))
}

computeGlobalStats <- function(Nstudies, input) {
Expand Down

0 comments on commit 85a17f4

Please sign in to comment.