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.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 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.
- Args:
custom_gulcalc_log_start (str): Custom message printed to the logs when a process starts. custom_gulcalc_log_finish (str): Custom message printed to the logs when a process ends.
- 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.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.
- Args:
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.
- Args:
gulmc (bool): if True, return the combined (model+ground up) command name, else use ‘modelpy | gulpy’ .
- Returns:
str: the ground-up loss calculation command
- oasislmf.execution.bash.get_fmcmd(fmpy_low_memory=False, fmpy_sort_output=False)[source]¶
Build the fmpy (financial module) command string.
- Args:
- fmpy_low_memory (bool): If True, append the
-lflag to enable low-memory mode.
- fmpy_sort_output (bool): If True, append the
--sort-outputflag so that output records are sorted.
- fmpy_low_memory (bool): If True, append the
- Returns:
str: The assembled
fmpycommand with any requested flags.
- 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
- Parameters:
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
}
}
- Returns:
True is leccalc is enables, False otherwise.
- 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.
- Args:
- 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-info commands to enrich outputs with summary metadata.
- runtype (str): The run type identifier (e.g.
- 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.- Args:
fifo_dir (str): Base directory for FIFOs (e.g.
'fifo/'). producer (str): Run type that produces the stream (e.g.'gul'). producer_id (int): Process number used as a suffix (P<id>). consumer (str): Optional consumer identifier appended between theproducer and the process id.
- Returns:
str: The fully-qualified FIFO path.
- oasislmf.execution.bash.do_fifo_exec(producer, producer_id, filename, fifo_dir, action='mkfifo', consumer='')[source]¶
Write a single FIFO create or remove command to the bash script.
- Args:
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.
- oasislmf.execution.bash.do_fifos_exec(runtype, max_process_id, filename, fifo_dir, process_number=None, action='mkfifo', consumer='')[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.- Args:
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 IDis used instead of the full range.
action (str): Shell command (
'mkfifo'or'rm'). consumer (str): Optional consumer identifier for FIFO names.
- 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>).- Args:
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')[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).One FIFO per active ORD output type (plt, elt, selt).
- Args:
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').
- 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).- Args:
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 indirectory 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.- Args:
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, appendjoin-summary-infocommands after each concatenation.
- Returns:
bool: True if any
katpycommands were emitted, False otherwise.
- 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)[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.
- Args:
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 thatredirects 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.
- oasislmf.execution.bash.do_tees(runtype, analysis_settings, process_id, filename, process_counter, fifo_dir='fifo/', work_dir='work/', inuring_priority=None)[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 leccalc or ALT output is enabled, a companion
.idxtee is also emitted.- Args:
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.
- 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.
- Args:
process_id (int): The process number. filename (str): Path to the bash script being generated. correlated_output_stems (dict): FIFO path stems returned by
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.- Args:
fifo_dir (str): Base directory for FIFOs (e.g.
'fifo/').- Returns:
dict: Mapping of stem names to FIFO path prefixes.
- 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.- Args:
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).
- Args:
- analysis_settings (dict): The full analysis settings dictionary,
checked for the
'ri_inuring_priorities'key.- num_reinsurance_iterations (int): Total number of reinsurance
iterations.
- Returns:
list[dict]: Each dict has
'text'(file-name prefix) and'level'(iteration number) keys.
- 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).- Args:
- num_reinsurance_iterations (int): Total number of reinsurance
iterations.
- Returns:
list[dict]: Each dict has
'text'(file-name prefix) and'level'(iteration number) keys.
- 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)[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.- Args:
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.
- 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)[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.- Args:
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.
- oasislmf.execution.bash.il(analysis_settings, max_process_id, filename, process_counter, fifo_dir='fifo/', work_dir='work/', stderr_guard=True, process_number=None)[source]¶
Write all insured loss (IL) consumer commands.
Emits
do_ord,do_tees, anddo_summarycalcscommands for every process in the range.- Args:
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.
- 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)[source]¶
Write all ground-up loss (GUL) consumer commands.
Emits
do_ord,do_tees, anddo_summarycalcscommands for every process in the range.- Args:
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.
- 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).- Args:
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=1, 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 :param number_of_samples: The number of samples to run :type number_of_samples: int :param gul_threshold: The GUL threshold to use :type gul_threshold: float :param use_random_number_file: flag to use the random number file :type use_random_number_file: bool :param gul_alloc_rule: back allocation rule for gulcalc :type gul_alloc_rule: int :param item_output: The item output :type item_output: str :param eve_shuffle_flag: The event shuffling rule :type eve_shuffle_flag: str :param model_df_engine: The engine to use when loading dataframes :type model_df_engine: str :return: 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.
- Args:
cmd (str): the command whose process ID is to be stored in a variable. process_counter (Counter or dict): the number of process IDs that are being tracked.
- Returns:
cmd (str): the updated command string.
- 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 :param cmd: either gulcalc command stream or correlated output file :type cmd: str :param process_id: ID corresponding to thread :type process_id: int :param il_output: If insured loss outputs required :type il_output: Boolean :param il_alloc_rule: insured loss allocation rule for fmcalc :type il_alloc_rule: int :param ri_alloc_rule: reinsurance allocation rule for fmcalc :type ri_alloc_rule: int :param num_reinsurance_iterations: number of reinsurance iterations :type num_reinsurance_iterations: int :param fifo_dir: path to fifo directory :type fifo_dir: str :param stderr_guard: send stderr output to log file :type stderr_guard: bool :param from_file: must be true if cmd is a file and false if it can be piped :type from_file: bool :param ri_inuring_priorities: Inuring priorities where net output has been requested :type ri_inuring_priorities: dict :param rl_inuring_priorities: Inuring priorities where gross output has been requested :type rl_inuring_priorities: dict
- 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 :param cmd: either gulcalc command stream or correlated output file :type cmd: str :param process_id: ID corresponding to thread :type process_id: int :param il_alloc_rule: insured loss allocation rule for fmcalc :type il_alloc_rule: int :param fifo_dir: path to fifo directory :type fifo_dir: str :param stderr_guard: send stderr output to log file :type stderr_guard: bool :param from_file: must be true if cmd is a file and false if it can be piped :type from_file: bool :return: 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 :param cmd: either gulcalc command stream or correlated output file :type cmd: str :param process_id: ID corresponding to thread :type process_id: int :param fifo_dir: path to fifo directory :type fifo_dir: str :param stderr_guard: send stderr output to log file :type stderr_guard: bool :param consumer: optional name of the consumer of the stream :type consumer: string :return: 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.- Args:
- custom_gulcalc_cmd (str or None): Explicit custom GUL binary name,
or None to attempt auto-detection.
- analysis_settings (dict): The full analysis settings dictionary
(used to infer the binary name via
model_supplier_idandmodel_name_id).
- Returns:
callable or None: 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.- Raises:
- OasisException: If
custom_gulcalc_cmdis explicitly set but cannot be found on
PATH.
- OasisException: If
- 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.- Args:
- 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.
- Args:
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_id and returning the output FIFO path.
- stderr_guard (bool): If True, wrap commands in a sub-shell that
redirects stderr to the log.
- get_output_stream_name (callable): Function accepting
- 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.
- Args:
pla (bool): flag to apply post loss amplification secondary_factor (float): secondary factor to apply to post loss
amplification
uniform_factor (float): uniform factor to apply across all losses
- Returns:
pla_cmd (str): post loss amplification 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, event_shuffle=None, gulmc=True, gul_random_generator=1, 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, **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.
- Args:
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 infmpy. fmpy_sort_output (bool): Sortfmpyoutput. event_shuffle (int or None): Event shuffle rule override. gulmc (bool): Usegulmc(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 forgulmc. 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 aservedataserver. 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.- Returns:
dict: Parameter dictionary ready for unpacking into
create_bash_analysis()andcreate_bash_outputs().- 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).- Args:
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 thecompletion-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.
- log_sub_dir (str or None): Sub-directory under
- 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, 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, **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, event_shuffle=None, gulmc=True, gul_random_generator=1, 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=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
filename (string) – The output file name
num_reinsurance_iterations (int) – The number of reinsurance iterations
fifo_tmp_dir (boolean) – 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_in_calc_block (Int) – number of gul in calc block
num_fm_in_calc_block (Int) – number of gul in calc block
get_getmodel_cmd (callable) – Method for getting the getmodel command, by default
GenerateLossesCmd.get_getmodel_cmdis used.base_df_engine (str) – The engine to use when loading dataframes.
model_df_engine (str) – The engine to use when loading model dataframes.
- oasislmf.execution.bash.add_server_call(call, analysis_pk=None, socket_server=False)[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, only the--socket-serverflag is added.- Args:
call (str): The full pipeline command string. analysis_pk (int or None): Analysis primary key for WebSocket mode. socket_server (bool): Default socket-server flag value.
- Returns:
str: The (possibly modified) command string.