-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Age distributions #205
Merged
Merged
Age distributions #205
Changes from 31 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
64db0c7
setting up test
robynstuart 6c5dd24
add datafiles
robynstuart 1d732a3
add age
robynstuart b3283de
not working
robynstuart a9f8f7b
still not right
robynstuart 0264e9b
no good
robynstuart 5e52b1f
move order
robynstuart 0221813
restored previous version
robynstuart 5c9fa10
add scaling
robynstuart 9218b7c
working but wrong
robynstuart 75dfaee
wrong interpretation of asfr
robynstuart 14287c3
still not right
robynstuart 2eeefa2
comparing
robynstuart d24e1f2
add more filters
robynstuart 3e5e613
revert
robynstuart 37ad14a
off by 33 per cent
robynstuart 4667442
deciding not to rename metadata
robynstuart febe4b7
fix conflicts
robynstuart 861ab97
with cbr
robynstuart 998ecd0
remov extra cols
robynstuart 350bab2
update with new result
robynstuart b527a96
one test passing
robynstuart 6f878ba
add test data
robynstuart 9054b2e
rerun tests
robynstuart 4da892a
finally working
robynstuart cd94506
update changelog
robynstuart 53d79ec
resolve merge conflicts
cliffckerr cd897c4
update baseline and benchmark
cliffckerr 3df7894
updates for merge
robynstuart 164c99c
reinstate comment
robynstuart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,9 +65,9 @@ def standardize_birth_data(self): | |
return birth_rate | ||
|
||
def init_results(self, sim): | ||
self.results += ss.Result(self.name, 'new', sim.npts, dtype=int) | ||
self.results += ss.Result(self.name, 'cumulative', sim.npts, dtype=int) | ||
self.results += ss.Result(self.name, 'cbr', sim.npts, dtype=int) | ||
self.results += ss.Result(self.name, 'new', sim.npts, dtype=int, scale=True) | ||
self.results += ss.Result(self.name, 'cumulative', sim.npts, dtype=int, scale=True) | ||
self.results += ss.Result(self.name, 'cbr', sim.npts, dtype=int, scale=False) | ||
return | ||
|
||
def update(self, sim): | ||
|
@@ -94,6 +94,7 @@ def add_births(self, sim): | |
# Add n_new births to each state in the sim | ||
n_new = self.get_births(sim) | ||
new_uids = sim.people.grow(n_new) | ||
sim.people.age[new_uids] = 0 | ||
return new_uids | ||
|
||
def update_results(self, n_new, sim): | ||
|
@@ -102,7 +103,7 @@ def update_results(self, n_new, sim): | |
def finalize(self, sim): | ||
super().finalize(sim) | ||
self.results['cumulative'] = np.cumsum(self.results['new']) | ||
self.results['cbr'] = np.divide(self.results['new'], sim.results['n_alive'], where=sim.results['n_alive']>0) | ||
self.results['cbr'] = 1000*np.divide(self.results['new'], sim.results['n_alive'], where=sim.results['n_alive']>0) | ||
|
||
|
||
class background_deaths(DemographicModule): | ||
|
@@ -202,9 +203,9 @@ def standardize_death_data(self): | |
return death_rate | ||
|
||
def init_results(self, sim): | ||
self.results += ss.Result(self.name, 'new', sim.npts, dtype=int) | ||
self.results += ss.Result(self.name, 'cumulative', sim.npts, dtype=int) | ||
self.results += ss.Result(self.name, 'cmr', sim.npts, dtype=int) | ||
self.results += ss.Result(self.name, 'new', sim.npts, dtype=int, scale=True) | ||
self.results += ss.Result(self.name, 'cumulative', sim.npts, dtype=int, scale=True) | ||
self.results += ss.Result(self.name, 'cmr', sim.npts, dtype=int, scale=False) | ||
return | ||
|
||
def update(self, sim): | ||
|
@@ -223,8 +224,9 @@ def update_results(self, n_deaths, sim): | |
self.results['new'][sim.ti] = n_deaths | ||
|
||
def finalize(self, sim): | ||
super().finalize(sim) | ||
self.results['cumulative'] = np.cumsum(self.results['new']) | ||
self.results['cmr'] = np.divide(self.results['new'], sim.results['n_alive'], where=sim.results['n_alive']>0) | ||
self.results['cmr'] = 1000*np.divide(self.results['new'], sim.results['n_alive'], where=sim.results['n_alive']>0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again here, units? |
||
|
||
|
||
class Pregnancy(DemographicModule): | ||
|
@@ -247,9 +249,9 @@ def __init__(self, pars=None, metadata=None): | |
'dur_postpartum': 0.5, # Make this a distribution? | ||
'fertility_rate': 0, # Usually this will be provided in CSV format | ||
'rel_fertility': 1, | ||
'maternal_death_rate': 0.15, | ||
'maternal_death_rate': 0, | ||
'sex_ratio': 0.5, # Ratio of babies born female | ||
'units': 1e-3, # Assumes fertility rates are per 1000. If using percentages, switch this to 1 | ||
'units': 1e-3, # ??? | ||
}, self.pars) | ||
|
||
# Process metadata. Defaults here are the labels used by UN data | ||
|
@@ -287,25 +289,22 @@ def make_fertility_prob_fn(module, sim, uids): | |
val_label = module.metadata.data_cols['value'] | ||
|
||
available_years = module.pars.fertility_rate[year_label].unique() | ||
year_ind = sc.findnearest(available_years, sim.year) | ||
year_ind = sc.findnearest(available_years, sim.year-module.pars.dur_pregnancy) | ||
nearest_year = available_years[year_ind] | ||
|
||
df = module.pars.fertility_rate.loc[module.pars.fertility_rate[year_label] == nearest_year] | ||
conception_arr = df[val_label].values | ||
conception_arr = np.append(conception_arr, 0) # Add zeros for those outside data range | ||
df_arr = df[val_label].values # Pull out dataframe values | ||
df_arr = np.append(df_arr, 0) # Add zeros for those outside data range | ||
|
||
# Process age data | ||
age_bins = df[age_label].unique() | ||
age_bins = np.append(age_bins, 50) | ||
age_inds = np.digitize(sim.people.age[uids], age_bins) - 1 | ||
age_inds[age_inds>=max(age_inds)] = -1 # This ensures women outside the data range will get a value of 0 | ||
|
||
# Make array of fertility rates - TODO, check indexing works | ||
# Make array of fertility rates | ||
fertility_rate = pd.Series(index=uids) | ||
fertility_rate[uids] = conception_arr[age_inds] | ||
fertility_rate[uids[sim.people.male[uids]]] = 0 | ||
fertility_rate[uids[(sim.people.age < 0)[uids]]] = 0 | ||
fertility_rate[uids[(sim.people.age > max(age_inds))[uids]]] = 0 | ||
fertility_rate[uids] = df_arr[age_inds] | ||
|
||
# Scale from rate to probability. Consider an exponential here. | ||
fertility_prob = fertility_rate * (module.pars.units * module.pars.rel_fertility * sim.pars.dt) | ||
|
@@ -330,8 +329,9 @@ def init_results(self, sim): | |
Still unclear whether this logic should live in the pregnancy module, the | ||
individual disease modules, the connectors, or the sim. | ||
""" | ||
self.results += ss.Result(self.name, 'pregnancies', sim.npts, dtype=int) | ||
self.results += ss.Result(self.name, 'births', sim.npts, dtype=int) | ||
self.results += ss.Result(self.name, 'pregnancies', sim.npts, dtype=int, scale=True) | ||
self.results += ss.Result(self.name, 'births', sim.npts, dtype=int, scale=True) | ||
self.results += ss.Result(self.name, 'cbr', sim.npts, dtype=int, scale=False) | ||
return | ||
|
||
def update(self, sim): | ||
|
@@ -356,7 +356,7 @@ def update_states(self, sim): | |
self.ti_delivery[deliveries] = sim.ti | ||
|
||
# Check for new women emerging from post-partum | ||
postpartum = ~self.pregnant & (self.ti_postpartum == sim.ti) | ||
postpartum = ~self.pregnant & (self.ti_postpartum <= sim.ti) | ||
self.postpartum[postpartum] = False | ||
self.susceptible[postpartum] = True | ||
self.ti_postpartum[postpartum] = sim.ti | ||
|
@@ -370,14 +370,11 @@ def update_states(self, sim): | |
def make_pregnancies(self, sim): | ||
""" | ||
Select people to make pregnancy using incidence data | ||
This should use ASFR data from https://population.un.org/wpp/Download/Standard/Fertility/ | ||
""" | ||
# Abbreviate key variables | ||
# Abbreviate | ||
ppl = sim.people | ||
|
||
# If incidence of pregnancy is non-zero, make some cases | ||
# Think about how to deal with age/time-varying fertility | ||
denom_conds = ppl.female & self.susceptible | ||
denom_conds = ppl.female & self.susceptible & ppl.alive | ||
inds_to_choose_from = ss.true(denom_conds) | ||
uids = self.fertility_dist.filter(inds_to_choose_from) | ||
|
||
|
@@ -390,10 +387,9 @@ def make_pregnancies(self, sim): | |
|
||
# Grow the arrays and set properties for the unborn agents | ||
new_uids = sim.people.grow(len(new_slots)) | ||
|
||
sim.people.age[new_uids] = -self.pars.dur_pregnancy | ||
sim.people.slot[new_uids] = new_slots # Before sampling female_dist | ||
sim.people.female[new_uids] = self.sex_dist.rvs(uids) | ||
sim.people.female[new_uids] = self.sex_dist.rvs(new_uids) | ||
|
||
# Add connections to any vertical transmission layers | ||
# Placeholder code to be moved / refactored. The maternal network may need to be | ||
|
@@ -436,3 +432,7 @@ def update_results(self, sim): | |
self.results['pregnancies'][sim.ti] = np.count_nonzero(self.ti_pregnant == sim.ti) | ||
self.results['births'][sim.ti] = np.count_nonzero(self.ti_delivery == sim.ti) | ||
return | ||
|
||
def finalize(self, sim): | ||
super().finalize(sim) | ||
self.results['cbr'] = 1000* np.divide(self.results['births'], sim.results['n_alive'], where=sim.results['n_alive']>0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this use units instead of 1000?