Skip to content

Commit

Permalink
Fixes Open-Systems-Pharmacology#876 introduce default statistics Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pchelle committed Oct 13, 2022
1 parent ca945f9 commit c080373
Show file tree
Hide file tree
Showing 29 changed files with 931 additions and 165 deletions.
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

export("geomean*sd")
export("geomean/sd")
export(AggregationConfiguration)
export(AllAvailableTasks)
export(ApplicationRanges)
Expand All @@ -23,6 +25,7 @@ export(SensitivityPlotSettings)
export(SimulationSet)
export(StandardPlotTasks)
export(StandardSimulationTasks)
export(StatisticsTypes)
export(Task)
export(activateWorkflowTasks)
export(addFigureChunk)
Expand Down Expand Up @@ -57,6 +60,7 @@ export(defaultSimulationNumberOfCores)
export(defaultVariationRange)
export(evalDataFilter)
export(generateResultFileNames)
export(geomean)
export(getAnchorName)
export(getDefaultDemographyXParameters)
export(getDefaultPKParametersXParameters)
Expand All @@ -74,6 +78,7 @@ export(getResiduals)
export(getSimulationDescriptor)
export(getSimulationParameterDisplayPaths)
export(getSimulationResultFileNames)
export(getStatisticsFromType)
export(getTaskInputs)
export(getWorkflowParameterDisplayPaths)
export(getXParametersForDemogrpahyPlot)
Expand Down Expand Up @@ -117,7 +122,9 @@ export(plotMassBalanceCumulativeTimeProfile)
export(plotMassBalancePieChart)
export(plotMassBalanceTimeProfile)
export(plotMeanTimeProfile)
export(plotMeanTimeProfileLog)
export(plotPopulationTimeProfile)
export(plotPopulationTimeProfileLog)
export(prettyCaption)
export(ratioBoxplot)
export(readObservedDataFile)
Expand All @@ -135,6 +142,7 @@ export(setDefaultPlotFormat)
export(setDefaultStairstep)
export(setDefaultTheme)
export(setDefaultThemeFromJson)
export(setDefaultTimeProfileStatistics)
export(setPlotFormat)
export(setSimulationDescriptor)
export(setWatermarkConfiguration)
Expand Down
3 changes: 3 additions & 0 deletions R/messages.R
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ messages <- list(
},
completedOnCores = function(objectNames) {
paste0(objectNames, " successfully completed on all cores")
},
negativeDataRemoved = function(n) {
paste0(n, " negative or null values were removed from logarithmic plot")
}
)

