PythonAnalysis adaptor
The python analysis adaptor enables the use of a Python scripts as an analysis
back end. It accomplishes this by embedding a Python interpreter and includes a
minimal set of the sensei python bindings. To author a new python analysis one
must provide a python script that implements three functions: Inititalize
,
Execute
and Finalize
. These functions implement the sensei::AnalysisAdaptor
API. The Execute
function is required while Initialize
and Finalize
functions are optional. The Execute
function is passed a sensei::DataAdaptor
instance from which one has access to simulation data structures. If an error
occurs during processing one should raise
an exception. If the analysis
required MPI communication, one must make use of the adaptor’s MPI communicator
which is stored in the global variable comm
. Additionally one can provide a
secondary script that is executed prior to the API functions. This script can
set global variables that control runtime behavior.
End users will make use of the sensei::ConfigurableAnalysis
and point to the
python analysis script. The script can be loaded in one of two ways: via
python’s import machinery or via a customized mechanism that reads the file on
MPI rank 0 and broadcasts it to the other ranks. The latter is the recommended
approach.
ConfigurableAnalysis XML
The <analysis>
element is used to create and configure the PythonAnalysis
instance.
name | type | allowable value(s) | description |
---|---|---|---|
type |
attribute | “python” | Creates a PythonAnalysis instance |
script_module |
attribute | a module name | Names a module that is in the PYTHONPATH. The module should define the 3 analysis adaptor API functions: Initialize , Execute , and Finalize . It is imported during initialization using python’s import machinery. * |
script_file |
attribute | a file path | A path to a python script to be loaded and broadcast by rank 0. The script should define the 3 analysis adaptor API functions: Initialize , Execute , and Finalize . * |
enabled |
attribute | 0,1 | When 0 the analysis is skipped |
initialize_source |
child element | python source code | A snippet of source code that can be used to control run time behavior. The source code must be properly formatted and indented. The contents of the element are taken verbatim including newline tabs and spaces. |
* – use one of script_file
or script_module
. Prefer script_file
.
See the example below.
Code Template
The following template provides stubs that one can fill in to write a new python analysis.
# YOUR IMPORTS HERE |
Example
The following example computes a histogram in parallel. It is included in the source code at sensei/Histogram.py
.
Histogram.py
import sys |
Histogram.xml
<analysis type="python" script_file="./Histogram.py" enabled="1"> |