Skip to content

Commit

Permalink
Merge pull request #22 from seroanalytics/seriestype
Browse files Browse the repository at this point in the history
add time series type
  • Loading branch information
hillalex authored Dec 11, 2024
2 parents 14b0afb + ec3b4ec commit 7f44d11
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
13 changes: 9 additions & 4 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_post_dataset <- function(req, res) {
logger::log_info("Parsing multipart form request")
parsed <- mime::parse_multipart(req)
xcol <- get_xcol(parsed)
series_type <- parsed$series_type
name <- get_dataset_name(parsed)
if (is.null(parsed$file$type) || parsed$file$type != "text/csv") {
return(invalid_file_type(res))
Expand All @@ -35,17 +36,18 @@ target_post_dataset <- function(req, res) {
}

logger::log_info(paste("Saving dataset", name, "to disk"))
save_dataset(path, file_body, xcol)
save_dataset(path, file_body, xcol, series_type)

response_success(jsonlite::unbox(name))
}

save_dataset <- function(path, file_body, xcol) {
save_dataset <- function(path, file_body, xcol, series_type) {
xtype <- get_xtype(file_body[, xcol])
dir.create(path, recursive = TRUE)
utils::write.csv(file_body, file.path(path, "data"), row.names = FALSE)
write(xcol, file.path(path, "xcol"))
write(xtype, file.path(path, "xtype"))
write(series_type, file.path(path, "series_type"))
}

get_parsed_values <- function(raw_values) {
Expand Down Expand Up @@ -111,6 +113,7 @@ target_get_dataset <- function(name, req) {
logger::log_info(paste("Found dataset:", name))
dat <- dataset$data
xcol <- dataset$xcol
series_type <- dataset$series_type
cols <- setdiff(colnames(dat), c("value", "biomarker", xcol))
if (length(cols) == 0) {
logger::log_info("No covariates detected")
Expand All @@ -128,7 +131,8 @@ target_get_dataset <- function(name, req) {
}
list(variables = unname(variables),
biomarkers = biomarkers,
xcol = jsonlite::unbox(xcol))
xcol = jsonlite::unbox(xcol),
type = jsonlite::unbox(series_type))
}

target_get_datasets <- function(req) {
Expand Down Expand Up @@ -293,11 +297,12 @@ read_dataset <- function(req, name, scale) {
}
xcol <- readLines(file.path(path, "xcol"))
xtype <- readLines(file.path(path, "xtype"))
series_type <- readLines(file.path(path, "series_type"))
logger::log_info("Parsing x column values")
if (xtype == "date") {
dat[, xcol] <- as.Date(dat[, xcol], origin = "1970-01-01")
}
list(data = dat, xcol = xcol, xtype = xtype)
list(data = dat, xcol = xcol, xtype = xtype, series_type = series_type)
}

model_out <- function(dat, xcol,
Expand Down
6 changes: 5 additions & 1 deletion inst/schema/DatasetMetadata.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
},
"xcol": {
"type": "string"
},
"type": {
"enum": ["surveillance", "post-exposure"]
}
},
"additionalProperties": false,
"required": [
"variables",
"biomarkers",
"xcol"
"xcol",
"type"
]
}
1 change: 1 addition & 0 deletions inst/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ paths:
- ab_unit
- ab_spike
xcol: day_of_study
type: surveillance
'404':
description: Dataset with the given name not found
content:
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local_add_dataset <- function(dat, name, session = session_id, env = parent.fram
write.csv(dat, file.path(filepath, "data"), row.names = FALSE)
write("day", file.path(filepath, "xcol"))
write("number", file.path(filepath, "xtype"))
write("surveillance", file.path(filepath, "series_type"))
withr::defer({
if (dir.exists(filepath)) {
fs::dir_delete(filepath)
Expand All @@ -33,6 +34,7 @@ local_add_dataset <- function(dat, name, session = session_id, env = parent.fram

local_POST_dataset_request <- function(dat, filename,
xcol = "day",
series_type = "surveillance",
env = parent.frame(),
session = session_id,
cookie = "") {
Expand All @@ -44,6 +46,9 @@ local_POST_dataset_request <- function(dat, filename,
"Content-Type: text/csv", EOL, EOL,
readr::format_csv(dat, eol = EOL), EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"series_type\"", EOL, EOL,
series_type, EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"xcol\"", EOL, EOL,
xcol, EOL,
boundary, "--")
Expand All @@ -70,6 +75,9 @@ local_POST_dataset_request_no_xcol <- function(dat, filename,
EOL,
"Content-Type: text/csv", EOL, EOL,
readr::format_csv(dat, eol = EOL), EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"series_type\"", EOL, EOL,
"surveillance", EOL,
boundary, "--")
filepath <- file.path("uploads", session_id, filename)
withr::defer({
Expand All @@ -88,6 +96,7 @@ local_POST_dataset_request_with_name <- function(dat,
filename,
name,
xcol = "day",
series_type = "surveillance",
env = parent.frame(),
cookie = cookie) {
EOL <- "\r\n"
Expand All @@ -101,6 +110,9 @@ local_POST_dataset_request_with_name <- function(dat,
"Content-Disposition: form-data; name=\"xcol\"", EOL, EOL,
xcol, EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"series_type\"", EOL, EOL,
series_type, EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"name\"", EOL, EOL,
name, EOL,
boundary, "--")
Expand Down Expand Up @@ -128,6 +140,9 @@ local_POST_dataset_request_bad_file <- function(env = parent.frame()) {
"Content-Type: image/png", EOL, EOL,
"1234", EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"series_type\"", EOL, EOL,
"surveillance", EOL,
boundary, EOL,
"Content-Disposition: form-data; name=\"xcol\"", EOL, EOL,
"day", EOL,
boundary, "--")
Expand Down
12 changes: 9 additions & 3 deletions tests/testthat/test-upload.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ test_that("uploading wrong file type returns 400", {
"Invalid file type; please upload file of type text/csv.")
})

test_that("saves file and xcol", {
test_that("saves file, xcol and series_type", {
router <- build_routes(cookie_key)
request <- local_POST_dataset_request(data.frame(biomarker = "ab",
time = 1:10,
value = 1),
filename = "testdataset",
xcol = "time",
series_type = "surveillance",
cookie = cookie)
res <- router$call(request)
expect_equal(res$status, 200)
Expand All @@ -115,6 +116,8 @@ test_that("saves file and xcol", {
expect_equal(nrow(dat), 10)
xcol <- readLines(file.path("uploads", session_id, "/testdataset/xcol"))
expect_equal(xcol, "time")
xcol <- readLines(file.path("uploads", session_id, "/testdataset/series_type"))
expect_equal(xcol, "surveillance")
})

test_that("can get uploaded dataset metadata with default xcol", {
Expand All @@ -140,14 +143,16 @@ test_that("can get uploaded dataset metadata with default xcol", {
expect_equal(body$data$xcol, "day")
})

test_that("can get uploaded dataset metadata with xcol", {
test_that("can get uploaded dataset metadata with xcol and series_type", {
request <- local_POST_dataset_request(data.frame(biomarker = c("ab", "ba"),
value = 1,
time = 1:10,
age = "0-5",
sex = c("M", "F")),
"testdata",
xcol = "time", cookie = cookie)
xcol = "time",
series_type = "post-exposure",
cookie = cookie)
router <- build_routes(cookie_key)
res <- router$call(request)
expect_equal(res$status, 200)
Expand All @@ -160,6 +165,7 @@ test_that("can get uploaded dataset metadata with xcol", {
expect_equal(body$data$variables$levels, list(c("0-5"), c("M", "F")))
expect_equal(body$data$biomarkers, c("ab", "ba"))
expect_equal(body$data$xcol, "time")
expect_equal(body$data$type, "post-exposure")
})

test_that("can get uploaded dataset without covariates", {
Expand Down

0 comments on commit 7f44d11

Please sign in to comment.