DemographicsGenerator
DemographicsGenerator
Generates demographics file based on population input file. The population input file is csv with structure
node_label, [lat], [lon], [pop]
[columns] are optional
Source code in emod_api/demographics/DemographicsGenerator.py
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 |
|
__init__(nodes, concerns=None, res_in_arcsec=CUSTOM_RESOLUTION, node_id_from_lat_long=False)
Initialize the Demographics generator Args: nodes: list of nodes node_concern (Optional[DemographicsNodeGeneratorConcern]): What DemographicsNodeGeneratorConcern should we apply. If not specified, we use the DefaultWorldBankEquilibriumConcern demographics_concern (Optional[DemographicsGeneratorConcern]): Any concern generator we need to execute after the Demographics object has been generated, but not saved res_in_arcsec: Simulation grid resolution
Source code in emod_api/demographics/DemographicsGenerator.py
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 |
|
__to_grid_file(grid_file_name, demographics, include_attributes=None, node_attributes=None)
staticmethod
Convert a demographics object(Full object represented as a nested dictionary) to a grid file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grid_file_name
|
str
|
Name of grid file to save |
required |
demographics
|
Demographics
|
Demographics object |
required |
include_attributes
|
Optional[List[str]]
|
Attributes to include in export |
None
|
node_attributes
|
Optional[List[str]]
|
Optional list of attributes from the NodeAttributes path to include |
None
|
Returns:
Source code in emod_api/demographics/DemographicsGenerator.py
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 |
|
generate_demographics()
return all demographics file components in a single dictionary; a valid DTK demographics file when dumped as json
Source code in emod_api/demographics/DemographicsGenerator.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
generate_metadata()
generate demographics file metadata
Source code in emod_api/demographics/DemographicsGenerator.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
|
generate_nodes(defaults)
generate demographics file nodes
The process for generating nodes starts with looping through the loaded demographics nodes. For each node, we: 1. First determine the node's id. If the node has a forced id set, we use that. If we are using a custom resolution, we use the index(ie 1, 2, 3...). Lastly, we build the node id from the lat and lon id of the node
-
We then start to populate the node_attributes and individual attributes for the current node. The node_attributes will have data loaded from the initial nodes fed into DemographicsGenerator. The individual attributes start off as an empty dict.
-
We next determine the birthrate for the node. If the node attributes contains a Country element, we first lookup the birthrate from the World Pop data. We then build a MortalityDistribution configuration with country specific configuration elements and add that to the individual attributes. If there is no Country element in the node attributes, we set the birth rate to the default_birth_rate. This value was set in initialization of the DemographicsGenerator to the birth rate of the specified country from the world pop data
-
We then calculate the per_node_birth_rate using get_per_node_birth_rate and then set the birth rate on the node attributes
-
We then calculate the equilibrium_age_distribution and use that to create the AgeDistribution in individual_attributes
-
We then add each new demographic node to a list to end returned at the end of the function
Source code in emod_api/demographics/DemographicsGenerator.py
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 |
|
set_resolution(res_in_arcsec)
The canonical way to set arcsecond/degree resolutions on a DemographicsGenerator object. Verifies everything is set properly
Parameters:
Name | Type | Description | Default |
---|---|---|---|
res_in_arcsec
|
Literal[30, 250, CUSTOM_RESOLUTION]
|
The requested resolution. e.g. 30, 250, 'custom' |
required |
Returns: No return value.
Source code in emod_api/demographics/DemographicsGenerator.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
DemographicsType
Bases: Enum
Enum
Source code in emod_api/demographics/DemographicsGenerator.py
40 41 42 43 44 45 46 47 |
|
InvalidResolution
Bases: BaseException
Custom Exception
Source code in emod_api/demographics/DemographicsGenerator.py
33 34 35 36 37 |
|
arcsec_to_deg(arcsec)
Arc second to degrees Args: arcsec: arcsecond as float
Returns:
Type | Description |
---|---|
float
|
arc second converted to degrees |
Source code in emod_api/demographics/DemographicsGenerator.py
50 51 52 53 54 55 56 57 58 59 |
|
from_dataframe(df, demographics_filename=None, concerns=None, res_in_arcsec=CUSTOM_RESOLUTION, node_id_from_lat_long=True, default_population=1000, load_other_columns_as_attributes=False, include_columns=None, exclude_columns=None, nodeid_column_name=None, latitude_column_name='lat', longitude_column_name='lon', population_column_name='pop')
Generates a demographics file from a dataframe
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
pandas DataFrame containing demographics information. Must contain all the columns specified by latitude_column_name, longitude_column_name. The population_column_name is optional. If not found, we fall back to default_population |
required |
demographics_filename
|
Optional[str]
|
demographics file to save the demographics file too. This is optional concerns (Optional[DemographicsNodeGeneratorConcern]): What DemographicsNodeGeneratorConcern should we apply. If not specified, we use the DefaultWorldBankEquilibriumConcern |
None
|
res_in_arcsec
|
Literal[30, 250, CUSTOM_RESOLUTION]
|
Resolution in Arcseconds |
CUSTOM_RESOLUTION
|
node_id_from_lat_long
|
bool
|
Determine if we should calculate the node id from the lat long. By default this is true unless you also set res_in_arcsec to CUSTOM_RESOLUTION. When not using lat/long for ids, the first fallback it to check the node for a forced id. If that is not found, we assign it an index as id |
True
|
load_other_columns_as_attributes
|
bool
|
Load additional columns from a csv file as node attributes |
False
|
include_columns
|
Optional[List[str]]
|
A list of columns that should be added as node attributes from the csv file. To be used in conjunction with load_other_columns_as_attributes. |
None
|
exclude_columns
|
Optional[List[str]]
|
A list of columns that should be ignored as attributes when load_other_columns_as_attributes is enabled. This cannot be combined with include_columns |
None
|
default_population
|
int
|
Default population. Only used if population_column_name does not exist |
1000
|
nodeid_column_name
|
Optional[str]
|
Column name to load nodeid values from |
None
|
latitude_column_name
|
str
|
Column name to load latitude values from |
'lat'
|
longitude_column_name
|
str
|
Column name to load longitude values from |
'lon'
|
population_column_name
|
str
|
Column name to load population values from |
'pop'
|
Returns:
Type | Description |
---|---|
Demographics
|
demographics file as a dictionary |
Source code in emod_api/demographics/DemographicsGenerator.py
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 367 368 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 |
|
from_file(population_input_file, demographics_filename=None, concerns=None, res_in_arcsec=CUSTOM_RESOLUTION, node_id_from_lat_long=True, default_population=1000, load_other_columns_as_attributes=False, include_columns=None, exclude_columns=None, nodeid_column_name=None, latitude_column_name='lat', longitude_column_name='lon', population_column_name='pop')
Generates a demographics file from a CSV population
Parameters:
Name | Type | Description | Default |
---|---|---|---|
population_input_file
|
str
|
CSV population file. Must contain all the columns specified by latitude_column_name, longitude_column_name. The population_column_name is optional. If not found, we fall back to default_population |
required |
demographics_filename
|
Optional[str]
|
demographics file to save the demographics file too. This is optional concerns (Optional[DemographicsNodeGeneratorConcern]): What DemographicsNodeGeneratorConcern should we apply. If not specified, we use the DefaultWorldBankEquilibriumConcern |
None
|
res_in_arcsec
|
Literal[30, 250, CUSTOM_RESOLUTION]
|
Resolution in Arcseconds |
CUSTOM_RESOLUTION
|
node_id_from_lat_long
|
bool
|
Determine if we should calculate the node id from the lat long. By default this is true unless you also set res_in_arcsec to CUSTOM_RESOLUTION. When not using lat/long for ids, the first fallback it to check the node for a forced id. If that is not found, we assign it an index as id |
True
|
load_other_columns_as_attributes
|
bool
|
Load additional columns from a csv file as node attributes |
False
|
include_columns
|
Optional[List[str]]
|
A list of columns that should be added as node attributes from the csv file. To be used in conjunction with load_other_columns_as_attributes. |
None
|
exclude_columns
|
Optional[List[str]]
|
A list of columns that should be ignored as attributes when load_other_columns_as_attributes is enabled. This cannot be combined with include_columns |
None
|
default_population
|
int
|
Default population. Only used if population_column_name does not exist |
1000
|
nodeid_column_name
|
Optional[str]
|
Column name to load nodeid values from |
None
|
latitude_column_name
|
str
|
Column name to load latitude values from |
'lat'
|
longitude_column_name
|
str
|
Column name to load longitude values from |
'lon'
|
population_column_name
|
str
|
Column name to load population values from |
'pop'
|
Returns:
Type | Description |
---|---|
Demographics
|
demographics file as a dictionary |
Source code in emod_api/demographics/DemographicsGenerator.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
|
validate_res_in_arcsec(res_in_arcsec)
Validate that the resolution is valid Args: res_in_arcsec: Resolution in arsecond. Supported values can be found in VALID_RESOLUTIONS
Returns:
Raise
KeyError: If the resolution is invalid, a key error is raised
Source code in emod_api/demographics/DemographicsGenerator.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|