oasislmf.execution.bash¶
Bash script generation for the pytools loss calculation pipeline.
This module generates bash scripts that orchestrate the pytools loss calculation pipeline. The generated scripts coordinate multiple concurrent processes connected via named pipes (FIFOs) to compute ground-up losses (GUL), insured losses (IL), reinsurance losses (RI), reinsurance gross losses (RL), and optionally fully-correlated (FC) and load-balanced (LB) streams.
The high-level flow of a generated bash script is:
Initialisation – set shell options, logging, error traps, and the completion-check function.
Directory and FIFO setup – create or clean work, output, and FIFO directories; create named pipes for every process/summary combination.
Consumer commands (written in reverse pipeline order so readers are ready before writers start): a.
do_ord– per-process ORD output consumers (eltpy, pltpy). b.do_tees–teecommands that fan summary streams out towork folders and ORD FIFOs.
do_summarycalcs–summarypycommands that read per-runtype FIFOs and write per-summary FIFOs.Main GUL pipeline commands –
evepy | modelpy | gulpy(orgulmc) piped throughfmpyfor IL/RI, writing into the FIFOs consumed above.Waits –
waiton all background PIDs from steps 3-4.Kat concatenation –
katpymerges per-process binary ORD files into single output files.Post-wait aggregation –
aalpyandlecpyrun over the collected work-folder data to produce ALT and EPT/PSEPT outputs.Cleanup – remove working directories and temporary FIFOs.
Completion check – verify every started process logged a successful finish.
Run types:
gul– ground-up loss
il– insured (direct) loss
ri– reinsurance net loss
rl– reinsurance gross loss
fc– fully correlated ground-up loss
lb– load-balanced stream (intermediary between GUL and FM)
Entry points:
genbash()– legacy single-call interface that writes a complete bash script.
bash_params()– builds a parameter dict consumed bycreate_bash_analysis()andcreate_bash_outputs().
Attributes¶
Functions¶
|
Creates a bash function to check the logs to ensure same number of process started and finsished. |
|
Creates an iterable for all the process ids, if process number is set |
|
Gets the construct model command line argument for the bash script. |
|
Get the ground-up loss calculation command. |
|
Build the fmpy (financial module) command string. |
|
Writes the supplied command to the end of the generated script |
|
Checks if ORD leccalc is enabled in a summaries section |
|
Write post-wait aggregation commands (aalpy, lecpy) to the bash script. |
|
Build the standardised path for a named pipe (FIFO). |
|
Write a single FIFO create or remove command to the bash script. |
|
Write FIFO create/remove commands for every process in the range. |
|
Write FIFO create/remove commands for full-correlation sumcalc and fmcalc pipes. |
|
Write FIFO create/remove commands for all summary and ORD output pipes. |
|
Write |
|
Write |
|
Write a |
|
Write |
|
Write a |
|
Return a dict of FIFO path stems for the full-correlation pipeline. |
|
Write ORD output consumer commands ( |
|
Build the list of inuring priority dicts for reinsurance net loss. |
|
Build the list of inuring priority dicts for reinsurance gross loss. |
|
Write all reinsurance gross loss (RL) consumer commands. |
|
Write all reinsurance net loss (RI) consumer commands. |
|
Write all insured loss (IL) consumer commands. |
|
Write all ground-up loss (GUL) consumer commands. |
|
Write GUL consumer commands for the full-correlation path. |
|
Add waits to the script |
|
Add pwaits to the script |
|
Add awaits to the script |
|
Add lwaits to the script |
|
Add kwaits to the script |
|
Add jwaits to the script (join-summary-info processes) |
|
Gets the GUL pipeline command (gulpy/gulmc) for a single process |
|
Add a variable to the end of a command in order to track the ID of the process executing it. |
|
Gets the fmpy command for the reinsurance stream |
|
Gets the fmpy command for the insured losses stream |
|
Gets the command to output ground up losses |
|
Return a custom GUL command function for complex (third-party) models. |
|
Execute a list of deferred compute operations. |
|
Yield |
|
Determine whether Post Loss Amplification should be implemented and issue |
|
Build the parameter dict consumed by |
|
Context manager that wraps the script body with header and footer boilerplate. |
|
Write the main analysis section of the bash script. |
|
Write the output-aggregation and cleanup section of the bash script. |
|
Generates a bash script containing pytools calculation instructions for an |
|
Inject WebSocket or socket-server flags into a GUL command string. |
Module Contents¶
- oasislmf.execution.bash.FIFO_CHECK_FUNC = Multiline-String[source]¶
Show Value
""" check_fifos() { local has_error=0 for f in "$@"; do [ -e "$f" ] || { echo "[ERROR] Expected FIFO not found: $f"; has_error=1; continue; } [ -p "$f" ] || { echo "[ERROR] Not a FIFO: $f"; has_error=1; } done [ "$has_error" -eq 0 ] || false }"""
- oasislmf.execution.bash.TRAP_FUNC = Multiline-String[source]¶
Show Value
""" touch $LOG_DIR/stderror.err oasis_exec_monitor.sh $$ $LOG_DIR & pid0=$! exit_handler(){ exit_code=$? # disable handler trap - QUIT HUP INT KILL TERM ERR EXIT kill -9 $pid0 2> /dev/null [ -n "${spid:-}" ] && kill -9 "$spid" 2>/dev/null || true if [ "$exit_code" -gt 0 ]; then # Error - run process clean up echo 'Kernel execution error - exitcode='$exit_code set +x group_pid=$(ps -p $$ -o pgid --no-headers) sess_pid=$(ps -p $$ -o sess --no-headers) script_pid=$$ printf "Script PID:%d, GPID:%s, SPID:%d " $script_pid $group_pid $sess_pid >> $LOG_DIR/killout.txt ps -jf f -g $sess_pid > $LOG_DIR/subprocess_list PIDS_KILL=$(pgrep -a --pgroup $group_pid | awk 'BEGIN { FS = "[ \t\n]+" }{ if ($1 >= '$script_pid') print}' | grep -v celery | egrep -v \.log$ | egrep -v startup.sh$ | sort -n -r) echo "$PIDS_KILL" >> $LOG_DIR/killout.txt kill -9 $(echo "$PIDS_KILL" | awk 'BEGIN { FS = "[ \t\n]+" }{ print $1 }') 2>/dev/null exit $exit_code else # script successful exit 0 fi } trap exit_handler QUIT HUP INT KILL TERM ERR EXIT"""
- oasislmf.execution.bash.get_check_function(custom_gulcalc_log_start=None, custom_gulcalc_log_finish=None)[source]¶
Creates a bash function to check the logs to ensure same number of process started and finsished.
- oasislmf.execution.bash.BASH_TRACE = Multiline-String[source]¶
Show Value
""" # --- Redirect Bash trace to file --- bash_logging_supported(){ local BASH_VER_MAJOR=${BASH_VERSION:0:1} local BASH_VER_MINOR=${BASH_VERSION:2:1} if [[ "$BASH_VER_MAJOR" -gt 4 ]]; then echo 1; exit fi if [[ $BASH_VER_MAJOR -eq 4 ]] && [[ $BASH_VER_MINOR -gt 3 ]]; then echo 1; exit fi echo 0 } if [ $(bash_logging_supported) == 1 ]; then exec > >(tee -ia $LOG_DIR/bash.log) exec 2> >(tee -ia $LOG_DIR/bash.log >& 2) exec 19> $LOG_DIR/bash.log export BASH_XTRACEFD="19" set -x else echo "WARNING: logging disabled, bash version '$BASH_VERSION' is not supported, minimum requirement is bash v4.4" fi """
- oasislmf.execution.bash.WAIT_FUNC = Multiline-String[source]¶
Show Value
""" exec_wait(){ local BASH_VER_MAJOR=${BASH_VERSION:0:1} local BASH_VER_MINOR=${BASH_VERSION:2:1} if [[ "$BASH_VER_MAJOR" -gt 5 ]] || { [[ "$BASH_VER_MAJOR" -eq 5 ]] && [[ "$BASH_VER_MINOR" -ge 1 ]]; }; then local pid_exitcode wait -p pid_exitcode "$@" else wait "$@" fi }"""
- oasislmf.execution.bash.process_range(max_process_id, process_number=None)[source]¶
Creates an iterable for all the process ids, if process number is set then an iterable containing only that number is returned.
This allows for the loss generation to be ran in different processes rather than accross multiple cores.
- Parameters:
max_process_id – The largest process number
process_number – If set iterable only containing this number is returned
- Returns:
iterable containing all the process numbers to process
- oasislmf.execution.bash.get_modelcmd(server=False, peril_filter=[]) str[source]¶
Gets the construct model command line argument for the bash script.
- Parameters:
server – (bool) if set then enable ‘TCP’ ipc server/client mode
peril_filter – (list) list of perils to include (all included if empty)
- oasislmf.execution.bash.get_gulcmd(gulmc, gul_random_generator, gulmc_effective_damageability, gulmc_vuln_cache_size, modelpy_server, peril_filter, model_df_engine='oasis_data_manager.df_reader.reader.OasisPandasReader', dynamic_footprint=False)[source]¶
Get the ground-up loss calculation command.
- Parameters:
gulmc (bool) – if True, return the combined (model+ground up) command name, else use ‘modelpy | gulpy’ .
gul_random_generator (int) – random number generator to use in gulmc or gulpy (0: Mersenne-Twister, 1: Latin Hypercube, 2: Latin Hypercube on Philox4x32-7)
gulmc_effective_damageability (bool) – if True, use the effective damageability to draw loss samples instead of the full Monte Carlo method
gulmc_vuln_cache_size (int) – size in MB of the cache for the vulnerability calculations
modelpy_server (bool) – if True, run gulmc against the ‘TCP’ ipc data server
peril_filter (list) – list of perils to include (all included if empty)
model_df_engine (str) – the dataframe reading engine to use when loading model files
dynamic_footprint (bool) – if True, enable dynamic footprint processing
- Returns:
the ground-up loss calculation command
- Return type:
- oasislmf.execution.bash.get_fmcmd(fmpy_low_memory=False, fmpy_sort_output=False)[source]¶
Build the fmpy (financial module) command string.
- oasislmf.execution.bash.print_command(command_file, cmd)[source]¶
Writes the supplied command to the end of the generated script
- Parameters:
command_file – File to append command to.
cmd – The command to append
- oasislmf.execution.bash.ord_enabled(summary_options, ORD_SWITCHES)[source]¶
Checks if ORD leccalc is enabled in a summaries section
Example: {
“id”: 1, “ord_output”: {
“ept_full_uncertainty_aep”: true, “ept_full_uncertainty_oep”: true, “ept_mean_sample_aep”: true, “ept_mean_sample_oep”: true, “ept_per_sample_mean_aep”: true, “ept_per_sample_mean_oep”: true, “psept_aep”: true, “psept_oep”: true, “return_period_file”: true
}
}
- oasislmf.execution.bash.do_post_wait_processing(runtype, analysis_settings, filename, process_counter, work_sub_dir='', output_dir='output/', stderr_guard=True, inuring_priority=None, join_summary_info=False)[source]¶
Write post-wait aggregation commands (aalpy, lecpy) to the bash script.
These commands run after all per-process
waitcalls have completed, operating on the binary work-folder data produced byteeduring the main pipeline. Specifically this handles:PALT (
aalpy) – Period Average Loss Table output.ALT mean-only (
aalpy) – Average Loss Table (mean only) output.EPT / PSEPT (
lecpy) – Exceedance Probability Table and Per-Sample Exceedance Probability Table outputs.
- Parameters:
runtype (str) – The run type identifier (e.g.
'gul','il','ri','rl').analysis_settings (dict) – The full analysis settings dictionary.
filename (str) – Path to the bash script being generated.
process_counter (dict) – Mutable counter dict tracking background PIDs (keys
lpid_monitor_count, etc.).work_sub_dir (str) – Relative path prefix for work sub-directories.
output_dir (str) – Directory where final output files are written.
stderr_guard (bool) – If True, wrap commands in a sub-shell that redirects stderr to the log.
inuring_priority (str or None) – Inuring priority label to embed in file names, or None / empty for the final priority.
join_summary_info (bool) – If True, append
join-summary-infocommands to enrich outputs with summary metadata.
- oasislmf.execution.bash.get_fifo_name(fifo_dir, producer, producer_id, consumer='')[source]¶
Build the standardised path for a named pipe (FIFO).
The naming convention is
<fifo_dir><producer>_<consumer>_P<id>when a consumer is specified, or<fifo_dir><producer>_P<id>otherwise.- Parameters:
- Returns:
The fully-qualified FIFO path.
- Return type:
- oasislmf.execution.bash.do_fifo_exec(producer, producer_id, filename, fifo_dir, action='mkfifo', consumer='') str[source]¶
Write a single FIFO create or remove command to the bash script.
- Parameters:
producer (str) – Run type that produces the stream.
producer_id (int) – Process number for the FIFO name.
filename (str) – Path to the bash script being generated.
fifo_dir (str) – Base directory for FIFOs.
action (str) – Shell command to execute (
'mkfifo'to create,'rm'to remove).consumer (str) – Optional consumer identifier for the FIFO name.
- Returns:
The FIFO path that was operated on.
- Return type:
- oasislmf.execution.bash.do_fifos_exec(runtype, max_process_id, filename, fifo_dir, process_number=None, action='mkfifo', consumer='') list[source]¶
Write FIFO create/remove commands for every process in the range.
Iterates over all process IDs and emits one
mkfifo(orrm) command per process for the given run type.- Parameters:
runtype (str) – The run type identifier (e.g.
'gul','il').max_process_id (int) – Upper bound of process IDs.
filename (str) – Path to the bash script being generated.
fifo_dir (str) – Base directory for FIFOs.
process_number (int or None) – If set, only that single process ID is used instead of the full range.
action (str) – Shell command (
'mkfifo'or'rm').consumer (str) – Optional consumer identifier for FIFO names.
- Returns:
The FIFO paths that were operated on.
- Return type:
- oasislmf.execution.bash.do_fifos_exec_full_correlation(runtype, max_process_id, filename, fifo_dir, process_number=None, action='mkfifo')[source]¶
Write FIFO create/remove commands for full-correlation sumcalc and fmcalc pipes.
For each process ID two FIFOs are created: one for the summarycalc input (
<runtype>_sumcalc_P<id>) and one for the fmcalc input (<runtype>_fmcalc_P<id>).- Parameters:
runtype (str) – The run type identifier (typically
'gul').max_process_id (int) – Upper bound of process IDs.
filename (str) – Path to the bash script being generated.
fifo_dir (str) – Base directory for FIFOs.
process_number (int or None) – If set, restrict to a single process.
action (str) – Shell command (
'mkfifo'or'rm').
- oasislmf.execution.bash.do_fifos_calc(runtype, analysis_settings, max_process_id, filename, fifo_dir='fifo/', process_number=None, consumer_prefix=None, action='mkfifo', summarypy_low_memory=False)[source]¶
Write FIFO create/remove commands for all summary and ORD output pipes.
For every (process, summary) pair this creates:
A summary FIFO (and its
.idxcompanion when leccalc or ALT output is enabled andsummarypy_low_memoryis True — only then willsummarypy -mopen the.idxwriter that the companion consumer reads from).One FIFO per active ORD output type (plt, elt, selt).
- Parameters:
runtype (str) – The run type identifier.
analysis_settings (dict) – The full analysis settings dictionary.
max_process_id (int) – Upper bound of process IDs.
filename (str) – Path to the bash script being generated.
fifo_dir (str) – Base directory for FIFOs.
process_number (int or None) – If set, restrict to a single process.
consumer_prefix (str or None) – Optional prefix prepended to consumer names (used for inuring priority labelling).
action (str) – Shell command (
'mkfifo'or'rm').summarypy_low_memory (bool) – Whether
summarypy -mwill be used. Gates creation of the.idxcompanion FIFO so we don’t leave a tee consumer blocked on an unwritten pipe (only the legacy ktoolsleccalc/aalcalcbinaries consume that pipe;lecpy/aalpyonly read.binwork files).
- oasislmf.execution.bash.create_workfolders(runtype, analysis_settings, filename, work_dir='work/', inuring_priority=None)[source]¶
Write
mkdir -pcommands for work sub-directories needed by summary consumers.Creates directories that
teewill write binary summary data into for subsequent aggregation bylecpy(leccalc) andaalpy(aalcalc).- Parameters:
runtype (str) – The run type identifier (e.g.
'gul','il').analysis_settings (dict) – The full analysis settings dictionary.
filename (str) – Path to the bash script being generated.
work_dir (str) – Root working directory (e.g.
'work/').inuring_priority (str or None) – Inuring priority label to embed in directory names, or None / empty for the final priority.
- oasislmf.execution.bash.do_kats(runtype, analysis_settings, max_process_id, filename, process_counter, work_dir='work/kat/', output_dir='output/', sort_by_event=False, process_number=None, inuring_priority=None, join_summary_info=False)[source]¶
Write
katpyconcatenation commands to merge per-process ORD files.After the main pipeline
waithas completed, per-process binary output files sit in the kat work directory.katpyreads them all and produces a single sorted (or unsorted) output file per summary/ORD-table combination.- Parameters:
runtype (str) – The run type identifier.
analysis_settings (dict) – The full analysis settings dictionary.
max_process_id (int) – Upper bound of process IDs.
filename (str) – Path to the bash script being generated.
process_counter (dict) – Mutable counter dict tracking background PIDs (key
kpid_monitor_count).work_dir (str) – Kat work directory containing per-process files.
output_dir (str) – Directory for final concatenated output files.
sort_by_event (bool) – If True,
katpysorts output by event id.process_number (int or None) – If set, restrict to a single process.
inuring_priority (str or None) – Inuring priority label for file names.
join_summary_info (bool) – If True, append
join-summary-infocommands after each concatenation.
- Returns:
True if any
katpycommands were emitted, False otherwise.- Return type:
- oasislmf.execution.bash.do_summarycalcs(runtype, analysis_settings, process_id, filename, fifo_dir='fifo/', stderr_guard=True, num_reinsurance_iterations=0, gul_full_correlation=False, inuring_priority=None, summarypy_low_memory=False)[source]¶
Write a
summarypycommand for one process to the bash script.Reads the per-process run-type FIFO and fans the stream out to one summary FIFO per summary set defined in the analysis settings.
- Parameters:
runtype (str) – The run type identifier (e.g.
'gul','il').analysis_settings (dict) – The full analysis settings dictionary.
process_id (int) – The process number for this command.
filename (str) – Path to the bash script being generated.
fifo_dir (str) – Base directory for FIFOs.
stderr_guard (bool) – If True, wrap the command in a sub-shell that redirects stderr to the log.
num_reinsurance_iterations (int) – Total number of reinsurance iterations (used for directory switching).
gul_full_correlation (bool) – If True, read from the
_sumcalcFIFO variant used by the full-correlation path.inuring_priority (dict or None) – Inuring priority dict with keys
'text'and'level', or None for non-reinsurance runs.summarypy_low_memory (bool) – If True, append the
-mflag tosummarypyso it writes a.idxside-file alongside each summary stream. The.idxis consumed by the legacy ktoolsleccalcandaalcalcbinaries via the parallelteechain (seedo_tees). The Python aggregators (lecpy,aalpy,eltpy,pltpy) glob*.binwork files and do not read.idx– so they don’t require this flag.
- oasislmf.execution.bash.do_tees(runtype, analysis_settings, process_id, filename, process_counter, fifo_dir='fifo/', work_dir='work/', inuring_priority=None, summarypy_low_memory=False)[source]¶
Write
teecommands that fan each summary stream to ORD and work FIFOs.For each summary set, a
teereads from the summary FIFO and duplicates the stream to:ORD output FIFOs (for
eltpy/pltpyconsumers).Work-folder binary files (for
aalpy/lecpypost-wait processing).
When
summarypy_low_memoryis True and leccalc or ALT output is enabled, a companion.idxtee is also emitted. Only the legacy ktoolsleccalc/aalcalcbinaries consume the.idxchain;lecpyandaalpyonly glob*.binwork files and don’t need it.- Parameters:
runtype (str) – The run type identifier.
analysis_settings (dict) – The full analysis settings dictionary.
process_id (int) – The process number.
filename (str) – Path to the bash script being generated.
process_counter (dict) – Mutable counter dict tracking background PIDs (key
pid_monitor_count).fifo_dir (str) – Base directory for FIFOs.
work_dir (str) – Root working directory.
inuring_priority (str or None) – Inuring priority label for file and FIFO names, or None / empty for the final priority.
summarypy_low_memory (bool) – Whether
summarypy -mis being used. When False the.idxcompanion is skipped to avoid leaving a tee blocked on a pipe nothing writes to.
- oasislmf.execution.bash.do_tees_fc_sumcalc_fmcalc(process_id, filename, correlated_output_stems)[source]¶
Write a
teecommand that splits the full-correlation GUL output.Duplicates the correlated GUL stream into two FIFOs: one for the summarycalc path and one for the fmcalc (financial module) path.
- Parameters:
process_id (int) – The process number.
filename (str) – Path to the bash script being generated.
correlated_output_stems (dict) – FIFO path stems returned by
get_correlated_output_stems().
Return a dict of FIFO path stems for the full-correlation pipeline.
The returned dict contains three keys:
'gulcalc_output'– stem for the GUL output FIFO.'fmcalc_input'– stem for the fmcalc input FIFO.'sumcalc_input'– stem for the summarycalc input FIFO.
Each value is a string ending with
_Pso that a process ID can be appended directly.
- oasislmf.execution.bash.do_ord(runtype, analysis_settings, process_id, filename, process_counter, fifo_dir='fifo/', work_dir='work/', stderr_guard=True, inuring_priority=None)[source]¶
Write ORD output consumer commands (
eltpy,pltpy) for one process.For each summary set and each active ORD output type, reads from the corresponding FIFO and writes per-process binary output files into the kat work directory for later concatenation by
katpy.- Parameters:
runtype (str) – The run type identifier.
analysis_settings (dict) – The full analysis settings dictionary.
process_id (int) – The process number.
filename (str) – Path to the bash script being generated.
process_counter (dict) – Mutable counter dict tracking background PIDs.
fifo_dir (str) – Base directory for FIFOs.
work_dir (str) – Root working directory (kat sub-directory is used).
stderr_guard (bool) – If True, wrap commands in a sub-shell that redirects stderr to the log.
inuring_priority (str or None) – Inuring priority label for file and FIFO names.
- oasislmf.execution.bash.get_ri_inuring_priorities(analysis_settings, num_reinsurance_iterations)[source]¶
Build the list of inuring priority dicts for reinsurance net loss.
Returns intermediate inuring priorities specified in the analysis settings plus the final priority (the full reinsurance iteration count).
- Parameters:
- Returns:
Each dict has
'text'(file-name prefix) and'level'(iteration number) keys.- Return type:
- oasislmf.execution.bash.get_rl_inuring_priorities(num_reinsurance_iterations)[source]¶
Build the list of inuring priority dicts for reinsurance gross loss.
Unlike
get_ri_inuring_priorities(), every iteration from 1 tonum_reinsurance_iterationsis included (there is no “final” entry).
- oasislmf.execution.bash.rl(analysis_settings, max_process_id, filename, process_counter, num_reinsurance_iterations, fifo_dir='fifo/', work_dir='work/', stderr_guard=True, process_number=None, summarypy_low_memory=False)[source]¶
Write all reinsurance gross loss (RL) consumer commands.
Iterates over each RL inuring priority and emits
do_ord,do_tees, anddo_summarycalcscommands for every process.- Parameters:
analysis_settings (dict) – The full analysis settings dictionary.
max_process_id (int) – Upper bound of process IDs.
filename (str) – Path to the bash script being generated.
process_counter (dict) – Mutable counter dict tracking background PIDs.
num_reinsurance_iterations (int) – Total number of reinsurance iterations.
fifo_dir (str) – Base directory for FIFOs.
work_dir (str) – Root working directory.
stderr_guard (bool) – If True, wrap commands in a sub-shell that redirects stderr to the log.
process_number (int or None) – If set, restrict to a single process.
summarypy_low_memory (bool) – Enable summarypy
-m(write.idxside-files for downstream seek-by-event consumers).
- oasislmf.execution.bash.ri(analysis_settings, max_process_id, filename, process_counter, num_reinsurance_iterations, fifo_dir='fifo/', work_dir='work/', stderr_guard=True, process_number=None, summarypy_low_memory=False)[source]¶
Write all reinsurance net loss (RI) consumer commands.
Iterates over each RI inuring priority and emits
do_ord,do_tees, anddo_summarycalcscommands for every process.- Parameters:
analysis_settings (dict) – The full analysis settings dictionary.
max_process_id (int) – Upper bound of process IDs.
filename (str) – Path to the bash script being generated.
process_counter (dict) – Mutable counter dict tracking background PIDs.
num_reinsurance_iterations (int) – Total number of reinsurance iterations.
fifo_dir (str) – Base directory for FIFOs.
work_dir (str) – Root working directory.
stderr_guard (bool) – If True, wrap commands in a sub-shell that redirects stderr to the log.
process_number (int or None) – If set, restrict to a single process.
summarypy_low_memory (bool) – Enable summarypy
-m(write.idxside-files for downstream seek-by-event consumers).
- oasislmf.execution.bash.il(analysis_settings, max_process_id, filename, process_counter, fifo_dir='fifo/', work_dir='work/', stderr_guard=True, process_number=None, summarypy_low_memory=False)[source]¶
Write all insured loss (IL) consumer commands.
Emits
do_ord,do_tees, anddo_summarycalcscommands for every process in the range.- Parameters:
analysis_settings (dict) – The full analysis settings dictionary.
max_process_id (int) – Upper bound of process IDs.
filename (str) – Path to the bash script being generated.
process_counter (dict) – Mutable counter dict tracking background PIDs.
fifo_dir (str) – Base directory for FIFOs.
work_dir (str) – Root working directory.
stderr_guard (bool) – If True, wrap commands in a sub-shell that redirects stderr to the log.
process_number (int or None) – If set, restrict to a single process.
summarypy_low_memory (bool) – Enable summarypy
-m.
- oasislmf.execution.bash.do_gul(analysis_settings, max_process_id, filename, process_counter, fifo_dir='fifo/', work_dir='work/', stderr_guard=True, process_number=None, summarypy_low_memory=False)[source]¶
Write all ground-up loss (GUL) consumer commands.
Emits
do_ord,do_tees, anddo_summarycalcscommands for every process in the range.- Parameters:
analysis_settings (dict) – The full analysis settings dictionary.
max_process_id (int) – Upper bound of process IDs.
filename (str) – Path to the bash script being generated.
process_counter (dict) – Mutable counter dict tracking background PIDs.
fifo_dir (str) – Base directory for FIFOs.
work_dir (str) – Root working directory.
stderr_guard (bool) – If True, wrap commands in a sub-shell that redirects stderr to the log.
process_number (int or None) – If set, restrict to a single process.
summarypy_low_memory (bool) – Enable summarypy
-m.
- oasislmf.execution.bash.do_gul_full_correlation(analysis_settings, max_process_id, filename, process_counter, fifo_dir='fifo/full_correlation/', work_dir='work/full_correlation/', stderr_guard=None, process_number=None)[source]¶
Write GUL consumer commands for the full-correlation path.
Similar to
do_gul()but operates on thefull_correlation/sub-directories and only emitsdo_tees(nodo_ordordo_summarycalcs).- Parameters:
analysis_settings (dict) – The full analysis settings dictionary.
max_process_id (int) – Upper bound of process IDs.
filename (str) – Path to the bash script being generated.
process_counter (dict) – Mutable counter dict tracking background PIDs.
fifo_dir (str) – FIFO directory for full-correlation pipes.
work_dir (str) – Work directory for full-correlation outputs.
stderr_guard – Unused (kept for interface consistency).
process_number (int or None) – If set, restrict to a single process.
- oasislmf.execution.bash.do_waits(wait_variable, wait_count, filename)[source]¶
Add waits to the script
- oasislmf.execution.bash.do_jwaits(filename, process_counter)[source]¶
Add jwaits to the script (join-summary-info processes)
- oasislmf.execution.bash.get_getmodel_cmd(number_of_samples, gul_threshold, use_random_number_file, gul_alloc_rule, item_output, process_id, max_process_id, correlated_output, eve_shuffle_flag, modelpy_server=False, peril_filter=[], gulmc=False, gul_random_generator=2, gulmc_effective_damageability=False, gulmc_vuln_cache_size=200, model_df_engine='oasis_data_manager.df_reader.reader.OasisPandasReader', dynamic_footprint=False, **kwargs)[source]¶
Gets the GUL pipeline command (gulpy/gulmc) for a single process
- Parameters:
number_of_samples (int) – The number of samples to run
gul_threshold (float) – The GUL threshold to use
use_random_number_file (bool) – flag to use the random number file
gul_alloc_rule (int) – back allocation rule for gulcalc
item_output (str) – The item output
process_id (int) – The number of this process, passed to evepy as the partition to run
max_process_id (int) – The total number of processes, passed to evepy as the partition count
correlated_output (str) – Redirection appended to the command when correlated output is requested, empty otherwise
eve_shuffle_flag (str) – The event shuffling rule
modelpy_server (bool) – if True, run against the ‘TCP’ ipc data server
peril_filter (list) – list of perils to include (all included if empty)
gulmc (bool) – if True, use the combined gulmc command, else ‘modelpy | gulpy’
gul_random_generator (int) – random number generator to use in gulmc or gulpy (0: Mersenne-Twister, 1: Latin Hypercube, 2: Latin Hypercube on Philox4x32-7)
gulmc_effective_damageability (bool) – if True, use the effective damageability to draw loss samples instead of the full Monte Carlo method
gulmc_vuln_cache_size (int) – size in MB of the cache for the vulnerability calculations
model_df_engine (str) – The engine to use when loading dataframes
dynamic_footprint (bool) – if True, enable dynamic footprint processing
**kwargs – additional keyword arguments, accepted and ignored so that callers can forward the whole bash_params dict
- Returns:
The generated getmodel command
- oasislmf.execution.bash.add_pid_to_shell_command(cmd, process_counter)[source]¶
Add a variable to the end of a command in order to track the ID of the process executing it. Each time this function is called, the counter process_counter is incremented.
- oasislmf.execution.bash.get_main_cmd_ri_stream(cmd, process_id, il_output, il_alloc_rule, ri_alloc_rule, num_reinsurance_iterations, fifo_dir='fifo/', stderr_guard=True, from_file=False, fmpy_low_memory=False, fmpy_sort_output=False, step_flag='', process_counter=None, ri_inuring_priorities=None, rl_inuring_priorities=None)[source]¶
Gets the fmpy command for the reinsurance stream
- Parameters:
cmd (str) – either gulcalc command stream or correlated output file
process_id (int) – ID corresponding to thread
il_output (bool) – If insured loss outputs required
il_alloc_rule (int) – insured loss allocation rule for fmcalc
ri_alloc_rule (int) – reinsurance allocation rule for fmcalc
num_reinsurance_iterations (int) – number of reinsurance iterations
fifo_dir (str) – path to fifo directory
stderr_guard (bool) – send stderr output to log file
from_file (bool) – must be true if cmd is a file and false if it can be piped
fmpy_low_memory (bool) – use memory map instead of RAM to store the loss array
fmpy_sort_output (bool) – order fmpy output by item_id
step_flag (str) – step policy flag appended to the first fmpy call
process_counter (Counter or dict) – if given, the process ID of the command is tracked in it
ri_inuring_priorities (dict) – Inuring priorities where net output has been requested
rl_inuring_priorities (dict) – Inuring priorities where gross output has been requested
- Returns:
the assembled reinsurance stream command
- Return type:
- oasislmf.execution.bash.get_main_cmd_il_stream(cmd, process_id, il_alloc_rule, fifo_dir='fifo/', stderr_guard=True, from_file=False, fmpy_low_memory=False, fmpy_sort_output=False, step_flag='', process_counter=None)[source]¶
Gets the fmpy command for the insured losses stream
- Parameters:
cmd (str) – either gulcalc command stream or correlated output file
process_id (int) – ID corresponding to thread
il_alloc_rule (int) – insured loss allocation rule for fmcalc
fifo_dir (str) – path to fifo directory
stderr_guard (bool) – send stderr output to log file
from_file (bool) – must be true if cmd is a file and false if it can be piped
fmpy_low_memory (bool) – use memory map instead of RAM to store the loss array
fmpy_sort_output (bool) – order fmpy output by item_id
step_flag (str) – step policy flag appended to the fmpy call
process_counter (Counter or dict) – if given, the process ID of the command is tracked in it
- Returns:
generated fmcalc command as str
- oasislmf.execution.bash.get_main_cmd_gul_stream(cmd, process_id, fifo_dir='fifo/', stderr_guard=True, consumer='', process_counter=None)[source]¶
Gets the command to output ground up losses
- Parameters:
cmd (str) – either gulcalc command stream or correlated output file
process_id (int) – ID corresponding to thread
fifo_dir (str) – path to fifo directory
stderr_guard (bool) – send stderr output to log file
consumer (str) – optional name of the consumer of the stream
process_counter (Counter or dict) – if given, the process ID of the command is tracked in it
- Returns:
generated command as str
- oasislmf.execution.bash.get_complex_model_cmd(custom_gulcalc_cmd, analysis_settings)[source]¶
Return a custom GUL command function for complex (third-party) models.
If
custom_gulcalc_cmdis explicitly provided it must exist inPATH; otherwise the function infers a command name from<supplier>_<model>_gulcalcand checks for its presence.- Parameters:
- Returns:
A function with the same signature as
get_getmodel_cmd()that builds the custom GUL command string, or None if no custom binary was found.- Return type:
callable or None
- Raises:
OasisException – If
custom_gulcalc_cmdis explicitly set but cannot be found onPATH.
- oasislmf.execution.bash.do_computes(outputs)[source]¶
Execute a list of deferred compute operations.
Each entry in outputs is a dict with keys
'compute_fun','compute_args', and'loss_type'. The function callscompute_fun(**compute_args)for every entry, preceded by a comment header identifying the loss type.- Parameters:
outputs (list[dict]) – Deferred compute descriptors built up by the caller (e.g.
create_bash_analysis()).
- oasislmf.execution.bash.get_main_cmd_lb(num_lb, num_in_per_lb, num_out_per_lb, get_input_stream_name, get_output_stream_name, stderr_guard)[source]¶
Yield
load_balancercommands that redistribute streams across FIFOs.Each load balancer reads from num_in_per_lb input FIFOs and writes to num_out_per_lb output FIFOs. Process IDs are assigned sequentially across all load balancers.
- Parameters:
num_lb (int) – Number of load balancer processes.
num_in_per_lb (int) – Number of input FIFOs per load balancer.
num_out_per_lb (int) – Number of output FIFOs per load balancer.
get_input_stream_name (callable) – Function accepting
producer_idand returning the input FIFO path.get_output_stream_name (callable) – Function accepting
producer_idand returning the output FIFO path.stderr_guard (bool) – If True, wrap commands in a sub-shell that redirects stderr to the log.
- Yields:
str – A shell command string for each load balancer process.
- oasislmf.execution.bash.get_pla_cmd(pla, secondary_factor, uniform_factor)[source]¶
Determine whether Post Loss Amplification should be implemented and issue plapy command.
- oasislmf.execution.bash.bash_params(analysis_settings, max_process_id=-1, number_of_processes=-1, num_reinsurance_iterations=0, model_storage_json=None, fifo_tmp_dir=True, gul_alloc_rule=None, il_alloc_rule=None, ri_alloc_rule=None, num_gul_per_lb=None, num_fm_per_lb=None, stderr_guard=True, bash_trace=False, filename='run_kools.sh', _get_getmodel_cmd=None, custom_gulcalc_cmd=None, custom_gulcalc_log_start=None, custom_gulcalc_log_finish=None, custom_args={}, fmpy_low_memory=False, fmpy_sort_output=False, summarypy_low_memory=False, event_shuffle=None, gulmc=True, gul_random_generator=2, gulmc_effective_damageability=False, gulmc_vuln_cache_size=200, process_number=None, remove_working_files=True, model_run_dir='', model_py_server=False, join_summary_info=False, peril_filter=[], exposure_df_engine='oasis_data_manager.df_reader.reader.OasisPandasReader', model_df_engine='oasis_data_manager.df_reader.reader.OasisPandasReader', dynamic_footprint=False, log_level=None, **kwargs)[source]¶
Build the parameter dict consumed by
create_bash_analysis()andcreate_bash_outputs().Validates analysis settings (checking output/summary coherence, reinsurance iteration requirements), resolves default allocation and event-shuffle rules, sets up directory paths (FIFO, work, output), and determines the full-correlation and complex-model flags.
- Parameters:
analysis_settings (dict) – The full analysis settings dictionary.
max_process_id (int) – Number of parallel processes (defaults to CPU count when
<= 0).number_of_processes (int) – Alias used elsewhere (defaults to CPU count when
<= 0).num_reinsurance_iterations (int) – Number of reinsurance iterations.
model_storage_json (dict or None) – Optional model storage metadata.
fifo_tmp_dir (bool) – If True, create FIFOs under
/tmp/.gul_alloc_rule (int or None) – GUL allocation rule override.
il_alloc_rule (int or None) – IL allocation rule override.
ri_alloc_rule (int or None) – RI allocation rule override.
num_gul_per_lb (int or None) – GUL streams per load balancer.
num_fm_per_lb (int or None) – FM streams per load balancer.
stderr_guard (bool) – Wrap commands with stderr redirection.
bash_trace (bool) – Enable bash
-xtracing.filename (str) – Output script filename.
_get_getmodel_cmd (callable or None) – Custom GUL command builder.
custom_gulcalc_cmd (str or None) – Explicit custom GUL binary name.
custom_gulcalc_log_start (str or None) – Custom log-start marker.
custom_gulcalc_log_finish (str or None) – Custom log-finish marker.
custom_args (dict) – Extra arguments forwarded to downstream functions.
fmpy_low_memory (bool) – Enable low-memory mode in
fmpy.fmpy_sort_output (bool) – Sort
fmpyoutput.summarypy_low_memory (bool) – Pass
-mtosummarypyso it writes a.idxside-file, consumed by the legacy ktools leccalc/aalcalc binaries.event_shuffle (int or None) – Event shuffle rule override.
gulmc (bool) – Use
gulmc(Monte Carlo sampler).gul_random_generator (int) – Random number generator selector.
gulmc_effective_damageability (bool) – Enable effective damageability.
gulmc_vuln_cache_size (int) – Vulnerability cache size for
gulmc.process_number (int or None) – Single chunk number (distributed mode).
remove_working_files (bool) – Clean up work dirs after completion.
model_run_dir (str) – Root directory for the model run.
model_py_server (bool) – Launch a
servedataserver.join_summary_info (bool) – Append summary metadata to outputs.
peril_filter (list) – Peril filter list.
exposure_df_engine (str) – DataFrame engine for exposure data.
model_df_engine (str) – DataFrame engine for model data.
dynamic_footprint (bool) – Enable dynamic footprint mode.
log_level (int or None) – Enable logging of pytools subprocesses.
**kwargs – additional keyword arguments, accepted and ignored so that callers can forward a whole computation-step parameter dict.
- Returns:
Parameter dictionary ready for unpacking into
create_bash_analysis()andcreate_bash_outputs().- Return type:
- Raises:
OasisException – If no valid output settings are found or an unknown event shuffle rule is specified.
- oasislmf.execution.bash.bash_wrapper(filename, bash_trace, stderr_guard, log_sub_dir=None, process_number=None, custom_gulcalc_log_start=None, custom_gulcalc_log_finish=None)[source]¶
Context manager that wraps the script body with header and footer boilerplate.
On entry, writes the bash shebang, shell options (
set -euET), log directory setup, optional bash tracing, the error-trap function, and the completion-check function.On exit (after the
yield), writes the footer: either acheck_completecall (single-script mode) or a chunk-validation block that verifies no output files are empty (distributed-chunk mode).- Parameters:
filename (str) – Path to the bash script being generated.
bash_trace (bool) – If True, enable bash
-xtracing.stderr_guard (bool) – If True, install the error trap and write the completion-check function.
log_sub_dir (str or None) – Sub-directory under
log/for chunk mode logging.process_number (int or None) – Chunk number when running in distributed mode.
custom_gulcalc_log_start (str or None) – Custom log-start marker for the completion check.
custom_gulcalc_log_finish (str or None) – Custom log-finish marker for the completion check.
- Yields:
None – Control is yielded to the caller to write the script body.
- oasislmf.execution.bash.create_bash_analysis(process_counter, max_process_id, num_reinsurance_iterations, fifo_tmp_dir, bash_trace, filename, _get_getmodel_cmd, custom_args, fmpy_low_memory, fmpy_sort_output, summarypy_low_memory, process_number, remove_working_files, model_run_dir, fifo_queue_dir, fifo_full_correlation_dir, stderr_guard, work_dir, work_kat_dir, work_full_correlation_dir, work_full_correlation_kat_dir, output_dir, output_full_correlation_dir, gul_alloc_rule, il_alloc_rule, ri_alloc_rule, num_gul_per_lb, num_fm_per_lb, event_shuffle, eve_shuffle_flag, kat_sort_by_event, gul_threshold, number_of_samples, use_random_number_file, full_correlation, gul_output, il_output, ri_output, rl_output, need_summary_fifo_for_gul, analysis_settings, gulmc, gul_random_generator, gulmc_effective_damageability, gulmc_vuln_cache_size, model_py_server, peril_filter, model_df_engine='oasis_data_manager.df_reader.reader.OasisPandasReader', dynamic_footprint=False, log_level=None, **kwargs)[source]¶
Write the main analysis section of the bash script.
This is the core function that assembles the calculation pipeline. It:
Sets up output, FIFO, and work directories.
Creates FIFOs for every (process, summary, ORD-type) combination.
Creates work-folder directories for post-wait aggregation.
Writes consumer commands in reverse pipeline order (ORD consumers, tees, summarycalcs) so readers are ready before writers.
Writes the main GUL pipeline commands (
evepy | modelpy | gulpy/gulmcpiped throughfmpyfor IL/RI), optionally with load balancers.Adds
waitcalls for all background processes.Cleans up FIFOs.
All parameters are typically supplied by unpacking the dict returned from
bash_params().
- oasislmf.execution.bash.create_bash_outputs(process_counter, fifo_tmp_dir, filename, remove_working_files, fifo_queue_dir, stderr_guard, work_dir, work_full_correlation_dir, output_dir, output_full_correlation_dir, full_correlation, gul_output, il_output, ri_output, rl_output, analysis_settings, num_reinsurance_iterations, num_gul_per_lb, num_fm_per_lb, max_process_id, work_kat_dir, kat_sort_by_event, work_full_correlation_kat_dir, join_summary_info, **kwargs)[source]¶
Write the output-aggregation and cleanup section of the bash script.
This function runs after the main analysis
waitand handles:Kat concatenation –
katpymerges per-process binary ORD files into single output files for each run type.Post-wait aggregation –
aalpy(PALT/ALT) andlecpy(EPT/PSEPT) run over collected work-folder data.Cleanup – removes working directories and temporary FIFOs.
All parameters are typically supplied by unpacking the dict returned from
bash_params().
- oasislmf.execution.bash.genbash(max_process_id, analysis_settings, num_reinsurance_iterations=0, fifo_tmp_dir=True, gul_alloc_rule=None, il_alloc_rule=None, ri_alloc_rule=None, num_gul_per_lb=None, num_fm_per_lb=None, stderr_guard=True, bash_trace=False, filename='run_kools.sh', _get_getmodel_cmd=None, custom_gulcalc_log_start=None, custom_gulcalc_log_finish=None, custom_args={}, fmpy_low_memory=False, fmpy_sort_output=False, summarypy_low_memory=False, event_shuffle=None, gulmc=True, gul_random_generator=2, gulmc_effective_damageability=False, gulmc_vuln_cache_size=200, model_py_server=False, peril_filter=[], join_summary_info=False, base_df_engine='oasis_data_manager.df_reader.reader.OasisPandasReader', model_df_engine=None, dynamic_footprint=False, analysis_pk=None, socket_server_size=None, socket_server_port=None, log_level=None)[source]¶
Generates a bash script containing pytools calculation instructions for an Oasis model.
- Parameters:
max_process_id (int) – The number of processes to create
analysis_settings (dict) – The analysis settings
num_reinsurance_iterations (int) – The number of reinsurance iterations
fifo_tmp_dir (bool) – When set to True, Create and use FIFO quese in /tmp/[A-Z,0-9]/fifo, if False run in ‘./fifo’
gul_alloc_rule (Int) – Allocation rule (None or 1) for gulcalc, if not set default to coverage stream
il_alloc_rule (Int) – Allocation rule (0, 1 or 2) for fmcalc
ri_alloc_rule (Int) – Allocation rule (0, 1 or 2) for fmcalc
num_gul_per_lb (int) – number of gul streams per load balancer
num_fm_per_lb (int) – number of fm streams per load balancer
stderr_guard (bool) – Wrap commands with stderr redirection.
bash_trace (bool) – Enable bash
-xtracing.filename (str) – The output file name
_get_getmodel_cmd (callable) – Method for getting the getmodel command, by default
GenerateLossesCmd.get_getmodel_cmdis used.custom_gulcalc_log_start (str or None) – Log message produced when the custom gulcalc binary process starts.
custom_gulcalc_log_finish (str or None) – Log message produced when the custom gulcalc binary process ends.
custom_args (dict) – Extra arguments forwarded to downstream functions.
fmpy_low_memory (bool) – use memory map instead of RAM to store the loss array.
fmpy_sort_output (bool) – order fmpy output by item_id.
summarypy_low_memory (bool) – Pass
-mtosummarypyso it writes a.idxside-file, consumed by the legacy ktools leccalc/aalcalc binaries.event_shuffle (int or None) – Event shuffle rule override.
gulmc (bool) – use the full Monte Carlo gulcalc python version.
gul_random_generator (int) – random number generator to use in gulmc or gulpy (0: Mersenne-Twister, 1: Latin Hypercube, 2: Latin Hypercube on Philox4x32-7).
gulmc_effective_damageability (bool) – use the effective damageability to draw loss samples instead of the full Monte Carlo method.
gulmc_vuln_cache_size (int) – size in MB of the cache for the vulnerability calculations.
model_py_server (bool) – run the data server for modelpy.
peril_filter (list) – list of perils to include (all included if empty).
join_summary_info (bool) – join summary id information to outputcalc csvs.
base_df_engine (str) – The engine to use when loading dataframes.
model_df_engine (str) – The engine to use when loading model dataframes.
dynamic_footprint (bool) – Enable dynamic footprint mode.
analysis_pk (int or None) – analysis primary key reported with progress pings. When set, no local socket-server is started.
socket_server_size (int or None) – total number of events, passed to the local socket-server that reports run progress.
socket_server_port (int or None) – port the local socket-server listens on.
log_level (int or None) – Enable logging of pytools subprocesses.
- oasislmf.execution.bash.add_server_call(call, analysis_pk=None, socket_server_port=None)[source]¶
Inject WebSocket or socket-server flags into a GUL command string.
If the environment variables
OASIS_WEBSOCKET_URLandOASIS_WEBSOCKET_PORTare set andanalysis_pkis provided, thegulmc/gulpyinvocation within call is augmented with--socket-serverand--analysis-pkflags. Otherwise, the--socket-serverflag is set toIP:portwhen a port is known.