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

Save a label for the draw when using Scenario class #1451

Open
tbhallett opened this issue Aug 2, 2024 · 0 comments · May be fixed by #1452
Open

Save a label for the draw when using Scenario class #1451

tbhallett opened this issue Aug 2, 2024 · 0 comments · May be fixed by #1452

Comments

@tbhallett
Copy link
Collaborator

The Scenario class works using draw-numbers, but analysis scripts want to label results with "friendly names". The work-around being used currently is for these friendly names to be generated in the instance of the Scenario class, which is then used in the analysis class. (e.g. this scenario and the corresponding part of the analysis file:

def get_parameter_names_from_scenario_file() -> Tuple[str]:
"""Get the tuple of names of the scenarios from `Scenario` class used to create the results."""
from scripts.overview_paper.C_impact_of_healthsystem_assumptions.scenario_impact_of_healthsystem import (
ImpactOfHealthSystemAssumptions,
)
e = ImpactOfHealthSystemAssumptions()
return tuple(e._scenarios.keys())
)

This a bit cumbersome:

  • analysis scripts need to import and use scenario classes, which limits how much we can recycle the scripts
  • in a worse-case-scenario, the scenario class code and the results being analysed, become out of sync, and so the wrong labels are applied.

A good solution would enable retrieving such a friendly name from the results themselves.

One solution: the friendly name could be logged in tlo.scenario log, in the same manner of the parameters that are being over-ridden. A helper function (like this one for the parameters) could then retrieve it:

def extract_params(results_folder: Path) -> Optional[pd.DataFrame]:
"""Utility function to get overridden parameters from scenario runs
Returns dateframe summarizing parameters that change across the draws. It produces a dataframe with index of draw
and columns of each parameters that is specified to be varied in the batch. NB. This does the extraction from run 0
in each draw, under the assumption that the over-written parameters are the same in each run.
"""
try:
# Get the paths for the draws
draws = [f for f in os.scandir(results_folder) if f.is_dir()]
list_of_param_changes = list()
for d in draws:
p = load_pickled_dataframes(results_folder, d.name, 0, name="tlo.scenario")
p = p["tlo.scenario"]["override_parameter"]
p['module_param'] = p['module'] + ':' + p['name']
p.index = [int(d.name)] * len(p.index)
list_of_param_changes.append(p[['module_param', 'new_value']])
params = pd.concat(list_of_param_changes)
params.index.name = 'draw'
params = params.rename(columns={'new_value': 'value'})
params = params.sort_index()
return params
except KeyError:
print("No parameters changed between the runs")
return None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant