Skip to content

Commit

Permalink
Properties should use the convention N, NValue
Browse files Browse the repository at this point in the history
Fixes #120
  • Loading branch information
k88hudson-cfa committed Dec 11, 2024
1 parent e6a6020 commit c7e08a6
Show file tree
Hide file tree
Showing 22 changed files with 206 additions and 203 deletions.
2 changes: 1 addition & 1 deletion examples/births-deaths/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Infected individuals are scheduled to recover based on the infection period. The
```rust
let plan_id = context
.add_plan(recovery_time, move |context| {
context.set_person_property(person_id, InfectionStatusType, InfectionStatus::R);
context.set_person_property(person_id, InfectionStatus, InfectionStatusValue::R);
})
.clone();
let plans_data_container = context.get_data_container_mut(InfectionPlansPlugin);
Expand Down
8 changes: 4 additions & 4 deletions examples/births-deaths/incidence_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::Path;
use std::path::PathBuf;

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

use serde::{Deserialize, Serialize};
Expand All @@ -21,14 +21,14 @@ struct IncidenceReportItem {
person_id: String,
age_group: AgeGroupRisk,
age: u8,
infection_status: InfectionStatus,
infection_status: InfectionStatusValue,
}

create_report_trait!(IncidenceReportItem);

fn handle_infection_status_change(
context: &mut Context,
event: PersonPropertyChangeEvent<InfectionStatusType>,
event: PersonPropertyChangeEvent<InfectionStatus>,
) {
let age_person = context.get_person_property(event.person_id, Age);
let age_group_person = context.get_person_property(event.person_id, AgeGroupFoi);
Expand All @@ -53,7 +53,7 @@ pub fn init(context: &mut Context) -> Result<(), IxaError> {

context.add_report::<IncidenceReportItem>(&parameters.output_file)?;
context.subscribe_to_event(
|context, event: PersonPropertyChangeEvent<InfectionStatusType>| {
|context, event: PersonPropertyChangeEvent<InfectionStatus>| {
handle_infection_status_change(context, event);
},
);
Expand Down
18 changes: 9 additions & 9 deletions examples/births-deaths/infection_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::population_manager::Alive;
use crate::population_manager::InfectionStatus;
use crate::population_manager::InfectionStatusType;
use crate::population_manager::InfectionStatusValue;
use crate::Parameters;
use ixa::context::Context;
use ixa::define_data_plugin;
Expand Down Expand Up @@ -37,7 +37,7 @@ fn schedule_recovery(context: &mut Context, person_id: PersonId) {

if context.get_person_property(person_id, Alive) {
let plan_id = context.add_plan(recovery_time, move |context| {
context.set_person_property(person_id, InfectionStatusType, InfectionStatus::R);
context.set_person_property(person_id, InfectionStatus, InfectionStatusValue::R);
});
let plans_data_container = context.get_data_container_mut(InfectionPlansPlugin);
plans_data_container
Expand Down Expand Up @@ -70,12 +70,12 @@ fn cancel_recovery_plans(context: &mut Context, person_id: PersonId) {

fn handle_infection_status_change(
context: &mut Context,
event: PersonPropertyChangeEvent<InfectionStatusType>,
event: PersonPropertyChangeEvent<InfectionStatus>,
) {
if matches!(event.current, InfectionStatus::I) {
if matches!(event.current, InfectionStatusValue::I) {
schedule_recovery(context, event.person_id);
}
if matches!(event.current, InfectionStatus::R) {
if matches!(event.current, InfectionStatusValue::R) {
remove_recovery_plan_data(context, event.person_id);
}
}
Expand All @@ -87,7 +87,7 @@ fn handle_person_removal(context: &mut Context, event: PersonPropertyChangeEvent
}
pub fn init(context: &mut Context) {
context.subscribe_to_event(
move |context, event: PersonPropertyChangeEvent<InfectionStatusType>| {
move |context, event: PersonPropertyChangeEvent<InfectionStatus>| {
handle_infection_status_change(context, event);
},
);
Expand Down Expand Up @@ -134,8 +134,8 @@ mod test {
init(&mut context);

context.subscribe_to_event(
move |context, event: PersonPropertyChangeEvent<InfectionStatusType>| {
if matches!(event.current, InfectionStatus::R) {
move |context, event: PersonPropertyChangeEvent<InfectionStatus>| {
if matches!(event.current, InfectionStatusValue::R) {
*context.get_data_container_mut(RecoveryPlugin) += 1;
}
},
Expand All @@ -146,7 +146,7 @@ mod test {
let person = context.create_new_person(0);

context.add_plan(1.0, move |context| {
context.set_person_property(person, InfectionStatusType, InfectionStatus::I);
context.set_person_property(person, InfectionStatus, InfectionStatusValue::I);
});

if index == 0 {
Expand Down
9 changes: 6 additions & 3 deletions examples/births-deaths/population_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use serde::Serialize;
use std::fmt;

#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy, Serialize, Deserialize)]
pub enum InfectionStatus {
pub enum InfectionStatusValue {
S,
I,
R,
Expand All @@ -35,8 +35,11 @@ impl fmt::Display for AgeGroupRisk {
}
}

define_person_property_with_default!(InfectionStatusType, InfectionStatus, InfectionStatus::S);

define_person_property_with_default!(
InfectionStatus,
InfectionStatusValue,
InfectionStatusValue::S
);
define_person_property!(Age, u8);
define_person_property_with_default!(Alive, bool, true);
define_derived_property!(AgeGroupFoi, AgeGroupRisk, [Age], |age| {
Expand Down
10 changes: 5 additions & 5 deletions examples/births-deaths/transmission_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::parameters_loader::Foi;
use crate::population_manager::AgeGroupRisk;
use crate::population_manager::ContextPopulationExt;
use crate::population_manager::InfectionStatus;
use crate::population_manager::InfectionStatusType;
use crate::population_manager::InfectionStatusValue;
use crate::Parameters;
use rand_distr::Exp;

Expand All @@ -29,11 +29,11 @@ fn attempt_infection(context: &mut Context, age_group: AgeGroupRisk) {
if population_size > 0 {
let person_to_infect = context.sample_person(age_group).unwrap();

let person_status: InfectionStatus =
context.get_person_property(person_to_infect, InfectionStatusType);
let person_status: InfectionStatusValue =
context.get_person_property(person_to_infect, InfectionStatus);

if matches!(person_status, InfectionStatus::S) {
context.set_person_property(person_to_infect, InfectionStatusType, InfectionStatus::I);
if matches!(person_status, InfectionStatusValue::S) {
context.set_person_property(person_to_infect, InfectionStatus, InfectionStatusValue::I);
}
#[allow(clippy::cast_precision_loss)]
let next_attempt_time = context.get_current_time()
Expand Down
12 changes: 6 additions & 6 deletions examples/load-people/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ For example, this model implements Age and RiskStatus using

```rust
#[derive(Copy)]
pub enum RiskCategory { High, Low }
pub enum RiskCategoryValue { High, Low }

