Creates a PowerShell script to run malariasimulation simulations (sequentially) locally.
This function generates a PowerShell script that:
1. Sets job parameters, including the path to the malariasimulation executable and the job directory.
2. Ensures the log directory exists, creating it if necessary.
3. Iterates over all experiments, running the malariasimulation for each one using the specified R script.
4. Logs the output and errors to respective log files.
Parameters: |
-
exp
(object )
–
An experiment object containing attributes such as ‘job_directory’, ‘nexps’, ‘r_local’, and paths
necessary for setting up and running the malariasimulation.
|
Raises: |
-
IOError
–
If there is an issue with writing the PowerShell script to the file.
|
Source code in malariasimulation\utils.py
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 | def write_ps1_malariasimulation(exp):
"""
Creates a PowerShell script to run malariasimulation simulations (sequentially) locally.
This function generates a PowerShell script that:
1. Sets job parameters, including the path to the malariasimulation executable and the job directory.
2. Ensures the log directory exists, creating it if necessary.
3. Iterates over all experiments, running the malariasimulation for each one using the specified R script.
4. Logs the output and errors to respective log files.
Args:
exp (object): An experiment object containing attributes such as 'job_directory', 'nexps', 'r_local', and paths
necessary for setting up and running the malariasimulation.
Raises:
IOError: If there is an issue with writing the PowerShell script to the file.
"""
file_path = os.path.join(exp.job_directory, 'run_malariasimulation.ps1') # Change file extension to .ps1
wdir = os.path.abspath(os.path.dirname(__file__))
wdir = os.path.abspath(os.path.join(wdir, os.pardir))
# Define the content of the PowerShell script
powershell_content = f"""
# Job settings
$job_name = "run_malariasimulation"
$exe_path = "{os.path.join(exp.r_local)}"
$work_dir = "{os.path.join(os.getcwd(),'malariasimulation')}"
$jobdir = "{exp.job_directory}"
$log_dir = Join-Path $jobdir "log"
# Navigate to the working directory
Set-Location -Path "$work_dir"
# Loop to run the malariasimulation command
for ($i = 1; $i -le {exp.nexps}; $i++) {{
# Run the malariasimulation command
& "$exe_path/Rscript.exe" "$work_dir\malariasimulation.R" "$work_dir" "$jobdir" $i >> "$log_dir/run_malariasimulation.log" 2>> "$log_dir/run_malariasimulation.err"
}}
# Pause to keep the console open
Write-Host "malariasimulation simulation complete."
"""
# Write the PowerShell script
with open(file_path, 'w') as file:
file.write(powershell_content)
print(f"PowerShell script written successfully at {file_path}")
|