Skip to content

Commit

Permalink
Version 0.7.0 - improved transformation of layered data
Browse files Browse the repository at this point in the history
  • Loading branch information
gk-crop committed Feb 21, 2025
1 parent 10fabca commit d30f766
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 13 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: simplaceUtil
Title: Provides Utility Functions and ShinyApps to work with the modeling framework 'SIMPLACE'
Version: 0.6.9
Date: 2024-07-10
Version: 0.7.0
Date: 2025-02-21
Authors@R:
person("Gunther", "Krauss", , "[email protected]", role = c("aut", "cre"))
Description: Provides Utility Functions and ShinyApps to work with the modeling framework 'SIMPLACE'. It visualises components of a solution, runs simulations and displays results.
Expand All @@ -25,4 +25,4 @@ VignetteBuilder: knitr
Suggests:
knitr, rmarkdown, rsvg, DiagrammeRsvg
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export(createCodeStubsForSimVariables)
export(createResourceStubsFromCsv)
export(createResourceStubsFromXml)
export(enhanceFunctionWithBoundaries)
export(enhanceFunctionWithPenalty)
export(fetchDescriptionFromWebsite)
export(fetchSimComponentlistFromWebsite)
export(fetchSimVariablesFromWebsite)
Expand Down
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Version 0.7.0
* transformLayeredData can take now arbitrary separator
* transformLayeredData now works on variable names that contain the separator multiple times


Version 0.6.9
* output filenames vector named by output ids
* enhance functions to return penalty values when parameters outside boundaries
Expand Down
14 changes: 10 additions & 4 deletions R/csvoutput_helper.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#' Transform layered output data in long format
#'
#' @param data dataframe
#' @param sep character that separates layer number from variable name (default "_")
#' @return dataframe in long format
#' @export
transformLayeredData <- function(data)
transformLayeredData <- function(data, sep="_")
{
if(any(grepl("[a-zA-Z]+_[0-9]+",names(data))))
if(sep %in% c(".","|","+","-","*","(",")","[","]")) {
sep = paste0("\\",sep)
}
pattern <- paste0("([a-zA-Z_0-9 .]+)",sep,"([0-9]+)")
if(sep!="" && any(grepl(pattern,names(data))))
{
tidyr::pivot_longer(
data,
dplyr::matches("[a-zA-Z]+_[0-9]+"),
dplyr::matches(pattern),
names_to = c(".value","layer"),
names_pattern ="([a-zA-Z_]+)_([0-9]+)$",
names_pattern =pattern,
names_transform = list(layer=as.integer)
)
}
Expand Down
54 changes: 54 additions & 0 deletions R/optim_helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,57 @@ enhanceFunctionWithBoundaries <- function(fun, l_bound, u_bound,
}
}
}

#' Modifies function to return a penalised value for parameters outside boundaries
#'
#' The method takes a function as well as values for lower and upper boundaries
#' and returns a modified function. The modified function returns the value of
#' the original function when the parameters are within boundaries and a penalised
#' function otherwise.
#'
#' Optionally an own function can be supplied to calculate the penalised value.
#' The function must take two arguments: the original value and the distance
#' of the parameter to the boundaries.
#'
#' Additionally an own distance function can be supplied, that has to take three
#' arguments: the parameter, the lower boundaries and the upper boundaries.
#'
#' A main use case of this method are optimisation / calibration tasks. If the
#' optimisation method and the function to optimise are both ignorant to boundaries
#' one can turn the function into a boundary sensitive one.
#'
#' @param fun function to be modified
#' @param l_bound vector with lower boundary values
#' @param u_bound vector with upper boundary values
#' @param penalty_fun function taking value and distance and returns
#' value modified value depending on distance
#' @param distance_fun function that takes x, l_bound and u_bound and computes
#' the distance of x to the boundaries
#' @param ... arguments passed to original function
#' @param param_pos argument position of the parameter
#' @return a modified function that changes value when parameter outside bounds
#' @export
#' @examples
#' sqr_bd <- enhanceFunctionWithPenalty(\(x) x^2, .1, 10)
#' sqr_bd(-1)
#' sqr_bd(1)
#' sqr_bd(11)
#'
enhanceFunctionWithPenalty <- function(fun, l_bound, u_bound,
penalty_fun= function(value, distance) (value+distance) * exp(1000*distance),
distance_fun = function(x,l,u) {max(0, (l-x)/(u-l), (x -u)/(u-l))},
param_pos=1,
...) {
function(...) {
x <- ...elt(param_pos)
invalid <- !all(l_bound <= x & x <= u_bound)
if(invalid) {
d <- distance_fun(x,l_bound, u_bound)
penalty_fun(fun(...),d)
}
else {
fun(...)
}
}
}

6 changes: 4 additions & 2 deletions R/plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,21 @@ plotScalarOutput <- function (data, column_x, columns_y,
#' @param datecol column name for date (default CURRENT.DATE)
#' @param nrow number of panel rows when plotting multiple simulation ids
#' @param ncol number of panels in a row when plotting multiple simulation ids
#' @param sep character that separates layer number from variable name (default "_")
#' @export
#' @importFrom rlang .data
plotLayeredOutput <- function(data, column, simulationid = NULL,
date_from=NULL, date_to=NULL,
datecol="CURRENT.DATE",
nrow=NULL,
ncol=NULL)
ncol=NULL,
sep="-")
{

data <- subsetDataForPlot(data, simulationid, date_from, date_to, datecol)
if(nrow(data)>0 && !is.null(column) && !is.na(column))
{
dt <- transformLayeredData(data)
dt <- transformLayeredData(data, sep)
if(nrow(dt)>0 && column %in% names(dt) && "layer" %in% names(dt))
{
ggplot2::ggplot(dt,
Expand Down
63 changes: 63 additions & 0 deletions man/enhanceFunctionWithPenalty.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/plotLayeredOutput.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/transformLayeredData.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vignettes/simplaceUtil.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ graph <- solutionToGraph(solfile)
DiagrammeR::render_graph(graph)
```
```{r echo=FALSE, out.width="100%"}
includeGraphic(graph,"graph")
try(includeGraphic(graph,"graph"))
```

We can restrict the graph to specific component types and the neighborhood of
Expand All @@ -202,7 +202,7 @@ subgraph <- getNeighborhood(graph, c("SlimWater","SlimRoots"),
DiagrammeR::render_graph(subgraph)
```
```{r echo=FALSE, out.width="100%"}
includeGraphic(subgraph,"subgraph")
try(includeGraphic(subgraph,"subgraph"))
```


Expand Down

0 comments on commit d30f766

Please sign in to comment.