Skip to content

Commit 0c041d6

Browse files
committed
Test only new version of run_rabimo_with_measures()
1 parent 214708b commit 0c041d6

2 files changed

Lines changed: 54 additions & 47 deletions

File tree

R/apply_measures_to_blocks.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ apply_measures_to_blocks <- function(
6767
# Provide the total areas and roof areas in advance. They are not changed by
6868
# the measures.
6969
a_total <- blocks$total_area
70-
a_roof <- blocks$total_area * blocks$roof
7170
a_total_sum <- sum(a_total)
71+
a_roof <- a_total * blocks$roof
7272

7373
# 1. Handle measure "green roof"
7474
if (!is.na(measures$green_roof)) {
@@ -125,7 +125,7 @@ apply_measures_to_blocks <- function(
125125
debug(sprintf(
126126
"%s area to be %s: %0.2f m2",
127127
ifelse(unpave, "Paved", "Unpaved"),
128-
ifelse(unpave, "unpaved", "paved"),
128+
ifelse(unpave, "Unpaved", "paved"),
129129
abs(a_unpaved_change)
130130
))
131131

@@ -162,6 +162,7 @@ apply_measures_to_blocks <- function(
162162

163163
# 3. Handle measure "Connection to swales"
164164
if (!is.na(measures$to_swale)) {
165+
165166
a_sealed <- a_roof + a_total * blocks$pvd
166167
a_to_swale <- blocks$to_swale * a_sealed
167168
a_to_swale_change <- measures$to_swale * a_total_sum - sum(a_to_swale)
@@ -208,7 +209,7 @@ apply_measures_to_blocks <- function(
208209
# Targets reached?
209210
if (check) {
210211
for (measure in names(measures)[!is.na(measures)]) {
211-
check_if_target_was_reached(blocks, measure)
212+
check_if_target_was_reached(blocks, measure)
212213
check_for_negative_values(blocks, measure)
213214
}
214215
}

tests/testthat/test-function-run_rabimo_with_measures.R

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,11 @@
8888
)
8989
)
9090

91+
#FEATURES
9192
SAFETY_FACTOR <- 0.9999
92-
RUN_NEW <- function(...) kwb.rabimo::run_rabimo_with_measures(..., silent = TRUE)
93-
RUN_OLD <- function(...) RUN_NEW(..., old_version = TRUE)
93+
RUN <- function(...) kwb.rabimo::run_rabimo_with_measures(..., silent = TRUE)
9494
GET_MAX <- function(x) lapply(kwb.rabimo:::get_measure_stats(x), `[[`, "max")
9595
APPLY_MEASURES <- kwb.rabimo:::apply_measures_to_blocks
96-
DATASETS <- lapply(
97-
X = list(
98-
d2020 = kwb.rabimo::rabimo_inputs_2020$data,
99-
d2025 = kwb.rabimo::rabimo_inputs_2025$data
100-
),
101-
FUN = function(df) {
102-
df[sample(seq_len(nrow(df)), 10L), ]
103-
}
104-
)
10596
ADD_DELTA <- function(x, element, delta) {
10697
x[[element]] <- x[[element]] + 0.01
10798
x
@@ -115,42 +106,57 @@
115106

116107
test_that("run_rabimo_with_measures(old_version = TRUE) works", {
117108

118-
expect_error(RUN_OLD())
119-
expect_error(RUN_NEW())
109+
expect_error(RUN())
110+
111+
sample_size <- 100L
120112

121-
for (blocks in DATASETS) {
113+
for (seed in sample(1e10, 5)) {
122114

123-
#blocks <- DATASETS$d2020
124-
m_max <- as.list(SAFETY_FACTOR * unlist(GET_MAX(blocks)))
115+
#seed <- seeds[1L]
116+
writeLines(paste("seed:", seed))
125117

126-
# The maximum values were ok in the old version
127-
expect_no_error(result <- RUN_OLD(blocks, measures = m_max))
128-
expect_true(all(result$surface_runoff == 0))
129-
130-
# Exceeding any maximum value results in an error
131-
expect_error(RUN_OLD(blocks, measures = ADD_DELTA(m_max, "green_roof")))
132-
expect_error(RUN_OLD(blocks, measures = ADD_DELTA(m_max, "unpaved")))
133-
expect_error(RUN_OLD(blocks, measures = ADD_DELTA(m_max, "to_swale")))
118+
DATASETS <- lapply(
119+
X = list(
120+
d2020 = kwb.rabimo::rabimo_inputs_2020$data,
121+
d2025 = kwb.rabimo::rabimo_inputs_2025$data
122+
),
123+
FUN = function(df) {
124+
df[sample(seq_len(nrow(df)), sample_size), ]
125+
}
126+
)
134127

135-
# The maximum values lead to an error in the new version because after
136-
# maximum unpaving there is nothing left to be connected to swales
137-
expect_error(expect_warning(RUN_NEW(blocks, measures = m_max)))
128+
for (blocks in DATASETS) {
129+
130+
#blocks <- DATASETS$d2025
131+
m_max_old <- as.list(SAFETY_FACTOR * unlist(GET_MAX(blocks)))
132+
m_max_new <- CORRECT_TO_SWALE_MAX(m_max_old, blocks)
138133

139-
# However, we can recalculate the maximum "to_swale"
140-
expect_no_error(
141-
result <- RUN_NEW(blocks, measures = CORRECT_TO_SWALE_MAX(m_max, blocks))
142-
)
143-
expect_true(all(result$runoff < 0.1))
134+
# The maximum values lead to an error in the new version because after
135+
# maximum unpaving there is nothing left to be connected to swales
136+
expect_error(suppressWarnings(RUN(blocks, measures = m_max_old)))
137+
138+
# However, with the corrected maximum value for "to_swale" it works
139+
# The new version should not produce runoff with the well-calculated values
140+
expect_no_error(suppressWarnings(result <- RUN(blocks, measures = m_max_new)))
144141

145-
} # end of for (data in DATASETS)
146-
142+
expect_true(all(result$runoff == 0))
143+
#expect_true(all(result$runoff < 0.1))
144+
145+
# Exceeding any maximum value results in an error
146+
expect_error(suppressWarnings(RUN(blocks, measures = ADD_DELTA(m_max_new, "green_roof"))))
147+
expect_error(suppressWarnings(RUN(blocks, measures = ADD_DELTA(m_max_new, "unpaved"))))
148+
expect_error(suppressWarnings(RUN(blocks, measures = ADD_DELTA(m_max_new, "to_swale"))))
149+
150+
} # end of for (data in DATASETS)
151+
}
152+
153+
# Testing the features that caused problems as reported by Luise
147154
measures <- list(green_roof = 0.009, to_swale = 0, unpaved = 0.3)
148-
expect_no_error(RUN_OLD(FEATURES, measures = measures))
149-
expect_error(RUN_OLD(FEATURES, measures = ADD_DELTA(measures, "green_roof")))
155+
expect_no_error(RUN(FEATURES, measures = measures))
156+
expect_error(suppressWarnings(RUN(FEATURES, measures = ADD_DELTA(measures, "green_roof"))))
150157

151-
expect_no_error(RUN_NEW(FEATURES, measures))
152-
expect_error(RUN_NEW(FEATURES, ADD_DELTA(measures, "green_roof")))
153-
158+
expect_no_error(RUN(FEATURES, measures))
159+
expect_error(suppressWarnings(RUN(FEATURES, ADD_DELTA(measures, "green_roof"))))
154160
})
155161

156162
test_that("Full connection to swales results in zero runoff", {
@@ -164,18 +170,18 @@ test_that("Full connection to swales results in zero runoff", {
164170
pvd = c(0.3, 0.2, 0.1)
165171
)
166172

167-
check_result <- function(result) {
173+
check_for_no_runoff <- function(result) {
168174
expect_true(all(result$runoff == 0))
169175
}
170176

171177
measures <- list(green_roof = NA, unpaved = NA, to_swale = 0.3)
172-
result <- RUN_NEW(blocks, measures, config = CONFIG)
173-
check_result(result)
178+
result <- RUN(blocks, measures, config = CONFIG)
179+
check_for_no_runoff(result)
174180

175181
# max. green_roof = mean(roof) = 0.1
176182
# max. unpaved = mean(1 - roof) = 0.9
177183
# correct max. to_swale
178184
m_max <- CORRECT_TO_SWALE_MAX(GET_MAX(blocks), blocks)
179-
result <- RUN_NEW(blocks, m_max, config = CONFIG)
180-
check_result(result)
185+
result <- RUN(blocks, m_max, config = CONFIG)
186+
check_for_no_runoff(result)
181187
})

0 commit comments

Comments
 (0)