year_age_rate
This module contains stuff related to a YearAgeRate dataframe object that can be used to create demographic objects.
YearAgeRate
The YearAgeRate class is a wrapper around a pandas dataframe such that the dataframe is expected to have a specific format that is used in the demographics. Objects of this class are used as output from UN World Pop functions and as input into the creation of a Demograhics object. This gives us a standard format of the data for most population-based demographic data.
The dataframe is expected to have four columns
- node_id: An integer representing the node the data is associated with
- min_year: A float representing the calendar start year of the period of years for which the represents.
- min_age: A float representing the age of the people in years.
- rate: The value (a float) of this column depends on the data contained. It can be a rate of fertility or mortality or a fraction of the population.
This format assumes that the year/age ranges are contiguous from one value to the next largest value. For example, if a row has a min_age = 15 and the next row of the same year with the next largest min_age is 20, then the data for the row where min_age = 15 is for the age range of [15-20), where 15 is included and 20 is excluded. This is the same for min_year.
For a given node id, the format assumes that each min_year has the exact same set of min_ages. Different nodes can have different min_years or min_ages, but within a given node, they must be the same. The format also assumes that min_year and min_age for a given node are not duplicated.
Source code in emodpy_hiv/demographics/year_age_rate.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
__init__(df=None, csv_filename=None)
Construct a YearAgeRate object from the given dataframe OR one that is contained in a CSV. You can only define the dataframe OR the csv_filename, bot NOT both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
A pandas dataframe that follows the format of a YearAgeRate object. |
None
|
csv_filename
|
str
|
A CSV file containing data for YearAgeRate dataframe. |
None
|
Source code in emodpy_hiv/demographics/year_age_rate.py
to_age_distributions()
Convert this YearAgeRate object ot a list of (node_id, AgeDistribution) tuples. For each node in the dataframe, there will be a tuple in the list where the first value is the node_id and the second is an AgeDistribution object that can be used when creating a Demographics object.
The "rate" column is assumed to be the fraction of people in that year and age range. The dataframe is also assumed to only have the data for one year.
NOTE: EMOD expects the ReslutValues/Ages to be maximums of the bin. This implies that if the last age has a DistributionValue = 1.0, then there should be no people aged greater than this last age. It also means that the first age is also a minimum. For example, if the first age were 1.0, then there can be zero people less than 1.0. !!!THIS APPLIES ONLY TO THE OUTPUT OF THIS FUNCTION AND NOT THE INPUT!!!
Source code in emodpy_hiv/demographics/year_age_rate.py
to_csv(csv_filename)
Save the dataframe to a csv file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_filename
|
str
|
The name of the file to write the dataframe to. |
required |
to_fertility_distributions()
Convert this YearAgeRate object to a list of tuples of node_id and FertilityDistribution.
The method assumes that the user wants the data in a step-wise format. That is, for a calendar year range and age range, the user wants EMOD to produce the same value/rate for the entire range. The rate doesn't change until the year or age moves to a new range.
For the max age of the last bin, a value of 125 is used and, for the max_year of the last bin, a value of 2100 is used. Using these maximums still produces the correct values because we want the result constant for the entire range. Having the range wider just produces the same constant.
Source code in emodpy_hiv/demographics/year_age_rate.py
to_mortality_distributions(stepwise_for_year=True)
Convert this YearAgeRate object to a dict of node_id: MortalityDistribution entries.
The method assumes that the user wants the data in a step-wise format. That is, for a calendar year range and age range, the user wants EMOD to produce the same value/rate for the entire range. The rate doesn't change until the year or age moves to a new range.
For the max age of the last bin, a value of 125 is used and, for the max_year of the last bin, a value of 2100 is used. Using these maximums still produces the correct values because we want the result constant for the entire range. Having the range wider just produces the same constant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stepwise_for_year
|
bool
|
If true, the age and calendar year both in step-wise format. If false, calendar year is adjust by 2.5 and the linear interpolation will be used between calendar years. |
True
|
Source code in emodpy_hiv/demographics/year_age_rate.py
plot(year_age_rate_list, title=None, node_id=0, img_dir=None, filename_to_save_to=None)
Create a plot window with one subplot for each min_age value and where the subplot will has min_year on the x-axis and "rate" on the y-axis. Each YearAgeRate object in the 'year_age_rate_list' will have one curve on each subplot
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
year_age_rate_list
|
List[YearAgeRate]
|
A list of YearAgeRate objects that have same min_age values for all min_year values. The min_year values do not need to be the same, just the min_ages. |
required |
title
|
str
|
The title of the plot window. |
None
|
node_id
|
int
|
Data will be extracted from the YearAgeRate objects for this node. |
0
|
img_dir
|
str
|
If this is defined, the images are saved to this directory. If not defined, the images are displayed in a window. |
None
|
filename_to_save_to
|
str
|
The name of the file to save the image to. This is only used if img_dir is defined. |
None
|
Source code in emodpy_hiv/demographics/year_age_rate.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |