-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
origin
committed
Mar 14, 2023
1 parent
31cb46e
commit c0b289f
Showing
56 changed files
with
562 additions
and
519 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
#' @title Transform every 1D prototype into a column matrix | ||
#' | ||
#' @param gamma a list of prototypes | ||
#' @param prototypes a list of prototypes | ||
#' | ||
#' @return A list of matrices prototypes | ||
#' @export | ||
#' | ||
#' @examples | ||
#' oned_to_matrix(list(1:5, runif(5), rep(0,5))) | ||
oned_to_matrix = function(gamma){ | ||
gamma = lapply(1:length(gamma), function(j){ | ||
if(is.null(dim(gamma[[j]])) | length(dim(gamma[[j]])) == 1){t(as.matrix(gamma[[j]]))} | ||
else{gamma[[j]]}}) | ||
return(gamma) | ||
oned_to_matrix = function(prototypes){ | ||
prototypes = lapply(1:length(prototypes), function(j){ | ||
if(is.null(dim(prototypes[[j]])) | length(dim(prototypes[[j]])) == 1){t(as.matrix(prototypes[[j]]))} | ||
else{prototypes[[j]]}}) | ||
return(prototypes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#' @title Compute the distance between a point and its nearest prototype, returning this distance and the associated cell number | ||
#' | ||
#' @param x A point in the space of the data elements | ||
#' @param prototypes A set of prototypes | ||
#' @param distance_func A function computing a distance between two data elements | ||
#' | ||
#' @return The distance between a point and its nearest centroid | ||
#' @export | ||
#' | ||
#' @examples | ||
#' distance_to_prototypes(array(1:9, dim = c(3,3)), list(array(10, dim = c(3,3)), | ||
#' array(5, dim = c(3,3)), array(6, dim = c(3,3))), | ||
#' distance_func = function(A1,A2){return(sqrt(sum((A1-A2)^2)))}) | ||
distance_to_prototypes = function(x, prototypes, distance_func = function(A1,A2){return(sqrt(sum((A1-A2)^2)))}){ | ||
distance = Vectorize(function(k){distance_func(x, prototypes[[k]])})(1:length(prototypes)) | ||
return(list(cellule = which.min(distance), dist = min(distance))) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#' @title Check that all the elements in a list of prototypes are distinct | ||
#' | ||
#' @param prototypes A list of prototypes | ||
#' | ||
#' @return A boolean indicating whether the elements in prototypes are distinct | ||
#' @export | ||
#' | ||
#' @examples | ||
#' distinct_prototypes(list(1,2,34,1)) | ||
distinct_prototypes = function(prototypes){ | ||
for(i in 1:(length(prototypes)-1)){ | ||
dist_prototypes = distance_to_prototypes(prototypes[[i]], lapply((i+1):length(prototypes), function(j){prototypes[[j]]}))$dist | ||
if(dist_prototypes == 0){return(FALSE)} | ||
} | ||
return(TRUE) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,32 @@ | ||
#' @title Compututation of the empirical quantization error | ||
#' | ||
|
||
#' @param outputs The output samples that need to be quantized. Useful only if cell_numbers == NULL. | ||
#' @param gamma A set of prototypes. Useful only if cell_numbers == NULL. | ||
#' @param density_ratio density_ratio indicates the weight fX/g of each output | ||
#' @param distance_func A function computing a distance between two elements in the output spaces. Useful only if cell_numbers == NULL. | ||
|
||
#' @param data The data that needs to be quantized. Useful only if cell_numbers == NULL. | ||
#' @param prototypes A set of prototypes. Useful only if cell_numbers == NULL. | ||
#' @param density_ratio density_ratio indicates the weight fX/g of each data element. | ||
#' @param distance_func A function computing a distance between two data elements. Useful only if cell_numbers == NULL. | ||
#' @param batch A boolean indicating whether the computations must be performed by batch or not. If TRUE, data, cell_numbers and density_ratio must be lists. Default is False. | ||
#' | ||
#' @return An estimation of the quantization error | ||
#' @export | ||
#' @import abind | ||
#' @examples | ||
#' gamma = list(array(10, dim = c(3,3)), array(5, dim = c(3,3)), array(6, dim = c(3,3))) | ||
#' outputs = array(runif(9*20)*20, dim = c(3,3,20)) | ||
#' prototypes = list(array(10, dim = c(3,3)), array(5, dim = c(3,3)), array(6, dim = c(3,3))) | ||
#' data = array(runif(9*20)*20, dim = c(3,3,20)) | ||
#' density_ratio = rep(1,20) | ||
#' distance_func = function(A1,A2){return(sqrt(sum((A1-A2)^2)))} | ||
#' quanti_error(outputs = outputs, gamma = gamma, density_ratio = density_ratio, | ||
#' quanti_error(data = data, prototypes = prototypes, density_ratio = density_ratio, | ||
#' distance_func = distance_func) | ||
quanti_error = function(outputs, gamma, density_ratio, distance_func = function(A1,A2){return(sqrt(sum((A1-A2)^2)))}){ | ||
quanti_error = function(data, prototypes, density_ratio, batch = FALSE, distance_func = function(A1,A2){return(sqrt(sum((A1-A2)^2)))}){ | ||
drop = "selected" | ||
if(sum(dim(gamma[[1]]) == 1) == length(dim(gamma[[1]]))){drop = FALSE} | ||
distances = Vectorize(function(it){distance_to_gamma(x = asub(x = outputs, dims = length(dim(outputs)), idx = it,drop = drop), gamma = gamma, distance_func = distance_func)$dist})(1:dim(outputs)[length(dim(outputs))]) | ||
return(sqrt(mean(distances^2*density_ratio))) | ||
if(sum(dim(prototypes[[1]]) == 1) == length(dim(prototypes[[1]]))){drop = FALSE} | ||
if(!batch){ | ||
distances = Vectorize(function(it){distance_to_prototypes(x = asub(x = data, dims = length(dim(data)), idx = it,drop = drop), prototypes = prototypes, distance_func = distance_func)$dist})(1:dim(data)[length(dim(data))]) | ||
res = sqrt(mean(distances^2*density_ratio)) | ||
} | ||
else{ | ||
distances = as.numeric(sapply(1:length(data), function(b){Vectorize(function(it){distance_to_prototypes(x = asub(x = data[[b]], dims = length(dim(data[[b]])), idx = it,drop = drop), prototypes = prototypes, distance_func = distance_func)$dist})(1:dim(data[[b]])[length(dim(data[[b]]))])})) | ||
res = sqrt(mean(distances^2*unlist(density_ratio))) | ||
} | ||
return(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.