Modules
The source code is in a folder named app
. There are only two source files in the folder, one (main.py
) for the FastAPI application and one (optipass.py
) that provides an abstract interface for running OptiPass.
app
├── main.py
└── optipass.py
main.py
init
Define global variables used in the rest of the application: paths to static data and names of static data files, a list of names of projects, a dictionary of region names for each project.
Source code in app/main.py
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 |
|
read_text_file
Read a text file from one of the static subdirectories.
Parameters: |
|
---|
Returns: |
|
---|
Source code in app/main.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
projects
Respond to GET requests of the form /projects
.
Returns: |
|
---|
Source code in app/main.py
101 102 103 104 105 106 107 108 109 |
|
barriers
Respond to GET requests of the form /barriers/P
where P is a project name.
Returns: |
|
---|
Source code in app/main.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
mapinfo
Respond to GET requests of the form /mapinfo/P
where P is a project name.
Returns: |
|
---|
Source code in app/main.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
|
targets
Respond to GET requests of the form /targets/P
where P is a project name.
Returns: |
|
---|
Source code in app/main.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
colnames
Respond to GET requests of the form /colnames/P
where P is a project name.
Returns: |
|
---|
Source code in app/main.py
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 |
|
optipass
A GET request of the form /optipass/project?ARGS
runs OptiPass using the parameter
values passed in the URL.
Parameters: |
|
---|
Returns: |
|
---|
Source code in app/main.py
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 |
|
optipass.py
optipass_is_installed
Make sure OptiPass is installed.
Returns: |
|
---|
Source code in app/optipass.py
17 18 19 20 21 22 23 24 |
|
run_optipass
Run OptiPass using the specified arguments. Instantiates an OP object with paths to data files, calls methods that create the input file, run the optimizer, and gather the results.
Parameters: |
|
---|
Returns: |
|
---|
Source code in app/optipass.py
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 |
|
OptiPass
An instance of this class has all the data and methods required to respond
to an optipass
request.
The entry point that runs OptiPass has query parameters that specify how many budget levels to explore. We need to run OptiPass.exe once for each budget level, then collect the results.
The general workflow:
- create an instance of this class, passing the constructor the parameter values for the regions, targets, and budget levels
- call a method to generate the input file (called a "barrier file" in the OP documentation) that will be read as input each time OP runs
- call the method that finds downstream barriers
- call the method that runs OP
- generate the output tables and plots
All of the intermediate data needed for these steps is saved in instance vars of the object.
Parameters: |
|
---|
Source code in app/optipass.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 |
|
create_input_frame()
Build a data frame that has the rows that will be passed to OptiPass. This frame is basically a subset of the columns of the barrier frame, using column names defined in the targets frame. The frame is saved as an instance variable of this object.
Source code in app/optipass.py
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 |
|
create_paths()
Create paths downstream from each gate (the paths will be used to compute cumulative passability). The paths are saved in an instance variable.
Source code in app/optipass.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
path_from(x, graph)
Helper function used to create paths -- return a list of nodes in the path
from x
to a downstream barrier that has no descendants.
Parameters: |
|
---|
Returns: |
|
---|
Source code in app/optipass.py
201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
set_target_weights(weights)
Create the target weight values that will be passed on the command line when OptiPass is run. If the list is None set each weight to 1. Weights are saved in an instance variable.
Parameters: |
|
---|
Source code in app/optipass.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
run(bmin, bdelta, bcount)
Run Optipass once for each budget level. Create the shell commands and run them. Outputs are saved in a temp directory.
Parameters: |
|
---|
Note:
- for unit tests the outputs are already in the temp directory so OptiPass isn't run
- when running OptiPass run it once with a budget of $0 and then once for each budget level
Source code in app/optipass.py
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 |
|
collect_results()
OptiPass makes one output file for each budget level. Iterate over those files to gather results into a pair of data frames.
Returns: |
|
---|
Source code in app/optipass.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
parse_output(fn, dct)
Parse an output file, appending results to the lists in dct. We need to handle two different formats, depending on whether there was one target or more than one. Values extracted from a file are appended to the lists passed in the dictionary argument.
Parameters: |
|
---|
Source code in app/optipass.py
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 |
|
add_potential_habitat()
Compute the potential habitat available after restoration, using the original unscaled habitat values. Adds a new table named summary: one column for each target, showing the potential habitat gain at each budget level, then the weighted potential habitat over all targets, and finally the net gain.
Source code in app/optipass.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
|