From ab6f9d1babbdc605dea6dbc44b45beb35fe2e4df Mon Sep 17 00:00:00 2001 From: Chirag Kumar Date: Mon, 2 Dec 2024 18:44:29 -0500 Subject: [PATCH] Update person properties periodic report in example to use real API (#115) --- Cargo.toml | 2 - examples/time-varying-infection/main.rs | 6 +- .../time-varying-infection/periodic_report.rs | 67 ------------------- examples/time-varying-infection/plot_output.R | 2 +- .../population_loader.rs | 3 +- 5 files changed, 6 insertions(+), 74 deletions(-) delete mode 100644 examples/time-varying-infection/periodic_report.rs diff --git a/Cargo.toml b/Cargo.toml index 2769f8ae..24b3d367 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,6 @@ serde_derive = "1.0" serde_json = "1.0.128" reikna = "0.12.3" roots = "0.0.8" -strum = "0.26.3" -strum_macros = "0.26.4" ixa-derive = { path = "ixa-derive" } seq-macro = "0.3.5" paste = "1.0.15" diff --git a/examples/time-varying-infection/main.rs b/examples/time-varying-infection/main.rs index 53d56272..8445905d 100644 --- a/examples/time-varying-infection/main.rs +++ b/examples/time-varying-infection/main.rs @@ -2,13 +2,14 @@ use std::path::PathBuf; use ixa::error::IxaError; use ixa::random::ContextRandomExt; +use ixa::report::ContextReportExt; use ixa::{context::Context, global_properties::ContextGlobalPropertiesExt}; +use population_loader::DiseaseStatusType; mod exposure_manager; mod incidence_report; mod infection_manager; mod parameters_loader; -mod periodic_report; mod population_loader; use crate::parameters_loader::Parameters; @@ -31,7 +32,8 @@ fn initialize() -> Result { population_loader::init(&mut context); infection_manager::init(&mut context); incidence_report::init(&mut context)?; - periodic_report::init(&mut context)?; + // add periodic report + context.add_periodic_report("person_property_count", 1.0, (DiseaseStatusType,))?; context.add_plan(parameters.max_time, |context| { context.shutdown(); diff --git a/examples/time-varying-infection/periodic_report.rs b/examples/time-varying-infection/periodic_report.rs deleted file mode 100644 index fd526c65..00000000 --- a/examples/time-varying-infection/periodic_report.rs +++ /dev/null @@ -1,67 +0,0 @@ -use ixa::context::{Context, ExecutionPhase}; -use ixa::error::IxaError; -use ixa::global_properties::ContextGlobalPropertiesExt; -use ixa::people::ContextPeopleExt; -use ixa::report::ContextReportExt; -use ixa::{create_report_trait, report::Report}; -use std::path::PathBuf; -use strum::IntoEnumIterator; - -use serde::{Deserialize, Serialize}; - -use crate::parameters_loader::Parameters; -use crate::population_loader::{DiseaseStatus, DiseaseStatusType}; - -#[derive(Serialize, Deserialize, Clone)] -struct PeriodicReportItem { - day: f64, - // use a string because eventually we want - // to be able to report on any property - // and want to be able to use this one - // struct for all properties, even those of - // different types - property_value: String, - count: usize, -} - -create_report_trait!(PeriodicReportItem); - -fn count_people_and_send_report(context: &mut Context) { - for disease_state in DiseaseStatus::iter() { - let mut counter = 0; - for usize_id in 0..context.get_current_population() { - if context.get_person_property(context.get_person_id(usize_id), DiseaseStatusType) - == disease_state - { - counter += 1; - } - } - context.send_report(PeriodicReportItem { - day: context.get_current_time(), - // format macro returns a string which is what - // PeriodicReportItem struct expects - // for generality across properties - property_value: format!("{disease_state:?}"), - count: counter, - }); - } -} - -pub fn init(context: &mut Context) -> Result<(), IxaError> { - let parameters = context - .get_global_property_value(Parameters) - .unwrap() - .clone(); - context - .report_options() - .directory(PathBuf::from(parameters.output_dir)); - context.add_report::("person_property_count")?; - context.add_periodic_plan_with_phase( - parameters.report_period, - |context| { - count_people_and_send_report(context); - }, - ExecutionPhase::Last, - ); - Ok(()) -} diff --git a/examples/time-varying-infection/plot_output.R b/examples/time-varying-infection/plot_output.R index 7403488e..51cdfb83 100644 --- a/examples/time-varying-infection/plot_output.R +++ b/examples/time-varying-infection/plot_output.R @@ -63,7 +63,7 @@ person_property_report <- readr::read_csv(file.path( )) ggplot2::ggplot() + - geom_point(aes(day, count, color = property_value), person_property_report) + + geom_point(aes(t, count, color = DiseaseStatusType), person_property_report) + geom_line(aes(time_array_susc, expected_susc), color = "black") + xlab("Time") + ylab("People") + diff --git a/examples/time-varying-infection/population_loader.rs b/examples/time-varying-infection/population_loader.rs index 89f1736e..34f40759 100644 --- a/examples/time-varying-infection/population_loader.rs +++ b/examples/time-varying-infection/population_loader.rs @@ -4,11 +4,10 @@ use ixa::people::ContextPeopleExt; use ixa::{define_person_property, define_person_property_with_default}; use ordered_float::OrderedFloat; use serde::{Deserialize, Serialize}; -use strum_macros::EnumIter; use crate::parameters_loader::Parameters; -#[derive(Deserialize, Serialize, Copy, Clone, PartialEq, Eq, Debug, EnumIter, Hash)] +#[derive(Deserialize, Serialize, Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum DiseaseStatus { S, I,