Fixing few bugs

This commit is contained in:
Roland Hollós 2019-02-14 18:54:43 +01:00
parent d00d513949
commit 9363ef8c9f
6 changed files with 43 additions and 50 deletions

View File

@ -34,6 +34,7 @@ export(updateMusoMapping)
import(ggplot2) import(ggplot2)
import(utils) import(utils)
importFrom(Rcpp,evalCpp) importFrom(Rcpp,evalCpp)
importFrom(data.table,data.table)
importFrom(data.table,fread) importFrom(data.table,fread)
importFrom(digest,digest) importFrom(digest,digest)
importFrom(dplyr,'%>%') importFrom(dplyr,'%>%')

View File

@ -143,7 +143,7 @@ readValuesFromFile <- function(epc, linums){
#' readMeasuredMuso #' readMeasuredMuso
#' #'
#' MuSo data reader #' MuSo data reader
#' @importFrom data.table fread #' @importFrom data.table fread data.table
#' @export #' @export
readMeasuredMuso <- function(inFile, readMeasuredMuso <- function(inFile,

View File

@ -26,12 +26,10 @@
#' @importFrom magrittr '%>%' #' @importFrom magrittr '%>%'
#' @importFrom gridExtra grid.arrange #' @importFrom gridExtra grid.arrange
#' @export #' @export
optiMuso <- function(measuredDataFile, parameters = NULL, optiMuso <- function(measuredData, parameters = NULL, startDate,
sep = ",", startDate,
endDate, formatString = "%Y-%m-%d", endDate, formatString = "%Y-%m-%d",
naString = NULL, leapYear = TRUE, leapYear = TRUE,
filterCol = NULL, filterVal = 1, dataVar, outLoc = "./calib",
selVar, outLoc = "./calib",
preTag = "cal-", preTag = "cal-",
settings = NULL, settings = NULL,
outVars = NULL, outVars = NULL,
@ -44,8 +42,8 @@ optiMuso <- function(measuredDataFile, parameters = NULL,
}, },
calPar = 3009) calPar = 3009)
{ {
measuredData <- readMeasuredMuso(inFile = measuredDataFile, sep = sep, selVar = selVar,filterCol = filterCol, filterVal = filterVal) dataCol <- grep(dataVar, colnames(measuredData))
if(is.null(parameters)){ if(is.null(parameters)){
parameters <- tryCatch(read.csv("parameters.csv", stringsAsFactor=FALSE), error = function (e) { parameters <- tryCatch(read.csv("parameters.csv", stringsAsFactor=FALSE), error = function (e) {
stop("You need to specify a path for the parameters.csv, or a matrix.") stop("You need to specify a path for the parameters.csv, or a matrix.")
@ -102,7 +100,7 @@ optiMuso <- function(measuredDataFile, parameters = NULL,
unlink unlink
randValues <- randVals[[2]] randValues <- randVals[[2]]
settings$calibrationPar <- randVals[[1]] settings$calibrationPar <- randVals[[1]]
list2env(alignData(measuredData,dataCol = 8,modellSettings = settings,startDate = startDate,endDate = endDate,leapYear = FALSE),envir=environment()) list2env(alignData(measuredData,dataCol = dataCol,modellSettings = settings,startDate = startDate,endDate = endDate,leapYear = FALSE),envir=environment())
modellOut <- numeric(iterations + 1) # single variable solution modellOut <- numeric(iterations + 1) # single variable solution
origModellOut <- calibMuso(settings=settings,silent=TRUE) origModellOut <- calibMuso(settings=settings,silent=TRUE)
@ -140,7 +138,7 @@ optiMuso <- function(measuredDataFile, parameters = NULL,
} }
ggsave(plotName,grid.arrange(grobs = p, ncol = floor(sqrt(ncol(preservedCalib)-1))),dpi = 600) ggsave(plotName,grid.arrange(grobs = p, ncol = floor(sqrt(ncol(preservedCalib)-1))),dpi = 600)
write.csv(preservedCalib,"preservedCalib.csv")
return(preservedCalib[preservedCalib[,"likelihood"]==max(preservedCalib[,"likelihood"]),]) return(preservedCalib[preservedCalib[,"likelihood"]==max(preservedCalib[,"likelihood"]),])
} }

View File

@ -247,40 +247,33 @@ plotMuso <- function(settings = NULL, variable = 1,
#' debugging=FALSE, keepEpc=FALSE, #' debugging=FALSE, keepEpc=FALSE,
#' logfilename=NULL, aggressive=FALSE, #' logfilename=NULL, aggressive=FALSE,
#' leapYear=FALSE, export=FALSE) #' leapYear=FALSE, export=FALSE)
#' @import ggplot2 #' @importFrom ggplot2 ggplot geom_line geom_point aes aes_string labs theme element_blank
#' @export #' @export
plotMusoWithData <- function(csvFile, variable, NACHAR=NA, settings=NULL, sep=",", savePlot=NULL,colour=c("black","blue"), calibrationPar=NULL, parameters=NULL){ plotMusoWithData <- function(mdata, plotName=NULL,
if(!is.na(NACHAR)){ startDate, endDate,
warning("NACHAR is not implemented yet") colour=c("black","blue"),dataVar, modelVar, settings = setupMuso(), silent = TRUE){
}
if(is.null(settings)){
settings <- setupMuso()
}
numberOfYears <- settings$numYears
startYear <- settings$startYear
yearVec <- seq(from = startYear, length=numberOfYears,by=1)
dataCol<- grep(paste0("^",dataVar,"$"), colnames(mdata))
data <- read.table(csvFile,header = TRUE, sep = ",") %>% selVar <- grep(modelVar,(settings$dailyVarCodes))+4
select(variable)
baseData <- calibMuso(settings,silent=TRUE) %>%
as.data.frame() %>%
rownames_to_column("date") %>%
mutate(date2=date,date=as.Date(date,"%d.%m.%Y"),yearDay=rep(1:365,numberOfYears)) %>%
separate(date2,c("day","month","year"),sep="\\.")
baseData <- cbind(baseData,data)
colnames(baseData)[ncol(baseData)] <- "measuredData"
p <- baseData %>% list2env(alignData(mdata, dataCol = dataCol,
ggplot(aes_string("date",variable)) + modellSettings = settings,
startDate = startDate,
endDate = endDate, leapYear = FALSE),envir=environment())
## measuredData is created
baseData <- calibMuso(settings = settings, silent = silent, prettyOut = TRUE)[modIndex,]
baseData[,1] <- as.Date(baseData[,1],format = "%d.%m.%Y")
selVarName <- colnames(baseData)[selVar]
p <- baseData %>%
ggplot(aes_string("date",selVarName)) +
geom_line(colour=colour[1]) + geom_line(colour=colour[1]) +
geom_point(colour=colour[2], aes(date,measuredData)) + geom_point(colour=colour[2], aes(date,measuredData)) +
labs(y = paste0(variable,"_measured"))+ labs(y = paste0(selVarName,"_measured"))+
theme(axis.title.x = element_blank()) theme(axis.title.x = element_blank())
if(!is.null(savePlot)){ if(!is.null(plotName)){
ggsave(savePlot,p) ggsave(plotName,p)
return(p) return(p)
} else { } else {
return(p) return(p)

View File

@ -5,12 +5,13 @@
\title{optiMuso} \title{optiMuso}
\usage{ \usage{
optiMuso(measuredDataFile, parameters = NULL, sep = ",", startDate, optiMuso(measuredDataFile, parameters = NULL, sep = ",", startDate,
endDate, formatString = "\%Y-\%m-\%d", naString = NULL, endDate, formatString = "\%Y-\%m-\%d",
leapYear = TRUE, filterCol = NULL, filterVal = 1, selVar, naString = getOption("datatable.na.strings", "NA"), leapYear = TRUE,
outLoc = "./calib", preTag = "cal-", settings = NULL, filterCol = NULL, filterVal = 1, selVar, outLoc = "./calib",
outVars = NULL, iterations = 30, skipSpinup = TRUE, preTag = "cal-", settings = NULL, outVars = NULL,
constrains = NULL, plotName = "calib.jpg", likelihood = function(x, iterations = 30, skipSpinup = TRUE, constrains = NULL,
y) { exp(-sqrt(mean((x - y)^2))) }, calPar = 3009) plotName = "calib.jpg", likelihood = function(x, y) {
exp(-sqrt(mean((x - y)^2))) }, calPar = 3009)
} }
\arguments{ \arguments{
\item{measuredDataFile}{a} \item{measuredDataFile}{a}

View File

@ -11,18 +11,18 @@ logfilename=NULL, aggressive=FALSE,
leapYear=FALSE, export=FALSE) leapYear=FALSE, export=FALSE)
} }
\arguments{ \arguments{
\item{csvFile}{This specifies the filename of the measurements. It must contain a header. Typically this is a CSV file.}
\item{variable}{The name of the output variable to plot}
\item{NACHAR}{This is not implemented yet}
\item{settings}{RBBGCMuso uses variables that define the entire simulation environment. Those environment variables include the name of the INI files, the name of the meteorology files, the path to the model executable and its file name, the entire output list, the entire output variable matrix, the dependency rules for the EPC parameters etc. Using the runMuso function RBBGCMuso can automatically create those environment variables by inspecting the files in the working directory (this happens through the setupMuso function). It means that by default model setup is performed automatically in the background and the user has nothing to do. With this settings parameter we can force runMuso to skip automatic environment setup as we provide the environment settings to runMuso. In a typical situation the user can skip this option.} \item{settings}{RBBGCMuso uses variables that define the entire simulation environment. Those environment variables include the name of the INI files, the name of the meteorology files, the path to the model executable and its file name, the entire output list, the entire output variable matrix, the dependency rules for the EPC parameters etc. Using the runMuso function RBBGCMuso can automatically create those environment variables by inspecting the files in the working directory (this happens through the setupMuso function). It means that by default model setup is performed automatically in the background and the user has nothing to do. With this settings parameter we can force runMuso to skip automatic environment setup as we provide the environment settings to runMuso. In a typical situation the user can skip this option.}
\item{sep}{This is the separator symbol used in the measurement file (that is supposed to be a delimited text file)} \item{sep}{This is the separator symbol used in the measurement file (that is supposed to be a delimited text file)}
\item{savePlot}{It it is specified, the plot will be saved in a graphical format specified by the immanent extension. For example, it the savePlot is set to image01.png then a PNG graphics file will be created.} \item{savePlot}{It it is specified, the plot will be saved in a graphical format specified by the immanent extension. For example, it the savePlot is set to image01.png then a PNG graphics file will be created.}
\item{variable}{The name of the output variable to plot}
\item{NACHAR}{This is not implemented yet}
\item{csvFile}{This specifies the filename of the measurements. It must contain a header. Typically this is a CSV file.}
\item{calibrationPar}{You might want to change some parameters in your EPC file before running the model. The function offers possibility for this without editing the EPC file. In this situation you have to select the appropirate model parameters first. You can refer to these parameters with the number of the line in the EPC file. Indexing of lines start from one. You should use a vector for this referencing like c(1,5,8)} \item{calibrationPar}{You might want to change some parameters in your EPC file before running the model. The function offers possibility for this without editing the EPC file. In this situation you have to select the appropirate model parameters first. You can refer to these parameters with the number of the line in the EPC file. Indexing of lines start from one. You should use a vector for this referencing like c(1,5,8)}
\item{parameters}{Using the function it is possible to change some of the EPC parameters prior to model execution. This can be achieved with this option. In the parameters variable you have set the row indices of the variables that you wish to change. In this parameters you can give an exact value for them in a vector form like c(1,2,3,4).} \item{parameters}{Using the function it is possible to change some of the EPC parameters prior to model execution. This can be achieved with this option. In the parameters variable you have set the row indices of the variables that you wish to change. In this parameters you can give an exact value for them in a vector form like c(1,2,3,4).}