Reference
The source code is in a folder named src
.
Inside that are files for the top level application (main.py
), a module that serves as the interface to the server (op.py
), and a folder named gui
that has definitions of the components in graphical user interface.
src
├── gui
│ ├── app.py
│ ├── budgets.py
│ ├── infobox.py
│ ├── output.py
│ ├── regionbox.py
│ ├── styles.py
│ ├── targetbox.py
│ └── tgmap.py
├── main.py
└── op.py
main.py
This file has the main entry point called when the program is launched from the command line.
It uses argparse
to get command line options, initializes the OptiPass interface, creates the Panel application, and starts the application.
make_app
Instantiate the top level widget.
Returns: |
|
---|
Source code in src/main.py
60 61 62 63 64 65 66 67 68 69 70 |
|
start_app
Launch the Bokeh server.
Source code in src/main.py
72 73 74 75 76 77 78 79 80 81 82 83 |
|
op.py
A class named OP provides an abstract interface to the data for the current project. For example, the "widgets" in the GUI call OP methods to get the list of region names or descriptions of restoration targets.
The module is essentially a "singleton object".
It defines a collection of static methods.
When the top level application starts it calls a method named setup
, which initializes all the data; after that the GUI objects call methods to get values of the data.
MetaOP
The OP class is actually constructed at runtime by a "metaclass".
When the application is started, OP has a few methods but no data.
The setup
method fetches the data from the server and adds it to the OP class.
Here's a concrete example of how this is done, using target data, which is stored in a Pandas dataframe.
The metaclass, which is named MetaOP, defines a method named target_frame
:
@property
def target_frame(cls):
return cls._target_frame
That definition defines the name, but that name doesn't refer to any acutal table of values at this point.
The setup
method includes these lines to fetch the target descriptions from the server, convert those descriptions into a Pandas dataframe, and save the frame:
req = f'{server}/targets/{project}'
resp = requests.get(req)
if resp.status_code != 200:
raise OPServerError(resp)
dct = resp.json()
buf = StringIO(dct['targets'])
cls._target_frame = pd.read_csv(buf).set_index('abbrev')
The first line creates the URL of the REST request to send to the remote server, the second line sends the request.
When the response comes back, the third line makes sure the request succeeded.
Lines 4 and 5 create the data frame, and the last line saves it in an internal variable named _target_frame
.
From this point on, methods in the GUI can access the data by using the expression OP.target_frame
.
That will call the target_frame
method shown above, which accesses the value in the internal variable and returns it to the GUI.
Bases: type
This metaclass creates the API for the OP class. It defines read-only attributes that can be accessed but not written from outside the OP module. The values of the attributes can only be set when the setup method is called. Note: one attribute (region_names) is writeable.
setup(server, project, tab)
Initialize the connection to the OptiPass server. Connect to the server, get the barrier file and other data for the project, save it in read-only class variables.
Raises an exception if the server is not accessible or if the project name is not known to the server.
Parameters: |
|
---|
Source code in src/op.py
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 |
|
OP
Interface to an OptiPass server. The module consists of a set of static methods that manage a single connection (i.e. it's basically a singleton object).
url_for_figure(fn)
staticmethod
Return the URL to use to fetch an image from the server.
Parameters: |
|
---|
Returns: |
|
---|
Source code in src/op.py
152 153 154 155 156 157 158 159 160 161 162 163 |
|
format_budgets(cols)
staticmethod
Create a dictionary that maps budget values to abbreviated dollar amounts.
Parameters: |
|
---|
Returns: |
|
---|
Source code in src/op.py
165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
format_budget_amount(n)
staticmethod
Convert an integer dollar amount into an abbreviation, e.g. 100000 becomes "$100K" and 2500000 becomes "$2.5M".
Parameters: |
|
---|
Returns: |
|
---|
Source code in src/op.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
fetch_html_file(fn)
staticmethod
Fetch an HTML file from the server.
Parameters: |
|
---|
Returns: |
|
---|
Source code in src/op.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
run_optimizer(regions, budgets, targets, weights, mapping)
staticmethod
Send a request to the op-server to run OptiPass using settings from the widgets.
Parameters: |
|
---|
Returns: |
|
---|
Source code in src/op.py
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 |
|
OPResult
The run_optimizer
method creates an instance of this class each time
the server returns a set of results from an optimization run.
Pass the constructor the dictionaries returned by the server and
the widget settings (region names, budget levels, target selection)
that were passed to run_optimizer
.
The code that creates the output tab calls methods of this class to make figures and tables displayed in the GUI.
Source code in src/op.py
278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
make_roi_curves()
Generate ROI plots based on computed benefits.
Source code in src/op.py
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 |
|
budget_table()
Make a table that has one column for each budget level, showing which barriers were included in the solution for that level.
Source code in src/op.py
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 |
|
gate_table()
Make a table that has one row per gate with columns that are relevant to the output display
Source code in src/op.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 |
|
DevOP
A collection of utility functions for developers. If there is an environment variable that defines a value for a widget the code that builds the GUI will put that value in the widget when it is created, e.g. if OPREGIONS is set to "Coos Umpqua Coquille" those three regions will be selected in the region box.
If OPTMPDIR is defined it should be the path to a directory on the server that has outputs from a previous optimization. The path will be included in the request URL that runs OptiPass. When the server see this it will return the previous results instead of running OptiPass again -- very useful for testing on macOS (which can't run OptiPass).
default_list(varname)
staticmethod
Split the value of an environment variable into a list
Source code in src/op.py
441 442 443 444 445 446 447 448 |
|
default_regions()
staticmethod
Return the value of OPREGIONS
Source code in src/op.py
450 451 452 453 454 455 |
|
default_budget()
staticmethod
Return the value of OPBUDGET
Source code in src/op.py
457 458 459 460 461 462 |
|
default_targets()
staticmethod
Return the value of OPTARGETS
Source code in src/op.py
464 465 466 467 468 469 |
|
results_dir()
staticmethod
Return the value of OPTMPDIR
Source code in src/op.py
471 472 473 474 475 476 |
|