Skip to content

Commit

Permalink
fix getThematicTrimming to return variables with empty file and file …
Browse files Browse the repository at this point in the history
…with only one section to skip all
  • Loading branch information
BERTHET Clement (Externe) committed Oct 9, 2023
1 parent 93a2833 commit c16b1fe
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 44 deletions.
95 changes: 60 additions & 35 deletions R/thematic_trimming.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,53 +30,78 @@ getThematicTrimming <- function(opts = simOptions()){
simulation = "input")

# use global referentiel
ref_list_vars_thematic <- pkgEnv$thematic
ref_list_vars_thematic <- antaresRead:::pkgEnv$thematic

# filter vars with version
ref_list_vars_thematic <- ref_list_vars_thematic[version<=opts_study$antaresVersion,]

# thematic vars
vs_section <- opts_study$parameters$`variables selection`

# case with no meta data
# generaldata.ini has no section "[variables selection]"
# with no data all variables are "active"
if(is.null(vs_section)){
warning("`variables selection` section in file 'generaldata.ini' does not exist",
call. = FALSE)
return()
warning("All variables status are 'active'",
call. = FALSE)

df_thematic <- data.frame(
variables = ref_list_vars_thematic$variable,
status_selection = "active"
)

return(df_thematic)
}

# case with all variables skiped
if(!vs_section$selected_vars_reset &
!length(vs_section)>1){
message("All variables status are 'skip'")

Check warning on line 61 in R/thematic_trimming.R

View check run for this annotation

Codecov / codecov/patch

R/thematic_trimming.R#L61

Added line #L61 was not covered by tests

df_thematic <- data.frame(
variables = ref_list_vars_thematic$variable,
status_selection = "skip"

Check warning on line 65 in R/thematic_trimming.R

View check run for this annotation

Codecov / codecov/patch

R/thematic_trimming.R#L63-L65

Added lines #L63 - L65 were not covered by tests
)

return(df_thematic)

Check warning on line 68 in R/thematic_trimming.R

View check run for this annotation

Codecov / codecov/patch

R/thematic_trimming.R#L68

Added line #L68 was not covered by tests
}
else{
pattern_add <- "select_var +"
pattern_remove <- "select_var -"
check_pattern_add <- grepl(pattern = pattern_add,
x = names(vs_section),
fixed = TRUE)
check_pattern_remove <- grepl(pattern = pattern_remove,
x = names(vs_section),
fixed = TRUE)

# case with patterns
pattern_add <- "select_var +"
pattern_remove <- "select_var -"
check_pattern_add <- grepl(pattern = pattern_add,
x = names(vs_section),
fixed = TRUE)
check_pattern_remove <- grepl(pattern = pattern_remove,
x = names(vs_section),
fixed = TRUE)

# manage addition variables
if(any(check_pattern_add)){
col_names <- vs_section[check_pattern_add]
col_names <- unlist(col_names, use.names = FALSE)

check_index <- ref_list_vars_thematic$variable %in% col_names

# manage addition variables
if(any(check_pattern_add)){
col_names <- vs_section[check_pattern_add]
col_names <- unlist(col_names, use.names = FALSE)

check_index <- ref_list_vars_thematic$variable %in% col_names

df_thematic <- data.frame(
variables = ref_list_vars_thematic$variable,
status_selection = ifelse(check_index, "active", "skip")
)
return(df_thematic)
}else{
# manage subtraction variables
col_names <- vs_section[check_pattern_remove]
col_names <- unlist(col_names, use.names = FALSE)

check_index <- ref_list_vars_thematic$variable %in% col_names

df_thematic <- data.frame(
variables = ref_list_vars_thematic$variable,
status_selection = ifelse(check_index, "skip", "active")
df_thematic <- data.frame(
variables = ref_list_vars_thematic$variable,
status_selection = ifelse(check_index, "active", "skip")
)
return(df_thematic)
}
return(df_thematic)
}else{
# manage subtraction variables
col_names <- vs_section[check_pattern_remove]
col_names <- unlist(col_names, use.names = FALSE)

check_index <- ref_list_vars_thematic$variable %in% col_names

df_thematic <- data.frame(
variables = ref_list_vars_thematic$variable,
status_selection = ifelse(check_index, "skip", "active")
)
return(df_thematic)
}

}
2 changes: 1 addition & 1 deletion man/getThematicTrimming.Rd

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

33 changes: 25 additions & 8 deletions tests/testthat/test-thematic_timming.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@
path_study_test <- grep(pattern = "86", x = studyPathSV8, value = TRUE)
opts_study_test <- setSimulationPath(path_study_test, simulation = "input")


# test_that("Empty selection variables", {
# # template study 860 as no section
#
# # read general data [variables selection] (thematic trimming)
# testthat::expect_warning(
# getThematicTrimming(opts = opts_study_test),
# regexp = "`variables selection` section in file 'generaldata.ini' does not exist"
# )
# })

# Empty section "selection variables" ----
test_that("Empty selection variables", {
# template study 860 as no section
test_that("read generaldata with empty [selection variables]", {
# read general data [variables selection]
read_thematic <- getThematicTrimming(opts = opts_study_test)

# read general data [variables selection] (thematic trimming)
testthat::expect_warning(
getThematicTrimming(opts = opts_study_test),
regexp = "`variables selection` section in file 'generaldata.ini' does not exist"
)
})
# check variables names according to antares version
antares_version <- opts_study_test$antaresVersion
filter_vars_version <- pkgEnv$thematic[version<=antares_version,]

# test if variables are all in output
testthat::expect_true(all(filter_vars_version$variable%in%
read_thematic$variables))
# test status values
testthat::expect_equal(object = unique(read_thematic$status_selection),
expected = "active")
})

# ADD VARIABLES ----
test_that("read selection variables (+)", {
Expand Down

0 comments on commit c16b1fe

Please sign in to comment.