utilities
Small utility methods for convenience.
Timer
Simple timer class for determining execution time.
Source code in src/utilities.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 |
|
get_duration()
Returns a string of duration in hours, minutes, seconds.
Source code in src/utilities.py
30 31 32 33 |
|
get_hhmmss()
Returns a string of duration in hh:mm:ss format.
Source code in src/utilities.py
34 35 36 37 |
|
get_time()
Returns durations in hours, minutes, seconds as a named tuple.
Source code in src/utilities.py
22 23 24 25 26 27 28 29 |
|
reset()
Reset timer to zero.
Source code in src/utilities.py
38 39 40 |
|
time_passed()
Return time passed since start, in seconds.
Source code in src/utilities.py
19 20 21 |
|
display_progress(completion, size=20, text='Progress: ')
Shows the current simulation progress as a percentage with a progress bar.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
completion
|
float
|
fraction representing completion. |
required |
size
|
int
|
total amount of simulations to run. |
20
|
text
|
str
|
optional text to display before progress bas. |
'Progress: '
|
Source code in src/utilities.py
74 75 76 77 78 79 80 81 82 83 84 85 |
|
exception_traceback(e, file=None)
Format exception traceback and print it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
e
|
Exception
|
exception from which to print traceback. |
required |
file
|
str
|
optional file path to write to, otherwise defaults to sys.stdout. |
None
|
Source code in src/utilities.py
43 44 45 46 47 48 49 50 51 52 |
|
fail_with_message(m=None, e=None, file=None, do_not_exit=False)
Prints the given exception traceback along with given message, and exits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m
|
str
|
optional message to print along with traceback. |
None
|
e
|
Exception
|
exception from which to print traceback. |
None
|
file
|
str
|
optional file path to write to, otherwise defaults to sys.stdout. |
None
|
do_not_exit
|
bool
|
optional flag to not exit. |
False
|
Source code in src/utilities.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
get_dist_from_picks(picks)
Takes an array of picks and returns the probability distribution for each percentage unit. Picks need to be fractions in [0, 1].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
picks
|
ndarray
|
array of random samples. |
required |
Returns:
Name | Type | Description |
---|---|---|
dist |
ndarray
|
probability distribution. |
Source code in src/utilities.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
get_pick(dist)
Makes a random pick within [0, 1] weighted by the given discrete distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dist
|
ndarray
|
probability distribution. |
required |
Returns:
Name | Type | Description |
---|---|---|
pick |
float
|
sample from the distribution. |
Source code in src/utilities.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
|
get_prob_dist(expecteds, lower_boundaries, upper_boundaries, weights)
Returns an aggregated probability distribution from all the individual expert answers provided. Each value in the argument arrays correspond to a PERT distribution characteristic (peak, high, low). Each individual distribution has a weight which impacts its contribution to the final aggregated distribution. All arguments should be 1D arrays with percentage as unit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expecteds
|
ndarray
|
individual distribution peaks. |
required |
lower_boundaries
|
ndarray
|
individual distributions lows. |
required |
upper_boundaries
|
ndarray
|
individual distribution highs. |
required |
weights
|
ndarray
|
individual distribution weights. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
numpy array |
Source code in src/utilities.py
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 |
|
pert_dist(peak, low, high, size)
Returns a set of random picks from a PERT distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
peak
|
float
|
distribution peak. |
required |
low
|
float
|
distribution lower tail. |
required |
high
|
float
|
distribution higher tail. |
required |
size
|
int
|
number of picks to return. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
numpy array |
Source code in src/utilities.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
plot_dist(dist)
Plot the given distribution
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dist
|
ndarray
|
probability distribution. |
required |
Source code in src/utilities.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
sanitize_string(s)
Makes a string valid for file and directory names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
string to sanitize. |
required |
Source code in src/utilities.py
262 263 264 265 266 267 268 269 270 271 272 273 |
|