Skip to content

write to_respilens() function#41

Merged
CyGei merged 6 commits into
mainfrom
write-to_respilens
Feb 18, 2026
Merged

write to_respilens() function#41
CyGei merged 6 commits into
mainfrom
write-to_respilens

Conversation

@emprzy
Copy link
Copy Markdown
Contributor

@emprzy emprzy commented Feb 12, 2026

to_respilens(hubdata)
Parameter:

  • hubdata object ($hubdata of output of get_fcast() function; a named list), which contains:
    • model_out_tbl, a tibble with model forecast data in Hubverse format
    • oracle_output, a tibble of corresponding ground truth data
      Returns:
  • A named list with one <loc>.json structure per location in input model data, and a single metadata.json structure to describe the data load. <loc>.json can be drag'n'dropped into MyRespiLens for visualization.

Assumptions/Notes:

  • output_type of forecast data will be quantile, pmf, or sample. sample entries will be filtered out; quantile and pmf will be loaded into the JSON. If there is another output_type outside of these 3, the script will fail with an unscripted error.
  • There is a patch in the code that filters out any targets with the word 'peak' in it (MyRespiLens does not yet do peak visualization). It notifies the user if it finds peaks keys and filters them without failure.
  • This file does NOT validate the output against a JSON schema that would validate it for compatibility with MyRespiLens. I think it would be a nice touch potentially, but would require an external dependency being added (jsonvalidate)
  • Assumes the ground truth data provided as part of the hubdata is the Hubverse oracle output style
  • Assumes the model_out_tbl and oracle_output have correct data types when they are loaded into the function
  • Assumes the user will be responsible for saving their own .json files for use within MyRespiLens
  • Assumes access to loc_data object (all locations present in model_out_tbl need to be in loc_data)

Resolves issue #40

@emprzy emprzy self-assigned this Feb 12, 2026
@emprzy emprzy requested review from CyGei and bestill3 February 12, 2026 17:24
@bestill3
Copy link
Copy Markdown
Contributor

I have a few comments for the current code as following. I provided my suggestions below instead of changing Cy or Emily's code in case you think your versions work better for the Respilens requirements.

  1. The code filters "peak" targets in to_respilens and again in metadata_key. We could do this once.
to_respilens <- function(accida_cast) {
    # Not sure if you also want to add a warning message for output_type != "sample"
 
    # Remove 'peak' targets and 'sample' types globally
    model_out_tbl <- model_out_tbl |>
        dplyr::filter(
        !grepl("peak", target, ignore.case = TRUE),
        output_type != "sample"
        )
  
    return (
        list(
            metadata = build_metadata(model_out_tbl),
            ground_truth = build_ground_truth(oracle_output),
            forecasts = build_forecasts(model_out_tbl)
            )
    )
}
  1. Helper Functions
metadata_key <- function(model_out_tbl) {
    #Given the to_respilens removes "peaks", we could directly use model_out_tbl as following.
    abbr <- model_out_tbl$location[[1]]
}
ground_truth_key <- function(oracle_output) {
    # No change to pivot to wide format code
    pivot_truth <- oracle_output...
  
    # Convert the pivot result to list (dates + one vector per target)
    ground_truth <- c(
        list(dates = as.character(pivot_truth$target_end_date)),
        as.list(pivot_truth[setdiff(names(pivot_truth), "target_end_date")])
    )

    return(ground_truth)
}

I will look at the forecasts_key function later.

@emprzy
Copy link
Copy Markdown
Contributor Author

emprzy commented Feb 18, 2026

Final version:

  • No metadata.json file
  • Assumes all columns in all incoming data are present with correct data types
  • can only handle processing one location's data at a time
  • requires users to save the data themselves
  • Returns only the JSON structure of the data that can be drag and dropped into MyRespiLens. No indexing based on file name

@CyGei CyGei merged commit 93302f2 into main Feb 18, 2026
@CyGei CyGei deleted the write-to_respilens branch February 18, 2026 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants