Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate parsing log files from simulation run #1547

Merged
merged 6 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/calibration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ jobs:
mkdir -p "${task_output_dir}"

tlo scenario-run --output-dir "${task_output_dir}" --draw "${draw}" ${{ matrix.index }} "${{ needs.setup.outputs.worktree_path }}/src/scripts/calibration_analyses/scenarios/long_run_all_diseases.py"
tlo parse-log ${task_output_dir}
working-directory: "${{ needs.setup.outputs.worktree_path }}"

# Do the postprocessing
Expand Down
14 changes: 6 additions & 8 deletions src/scripts/dev/scenarios/playing_22.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ def __init__(self):
super().__init__()
self.seed = 655123742
self.start_date = Date(2010, 1, 1)
self.end_date = Date(2011, 1, 1)
self.pop_size = 200
self.number_of_draws = 5
self.runs_per_draw = 1
self.end_date = Date(2013, 1, 1)
self.pop_size = 1000
self.number_of_draws = 3
self.runs_per_draw = 3

def log_configuration(self):
return {
# 'filename': 'my-scenario',
'directory': './outputs',
'custom_levels': {
'*': logging.INFO,
Expand All @@ -39,9 +38,8 @@ def modules(self):

def draw_parameters(self, draw_number, rng):
return {
'Lifestyle': {
'init_p_urban': rng.randint(10, 20) / 100.0,
'init_p_high_sugar': 0.52,
'Demography': {
'max_age_initial': [80, 90, 100][draw_number],
},
}

Expand Down
18 changes: 18 additions & 0 deletions src/tlo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import math
import os
import pickle
import tempfile
from collections import defaultdict
from pathlib import Path
Expand All @@ -22,6 +23,7 @@
from azure.storage.fileshare import ShareClient, ShareDirectoryClient, ShareFileClient
from git import Repo

from tlo.analysis.utils import parse_log_file
from tlo.scenario import SampleRunner, ScenarioLoader

JOB_LABEL_PADDING = len("State transition time")
Expand Down Expand Up @@ -88,6 +90,21 @@ def scenario_run(scenario_file, draw_only, draw: tuple, output_dir=None, scenari
else:
runner.run()

@cli.command()
@click.argument("LOG_DIRECTORY", type=click.Path(exists=True))
def parse_log(log_directory):
assert os.path.isdir(log_directory), f"{log_directory} must be a directory"

path = Path(log_directory)

log_files = list(path.glob("*.log"))
assert len(log_files) == 1, f"directory {log_directory} must contain exactly one file with extension .log"

outputs = parse_log_file(log_files[0])
for key, output in outputs.items():
if key.startswith("tlo."):
with open(path / f"{key}.pickle", "wb") as f:
pickle.dump(output, f)

@cli.command()
@click.argument("scenario_file", type=click.Path(exists=True))
Expand Down Expand Up @@ -237,6 +254,7 @@ def batch_submit(ctx, scenario_file, asserts_on, more_memory, keep_pool_alive, i
pip install -r requirements/base.txt
env | grep "^AZ_" | while read line; do echo "$line"; done
{py_opt} tlo --config-file tlo.example.conf batch-run {azure_run_json} {working_dir} {{draw_number}} {{run_number}}
tlo --config-file tlo.example.conf parse-log {working_dir}/{{draw_number}}/{{run_number}}
cp {task_dir}/std*.txt {working_dir}/{{draw_number}}/{{run_number}}/.
gzip {working_dir}/{{draw_number}}/{{run_number}}/*.{gzip_pattern_match}
cp -r {working_dir}/* {azure_directory}/.
Expand Down
9 changes: 0 additions & 9 deletions src/tlo/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def draw_parameters(self, draw_number, rng):
import argparse
import datetime
import json
import pickle
from collections.abc import Iterable
from itertools import product
from pathlib import Path, PurePosixPath
Expand All @@ -72,7 +71,6 @@ def draw_parameters(self, draw_number, rng):
import numpy as np

from tlo import Date, Simulation, logging
from tlo.analysis.utils import parse_log_file
from tlo.util import str_to_pandas_date

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -446,13 +444,6 @@ def run_sample_by_number(self, output_directory, draw_number, sample_number):
sim.run_simulation_to(to_date=self.scenario.end_date)
sim.finalise()

if sim.log_filepath is not None:
outputs = parse_log_file(sim.log_filepath)
for key, output in outputs.items():
if key.startswith("tlo."):
with open(Path(log_config["directory"]) / f"{key}.pickle", "wb") as f:
pickle.dump(output, f)

def run(self):
"""Run all samples for the scenario. Used by `tlo scenario-run` to run the scenario locally"""
log_config = self.scenario.get_log_config()
Expand Down
Loading