def submit_run_pyscript(exp, pyscript='plotter/plot_relationship.py', shname='run_relationship_plots.sh',
custom_args='--models EMOD malariasimulation OpenMalaria', t='05:00:00', memG=20, job_id_EMOD=False,
job_id_malariasimulation=False, job_id_OpenMalaria=False,
job='pyjob', wdir=None, write_only=False):
"""
Submits a job to run a specified Python script using SLURM.
Args:
exp (Experiment): The experiment object containing job directory and other related information.
pyscript (str, optional): The name of the Python script to run. Defaults to 'plotter/plot_relationship.py'.
shname (str, optional): The name of the shell script to submit. Defaults to 'run_relationship_plots.sh'.
custom_args (str, optional): Custom arguments to pass to the Python script.
Defaults to '--models EMOD malariasimulation OpenMalaria'.
t (str, optional): Wall time for the job in the format 'HH:MM:SS'. Defaults to '05:00:00'.
memG (int, optional): Memory required for the job in GB. Defaults to 20.
job_id_EMOD (bool, optional): If True, sets the job ID as a dependency for EMOD. Defaults to False.
job_id_malariasimulation (bool, optional): If True, sets the job ID as a dependency for malariasimulation. Defaults to False.
job_id_OpenMalaria (bool, optional): If True, sets the job ID as a dependency for OpenMalaria. Defaults to False.
job (str, optional): Name of the job. Defaults to 'pyjob'.
wdir (str, optional): Location of the working directory. If None, uses the current directory.
write_only (bool, optional): If True, does not submit the job but only writes the script. Defaults to False.
Returns:
None
Raises:
FileNotFoundError: If the specified job directory or dependencies do not exist.
Notes:
- The function generates a shell script that includes the SLURM header and the command
to run the specified Python script.
- It handles job dependencies based on the provided job IDs for EMOD, malariasimulation,
and OpenMalaria.
- The script is written to the job directory and submitted to the SLURM workload manager.
- The submitted job ID is printed to the console for tracking purposes.
"""
# Get the current working directory
if wdir is None:
wdir = os.path.abspath(os.path.dirname(__file__))
wdir = os.path.abspath(os.path.join(wdir, os.pardir))
# Generate the SLURM shell script header for the job
header_post = shell_header_quest(exp.sh_hpc_config, t, memG, job_name=job, mem_scl=1)
# Generate the Python command to run the script
if exp.use_container:
pycommand = f'\n{exp.EMOD_venv} && python {pyscript} -d {exp.output_directory} {custom_args}'
pycommand = (f'module load singularity '
f'\nsingularity exec --bind {exp.bind_path} --pwd {exp.ROOT_DIR} {exp.image_path} bash -c "{pycommand}"')
else:
pycommand = f'\npython {pyscript} -d {exp.output_directory} {custom_args}'
pycommand = exp.EMOD_venv + f'\ncd {wdir}' + pycommand
# Write the shell script to a file
script_path = os.path.join(exp.job_directory, shname)
file = open(script_path, 'w')
file.write(header_post + pycommand)
file.close()
dependencies = '--dependency=afterany:'
prior_dependency = False
# Check if job_id is provided as a string to define dependendies
if not write_only:
if job_id_EMOD:
id_EMOD = open(os.path.join(exp.job_directory, 'job_id_EMODanalyze.txt')).read().strip()
dependencies = dependencies + f'{id_EMOD}'
prior_dependency = True
# Check if job_id_malariasimulation is provided as a string
if job_id_malariasimulation:
id_malariasimulation = open(os.path.join(exp.job_directory, 'job_id_malariasimulation_analyze.txt')).read().strip()
if prior_dependency:
dependencies = dependencies + f',{id_malariasimulation}'
else:
dependencies = dependencies + f'{id_malariasimulation}'
prior_dependency = True
if job_id_OpenMalaria:
id_OpenMalaria = open(os.path.join(exp.job_directory, 'job_id_OManalyze.txt')).read().strip()
if prior_dependency:
dependencies = dependencies + f',{id_OpenMalaria}'
else:
dependencies = dependencies + f'{id_OpenMalaria}'
# Submit job with dependency
if not job_id_EMOD and not job_id_malariasimulation and not job_id_OpenMalaria:
# Submit job without dependency
p = subprocess.run(['sbatch', '--parsable', script_path], stdout=subprocess.PIPE,
cwd=str(exp.job_directory))
else:
p = subprocess.run(['sbatch', '--parsable', dependencies, script_path], stdout=subprocess.PIPE,
cwd=str(exp.job_directory))
# Extract the SLURM job ID from the output
slurm_job_id = p.stdout.decode('utf-8').strip().split(';')[0]
# Print the submitted job ID
print(f'Submitted {shname} to run {pyscript} - job id: {slurm_job_id}')