Skip to content

Commit

Permalink
Changed agegroups as a derived property
Browse files Browse the repository at this point in the history
  • Loading branch information
confunguido committed Oct 31, 2024
1 parent 86345d8 commit 703e226
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
10 changes: 5 additions & 5 deletions examples/births-deaths/demographics_report.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::population_manager::{
AgeGroupRisk,
Alive,Age,
ContextPopulationExt};
Alive,Age,AgeGroupFoi
};
use crate::Parameters;
use ixa::{
context::Context,
Expand All @@ -28,7 +28,7 @@ create_report_trait!(PersonReportItem);

fn handle_person_created(context: &mut Context, event: PersonCreatedEvent) {
let person = event.person_id;
let age_group_person = context.get_person_age_group(person);
let age_group_person = context.get_person_property(person, AgeGroupFoi);
context.send_report(PersonReportItem {
time: context.get_current_time(),
person_id: format!("{person}"),
Expand All @@ -41,7 +41,7 @@ fn handle_person_created(context: &mut Context, event: PersonCreatedEvent) {

fn handle_person_aging(context: &mut Context, event: PersonPropertyChangeEvent<Age>) {
let person = event.person_id;
let age_group_person = context.get_person_age_group(person);
let age_group_person = context.get_person_property(person, AgeGroupFoi);
context.send_report(PersonReportItem {
time: context.get_current_time(),
person_id: format!("{person}"),
Expand All @@ -55,7 +55,7 @@ fn handle_person_aging(context: &mut Context, event: PersonPropertyChangeEvent<A
fn handle_death_events(context: &mut Context, event: PersonPropertyChangeEvent<Alive>) {
if !event.current {
let person = event.person_id;
let age_group_person = context.get_person_age_group(person);
let age_group_person = context.get_person_property(person, AgeGroupFoi);
context.send_report(PersonReportItem {
time: context.get_current_time(),
person_id: format!("{person}"),
Expand Down
6 changes: 3 additions & 3 deletions examples/births-deaths/incidence_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::path::Path;
use std::path::PathBuf;

use crate::population_manager::{
AgeGroupRisk, Age,
ContextPopulationExt, InfectionStatus, InfectionStatusType,
AgeGroupRisk, Age, AgeGroupFoi,
InfectionStatus, InfectionStatusType,
};

use serde::{Deserialize, Serialize};
Expand All @@ -31,7 +31,7 @@ fn handle_infection_status_change(
event: PersonPropertyChangeEvent<InfectionStatusType>,
) {
let age_person = context.get_person_property(event.person_id, Age);
let age_group_person = context.get_person_age_group(event.person_id);
let age_group_person = context.get_person_property(event.person_id, AgeGroupFoi);
context.send_report(IncidenceReportItem {
time: context.get_current_time(),
person_id: format!("{}", event.person_id),
Expand Down
4 changes: 2 additions & 2 deletions examples/births-deaths/infection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ mod test {

let population_size: usize = 10;
for _ in 0..population_size {
let person = context.create_new_person(0.0);
let person = context.create_new_person(0);

context.add_plan(1.0, move |context| {
context.set_person_property(person, InfectionStatusType, InfectionStatus::I);
Expand All @@ -164,7 +164,7 @@ mod test {
context.init_random(42);
init(&mut context);

let person = context.create_new_person(0.0);
let person = context.create_new_person(0);
context.add_plan(1.1, move |context| {
cancel_recovery_plans(context, person);
});
Expand Down
64 changes: 34 additions & 30 deletions examples/births-deaths/population_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::parameters_loader::Parameters;
use ixa::{
define_global_property, define_person_property,
define_derived_property,
define_person_property_with_default,
context::Context,
global_properties::ContextGlobalPropertiesExt,
Expand Down Expand Up @@ -43,6 +44,15 @@ define_person_property_with_default!(InfectionStatusType, InfectionStatus, Infec

define_person_property!(Age, u8);
define_person_property!(Alive, bool);
define_derived_property!(AgeGroupFoi, AgeGroupRisk, [Age], |age| {
if age <= 1 {
AgeGroupRisk::NewBorn
} else if age <= 65 {
AgeGroupRisk::General
} else {
AgeGroupRisk::OldAdult
}
});

fn schedule_aging(context: &mut Context, person_id: PersonId) {
if context.get_person_property(person_id, Alive) {
Expand Down Expand Up @@ -120,7 +130,6 @@ pub fn init(context: &mut Context) {
pub trait ContextPopulationExt {
fn create_new_person(&mut self, age: u8) -> PersonId;
fn attempt_death(&mut self, person_id: PersonId);
fn get_person_age_group(&mut self, person_id: PersonId) -> AgeGroupRisk;
fn get_current_group_population(&mut self, age_group: AgeGroupRisk) -> usize;
fn sample_person(&mut self, age_group: AgeGroupRisk) -> Option<PersonId>;
#[allow(dead_code)]
Expand Down Expand Up @@ -152,23 +161,12 @@ impl ContextPopulationExt for Context {
person
}

fn get_person_age_group(&mut self, person_id: PersonId) -> AgeGroupRisk {
let current_age = self.get_person_property(person_id, Age);
if current_age <= 1 {
AgeGroupRisk::NewBorn
} else if current_age <= 65 {
AgeGroupRisk::General
} else {
AgeGroupRisk::OldAdult
}
}

fn get_current_group_population(&mut self, age_group: AgeGroupRisk) -> usize {
let mut current_population = 0;
for i in 0..self.get_current_population() {
let person_id = self.get_person_id(i);
if self.get_person_property(person_id, Alive)
&& self.get_person_age_group(person_id) == age_group
&& self.get_person_property(person_id, AgeGroupFoi) == age_group
{
current_population += 1;
}
Expand All @@ -181,7 +179,7 @@ impl ContextPopulationExt for Context {
for i in 0..self.get_current_population() {
let person_id = self.get_person_id(i);
if self.get_person_property(person_id, Alive)
&& self.get_person_age_group(person_id) == age_group
&& self.get_person_property(person_id, AgeGroupFoi) == age_group
{
people_vec.push(person_id);
}
Expand Down Expand Up @@ -243,29 +241,29 @@ mod test {
fn test_birth_death() {
let mut context = Context::new();

let person = context.create_new_person(-10.0);
context.add_plan(10.0, |context| {
_ = context.create_new_person(10.0);
let person = context.create_new_person(10);
context.add_plan(380.0, |context| {
_ = context.create_new_person(0);
});
context.add_plan(20.0, move |context| {
context.add_plan(400.0, move |context| {
context.attempt_death(person);
});
context.add_plan(11.0, |context| {
context.add_plan(390.0, |context| {
let pop = context.get_population_by_property(Alive, true);
assert_eq!(pop, 2);
});
context.add_plan(21.0, |context| {
context.add_plan(401.0, |context| {
let pop = context.get_population_by_property(Alive, true);
assert_eq!(pop, 1);
});
context.execute();
let population = context.get_current_population();

// Even if these people have died during simulation, we can still get their properties
let birth_day_0 = context.get_person_property(context.get_person_id(0), Birth);
let birth_day_1 = context.get_person_property(context.get_person_id(1), Birth);
assert_eq!(birth_day_0, -10.0);
assert_eq!(birth_day_1, 10.0);
let age_0 = context.get_person_property(context.get_person_id(0), Age);
let age_1 = context.get_person_property(context.get_person_id(1),Age);
assert_eq!(age_0, 10);
assert_eq!(age_1, 0);

// Ixa population contains all individuals ever created
assert_eq!(population, 2);
Expand Down Expand Up @@ -311,14 +309,14 @@ mod test {
let mut context = Context::new();
context.set_global_property_value(Parameters, p_values.clone());
context.init_random(p_values.seed);
let _person = context.create_new_person(-10.0);
let _person = context.create_new_person(0);
schedule_death(&mut context);
}

#[test]
fn test_sample_person_group() {
let mut context = Context::new();
let age_vec = vec![0.5, 5.0, 62.0, 80.0];
let age_vec = vec![0, 5, 62, 80];
let years = 5.0;
let age_groups = vec![
AgeGroupRisk::NewBorn,
Expand All @@ -327,14 +325,16 @@ mod test {
AgeGroupRisk::OldAdult,
];
for age in &age_vec {
let birth = age * (-365.0);
let _person = context.create_new_person(birth);
let _person = context.create_new_person(*age);
}

for p in 0..context.get_current_population() {
let person = context.get_person_id(p);
context.add_plan(365.0, move |context| {
schedule_aging(context, person);
});
let age_group = age_groups[p];
assert_eq!(age_group, context.get_person_age_group(person));
assert_eq!(age_group, context.get_person_property(person, AgeGroupFoi));
}

// Plan to check in 5 years
Expand All @@ -348,9 +348,13 @@ mod test {
for p in 0..context.get_current_population() {
let person = context.get_person_id(p);
let age_group = future_age_groups[p];
assert_eq!(age_group, context.get_person_age_group(person));
assert_eq!(age_group, context.get_person_property(person, AgeGroupFoi));
}
});

context.add_plan((years * 365.0) + 1.0, |context| {
context.shutdown();
});
context.execute();
}
}

0 comments on commit 703e226

Please sign in to comment.