Skip to content

Update costing function to accommodate model output changes - #1840

Open
sakshimohan wants to merge 7 commits into
masterfrom
sakshi/costing_function_fix
Open

sakshimohan wants to merge 7 commits into
masterfrom
sakshi/costing_function_fix

Conversation

@sakshimohan

Copy link
Copy Markdown
Collaborator

This PR makes two updates to the utility functions to extract results and estimate costs -

  1. Update estimate_input_cost_of_scenarios() to extract the new format of number_of_hcw_staff in the healthsystem.summary log
  2. Update extract_results() to accommodate the use of the suspended results folder when simulations are run using the suspend and resume functionality

1. Add suspended_results_folder to extract population scaling factor
2. update get_staff_count_by_facid_and_officer_type to get available_staff_count_by_facid_and_officertype
…esults_folder

- if suspend and resume is not used
…tion_fix

# Conflicts:
#	src/tlo/analysis/utils.py
… and refactor for quicker runs

- extract_results (utils.py): add optional scaling_factor argument. When provided it is used for scaling in place of the factor logged in the suspended or (resume) results folder.
- estimate_input_cost_of_scenarios: add optional scaling_factor argument and add it to every extract_results
  call.
- Equipment costing: build the item-use table in one step instead of inserting one column per item, and concatenate draw/run results once. Removes the DataFrame fragmentation warnings and speeds up the loop.
- Fix level-1b equipment use: the filter `whether_item_was_used is True` was always False, so level-2 equipment use was never copied to level
  1b. Now uses .eq(True). This raises level-1b equipment costs.

Apart from the level-1b equipment fix, results are unchanged.
- HR: aggregate staff counts by year/Facility_Level/OfficerType without dropping labels from the unsorted column MultiIndex
- Consumables: take explicit copies when separating oxygen, blood and IRS from other consumables (removes SettingWithCopyWarning).

No change to results.
@sakshimohan

Copy link
Copy Markdown
Collaborator Author

The table below compares the cost estimated by the original costing function and the updated function, as applied to the improved consumables availability scenarios. There has been a change in the estimate for medical equipment at level 1b. This is due to an error in the previous function. Because levels 1b and 2 are collapsed together, the function assumes that any equipment recorded as used at level 2 in a district is also used at level 1b in that district. However, the filter that identified level-2 equipment use, _df['whether_item_was_used'] is True, compared the whole column to the object True, which always returns False. As a result, level-2 equipment use was never copied to level 1b, and level 1b was costed only for the equipment logged directly at level 1b. The updated function uses .eq(True), so the copy now works as intended.

Estimates for facility operating costs, human resources for health, medical consumables and medical equipment at all other levels are unchanged.

cost_function_comparison

@sakshimohan

Copy link
Copy Markdown
Collaborator Author

@marghe-molaro In this PR, I've updated the costing function (estimate_input_cost_of_scenarios()) and extract_results() to allow for either suspended_results_folder OR scaling_factor as input, to overcome the issue that the (resume) results folder no longer contains scaling_factor in it's demography log. The extract_results() function first uses scaling_factor if this is provided. If not, and suspended_results_folder is provided, it extracts the scaling factor from the demography log of draw 0, run 0 of the suspended results folder. If neither is provided, it falls back to the previous behaviour of extracting the scaling factor from the demography log of each draw and run in the results folder, which only works when suspend-and-resume is not used.
I know you've raised this as an issue (#1873). Let me know if you had any thoughts on whether there is a better way to do this.

The other problem that I've run into is that the healthsystem.summary log no longer captures number_of_hcw_staff for the first year in the resumed simulation. This drops the HR cost estimate for year 1 (of the resumed simulation) to 0 because this estimate relies on number_of_hcw_staff. Is there a simple way to update the logging to address this?
I've tried this with the logged outputs for job_ID consumables_impact-2026-08-29T161006Z

@sakshimohan

Copy link
Copy Markdown
Collaborator Author

@joehcollins This PR incorporates the equipment costing refactoring you did in the branch https://github.com/UCL/TLOmodel/tree/jcollins/pregnancy_cohort_jan26.
Instead of alt_scaling_factor, I have included scaling_factor as an input into the function. I assume it will still work for the specific use case you have for the cohort model. But it also overcomes the challenge that when the simulation is run using the suspend and resume functionality, the resume results folder does not contain the scaling_factor in its demography log.
I have checked that the cost outputs are the same with the new function above. In fact, your refactoring fixed a previous error in the costing of equipment for level 1b. If the treatment of level 1b remains unchanged, i.e. it's merged with level 2, it makes sense that any type of equipment used at level 2, should be assumed to be used also at level 1b and therefore costed accordingly.

This branch has not been deployed

No deployments
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 this pull request may close these issues.

1 participant