define_person_property!(Age, u8);
define_person_property!(RiskCategoryType, RiskCategory);
define_person_property!(RiskCategory, RiskCategoryValue);
```

Person property value types **must** implement `Copy` in order to make them efficient
Expand Down Expand Up @@ -73,8 +73,8 @@ status:

```rust
#[derive(Copy)]
pub enum DiseaseStatus { S, I, R }
define_person_property_with_default!(DiseaseStatusType, DiseaseStatus, DiseaseStatus::S);
pub enum DiseaseStatusValue { S, I, R }
define_person_property_with_default!(DiseaseStatus, DiseaseStatusValue, DiseaseStatus::S);
```

#### Custom initializer
Expand Down Expand Up @@ -133,7 +133,7 @@ You can see an example of this in `population_loader.rs`:
```rust
let person = context.add_person();
context.set_person_property(person, Age, record.age);
context.set_person_property(person, RiskCategoryType, record.risk_category);
context.set_person_property(person, RiskCategory, record.risk_category);
let (vaccine_efficacy, vaccine_type) = context.generate_vaccine_props(record.risk_category);
context.set_person_property(person, VaccineEfficacy, vaccine_efficacy);
context.set_person_property(person, VaccineType, vaccine_type);
Expand All @@ -151,7 +151,7 @@ that when properties are first set, they will *not* emit any change events;

