101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189 | def submit_run_pyscript(exp, pyscript='plotter/plot_relationship.py', shname='run_relationship_plots.sh',
custom_args='--modelname 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 '--modelname 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
pycommand = f'\npython {pyscript} -d {exp.sim_out_dir} {custom_args}'
# 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 + exp.EMOD_venv + f'\ncd {wdir}' + 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}')
|