mrc-ide / individual

R Package for individual based epidemiological models
https://mrc-ide.github.io/individual
Other
30 stars 16 forks source link

Different formats for output data? #77

Open marialma opened 3 years ago

marialma commented 3 years ago

Hi individual team! I had a question/ request about the output formats

Instead of generating a data frame that contains the counts of each compartment at each time step, I'm interested in creating a data frame that shows each individual's status at each time point. Is there a way to do this within the Render class?

giovannic commented 3 years ago

Hi Maria,

Nope, we don't have that implemented at the moment.

For your case I'd recommend implementing a new renderer. Something like,

CategoryRender <- R6::R6Class(
  'CategoryRender',
  private = list(
    .output = NULL, # a char matrix representing the state of every individual at each timestep
    .categories = NULL
  ),
  public = list(
    initialize = function(timesteps, n_individuals, categories) {
      private$.categories <- categories
      private$.output <- matrix(NA, nrow = timesteps, ncol = n_individuals)
    },

    render = function(category_variable, timestep) {
      for (category in private$.categories) {
        private$.output[timestep, category_variable$get_index_of(category)$to_vector()] <- category
      }
    },

    #' @description
    #' Make a dataframe for the render
    to_dataframe = function() {
      # turn your private$.output into the dataframe you want :)
    }
  )
)

If it works well for you we're happy to accept a pull request :)

marialma commented 3 years ago

Thank you! I'll give it a shot and report back 😄

giovannic commented 3 years ago

Great! LMK

slwu89 commented 2 years ago

This is becoming relevant again to output population NAT for safir to calculate seroprevalence. @giovannic I'm assigning myself to this, I'll get to it in the next few weeks.

  1. Double renderer should be store a matrix, rows are persons, columns are timesteps, so that insertion is fastest (matrices stored column major). Remember to set storage.mode to the proper integer type, or could just do it in C++ and return NumericMatrix.
  2. Same for integer variable renderer.

One thing to be aware of is that if we resize variables mid-simulation, the size of rendered objects changes. Not sure yet how to deal with that, for now I'll assume we're not using resizable variables. One nice feature would be to only output at some subset of time points, such as every integer day, for models which update with small dt.

giovannic commented 2 years ago

Very cool!

if we resize variables mid-simulation

That's cool, let's not support resizeable variables until we understand what works for standard variables

slwu89 commented 2 years ago

That's cool, let's not support resizeable variables until we understand what works for standard variables

We may not even need to do anything, based on what Azra and Alexandra need in safir, we can just do the appropriate summary of data in the render process as double_count_render_process_daily in this vignette! https://mrc-ide.github.io/safir/articles/vaccine_notypes.html#run-safir