WSL/SLF GitLab Repository

Commit 86a2706d authored by Adrien Michel's avatar Adrien Michel
Browse files

Updated the wrapper function for quantile application (io is now in the...

Updated the wrapper function for quantile application (io is now in the wrapper), slight bug fix in the R implementation
parent e218be22
......@@ -8,6 +8,7 @@ apply.qm.internal <- function(data, doy.selection, source.path, target.path,
`%do%` <- foreach::`%do%`
}
data.corrected <- data
pixels <- c(1:length(data$data[,1]))
for(doy in names(doy.selection)) {
doy.subset <- doy.selection[[doy]]
PDF.source <- as.matrix(data.table::fread(paste0(source.path,
......@@ -32,7 +33,7 @@ apply.qm.internal <- function(data, doy.selection, source.path, target.path,
i <- 1
for(d in doy.subset) {
quantile <- which(PDF.source[p,] > data$data[p,d])[1]-1
quantile <- boundQuantile(quantile, length(PDF.source[p,]))
quantile <- bound.quantile(quantile, length(PDF.source[p,]))
corrected[i] <- data$data[p,d] - PDF.source[p,quantile] + PDF.dest[p,quantile]
i <- i+1
}
......@@ -58,26 +59,26 @@ apply.qm.internal <- function(data, doy.selection, source.path, target.path,
#' \code{apply.model} applies a trained QM model (see
#' \code{\link{compute.quantiles()}}) to the dataset given as input.
#'
#' @param data Dataset on which the QM model should be applied. Dataset
#' should be in a vectdata format (see \code{\link{vectorize.list()}})
#' @param source.path Directory containing the quantile distributions of the
#' source training dataset (see \code{\link{vectorize.list()}})
#' @param source.path Directory containing the quantile distributions of the
#' target training dataset (see \code{\link{vectorize.list()}})
#' @param source.path R or cpp to choos3 between the R or the C++ implementation
#' (see \code{\link{apply.qm.internal()}} and \code{\link{computeQuantilesCpp()}}).
#' @param in.data.file Dataset on which the QM model should be applied. Dataset
#' should be a RDS file in a vectdata format (see \code{\link{vectorize.list()}})
#' @param in.data.file Path of the file to write the corrected dataset. Dataset
#' will be a RDS file in a vectdata format (see \code{\link{vectorize.list()}})
#' @param source.path Directory containing the directories with quantile distributions of the
#' training and target datasets (see \code{\link{vectorize.list()}})
#' @param type R or cpp to choose between the R or the C++ implementation
#' (see \code{\link{apply.qm.internal()}} and \code{\link{applyQMCpp()}}).
#' C++ implementation is faster and recommended, given the package has been
#' installed with OpenMP support.
#' @param ncores number of cores to be used
#'
#' @return Corrected dataset. same vectdata object as the \code{data} input
#' parameter, but with the \code{data} field corrected using the QM model.
#'
#' @author Adrien Michel, 2021 (WSL/SLF)
#' @export
apply.model <- function(data, source.path, target.path, type, ncores)
apply.model <- function(in.data.file, out.data.file, quantile.path, type, ncores)
{
print("[I] Reading data")
data <- readRDS(in.data.file)
print("[I] Computing days of the year")
doys=as.integer(strftime(data$time.date, format = "%j"))
doys.unique=sort(unique(doys))
`%do%` <- foreach::`%do%`
......@@ -86,25 +87,34 @@ apply.model <- function(data, source.path, target.path, type, ncores)
}
names(doy.selection)=doys.unique
print("[I] Correcting data")
start.time <- Sys.time()
if(type == "cpp") {
corrected.swe <- applyQMCpp(data$data,
doySelection = doy.selection,
sourcePath = source.path,
targetPath = target.path,
sourcePath = paste0(quantile.path,"/training_quantiles"),
targetPath = paste0(quantile.path,"/target_quantiles"),
ncores = ncores)
data.corrected <- data
data.corrected$data <- matrix(corrected.swe, ncol=dim(data$data)[2])
} else if (type == "R"){
data.corrected <- apply.qm.internal(data = data,
doy.selection = doy.selection,
source.path = source.path,
target.path = target.path,
source.path = paste0(quantile.path,"/training_quantiles"),
target.path = paste0(quantile.path,"/target_quantiles"),
ncores = ncores)
} else {
stop("Type sould be either cpp or R")
}
return(data.corrected)
print(paste("[I] Correction computed in",
as.numeric(round(difftime(Sys.time(),start.time, units = "mins"),2)),
"minutes"))
saveRDS(data.corrected,out.data.file)
print("[I] Cleaning memory")
rm(list=ls())
invisible(gc())
}
......
......@@ -4,22 +4,21 @@
\alias{apply.model}
\title{Apply the trained QM model to a given dataset}
\usage{
apply.model(data, source.path, target.path, type, ncores)
apply.model(in.data.file, out.data.file, quantile.path, type, ncores)
}
\arguments{
\item{data}{Dataset on which the QM model should be applied. Dataset
should be in a vectdata format (see \code{\link{vectorize.list()}})}
\item{in.data.file}{Path of the file to write the corrected dataset. Dataset
will be a RDS file in a vectdata format (see \code{\link{vectorize.list()}})}
\item{source.path}{R or cpp to choos3 between the R or the C++ implementation
(see \code{\link{apply.qm.internal()}} and \code{\link{computeQuantilesCpp()}}).
\item{type}{R or cpp to choose between the R or the C++ implementation
(see \code{\link{apply.qm.internal()}} and \code{\link{applyQMCpp()}}).
C++ implementation is faster and recommended, given the package has been
installed with OpenMP support.}
\item{ncores}{number of cores to be used}
}
\value{
Corrected dataset. same vectdata object as the \code{data} input
parameter, but with the \code{data} field corrected using the QM model.
\item{source.path}{Directory containing the directories with quantile distributions of the
training and target datasets (see \code{\link{vectorize.list()}})}
}
\description{
\code{apply.model} applies a trained QM model (see
......
......@@ -136,13 +136,13 @@ NumericVector applyQMCpp(const Rcpp::NumericMatrix swe,
const std::string doyNumber = doyRealNumber_cpp[doy];
// Read QM files
std::vector< std::vector <double>> sourcePDF;
if(!getFileContent(sourcePath+"quantiles_"+doyNumber+".txt",
if(!getFileContent(sourcePath+"/quantiles_"+doyNumber+".txt",
sourcePDF, num_pixels)){
file_io_error = true;
continue;
}
std::vector< std::vector <double>> targetPDF;
if(!getFileContent(targetPath+"quantiles_"+doyNumber+".txt",
if(!getFileContent(targetPath+"/quantiles_"+doyNumber+".txt",
targetPDF, num_pixels)){
file_io_error = true;
continue;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment