Skip to content

Commit

Permalink
ReadInputTS()/WriteInputTS() synchronization (#113)
Browse files Browse the repository at this point in the history
* Increase version to 0.6.1

* Allow the user to write the argument in writeInputTS() link with the separator ' - '

* Enrich the vignette to show how to use readInputTS() then writeInputTS()

* Add help for argument link in writeInputTS()

* Add sections to have more readable test file in test-writeInputTS.R
  • Loading branch information
KKamel67 authored Dec 6, 2023
1 parent 28f0100 commit 6887ab8
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 99 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: antaresEditObject
Type: Package
Title: Edit an 'Antares' Simulation
Version: 0.6.0
Version: 0.6.1
Authors@R: c(
person("Tatiana", "Vargas", email = "[email protected]", role = c("aut", "cre")),
person("Frederic", "Breant", role = "aut"),
Expand Down
16 changes: 12 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# antaresEditObject 0.6.1

### Breaking changes

* `writeInputTS()` allows the user to set a link with the separator ' - ' (ex: 'area1 - area2')


# antaresEditObject 0.6.0

### Breaking changes (Antares v8.6, cf. Antares v8.6 changelog) :
Expand All @@ -24,10 +31,10 @@ NEW FEATURES :
* Add `writeIniHydro()` function to make easier the edition of the `input/hydro/hydro.ini` file
* Call `writeIniHydro()` in `createArea()` and `removeArea()`
* Enable edition of hydro levels in `settings/scenariobuilder.dat` by using `scenarioBuilder()` and `updateScenarioBuilder()`
* Add deduplicateScenarioBuilder() function to keep the last value if a key is duplicated in settings/scenariobuilder.dat
* Add writeIniHydro() function to make easier the edition of the input/hydro/hydro.ini file
* Call writeIniHydro() in createArea() and removeArea()
* removeArea() removes only expected files in links directory
* Add `deduplicateScenarioBuilder()` function to keep the last value if a key is duplicated in settings/scenariobuilder.dat
* Add `writeIniHydro()` function to make easier the edition of the input/hydro/hydro.ini file
* Call `writeIniHydro()` in `createArea()` and `removeArea()`
* `removeArea()` removes only expected files in links directory



Expand All @@ -39,6 +46,7 @@ NEW FEATURES :
* `removeClusterRES()` in API mode
* `removeLink()` delete properly data for an Antares version >= 820
* `rollback_to_previous_data()` enable to rollback if original value is NULL
* `writeInputTS()` allows the user to set a link with the separator ' - ' (ex: 'area1 - area2')

BUGFIXES :

Expand Down
16 changes: 11 additions & 5 deletions R/writeInputTS.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#'
#' @param area The area where to write the input time series.
#' @param link Link for which writing transmission capacities time series,
#' must like `"area01%area02"` or `c("area01", "area02")`.
#' must like `"area01%area02"` or `"area01 - area02"` or `c("area01", "area02")`.
#' @param overwrite Logical. Overwrite the values if a file already exists.
#'
#'
Expand Down Expand Up @@ -163,13 +163,19 @@ writeInputTS <- function(data,
# tsLink block (file & API)
if (!is.null(link)) {
stopifnot(
"link must be a character, like 'area01%area02' or c('area01', 'area02')" = is.character(link)
"link must be a character, like 'area01%area02' or 'area01 - area02' or c('area01', 'area02')" = is.character(link)
)
if (length(link) == 1)
link <- strsplit(x = link, split = "%")[[1]]
if (length(link) == 1) {
sep_hyphen <- " - "
sep_expected <- "%"
if (grepl(pattern = sep_hyphen, x = link)) {
link <- gsub(pattern = sep_hyphen, replacement = sep_expected, x = link)
}
link <- strsplit(x = link, split = sep_expected)[[1]]
}

stopifnot(
"Invalid link specification, must be 'area01%area02' or c('area01', 'area02')" = length(link) == 2
"Invalid link specification, must be 'area01%area02' or 'area01 - area02' or c('area01', 'area02')" = length(link) == 2
)

from <- tolower(as.character(link[1]))
Expand Down
2 changes: 1 addition & 1 deletion man/writeInputTS.Rd

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

231 changes: 143 additions & 88 deletions tests/testthat/test-writeInputTS.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,124 @@ sapply(studies, function(study) {
})


# v860 ----
# >= 820 ----
## Alphabetical order links ----
test_that("Check if writeInputTS() writes time series link regardless alphabetical order", {

ant_version <- "8.2.0"
st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
area <- "aa"
area2 <- "zz"
createArea(area)
createArea(area2)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

createLink(from = area, to = area2, opts = opts)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

path_direct_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_direct.txt"))
path_indirect_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_indirect.txt"))

dat_mat <- c(1,3,2,4)
dat_mat_inv <- c(4,2,3,1)
nb_cols <- length(dat_mat)

# alphabetical order
mat_multi_scen <- matrix(data = rep(dat_mat, each = 8760), ncol = nb_cols)
writeInputTS(data = mat_multi_scen, link = paste0(area,"%",area2), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_multi_scen[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_multi_scen[,seq((nb_cols/2)+1, nb_cols)]))

# no alphabetical order
mat_multi_scen_inv <- matrix(data = rep(dat_mat_inv, each = 8760), ncol = nb_cols)
writeInputTS(data = mat_multi_scen_inv, link = paste0(area2,"%",area), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_multi_scen_inv[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_multi_scen_inv[,seq((nb_cols/2)+1, nb_cols)]))

})


## Separator link type ----
test_that("Check if writeInputTS() writes links time series with argument link 'area1 - area2' or 'area1%area2'", {

ant_version <- "8.2.0"
st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
area <- "aa"
area2 <- "zz"
createArea(area)
createArea(area2)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

createLink(from = area, to = area2, opts = opts)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

path_direct_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_direct.txt"))
path_indirect_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_indirect.txt"))

dat_mat_sep_1 <- c(1,3,2,4)
nb_cols <- length(dat_mat_sep_1)
mat_ts_sep_1 <- matrix(data = rep(dat_mat_sep_1, each = 8760), ncol = nb_cols)

dat_mat_sep_2 <- c(5,7,6,8)
nb_cols <- length(dat_mat_sep_2)
mat_ts_sep_2 <- matrix(data = rep(dat_mat_sep_2, each = 8760), ncol = nb_cols)

# link separator '%'
writeInputTS(data = mat_ts_sep_1, link = paste0(area,"%",area2), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_ts_sep_1[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_ts_sep_1[,seq((nb_cols/2)+1, nb_cols)]))

# link separator ' - '
writeInputTS(data = mat_ts_sep_2, link = paste0(area," - ",area2), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_ts_sep_2[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_ts_sep_2[,seq((nb_cols/2)+1, nb_cols)]))

})



# >= v860 ----
setup_study_860(sourcedir860)

#Avoid warning related to code writed outside test_that.
suppressWarnings(opts <- antaresRead::setSimulationPath(study_temp_path, "input"))

## Check column dimension ----
test_that("create mingen file data v860", {

#Initialize mingen data
M_mingen = matrix(0,8760,5)


# [management rules] for mingen data :
# file mod.txt (in /series) have to be same column dimension
# or column dimension of 1 or NULL (empty file)
# file mod.txt (in /series) have to be same column dimension
# or column dimension of 1 or NULL (empty file)

# check dimensions of mod.txt for every areas
path_file_mod <- file.path(opts$inputPath, "hydro", "series",
Expand All @@ -127,7 +229,7 @@ test_that("create mingen file data v860", {
names(list_dim) <- getAreas()

## trivial case
# mod.txt column dimension == 1
# mod.txt column dimension == 1
area_1 <- getAreas()[list_dim==1][1]

# write for an area with file mod.txt NULL or nb columns == 1
Expand All @@ -138,15 +240,15 @@ test_that("create mingen file data v860", {
read_ts_file <- readInputTS(mingen = "all", opts = opts)

# tests correct reading data
# check col name "mingen"
# check col name "mingen"
testthat::expect_true("mingen" %in% names(read_ts_file))
# check your area
# check your area
testthat::expect_true(area_1 %in% unique(read_ts_file$area))
# check dimension data for your area
# check dimension data for your area
testthat::expect_equal(dim(M_mingen)[2], max(read_ts_file[area %in% area_1, tsId]))


# mod.txt column dimension == 0 (empty file)
# mod.txt column dimension == 0 (empty file)
area_0 <- getAreas()[list_dim==0][1]

# write for an area with file mod.txt empty columns == 0
Expand All @@ -161,14 +263,14 @@ test_that("create mingen file data v860", {


## multi columns cas for mod.txt file
# mod.txt column dimension >= 1
# mod.txt column dimension >= 1
area_mult <- getAreas()[list_dim>1][1]

# write for an area with file mod.txt >1 columns
# error case cause mod.txt dimension
# error case cause mod.txt dimension
testthat::expect_error(writeInputTS(area = area_mult, type = "mingen",
data = M_mingen , overwrite = TRUE, opts = opts),
regexp = 'mingen \'data\' must be either a 8760\\*1 or 8760\\*3 matrix.')
data = M_mingen , overwrite = TRUE, opts = opts),
regexp = 'mingen \'data\' must be either a 8760\\*1 or 8760\\*3 matrix.')

# you can write only mingen file with dimension 1
writeInputTS(area = area_mult, type = "mingen",
Expand Down Expand Up @@ -199,9 +301,34 @@ test_that("create mingen file data v860", {
})


## Rollback to empty file ----
test_that("writeInputTS() in 8.6.0 : rollback to an empty file", {

test_that("writeInputTS() in 8.6.0 : check if there is an error when control is enabled and data is inconsistent between mingen.txt and mod.txt", {
ant_version <- "8.6.0"
st_test <- paste0("my_study_860_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
area <- "zone51"
createArea(area)
opts <- setSimulationPath(opts$studyPath, simulation = "input")

path_mingen <- file.path(opts$inputPath, "hydro", "series", area, "mingen.txt")
mat_mingen <- matrix(6,8760,5)
expect_error(writeInputTS(area = area,
data = mat_mingen,
type = "mingen",
opts = opts
)
,regexp = "can not be updated"
)
expect_true(file.size(path_mingen) == 0)

unlink(x = opts$studyPath, recursive = TRUE)
})


## Error mingen.txt vs mod.txt ----
test_that("writeInputTS() in 8.6.0 : check if there is an error when control is enabled and data is inconsistent between mingen.txt and mod.txt", {

ant_version <- "8.6.0"
st_test <- paste0("my_study_860_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
Expand Down Expand Up @@ -239,7 +366,7 @@ test_that("writeInputTS() in 8.6.0 : check if there is an error when control is
data = mat_mingen_false,
type = "mingen",
opts = opts
)
)
,regexp = "can not be updated"
)
# ref mingen
Expand Down Expand Up @@ -299,6 +426,7 @@ test_that("writeInputTS() in 8.6.0 : check if there is an error when control is
})


## Success mingen.txt vs mod.txt ----
test_that("writeInputTS() in 8.6.0 : check if new data is written when control is enabled and data is consistent between mingen.txt and mod.txt", {

ant_version <- "8.6.0"
Expand Down Expand Up @@ -383,6 +511,7 @@ test_that("writeInputTS() in 8.6.0 : check if new data is written when control i
})


## Success when disabled control ----
test_that("writeInputTS() in 8.6.0 : check if new data is written when control is disabled", {

ant_version <- "8.6.0"
Expand Down Expand Up @@ -449,77 +578,3 @@ test_that("writeInputTS() in 8.6.0 : check if new data is written when control i
})


test_that("Check if writeInputTS() writes time series link regardless alphabetical order", {

ant_version <- "8.2.0"
st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
area <- "aa"
area2 <- "zz"
createArea(area)
createArea(area2)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

createLink(from = area, to = area2, opts = opts)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

path_direct_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_direct.txt"))
path_indirect_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_indirect.txt"))

dat_mat <- c(1,3,2,4)
dat_mat_inv <- c(4,2,3,1)
nb_cols <- length(dat_mat)

## Future developments will come because this is not the expected behaviour
## Time series direct and indirect have to be reordered
# alphabetical order
mat_multi_scen <- matrix(data = rep(dat_mat, each = 8760), ncol = nb_cols)
writeInputTS(data = mat_multi_scen, link = paste0(area,"%",area2), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_multi_scen[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_multi_scen[,seq((nb_cols/2)+1, nb_cols)]))

# no alphabetical order
mat_multi_scen_inv <- matrix(data = rep(dat_mat_inv, each = 8760), ncol = nb_cols)
writeInputTS(data = mat_multi_scen_inv, link = paste0(area2,"%",area), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_multi_scen_inv[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_multi_scen_inv[,seq((nb_cols/2)+1, nb_cols)]))

})


test_that("writeInputTS() in 8.6.0 : rollback to an empty file", {

ant_version <- "8.6.0"
st_test <- paste0("my_study_860_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
area <- "zone51"
createArea(area)
opts <- setSimulationPath(opts$studyPath, simulation = "input")

path_mingen <- file.path(opts$inputPath, "hydro", "series", area, "mingen.txt")
mat_mingen <- matrix(6,8760,5)
expect_error(writeInputTS(area = area,
data = mat_mingen,
type = "mingen",
opts = opts
)
,regexp = "can not be updated"
)
expect_true(file.size(path_mingen) == 0)

unlink(x = opts$studyPath, recursive = TRUE)
})


Loading

0 comments on commit 6887ab8

Please sign in to comment.