Skip to content

Commit b27171a

Browse files
authored
Merge pull request #67 from KWB-R/dev
Release v0.14.0
2 parents 7ef53db + f43f9d7 commit b27171a

20 files changed

Lines changed: 196 additions & 52 deletions

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: fhpredict
22
Title: R Package for the Project Flusshygiene
3-
Version: 0.13.0
3+
Version: 0.14.0
44
Authors@R:
55
c(person(given = "Wolfgang",
66
family = "Seis",
@@ -66,5 +66,5 @@ Remotes:
6666
github::cloudyr/aws.s3@v0.3.12
6767
Encoding: UTF-8
6868
LazyData: true
69-
RoxygenNote: 6.1.1
69+
RoxygenNote: 7.1.0
7070
VignetteBuilder: knitr

NEWS.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# fhpredict latest developments
2+
3+
# fhpredict 0.14.0 (2020-05-28)
4+
5+
* Fix bug in api_replace_predictions(): do not fail on missing data in db
6+
* Fix bug in get_indipendent_variables(): use all.vars()
7+
* predict_quality(): Add argument "return_debug_info"
8+
* provide_input_data():
9+
+ Add argument "require_hygiene"
10+
+ Actually return purification plant and generic time series
11+
112
# fhpredict 0.13.0 (2020-04-10)
213

314
* Let predict_quality() not delete all existing predictions before adding the

R/predict.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ api_replace_predictions <- function(user_id, spot_id, percentiles)
4848
predictions_db <- api_get_timeseries(path)
4949

5050
# Date strings of the predictions in the database
51-
date_strings_db <- format(get(predictions_db, "dateTime"), "%Y-%m-%d")
51+
date_strings_db <- if (nrow(predictions_db)) {
52+
format(get(predictions_db, "dateTime"), "%Y-%m-%d")
53+
} else {
54+
character()
55+
}
5256

5357
# Date strings of the new predictions
5458
date_strings_new <- as.character(get(percentiles, "dateTime"))

R/predict_quality.R

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,25 @@
1616
#' of the time period to be predicted. Default: "tomorrrow"
1717
#' @param import logical telling whether to import new rain data or not.
1818
#' Default: \code{TRUE}.
19-
#' @return list with elements \code{data}, \code{success}, \code{message}
19+
#' @param return_debug_info logical with default \code{FALSE}. If \code{TRUE}
20+
#' the prediction is not written to the database. Instead, what would be send
21+
#' to the database is returned with all relevant variables that were used to
22+
#' prepare the prediction being set as attributes.
23+
#' @return list with elements \code{data}, \code{success}, \code{message} or (if
24+
#' \code{return_debug_info = TRUE}) data frame representing the predictions
25+
#' with attributes \code{spot_data}, \code{riverdata_raw}, \code{riverdata},
26+
#' \code{newdata_raw}, \code{newdata}, \code{prediction} representing
27+
#' intermediate variables that were used to prepare the prediction (see
28+
#' source code of \code{fhpredict::predict_quality} to understand their
29+
#' meaning)
2030
#' @export
2131
predict_quality <- function(
22-
user_id, spot_id, from = Sys.Date() - 1L, to = Sys.Date() + 1L, import = TRUE
32+
user_id, spot_id, from = Sys.Date() - 1L, to = Sys.Date() + 1L, import = TRUE,
33+
return_debug_info = FALSE
2334
)
2435
{
2536
#kwb.utils::assignPackageObjects("fhpredict")
26-
#user_id=8;spot_id=43;from=Sys.Date()-1L;to=Sys.Date()+1L;import=TRUE
27-
#user_id=9;spot_id=41
37+
#user_id=11;spot_id=57;from=Sys.Date()-1L;to=Sys.Date()+1L;import=FALSE
2838

2939
# Try to get the model that was added last (if any)
3040
model <- try(get_last_added_model(user_id, spot_id))
@@ -45,7 +55,7 @@ predict_quality <- function(
4555
}
4656

4757
# Collect all data that are available for the given bathing spot
48-
spot_data <- provide_input_data(user_id, spot_id)
58+
spot_data <- provide_input_data(user_id, spot_id, require_hygiene = FALSE)
4959

5060
# Prepare the data (filter for bathing season, log-transform rain)
5161
riverdata_raw <- prepare_river_data(spot_data)
@@ -101,6 +111,19 @@ predict_quality <- function(
101111
# data = percentiles
102112
# )
103113

114+
if (return_debug_info) {
115+
116+
return(structure(
117+
percentiles,
118+
spot_data = spot_data,
119+
riverdata_raw = riverdata_raw,
120+
riverdata = riverdata,
121+
newdata_raw = newdata_raw,
122+
newdata = newdata,
123+
prediction = prediction
124+
))
125+
}
126+
104127
api_replace_predictions(user_id, spot_id, percentiles)
105128
})
106129

R/provide_input_data.R

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
#'
77
#' @param user_id user id
88
#' @param spot_id bathing spot id
9+
#' @param require_hygiene logical. If \code{TRUE} (default) the function will
10+
#' raise an error if there are no hygienic data
911
#' @export
1012
#'
11-
provide_input_data <- function(user_id, spot_id)
13+
provide_input_data <- function(user_id, spot_id, require_hygiene = TRUE)
1214
{
15+
#kwb.utils::assignPackageObjects("fhpredict")
16+
#user_id=11;spot_id=57
17+
1318
# Define shortcut to kwb.utils::selectColumns()
1419
get <- kwb.utils::selectColumns
1520

@@ -24,11 +29,9 @@ provide_input_data <- function(user_id, spot_id)
2429

2530
# Add microbiological measurements to the result or return if there are no
2631
# measurements
27-
if (nrow(measurements) == 0) {
28-
clean_stop(get_text(
29-
"no_measurements", user_id = user_id, spot_id = spot_id
32+
if (nrow(measurements) == 0 && require_hygiene) clean_stop(
33+
get_text("no_measurements", user_id = user_id, spot_id = spot_id
3034
))
31-
}
3235

3336
# Create "hygiene" data frame
3437
result[[result_element("hygiene")]] <- data.frame(
@@ -72,15 +75,27 @@ provide_input_data <- function(user_id, spot_id)
7275
)
7376
}
7477

75-
# Look for purification plant measurements
76-
plant_measurements <- collect_series_measurements(
77-
type = "plant", prefix = "ka", user_id, spot_id
78-
)
78+
# Helper function to add further list entries (or not if length(x) == 0)
79+
add_series <- function(result, type, prefix, x) {
7980

80-
# Look for generic input measurements
81-
generic_measurements <- collect_series_measurements(
82-
type = "generic", prefix = "gen", user_id, spot_id
83-
)
81+
x <- collect_series_measurements(type, prefix, user_id, spot_id)
82+
83+
if (length(x)) {
84+
85+
result[[result_element(prefix)]] <- kwb.utils::renameColumns(
86+
merge_series_measurements(x),
87+
list(dateTime = "datum")
88+
)
89+
}
90+
91+
result
92+
}
93+
94+
# Add purification plant measurements if there are any
95+
result <- add_series(result, type = "plant", prefix = "ka")
96+
97+
# Add generic input measurements if there are any
98+
result <- add_series(result, type = "generic", prefix = "gen")
8499

85100
# Rename the elements in the result data frame?
86101

@@ -115,3 +130,19 @@ collect_series_measurements <- function(type, prefix, user_id, spot_id)
115130
)
116131
}
117132
}
133+
134+
# merge_series_measurements ----------------------------------------------------
135+
merge_series_measurements <- function(x)
136+
{
137+
stopifnot(is.list(x))
138+
139+
merged <- kwb.utils::mergeAll(
140+
lapply(x, kwb.utils::removeColumns, "id"),
141+
by = "dateTime",
142+
all = TRUE
143+
)
144+
145+
names(merged) <- gsub("^value\\.", "", names(merged))
146+
147+
merged
148+
}

R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ get_indipendent_variables <- function(x)
207207
{
208208
stopifnot(rlang::is_formula(x))
209209

210-
unlist(lapply(x[[3L]][-1L], as.character))
210+
all.vars(x)[-1L]
211211
}
212212

213213
# get_prefix -------------------------------------------------------------------

man/api_add_model.Rd

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/api_add_rain.Rd

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/api_get_bathingspot.Rd

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/api_get_timeseries.Rd

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)