|
17 | 17 | * - | **test** |
18 | 18 | - | Runs an example simulation that comes packaged with ``vivarium``. |
19 | 19 | | Useful as an installation test. |
20 | | - * - | **profile** |
21 | | - - | Produces a profile of a simulation using the python |
22 | | - | :mod:`cProfile` module |
23 | 20 |
|
24 | 21 | For more information, see the :ref:`tutorial <cli_tutorial>` on running |
25 | 22 | simulations from the command line. |
|
30 | 27 |
|
31 | 28 | """ |
32 | 29 |
|
33 | | -import cProfile |
34 | 30 | import os |
35 | | -import pstats |
36 | | -from datetime import datetime as dt |
37 | 31 | from pathlib import Path |
38 | 32 | from time import time |
39 | 33 |
|
@@ -173,71 +167,3 @@ def test() -> None: |
173 | 167 |
|
174 | 168 | click.echo() |
175 | 169 | click.secho("Installation test successful!", fg="green") |
176 | | - |
177 | | - |
178 | | -@simulate.command() |
179 | | -@click.argument( |
180 | | - "model_specification", |
181 | | - type=click.Path(exists=True, dir_okay=False, resolve_path=True), |
182 | | -) |
183 | | -@click.option( |
184 | | - "--results_directory", |
185 | | - "-o", |
186 | | - type=click.Path(resolve_path=True), |
187 | | - default=Path("~/vivarium_results/").expanduser(), |
188 | | - show_default=True, |
189 | | - help=( |
190 | | - "The directory to write results to. A folder will be created " |
191 | | - "in this directory with the same name as the configuration file." |
192 | | - ), |
193 | | -) |
194 | | -@click.option( |
195 | | - "--skip_writing", |
196 | | - is_flag=True, |
197 | | - help=( |
198 | | - "Skip writing the simulation results to the output directory; the time spent " |
199 | | - "normally writing simulation results to disk will not be included in the profiling " |
200 | | - "statistics." |
201 | | - ), |
202 | | -) |
203 | | -@click.option( |
204 | | - "--skip_processing", |
205 | | - is_flag=True, |
206 | | - help=( |
207 | | - "Skip processing the resulting binary file to a human-readable .txt file " |
208 | | - "sorted by cumulative runtime; the resulting .stats file can still be read " |
209 | | - "and processed later using the pstats module." |
210 | | - ), |
211 | | -) |
212 | | -def profile( |
213 | | - model_specification: Path, |
214 | | - results_directory: Path, |
215 | | - skip_writing: bool, |
216 | | - skip_processing: bool, |
217 | | -) -> None: |
218 | | - """Run a simulation based on the provided MODEL_SPECIFICATION and profile the run.""" |
219 | | - model_specification = Path(model_specification) |
220 | | - results_directory = Path(results_directory) |
221 | | - results_root = results_directory / f"{dt.now().strftime('%Y_%m_%d_%H_%M_%S')}" |
222 | | - configure_logging_to_file(output_directory=results_root) |
223 | | - |
224 | | - if skip_writing: |
225 | | - configuration_override = {} |
226 | | - else: |
227 | | - output_data_root = results_root / "results" |
228 | | - output_data_root.mkdir(parents=True, exist_ok=False) |
229 | | - configuration_override = { |
230 | | - "output_data": {"results_directory": str(output_data_root)}, |
231 | | - } |
232 | | - |
233 | | - out_stats_file = results_root / f"{model_specification.name}".replace("yaml", "stats") |
234 | | - sim = SimulationContext(model_specification, configuration=configuration_override) |
235 | | - command = f"sim.run_simulation()" |
236 | | - cProfile.runctx(command, globals=globals(), locals=locals(), filename=str(out_stats_file)) |
237 | | - |
238 | | - if not skip_processing: |
239 | | - out_txt_file = Path(str(out_stats_file) + ".txt") |
240 | | - with out_txt_file.open("w") as f: |
241 | | - p = pstats.Stats(str(out_stats_file), stream=f) |
242 | | - p.sort_stats("cumulative") |
243 | | - p.print_stats() |
0 commit comments