IMPALA-Consortium / ctas

Time Series Outliers and Anomalies
https://impala-consortium.github.io/ctas/
Other
3 stars 2 forks source link

catch all NULL in entry data #44

Closed erblast closed 10 months ago

erblast commented 11 months ago
library(tsoa)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

data("tsoa_data", package = "tsoa")

feats <- c(
  "autocorr",
  "average",
  "own_site_simil_score",
  "sd",
  "unique_value_count_relative",
  "lof",
  "range"
)

feats_collapse = paste(feats, collapse = ";")

data("tsoa_data", package = "tsoa")

tsoa_data$data <- tsoa_data$data %>%
  mutate(
    result = rep(as.numeric(NA), nrow(.))
  )

ls_tsoa <- process_a_study(
  data = tsoa_data$data,
  subjects = tsoa_data$subjects,
  parameters = tsoa_data$parameters,
  custom_timeseries = tsoa_data$custom_timeseries,
  custom_reference_groups = tsoa_data$custom_reference_groups,
  default_timeseries_features_to_calculate = feats_collapse,
  default_minimum_timepoints_per_series = 1,
  default_minimum_subjects_per_series = 3,
  default_max_share_missing_timepoints_per_series = 1,
  default_generate_change_from_baseline = FALSE,
  autogenerate_timeseries = TRUE
)
#> Error in `mutate()`:
#> ℹ In argument: `output = list(...)`.
#> Caused by error in `if (this_baseline == "cfb") ...`:
#> ! argument is of length zero
#> Backtrace:
#>      ▆
#>   1. ├─tsoa::process_a_study(...)
#>   2. │ └─... %>% ... at tsoa/R/tsoa.R:65:4
#>   3. ├─dplyr::select(...)
#>   4. ├─dplyr::mutate(...)
#>   5. ├─dplyr::mutate(...)
#>   6. ├─dplyr::ungroup(.)
#>   7. ├─tidyr::unnest(., "output")
#>   8. ├─dplyr::mutate(...)
#>   9. ├─dplyr:::mutate.data.frame(...)
#>  10. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#>  11. │   ├─base::withCallingHandlers(...)
#>  12. │   └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#>  13. │     └─mask$eval_all_mutate(quo)
#>  14. │       └─dplyr (local) eval()
#>  15. ├─tsoa:::pick_timepoint_combos(...)
#>  16. └─base::.handleSimpleError(...) at tsoa/R/tsoa.R:774:2
#>  17.   └─dplyr (local) h(simpleError(msg, call))
#>  18.     └─rlang::abort(message, class = error_class, parent = parent, call = error_call)

Created on 2023-12-04 with reprex v2.0.2

Since we allow NA in general and the standard behaviour is to return a named list with all NULL when no data matches the default criteria I would suggest the following unit test:

test_that("process_a_study - NA only", {

  data("tsoa_data", package = "tsoa")

  tsoa_data$data <- tsoa_data$data %>%
    mutate(
      result = rep(as.numeric(NA), nrow(.))
    )

  ls_tsoa <- process_a_study(
    data = tsoa_data$data,
    subjects = tsoa_data$subjects,
    parameters = tsoa_data$parameters,
    custom_timeseries = tsoa_data$custom_timeseries,
    custom_reference_groups = tsoa_data$custom_reference_groups,
    default_timeseries_features_to_calculate = feats_collapse,
    default_minimum_timepoints_per_series = 1,
    default_minimum_subjects_per_series = 3,
    default_max_share_missing_timepoints_per_series = 1,
    default_generate_change_from_baseline = FALSE,
    autogenerate_timeseries = TRUE
  )

  expect_true(all(purrr::map_lgl(ls_tsoa, is.null)))

})
erblast commented 11 months ago

possible solutions:

pekkatii commented 10 months ago

If test added into the main function which aborts execution if there are no result values in the data.