```rust
context.subscribe_to_event(
|_context, event: PersonPropertyChangeEvent<DiseaseStatusType>| {
|_context, event: PersonPropertyChangeEvent<DiseaseStatus>| {
let person = event.person_id;
println!(
"Person {} changed disease status from {:?} to {:?}",
Expand Down
4 changes: 2 additions & 2 deletions examples/load-people/logger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
population_loader::Age,
sir::DiseaseStatusType,
sir::DiseaseStatus,
vaccine::{VaccineDoses, VaccineEfficacy, VaccineType},
};
use ixa::{
Expand All @@ -12,7 +12,7 @@ pub fn init(context: &mut Context) {
// This subscribes to the disease status change events
// Note that no event gets fired when the property is set the first time
context.subscribe_to_event(
|_context, event: PersonPropertyChangeEvent<DiseaseStatusType>| {
|_context, event: PersonPropertyChangeEvent<DiseaseStatus>| {
let person = event.person_id;
println!(
"{:?} changed disease status from {:?} to {:?}",
Expand Down
14 changes: 7 additions & 7 deletions examples/load-people/population_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ use ixa::{ContextPeopleExt, PersonId};
use serde::Deserialize;

#[derive(Deserialize, Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum RiskCategory {
pub enum RiskCategoryValue {
High,
Low,
}

#[derive(Deserialize, Debug)]
struct PeopleRecord {
age: u8,
risk_category: RiskCategory,
risk_category: RiskCategoryValue,
}

define_person_property!(Age, u8);
define_person_property!(RiskCategoryType, RiskCategory);
define_person_property!(RiskCategory, RiskCategoryValue);

fn create_person_from_record(context: &mut Context, record: &PeopleRecord) -> PersonId {
let (t, e) = context.get_vaccine_props(record.risk_category);
context
.add_person((
(Age, record.age),
(RiskCategoryType, record.risk_category),
(RiskCategory, record.risk_category),
(VaccineType, t),
(VaccineEfficacy, e),
))
Expand Down Expand Up @@ -75,8 +75,8 @@ mod tests {

// Define expected computed values for each person
let expected_computed = vec![
(20, RiskCategory::Low, VaccineTypeValue::B, 0.8, 1),
(80, RiskCategory::High, VaccineTypeValue::A, 0.9, 2),
(20, RiskCategoryValue::Low, VaccineTypeValue::B, 0.8, 1),
(80, RiskCategoryValue::High, VaccineTypeValue::A, 0.9, 2),
];

let mut context = Context::new();
Expand Down Expand Up @@ -105,7 +105,7 @@ mod tests {

assert_eq!(context.get_person_property(person, Age), age);
assert_eq!(
context.get_person_property(person, RiskCategoryType),
context.get_person_property(person, RiskCategory),
risk_category
);
assert_eq!(
Expand Down
36 changes: 17 additions & 19 deletions examples/load-people/sir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ use ixa::{
};

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum DiseaseStatus {
pub enum DiseaseStatusValue {
S,
I,
R,
}

define_person_property_with_default!(DiseaseStatusType, DiseaseStatus, DiseaseStatus::S);
define_person_property_with_default!(DiseaseStatus, DiseaseStatusValue, DiseaseStatusValue::S);

pub fn init(context: &mut Context) {
context.subscribe_to_event(move |context, event: PersonCreatedEvent| {
let person = event.person_id;
context.add_plan(1.0, move |context| {
context.set_person_property(person, DiseaseStatusType, DiseaseStatus::I);
context.set_person_property(person, DiseaseStatus, DiseaseStatusValue::I);
});
context.add_plan(2.0, move |context| {
context.set_person_property(person, DiseaseStatusType, DiseaseStatus::R);
context.set_person_property(person, DiseaseStatus, DiseaseStatusValue::R);
});
});
}
Expand All @@ -39,29 +39,27 @@ mod tests {

// People should start in the S state
assert_eq!(
context.get_person_property(person, DiseaseStatusType),
DiseaseStatus::S
context.get_person_property(person, DiseaseStatus),
DiseaseStatusValue::S
);

// At 1.0, people should be in the I state
context.subscribe_to_event(
|context, event: PersonPropertyChangeEvent<DiseaseStatusType>| {
let person = event.person_id;
if context.get_current_time() == 1.0 {
assert_eq!(
context.get_person_property(person, DiseaseStatusType),
DiseaseStatus::I
);
}
},
);
context.subscribe_to_event(|context, event: PersonPropertyChangeEvent<DiseaseStatus>| {
let person = event.person_id;
if context.get_current_time() == 1.0 {
assert_eq!(
context.get_person_property(person, DiseaseStatus),
DiseaseStatusValue::I
);
}
});

context.execute();

// People should end up in the R state by the end of the simulation
assert_eq!(
context.get_person_property(person, DiseaseStatusType),
DiseaseStatus::R
context.get_person_property(person, DiseaseStatus),
DiseaseStatusValue::R
);
}
}
8 changes: 4 additions & 4 deletions examples/load-people/vaccine.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::hash::Hash;

use crate::population_loader::{Age, RiskCategory};
use crate::population_loader::{Age, RiskCategoryValue};
use ixa::{
context::Context, define_person_property, define_rng, people::ContextPeopleExt,
random::ContextRandomExt,
Expand Down Expand Up @@ -28,15 +28,15 @@ define_person_property!(VaccineDoses, u8, |context: &Context, person_id| {
});

pub trait ContextVaccineExt {
fn get_vaccine_props(&self, risk: RiskCategory) -> (VaccineTypeValue, OrderedFloat<f64>);
fn get_vaccine_props(&self, risk: RiskCategoryValue) -> (VaccineTypeValue, OrderedFloat<f64>);
}

impl ContextVaccineExt for Context {
fn get_vaccine_props(
self: &Context,
risk: RiskCategory,
risk: RiskCategoryValue,
) -> (VaccineTypeValue, OrderedFloat<f64>) {
if risk == RiskCategory::High {
if risk == RiskCategoryValue::High {
(VaccineTypeValue::A, OrderedFloat(0.9))
} else {
(VaccineTypeValue::B, OrderedFloat(0.8))
Expand Down
8 changes: 4 additions & 4 deletions examples/parameter-loading/incidence_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ixa::{create_report_trait, report::Report};
use std::path::PathBuf;

use crate::InfectionStatus;
use crate::InfectionStatusType;
use crate::InfectionStatusValue;
use serde::{Deserialize, Serialize};

use crate::Parameters;
Expand All @@ -16,14 +16,14 @@ use crate::Parameters;
struct IncidenceReportItem {
time: f64,
person_id: String,
infection_status: InfectionStatus,
infection_status: InfectionStatusValue,
}

create_report_trait!(IncidenceReportItem);

fn handle_infection_status_change(
context: &mut Context,
event: PersonPropertyChangeEvent<InfectionStatusType>,
event: PersonPropertyChangeEvent<InfectionStatus>,
) {
context.send_report(IncidenceReportItem {
time: context.get_current_time(),
Expand All @@ -42,7 +42,7 @@ pub fn init(context: &mut Context) -> Result<(), IxaError> {
.directory(PathBuf::from(parameters.output_dir));
context.add_report::<IncidenceReportItem>(&parameters.output_file)?;
context.subscribe_to_event(
|context, event: PersonPropertyChangeEvent<InfectionStatusType>| {
|context, event: PersonPropertyChangeEvent<InfectionStatus>| {
handle_infection_status_change(context, event);
},
);
Expand Down
Loading

0 comments on commit c7e08a6

Please sign in to comment.