Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

demographics_nested_arrays_fix #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,9 @@ def update_node(
keys = {k.lower(): k for k in node_attributes.keys()}

age_dist_config = {
"DistributionValues": [distval.tolist()],
"DistributionValues": distval.tolist(),
"ResultScaleFactor": 1,
"ResultValues": [resval.tolist()],
"ResultValues": resval.tolist(),
}
if "country" in keys and node_attributes[keys["country"]]:
node_individual_attributes.update(
Expand Down
46 changes: 18 additions & 28 deletions emod_api/demographics/DemographicsTemplates.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,9 @@ def EveryoneInitiallySusceptible( demog, setting=1.0 ):
suscDist = {
"SusceptibilityDist_Description": f"Everyone is initially susceptible with probability {setting}",
"SusceptibilityDistribution": {
"DistributionValues": [
[0, 36500]
],
"DistributionValues": [0, 36500],
"ResultScaleFactor": 1,
"ResultValues": [
[setting, setting]
]
"ResultValues": [setting, setting]
}
}
demog.SetDefaultFromTemplate( suscDist, _set_suscept_complex )
Expand All @@ -305,13 +301,9 @@ def StepFunctionSusceptibility( demog, protected_setting=0.0, threshold_age=365*
suscDist = {
"SusceptibilityDist_Description": "Youngers are somewhat protected",
"SusceptibilityDistribution": {
"DistributionValues": [
[0, threshold_age, threshold_age, 36500]
],
"DistributionValues": [0, threshold_age, threshold_age, 36500],
"ResultScaleFactor": 1,
"ResultValues": [
[protected_setting, protected_setting, 1.0, 1.0]
]
"ResultValues": [protected_setting, protected_setting, 1.0, 1.0]
}
}
demog.SetDefaultFromTemplate( suscDist, _set_suscept_complex )
Expand All @@ -335,31 +327,29 @@ def SimpleSusceptibilityDistribution( demog, meanAgeAtInfection=2.5):
# Calling code in emodpy will call this and pass the demographics instance then we
# call SetDefaultFromTemplate on the demog object so we can also pass the setter function
suscDist = {
"SusceptibilityDist_Description": f"Rough initialization to reduce burn-in and prevent huge outbreaks at sim start. Exponential distribution, Average age at infection ~{meanAgeAtInfection} years, minimum susceptibility is 2.5% at old ages",
"SusceptibilityDist_Description": f"Rough initialization to reduce burn-in and prevent huge outbreaks at "
f"sim start. Exponential distribution, Average age at infection "
f"~{meanAgeAtInfection} years, minimum susceptibility is 2.5% at old ages",
"SusceptibilityDistribution": {
"DistributionValues": [
[i * 365 for i in range(100)]
],
"DistributionValues": [i * 365 for i in range(100)],
"ResultScaleFactor": 1,
"ResultValues": [
[1.0, 1.0] + [0.025 + 0.975 * math.exp(-(i - 1) / (meanAgeAtInfection / math.log(2))) for i in range(2, 100, 1)]
]
"ResultValues": [1.0, 1.0] + [0.025 + 0.975 * math.exp(-(i - 1) / (meanAgeAtInfection / math.log(2)))
for i in range(2, 100, 1)]
}
}
demog.SetDefaultFromTemplate( suscDist, _set_suscept_complex )

def DefaultSusceptibilityDistribution( demog ):
# set config.Susceptibility_Initialization_Distribution_Type=COMPLEX
suscDist = {
"SusceptibilityDist_Description": "Rough initialization to reduce burn-in and prevent huge outbreaks at sim start. Exponential distribution, Average age at infection ~3.5 years, minimum susceptibility is 2.5% at old ages",
"SusceptibilityDist_Description": "Rough initialization to reduce burn-in and prevent huge outbreaks at sim "
"start. Exponential distribution, Average age at infection ~3.5 years,"
"minimum susceptibility is 2.5% at old ages",
"SusceptibilityDistribution": {
"DistributionValues": [
[i * 365 for i in range(100)]
],
"DistributionValues": [i * 365 for i in range(100)],
"ResultScaleFactor": 1,
"ResultValues": [
[1.0, 1.0] + [0.025 + 0.975 * math.exp(-(i - 1) / (2.5 / math.log(2))) for i in range(2, 100, 1)]
]
"ResultValues": [1.0, 1.0] + [0.025 + 0.975 * math.exp(-(i - 1) / (2.5 / math.log(2)))
for i in range(2, 100, 1)]
}
}
demog.SetDefaultFromTemplate( suscDist, _set_suscept_complex )
Expand Down Expand Up @@ -780,9 +770,9 @@ def demographicsBuilder(pop_dat_file: Path, base_year: int, start_year: int=1950
age_x = [0] + age_init_cdf.tolist()

ad = IndividualAttributes.AgeDistribution()
ad.distribution_values = [age_x]
ad.distribution_values = age_x
ad.result_scale_factor = 1
ad.result_values = [age_y]
ad.result_values = age_y

mort_vec_x = mortality_rate_x_values
mort_year = np.zeros(2*year_vec.shape[0]-3)
Expand Down
5 changes: 1 addition & 4 deletions tests/data/demographics/demographics_vd_ref.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"IndividualAttributes": {
"AgeDistribution": {
"DistributionValues": [
[
0,
0.1562234659361927,
0.28960631345041227,
Expand All @@ -25,11 +24,9 @@
0.9991245676421484,
0.9998673546338755,
0.9999999973471994
]
],
"ResultScaleFactor": 1,
"ResultValues": [
[
0,
1825,
3650,
Expand All @@ -51,7 +48,6 @@
32850,
34675,
36500
]
]
},
"MortalityDistributionMale": {
Expand Down Expand Up @@ -121,6 +117,7 @@
10.0,
14.9999,
15.0,
15.0,
19.9999,
20.0,
24.9999,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
"SusceptibilityDist_Description": "Rough initialization to reduce burn-in and prevent huge outbreaks at sim start. Exponential distribution, Average age at infection ~3.5 years, minimum susceptibility is 2.5% at old ages",
"SusceptibilityDistribution": {
"DistributionValues": [
[
0,
365,
730,
Expand Down Expand Up @@ -241,11 +240,9 @@
35405,
35770,
36135
]
],
"ResultScaleFactor": 1,
"ResultValues": [
[
1.0,
1.0,
0.7639118261738191,
Expand Down Expand Up @@ -346,7 +343,6 @@
0.025000000002688146,
0.025000000002037236,
0.025000000001543937
]
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
"SusceptibilityDist_Description": "Rough initialization to reduce burn-in and prevent huge outbreaks at sim start. Exponential distribution, Average age at infection ~3.5 years, minimum susceptibility is 2.5% at old ages",
"SusceptibilityDistribution": {
"DistributionValues": [
[
0,
365,
730,
Expand Down Expand Up @@ -241,11 +240,9 @@
35405,
35770,
36135
]
],
"ResultScaleFactor": 1,
"ResultValues": [
[
1.0,
1.0,
0.7639118261738191,
Expand Down Expand Up @@ -346,7 +343,6 @@
0.025000000002688146,
0.025000000002037236,
0.025000000001543937
]
]
}
},
Expand Down
4 changes: 0 additions & 4 deletions tests/data/demographics/single_node_demographics.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
"IndividualAttributes": {
"AgeDistribution": {
"DistributionValues": [
[
0.0,
0.09283128862223852,
0.1772211025454774,
Expand Down Expand Up @@ -129,11 +128,9 @@
1.0000000000000013,
1.0000000000000013,
1.0000000000000013
]
],
"ResultScaleFactor": 1,
"ResultValues": [
[
0.0,
894.0,
1788.0,
Expand Down Expand Up @@ -184,7 +181,6 @@
42012.0,
42906.0,
43800.0
]
]
},
"FertilityDistribution": {},
Expand Down
21 changes: 8 additions & 13 deletions tests/test_demog.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ def test_set_default_properties_fert_mort(self):
demog = Demographics.from_template_node()
c_birth_rate = 20
c_mort_rate = 30
default_susc = {"DistributionValues": [[i * 365 for i in range(100)]], "ResultScaleFactor": 1, "ResultValues": [[1.0, 1.0] + [0.025 + 0.975 * math.exp(-(i - 1) / (2.5 / math.log(2))) for i in range(2, 100, 1)]]}
default_susc = {"DistributionValues": [i * 365 for i in range(100)],
"ResultScaleFactor": 1,
"ResultValues": [1.0, 1.0] + [0.025 + 0.975 * math.exp(-(i - 1) / (2.5 / math.log(2)))
for i in range(2, 100, 1)]}

demog.SetDefaultPropertiesFertMort(crude_birth_rate=c_birth_rate, crude_mort_rate=c_mort_rate)
from emod_api.demographics.DemographicsTemplates import CrudeRate
Expand Down Expand Up @@ -274,13 +277,9 @@ def test_template_everyone_initially_susceptible(self):
template = DT.EveryoneInitiallySusceptible
demog = self.demog_template_test(template=template)
expect_susceptibility_distribution = {
"DistributionValues": [
[0, 36500]
],
"DistributionValues": [0, 36500],
"ResultScaleFactor": 1,
"ResultValues": [
[1.0, 1.0]
]
"ResultValues": [1.0, 1.0]
}
self.assertEqual(expect_susceptibility_distribution, demog.raw['Defaults']['IndividualAttributes'][
'SusceptibilityDistribution'])
Expand Down Expand Up @@ -1912,17 +1911,13 @@ def test_create_overlay_file_2(self):
"IndividualAttributes": {
"AgeDistribution": {
"DistributionValues": [
[
0.0,
1.0
]
],
"ResultScaleFactor": 1,
"ResultValues": [
[
0,
43769
]
]
},
"MortalityDistribution": {
Expand Down Expand Up @@ -1981,9 +1976,9 @@ def test_create_overlay_file_2(self):
}

individual_attributes = IndividualAttributes()
individual_attributes.age_distribution = IndividualAttributes.AgeDistribution(distribution_values=[[0.0, 1.0]],
individual_attributes.age_distribution = IndividualAttributes.AgeDistribution(distribution_values=[0.0, 1.0],
result_scale_factor=1,
result_values=[[0, 43769]])
result_values=[0, 43769])

individual_attributes.mortality_distribution = IndividualAttributes.MortalityDistribution(axis_names=["gender", "age"],
axis_scale_factors=[1, 1],
Expand Down
Loading