Utility Functions
General Utilities
binom_0(n, p)
Mirrors scipy binom.pmf as used in code
Source code in titan/utils.py
180 181 182 183 184 |
|
connected_components(graph)
Get connected components in graph
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
the model's underlying graph |
required |
Returns:
Type | Description |
---|---|
list of connected components |
Source code in titan/utils.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
get_check_rand_int(seed)
Check the value passed of a seed, make sure it's an int, if 0, get a random seed
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed |
int
|
integer to check or replace with a seed |
required |
Returns:
Type | Description |
---|---|
int
|
validated seed |
Source code in titan/utils.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
get_cumulative_bin(rand_gen, bin_def)
Get the bin key given cumulative bins. A probability is selected at random, then each bin's prob
is compared to it, the first bin that has a cumulative prob
(e.g. for bin 2, the prob of bin 1 plus the prob of bin 2) less than or equal to that probability is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rand_gen |
random number generator |
required | |
bin_def |
ObjMap
|
ObjMap containing the bins |
required |
Returns:
Type | Description |
---|---|
int
|
integer key of the matched bin (or last bin if no matches) |
Source code in titan/utils.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
|
get_independent_bin(rand_gen, bin_def)
Get the bin key given independent bins. A probability is selected at random, then each bin's prob
is compared to it, the first bin that has a prob
less than or equal to that probability is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rand_gen |
A random number generator |
required | |
bin_def |
ObjMap
|
The ObjMap containing the bins |
required |
Returns:
Type | Description |
---|---|
int
|
The integer key of the matched bin (or last bin if no matches) |
Source code in titan/utils.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|
get_param_from_path(params, param_path, delimiter)
Given a params object and a delimited path, get the leaf of the params tree and the last key to access it
Source code in titan/utils.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
grid_file_to_edge_yml(file_path, outfile_path, diagonal_neighbors=False)
Read a csv describing the layout of locations and write the results to a yml file describing the location edges, which can then be used in the params [location.edges].
Sample csv:
location_1,location_3,location_4
location_1,location_3,location_4
location_1,location_3,location_4
location_1,location_2,
location_2,location_2,
Would generate the edges: * location_1, location_3 * location_1, location_2 * location_2, location_3 * location_3, location_4
If diagonal_neighbors
were True, the edge [location_2, location_4] would also be returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
path to a csv file which contains a layout for the locations in the model. |
required |
outfile_path |
str
|
path where the resulting yml file should be saved |
required |
diagonal_neighbors |
bool
|
whether diagonally adjacent cells should be considered neighbors [default false] |
False
|
Source code in titan/utils.py
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 |
|
grid_file_to_edges(file_path, diagonal_neighbors=False)
Read a csv describing the layout of locations and return a dictionary describing the location edges, which can then be used in the params [location.edges].
Sample csv:
location_1,location_3,location_4
location_1,location_3,location_4
location_1,location_3,location_4
location_1,location_2,
location_2,location_2,
Would generate the edges: * location_1, location_3 * location_1, location_2 * location_2, location_3 * location_3, location_4
If diagonal_neighbors
were True, the edge [location_2, location_4] would also be returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
path to a csv file which contains a layout for the locations in the model. |
required |
diagonal_neighbors |
bool
|
whether diagonally adjacent cells should be considered neighbors [default false] |
False
|
Returns:
Type | Description |
---|---|
Dict
|
A dictionary with generated edge names to locations |
Source code in titan/utils.py
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 |
|
memo(f)
Decorator to memoize a function (caches results given args, only use if deterministic)
Source code in titan/utils.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
override_param(params, param_path, value, delimiter='|')
Given the params and a parameter path in the format prep|cap, change the current value to new value
Source code in titan/utils.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
safe_dist(dist_info, rand_gen)
Draw a value from a distribution as defined in dist_info
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dist_info |
ObjMap
|
a definition of a distribution to use [params.classes.distributions] |
required |
rand_gen |
random number generator |
required |
Returns:
Type | Description |
---|---|
Union[int, float]
|
a value drawn from the distribution |
Source code in titan/utils.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
safe_divide(numerator, denominator)
Divide two numbers, but default 0 if denominator is 0, otherwise divide as normal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
numerator |
int
|
number being divided |
required |
denominator |
int
|
number doing the dividing |
required |
Returns:
Type | Description |
---|---|
float
|
resulting number |
Source code in titan/utils.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
safe_random_choice(seq, rand_gen, weights=None)
Return None or a random choice from a collection of items
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
collection to select a random item from |
required | |
rand_gen |
random number generator |
required | |
weights |
an optional collection of weights to use instead of a uniform distribution |
None
|
Returns:
Type | Description |
---|---|
an item, or |
Source code in titan/utils.py
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 |
|
safe_random_int(start, stop, rand_gen)
Return an integer between [start, stop)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start |
int
|
start value |
required |
stop |
int
|
stop value |
required |
rand_gen |
random number generator |
required |
Returns:
Type | Description |
---|---|
int
|
an item, or |
Source code in titan/utils.py
101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
safe_shuffle(seq, rand_gen)
Return None or a shuffled sequence
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Collection[T]
|
collection to shuffle |
required |
rand_gen |
random number generator |
required |
Returns:
Type | Description |
---|---|
Iterable[T]
|
shuffled sequence, or |
Source code in titan/utils.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
scale_param(params, param_path, scalar, delimiter='|')
Given the params and a parameter path in the format prep|cap, scale the current value by the scalar
Source code in titan/utils.py
203 204 205 206 207 208 209 210 211 212 |
|
total_probability(p, num_acts)
Given a per act probability and a number of acts, return the total probability.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p |
float
|
the per act probability |
required |
num_acts |
int
|
the number of acts |
required |
Returns:
Type | Description |
---|---|
float
|
the total probability |
Source code in titan/utils.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
Params
ObjMap
Bases: dict
A dictionary-like class which allows accessing members either using standard dictionary notation or dots. Note the hash function is hard-coded - beware.
Source code in titan/parse_params.py
12 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 |
|
check_params(params)
Consistency checks for param populations
Source code in titan/parse_params.py
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 |
|
create_params(setting_name, param_path, outdir, error_on_unused=False)
Entry function - given the path to the setting, params, output directory and whether or not to use the base setting. Parse and create a params (ObjMap) object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
setting_name |
Optional[str]
|
path to a settings file or directory or |
required |
param_path |
str
|
path to parameter file or directory |
required |
outdir |
str
|
path to directory where computed params will be saved |
required |
error_on_unused |
bool
|
throw a hard error if there are unused parameters, otherwise warnings are only printed |
False
|
Returns:
Type | Description |
---|---|
ObjMap
|
computed/validated model paramters with defaults filled in where needed |
Source code in titan/parse_params.py
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 |
|
Probability Distributions
pert(np_random, low, peak, high, temperature)
A pert distribution, inspired by tensorflow
arguments must be so that:
- low < peak < high
- temperature > 0
The support is [low, high]
. The peak
must fit in that interval:
low < peak < high
. The temperature
is a positive parameter that
controls the shape of the distribution. Higher values yield a sharper peak.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
np_random |
random number generator (used to get beta) |
required | |
low |
distribution low value |
required | |
peak |
modal point in distribution |
required | |
high |
distribution high value |
required | |
temperature |
scaling factor |
required |
Source code in titan/distributions.py
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 |
|
poisson(np_rand, mu)
Mirrors scipy poisson.rvs function as used in code
Source code in titan/distributions.py
61 62 63 64 65 66 67 |
|
set_value(np_random, value)
A distribution that always returns the value passed
Parameters:
Name | Type | Description | Default |
---|---|---|---|
np_random |
random number generator (to conform to distribution interface) |
required | |
value |
value to return |
required |
Source code in titan/distributions.py
8 9 10 11 12 13 14 15 16 |
|
weibull_modified(np_random, shape, scale)
Modified version of numpy's (single parameter) weibull distribution to use the 2-parameter weibull.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
np_random |
random number generator |
required | |
shape |
weibull shape parameter |
required | |
scale |
weibull scale parameter |
required |
Source code in titan/distributions.py
48 49 50 51 52 53 54 55 56 57 58 |
|
Complex Probabilities
get_death_rate(hiv, aids, drug_type, sex_type, haart_adh, race, location, steps_per_year, exit_class)
Find the death rate of an agent given a set of attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hiv |
bool
|
whether the agent is HIV+ |
required |
aids |
bool
|
whether the agent has AIDS |
required |
drug_type |
str
|
whether the PWID base death rate should be used or the base one |
required |
haart_adh |
bool
|
whether an agent is haart adherent |
required |
race |
str
|
the race of the agent |
required |
location |
Location
|
agent's location |
required |
steps_per_year |
int
|
the number of model steps in a year |
required |
exit_class |
str
|
the exit class to access in agent params |
required |
Returns:
Type | Description |
---|---|
float
|
the probability of an agent with these characteristics dying in a given time step |
Source code in titan/probabilities.py
7 8 9 10 11 12 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 |
|