Expand Down
130 changes: 129 additions & 1 deletion R/reportingengine-env.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ reEnv$ddiRatioSubsetsDictionary <- list(
# Default value for a scale factor used in a parallel simulation. The product of this scale factor and the number of allowable cores (allowedCores) sets the maximum number of simulations that may be run on one core.
reEnv$defaultMaxSimulationsPerCore <- 2


#' @title setWatermarkConfiguration
#' @description Set default watermark configuration for current theme
#' @param watermark character or \code{Label} class object from `tlf` package
Expand Down Expand Up @@ -250,3 +249,132 @@ setDefaultAutoAxisLimitMargin <- function(margin) {
validateIsNumeric(margin)
reEnv$autoAxisLimitMargin <- margin
}

#' @title StatisticsTypes
#' @description List of available statistic types summarizing data for time profile plots
#' \itemize{
#' \item `"2.5th-97.5th Percentiles"` summarizes data using median, 2.5th and 97.5th percentiles
#' \item `"5th-95th Percentiles"` summarizes data using median, 5th and 95th percentiles
#' \item `"10th-90th Percentiles"` summarizes data using median, 10th and 90th percentiles
#' \item `"Geometric mean"` summarizes data using geometric mean and mean */ geometric standard deviation
#' \item `"Arithmetic mean"` summarizes data using arithmetic mean and mean +/- standard deviation
#' }
#'
#' @export
#' @family enum helpers
#' @examples dontrun{
#' getStatisticsFromType(StatisticsTypes)
#' }
StatisticsTypes <- enum(c(
"2.5th-97.5th Percentiles",
"5th-95th Percentiles",
"10th-90th Percentiles",
"Geometric mean",
"Arithmetic mean"
))


#' @title getStatisticsFromType
#' @description Get statistics
#' @param statisticsType Statistics summarizing time profile simulated data
#' as defined by helper enum `StatisticsType`
#' @return A list including `y`, `ymin` and `ymax` summary statistics as well as their `caption`
#' @export
#' @examples dontrun{
#' workflow$plotTime
#' }
getStatisticsFromType <- function(statisticsType){
validateIsIncluded(statisticsType, StatisticsTypes)
if(isIncluded(statisticsType, StatisticsTypes$`2.5th-97.5th Percentiles`)){
return(list(
y = tlf::tlfStatFunctions$`Percentile50%`,
ymin = tlf::tlfStatFunctions$`Percentile2.5%`,
ymax = tlf::tlfStatFunctions$`Percentile97.5%`,
yCaption = "median",
# The unicode characters below are superscript th
rangeCaption = "[2.5\u1d57\u02b0-97.5\u1d57\u02b0] percentiles"
))
}
if(isIncluded(statisticsType, StatisticsTypes$`5th-95th Percentiles`)){
return(list(
y = tlf::tlfStatFunctions$`Percentile50%`,
ymin = tlf::tlfStatFunctions$`Percentile5%`,
ymax = tlf::tlfStatFunctions$`Percentile95%`,
yCaption = "median",
rangeCaption = "[5\u1d57\u02b0-95\u1d57\u02b0] percentiles"
))
}
if(isIncluded(statisticsType, StatisticsTypes$`10th-90th Percentiles`)){
return(list(
y = tlf::tlfStatFunctions$`Percentile50%`,
ymin = tlf::tlfStatFunctions$`Percentile10%`,
ymax = tlf::tlfStatFunctions$`Percentile90%`,
yCaption = "median",
rangeCaption = "[10\u1d57\u02b0-90\u1d57\u02b0] percentiles"
))
}
if(isIncluded(statisticsType, StatisticsTypes$`Arithmetic mean`)){
return(list(
y = tlf::tlfStatFunctions$mean,
ymin = tlf::tlfStatFunctions$`mean-sd`,
ymax = tlf::tlfStatFunctions$`mean+sd`,
yCaption = "arithmetic mean",
# The unicode character below is +/- symbol
rangeCaption = "mean \u00b1 SD range"
))
}
#TODO : define geometric mean in tlf !
return(list(
y = "geomean",
ymin = "geomean/sd",
ymax = "geomean*sd",
yCaption = "geometric mean",
# The unicode character below is supposed to be */ symbol
rangeCaption = "mean \u22c7 geometric SD range"
))
}

reEnv$defaultTimeProfileStatistics <- getStatisticsFromType(StatisticsTypes$`5th-95th Percentiles`)

#' @title setDefaultTimeProfileStatistics
#' @description Set default statistics used in population time profiles and residuals plots
#' @param statisticsType Name of statistics type as defined in enum `StatisticsTypes`
#' @param y Function or function name for middle values statistics
#' @param ymin Function or function name for min values statistics
#' @param ymax Function or function name for max values statistics
#' @param yCaption Legend caption for middle values statistics
#' @param rangeCaption Legend caption for range values statistics
#' @export
#' @examples \dontrun{
#' # Set the default statistics as geometric mean
#' setDefaultTimeProfileStatistics(statisticsType = StatisticsTypes$`Geometric mean`)
#'
#' # Set the default legend caption displayed for range
#' setDefaultTimeProfileStatistics(rangeCaption = "90% population range")
#'
#' }
#'
setDefaultTimeProfileStatistics <- function(statisticsType = NULL,
y = NULL,
ymin = NULL,
ymax = NULL,
yCaption = NULL,
rangeCaption = NULL){

validateIsIncluded(statisticsType, StatisticsTypes, nullAllowed = TRUE)
# Allow user to enter the function directly
validateIsOfType(y, c("character", "closure"), nullAllowed = TRUE)
validateIsOfType(ymin, c("character", "closure"), nullAllowed = TRUE)
validateIsOfType(ymax, c("character", "closure"), nullAllowed = TRUE)

if(!isEmpty(statisticsType)){
reEnv$defaultTimeProfileStatistics <- getStatisticsFromType(statisticsType)
}
# Assign variables to reEnv only if defined
eval(parseVariableToObject(
objectName = "reEnv$defaultTimeProfileStatistics",
variableName = c("y", "ymin", "ymax", "yCaption", "rangeCaption"),
keepIfNull = TRUE))

return(invisible())
}
75 changes: 72 additions & 3 deletions R/task-settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#' @keywords internal
TaskSettings <- R6::R6Class(
"TaskSettings",
# This allows the R6 class to accept new fields
lock_objects = FALSE,
public = list(
#' @description
#' Create a `TaskSettings` object
Expand Down Expand Up @@ -147,18 +149,83 @@ GofTaskSettings <- R6::R6Class(
public = list(
#' @field referenceData Data results obtained by TimeProfilesAndResiduals task corresponding to referencePopulation
referenceData = NULL,

#' @description
#' Create a `GofTaskSettings` object
#' @param taskName name of the task using the settings
#' @param outputSelections subset of simulationSet outputs to be used in GoF plot
#' @param statisticsType Statistics summarizing time profile simulated data
#' @return A new `GofTaskSettings` object
initialize = function(taskName = AllAvailableTasks$plotTimeProfilesAndResiduals, outputSelections = NULL) {
initialize = function(taskName = AllAvailableTasks$plotTimeProfilesAndResiduals,
outputSelections = NULL,
statisticsType = NULL) {
validateIsIncluded(taskName, AllAvailableTasks$plotTimeProfilesAndResiduals)
validateIsIncluded(statisticsType, StatisticsTypes, nullAllowed = TRUE)

super$initialize(taskName)

private$.includeReferenceData <- TRUE
private$.outputSelections <- outputSelections
private$.statistics <- reEnv$defaultTimeProfileStatistics
self$setStatistics(statisticsType = statisticsType)

},

#' @description Set statistics used in population time profiles and residuals plots
#' @param statisticsType Name of statistics type as defined in enum `StatisticsTypes`
#' @param y Function or function name for middle values statistics
#' @param ymin Function or function name for min values statistics
#' @param ymax Function or function name for max values statistics
#' @param yCaption Legend caption for middle values statistics
#' @param rangeCaption Legend caption for range values statistics
#' @examples \dontrun{
#' # Set the statistics as geometric mean
#' workflow$plotTimeProfilesAndResiduals$settings$setStatistics(
#' statisticsType = StatisticsTypes$`Geometric mean`
#' )
#'
#' # Set the legend caption displayed for range
#' workflow$plotTimeProfilesAndResiduals$settings$setStatistics(
#' statisticsType = StatisticsTypes$`Geometric mean`
#' rangeCaption = "90% population range"
#' )
#'
#' }
#'
setStatistics = function(statisticsType = NULL,
y = NULL,
ymin = NULL,
ymax = NULL,
yCaption = NULL,
rangeCaption = NULL){

validateIsIncluded(statisticsType, StatisticsTypes, nullAllowed = TRUE)
# Allow user to enter the function directly
validateIsOfType(y, c("character", "closure"), nullAllowed = TRUE)
validateIsOfType(ymin, c("character", "closure"), nullAllowed = TRUE)
validateIsOfType(ymax, c("character", "closure"), nullAllowed = TRUE)

if(!isEmpty(statisticsType)){
private$.statistics <- getStatisticsFromType(statisticsType)
}
# Assign variables to reEnv only if defined
eval(parseVariableToObject(
objectName = "private$.statistics",
variableName = c("y", "ymin", "ymax", "yCaption", "rangeCaption"),
keepIfNull = TRUE))
return(invisible())
},

#' @description Get statistics used in population time profiles and residuals plots
#' @examples \dontrun{
#' # Get the statistics of time profiles task
#' workflow$plotTimeProfilesAndResiduals$settings$getStatistics()
#' }
#'
getStatistics = function(){
return(private$.statistics)
}

),
active = list(
#' @field includeReferenceData logical defining if reference population should be included in
Expand All @@ -181,9 +248,11 @@ GofTaskSettings <- R6::R6Class(
private$.outputSelections <- value %||% private$.outputSelections
return(invisible())
}

),
private = list(
.includeReferenceData = NULL,
.outputSelections = NULL
.outputSelections = NULL,
.statistics = NULL
)
)
Loading

0 comments on commit c080373

Please sign in to comment.