Skip to content

Commit

Permalink
Refactor zip_unzip
Browse files Browse the repository at this point in the history
  • Loading branch information
henrykironde committed Nov 29, 2023
1 parent 957dc6b commit 4de35f5
Showing 1 changed file with 87 additions and 86 deletions.
173 changes: 87 additions & 86 deletions R/zip_unzip_forecasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,102 @@
#'
#' @param type either zip or unzip
#' @param forecast_path location of forecast directory
#' @param date forecaset date to use while zipping
#' @param date forecast date to use while zipping
#'
#' @export
#'
zip_unzip <-
function(type = NULL,
forecast_path = "forecasts/",
date = NULL) {
print("Preparing forecasts files")
proj_path <- forecast_path
forecasts_metadata = paste0(proj_path, "/forecasts_metadata.csv")

proj_path <- normalizePath(proj_path, mustWork = FALSE)
forecasts_metadata <- normalizePath(forecasts_metadata, mustWork = FALSE)
metadata <- read.csv(forecasts_metadata)
unique_dates <- unique(metadata$forecast_date)
unique_dates = sort(unique_dates)

if (type == "zip") {
csv_file <- "_forecast_table.csv"
yaml_file <- "_metadata.yaml"
json_file <- "_model_forecast.json"
if (!is.null(date)) {
unique_dates = c(date)
print(paste0("Zipping forecasts files for ", date))
}
for (forecast_day in unique_dates) {
id_date_files <- c()
zipfile <- paste0(proj_path, "/forecast_id_", forecast_day, ".zip")
# Get all the values of that particular day in a data frame
newdata <-
subset(metadata,
forecast_date == forecast_day,
select = c(forecast_id, forecast_date))
# for each forecast_id get 3 files
All_ids <- newdata$forecast_id
for (id in All_ids) {
csv_file_path = paste0(proj_path, "/forecast_id_", id, csv_file)
if (file.exists(csv_file_path)) {
id_date_files <- c(id_date_files, csv_file_path)
}
yaml_file_path = paste0(proj_path, "/forecast_id_", id, yaml_file)
if (file.exists(yaml_file_path)) {
id_date_files <- c(id_date_files, yaml_file_path)
}
json_file_path = paste0(proj_path, "/forecast_id_", id, json_file)
if (file.exists(json_file_path)) {
id_date_files <- c(id_date_files, json_file_path)
}
zip_unzip <- function(type = NULL,
forecast_path = "forecasts/",
date = NULL) {

# Function to zip a list of files
zip_files <- function(zipfile, files) {
zipfile <- normalizePath(zipfile, mustWork = FALSE)
if (file.exists(zipfile)) {

Check warning on line 16 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L15-L16

Added lines #L15 - L16 were not covered by tests
# First remove old zip file if exists
unlink(zipfile)

Check warning on line 18 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L18

Added line #L18 was not covered by tests
}
# Zip all files in the list
zip(zipfile, files)
unlink(files)

Check warning on line 22 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L21-L22

Added lines #L21 - L22 were not covered by tests
}

print("Preparing forecasts files")
proj_path <- forecast_path
forecasts_metadata <- paste0(proj_path, "/forecasts_metadata.csv")

proj_path <- normalizePath(proj_path, mustWork = FALSE)
forecasts_metadata <- normalizePath(forecasts_metadata, mustWork = FALSE)
metadata <- read.csv(forecasts_metadata)
unique_dates <- sort(unique(metadata$forecast_date))

if (type == "zip") {
csv_file <- "_forecast_table.csv"
yaml_file <- "_metadata.yaml"
json_file <- "_model_forecast.json"

Check warning on line 37 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L35-L37

Added lines #L35 - L37 were not covered by tests

if (!is.null(date)) {
unique_dates = c(date)
print(paste0("Zipping forecasts files for ", date))

Check warning on line 41 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L39-L41

Added lines #L39 - L41 were not covered by tests
}

for (forecast_day in unique_dates) {
id_date_files <- c()
zipfile <- paste0(proj_path, "/forecast_id_", forecast_day, ".zip")

Check warning on line 46 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L44-L46

Added lines #L44 - L46 were not covered by tests

# Get all the values of that particular day in a data frame
newdata <- subset(metadata,
forecast_date == forecast_day,
select = c(forecast_id, forecast_date))

Check warning on line 51 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L49-L51

Added lines #L49 - L51 were not covered by tests

# For each forecast_id get 3 files
All_ids <- newdata$forecast_id
for (id in All_ids) {
csv_file_path <- paste0(proj_path, "/forecast_id_", id, csv_file)
if (file.exists(csv_file_path)) {
id_date_files <- c(id_date_files, csv_file_path)

Check warning on line 58 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L54-L58

Added lines #L54 - L58 were not covered by tests
}

if (length(id_date_files)){
zipfile <- normalizePath(zipfile, mustWork = FALSE)
if (file.exists(zipfile)) {
# First remove old zip file if exists
unlink(zipfile)
}
# zip all id_date_files
zip(zipfile, id_date_files)
unlink(id_date_files)
yaml_file_path <- paste0(proj_path, "/forecast_id_", id, yaml_file)
if (file.exists(yaml_file_path)) {
id_date_files <- c(id_date_files, yaml_file_path)

Check warning on line 62 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L60-L62

Added lines #L60 - L62 were not covered by tests
}
json_file_path <- paste0(proj_path, "/forecast_id_", id, json_file)
if (file.exists(json_file_path)) {
id_date_files <- c(id_date_files, json_file_path)

Check warning on line 66 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L64-L66

Added lines #L64 - L66 were not covered by tests
}
}

if (length(id_date_files)) {

Check warning on line 70 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L70

Added line #L70 was not covered by tests
# Zip forecast files for the current date
zip_files(zipfile, id_date_files)

Check warning on line 72 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L72

Added line #L72 was not covered by tests
}

# Zip forecasts_evaluations.csv
id_date_files <- c(paste0(proj_path, "/forecasts_evaluations.csv"))
id_date_files <- normalizePath(id_date_files, mustWork = FALSE)

zipfile <- paste0(proj_path, "/forecasts_evaluations.zip")
}

# Zip forecasts_evaluations.csv
eval_file <- paste0(proj_path, "/forecasts_evaluations.csv")
zipfile <- paste0(proj_path, "/forecasts_evaluations.zip")
zip_files(zipfile, eval_file)

Check warning on line 79 in R/zip_unzip_forecasts.R

View check run for this annotation

Codecov / codecov/patch

R/zip_unzip_forecasts.R#L77-L79

Added lines #L77 - L79 were not covered by tests
}

if (type == "unzip") {
print("Unzipping forecasts files")

# Unzip files based on unique_dates
for (forecast_day in unique_dates) {
zipfile <- paste0(proj_path, "/forecast_id_", forecast_day, ".zip")
zipfile <- normalizePath(zipfile, mustWork = FALSE)
if (file.exists(zipfile)) {
# First remove old zip file if exists
unzip(zipfile, exdir = proj_path, junkpaths = TRUE)
unlink(zipfile)
}
zip(zipfile, id_date_files)
unlink(id_date_files)
}

if (type == "unzip") {
print("Unzipping forecasts files")
# unzip files basing on unique_dates
for (forecast_day in unique_dates) {
zipfile <- paste0(proj_path, "/forecast_id_", forecast_day, ".zip")
zipfile <- normalizePath(zipfile, mustWork = FALSE)
if (file.exists(zipfile)) {
unzip(zipfile, exdir = proj_path, junkpaths = TRUE)
unlink(zipfile)
}
}

# Unzip forecasts_evaluations.csv
eval_zip <- paste0(proj_path, "/forecasts_evaluations.zip")
eval_zip <- normalizePath(eval_zip, mustWork = FALSE)
if (file.exists(eval_zip)) {
unzip(eval_zip, exdir = proj_path, junkpaths = TRUE)
unlink(eval_zip)
}

# Unzip forecasts_evaluations.csv
eval_zip <- paste0(proj_path, "/forecasts_evaluations.zip")
eval_zip <- normalizePath(eval_zip, mustWork = FALSE)
if (file.exists(eval_zip)) {
unzip(eval_zip, exdir = proj_path, junkpaths = TRUE)
unlink(eval_zip)
}
}
}
}

0 comments on commit 4de35f5

Please sign in to comment.