Skip to content

Commit 653cce1

Browse files
authored
Merge pull request #501 from The-Strategy-Unit/copilot/fix-59ab13b9-7b4a-45d4-b591-8d9def500081
Update docstrings to Google-style format and fix outdated documentation
2 parents bbaff43 + 807c727 commit 653cce1

File tree

18 files changed

+595
-598
lines changed

18 files changed

+595
-598
lines changed

src/nhp/docker/run.py

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,27 @@ class RunWithLocalStorage:
2323
def __init__(self, filename: str):
2424
"""Initialize the RunWithLocalStorage instance.
2525
26-
:param filename: Name of the parameter file to load.
27-
:type filename: str
26+
Args:
27+
filename: Name of the parameter file to load.
2828
"""
2929
self.params = load_params(f"queue/{filename}")
3030

3131
def finish(self, results_file: str, saved_files: list, save_full_model_results: bool) -> None:
3232
"""Post model run steps.
3333
34-
:param results_file: the path to the results file
35-
:type results_file: str
36-
:param saved_files: filepaths of results, saved in parquet format and params
37-
in json format
38-
:type saved_files: list
39-
:param save_full_model_results: whether to save the full model results or not
40-
:type save_full_model_results: bool
34+
Args:
35+
results_file: The path to the results file.
36+
saved_files: Filepaths of results, saved in parquet format and params in json format.
37+
save_full_model_results: Whether to save the full model results or not.
4138
"""
4239

4340
def progress_callback(self) -> Callable[[Any], Callable[[Any], None]]:
4441
"""Progress callback method.
4542
46-
for local storage do nothing
43+
For local storage do nothing.
44+
45+
Returns:
46+
A no-op progress callback function.
4747
"""
4848
return noop_progress_callback
4949

@@ -54,10 +54,9 @@ class RunWithAzureStorage:
5454
def __init__(self, filename: str, config: Config = Config()):
5555
"""Initialise RunWithAzureStorage.
5656
57-
:param filename:
58-
:type filename: str
59-
:param config: The configuration for the run
60-
:type config: Config
57+
Args:
58+
filename: Name of the parameter file to load.
59+
config: The configuration for the run. Defaults to Config().
6160
"""
6261
logging.getLogger("azure.storage.common.storageclient").setLevel(logging.WARNING)
6362
logging.getLogger("azure.core.pipeline.policies.http_logging_policy").setLevel(
@@ -86,10 +85,11 @@ def _get_container(self, container_name: str):
8685
def _get_params(self, filename: str) -> dict:
8786
"""Get the parameters for the model.
8887
89-
:param filename: the name of the params file
90-
:type filename: str
91-
:return: the parameters for the model
92-
:rtype: dict
88+
Args:
89+
filename: The name of the params file.
90+
91+
Returns:
92+
The parameters for the model.
9393
"""
9494
logging.info("downloading params: %s", filename)
9595

@@ -102,12 +102,11 @@ def _get_params(self, filename: str) -> dict:
102102
def _get_data(self, year: str, dataset: str) -> None:
103103
"""Get data to run the model.
104104
105-
for local storage, the data is already available, so do nothing.
105+
Downloads data from Azure storage for the specified year and dataset.
106106
107-
:param year: the year of data to load
108-
:type year: str
109-
:param year: the year of data to load
110-
:type year: str
107+
Args:
108+
year: The year of data to load.
109+
dataset: The dataset to load.
111110
"""
112111
logging.info("downloading data (%s / %s)", year, dataset)
113112
fs_client = DataLakeServiceClient(
@@ -137,12 +136,11 @@ def _get_data(self, year: str, dataset: str) -> None:
137136
def _upload_results_json(self, results_file: str, metadata: dict) -> None:
138137
"""Upload the results.
139138
140-
once the model has run, upload the results to blob storage
139+
Once the model has run, upload the results to blob storage.
141140
142-
:param results_file: the saved results file
143-
:type results_file: str
144-
:param metadata: the metadata to attach to the blob
145-
:type metadata: dict
141+
Args:
142+
results_file: The saved results file.
143+
metadata: The metadata to attach to the blob.
146144
"""
147145
container = self._get_container("results")
148146

@@ -157,14 +155,12 @@ def _upload_results_json(self, results_file: str, metadata: dict) -> None:
157155
def _upload_results_files(self, files: list, metadata: dict) -> None:
158156
"""Upload the results.
159157
160-
once the model has run, upload the files (parquet for model results and json for
161-
model params) to blob storage
162-
163-
:param files: list of files to be uploaded
164-
:type files: list
165-
:param metadata: the metadata to attach to the blob
166-
:type metadata: dict
158+
Once the model has run, upload the files (parquet for model results and json for
159+
model params) to blob storage.
167160
161+
Args:
162+
files: List of files to be uploaded.
163+
metadata: The metadata to attach to the blob.
168164
"""
169165
container = self._get_container("results")
170166
for file in files:
@@ -202,7 +198,7 @@ def _upload_full_model_results(self) -> None:
202198
def _cleanup(self) -> None:
203199
"""Cleanup.
204200
205-
once the model has run, remove the file from the queue
201+
Once the model has run, remove the file from the queue.
206202
"""
207203
logging.info("cleaning up queue")
208204

@@ -211,13 +207,10 @@ def _cleanup(self) -> None:
211207
def finish(self, results_file: str, saved_files: list, save_full_model_results: bool) -> None:
212208
"""Post model run steps.
213209
214-
:param results_file: the path to the results file
215-
:type results_file: str
216-
:param saved_files: filepaths of results, saved in parquet format and params
217-
in json format
218-
:type saved_files: list
219-
:param save_full_model_results: whether to save the full model results or not
220-
:type save_full_model_results: bool
210+
Args:
211+
results_file: The path to the results file.
212+
saved_files: Filepaths of results, saved in parquet format and params in json format.
213+
save_full_model_results: Whether to save the full model results or not.
221214
"""
222215
metadata = {
223216
k: str(v)
@@ -233,7 +226,10 @@ def finish(self, results_file: str, saved_files: list, save_full_model_results:
233226
def progress_callback(self) -> Callable[[Any], Callable[[Any], None]]:
234227
"""Progress callback method.
235228
236-
updates the metadata for the blob in the queue to give progress
229+
Updates the metadata for the blob in the queue to give progress.
230+
231+
Returns:
232+
A callback function that updates progress for each model type.
237233
"""
238234
blob = self._queue_blob
239235

src/nhp/model/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
There are existing launch profiles for vscode that use this file, or you can use it directly in the
88
console, e.g.
99
10-
python run_model.py data/[DATASET]/results/[SCENARIO]/[RUN_TIME] 0 1 --debug -t=ip
10+
python -m nhp.model -d data --model-run 1 -t ip
1111
1212
will run a single run of the inpatients model, returning the results to display.
1313
"""

src/nhp/model/aae.py

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,32 @@ class AaEModel(Model):
1818
1919
Implementation of the Model for Accident and Emergency attendances.
2020
21-
:param params: the parameters to run the model with, or the path to a params file to load
22-
:type params: dict or string
23-
:param data: a Data class ready to be constructed
24-
:type data: Data
25-
:param hsa: An instance of the HealthStatusAdjustment class. If left as None an instance is
26-
created
27-
:type hsa: HealthStatusAdjustment, optional
28-
:param run_params: the parameters to use for each model run. generated automatically if left as
29-
None
30-
:type run_params: dict
31-
:param save_full_model_results: whether to save the full model results or not
32-
:type save_full_model_results: bool, optional
21+
Args:
22+
params: The parameters to run the model with, or the path to a params file to load.
23+
data: A callable that creates a Data instance.
24+
hsa: An instance of the HealthStatusAdjustment class. If left as None an instance is
25+
created. Defaults to None.
26+
run_params: The parameters to use for each model run. Generated automatically if left as
27+
None. Defaults to None.
28+
save_full_model_results: Whether to save the full model results or not. Defaults to False.
3329
"""
3430

3531
def __init__(
3632
self,
37-
params: dict,
33+
params: dict | str,
3834
data: Callable[[int, str], Data],
3935
hsa: Any = None,
4036
run_params: dict | None = None,
4137
save_full_model_results: bool = False,
4238
) -> None:
4339
"""Initialise the A&E Model.
4440
45-
:param params: the parameters to use
46-
:type params: dict
47-
:param data: a method to create a Data instance
48-
:type data: Callable[[int, str], Data]
49-
:param hsa: _Health Status Adjustment object, defaults to None
50-
:type hsa: Any, optional
51-
:param run_params: the run parameters to use, defaults to None
52-
:type run_params: dict | None, optional
53-
:param save_full_model_results: whether to save full model results, defaults to False
54-
:type save_full_model_results: bool, optional
41+
Args:
42+
params: The parameters to use.
43+
data: A method to create a Data instance.
44+
hsa: Health Status Adjustment object. Defaults to None.
45+
run_params: The run parameters to use. Defaults to None.
46+
save_full_model_results: Whether to save full model results. Defaults to False.
5547
"""
5648
# call the parent init function
5749
super().__init__(
@@ -70,10 +62,11 @@ def _get_data(self, data_loader: Data) -> pd.DataFrame:
7062
def get_data_counts(self, data: pd.DataFrame) -> np.ndarray:
7163
"""Get row counts of data.
7264
73-
:param data: the data to get the counts of
74-
:type data: pd.DataFrame
75-
:return: the counts of the data, required for activity avoidance steps
76-
:rtype: np.ndarray
65+
Args:
66+
data: The data to get the counts of.
67+
68+
Returns:
69+
The counts of the data, required for activity avoidance steps.
7770
"""
7871
return np.array([data["arrivals"]]).astype(float)
7972

@@ -99,15 +92,15 @@ def _load_strategies(self, data_loader: Data) -> None:
9992
def apply_resampling(self, row_samples: np.ndarray, data: pd.DataFrame) -> pd.DataFrame:
10093
"""Apply row resampling.
10194
102-
Called from within `model.activity_resampling.ActivityResampling.apply_resampling`
95+
Called from within `model.activity_resampling.ActivityResampling.apply_resampling`.
96+
97+
Args:
98+
row_samples: [1xn] array, where n is the number of rows in `data`, containing the new
99+
values for `data["arrivals"]`.
100+
data: The data that we want to update.
103101
104-
:param row_samples: [1xn] array, where n is the number of rows in `data`, containing the new
105-
values for `data["arrivals"]`
106-
:type row_samples: np.ndarray
107-
:param data: the data that we want to update
108-
:type data: pd.DataFrame
109-
:return: the updated data
110-
:rtype: pd.DataFrame
102+
Returns:
103+
The updated data.
111104
"""
112105
data["arrivals"] = row_samples[0]
113106
# return the altered data
@@ -118,18 +111,25 @@ def efficiencies(
118111
) -> tuple[pd.DataFrame, pd.DataFrame | None]:
119112
"""Run the efficiencies steps of the model.
120113
121-
:param model_iteration: an instance of the ModelIteration class
122-
:type model_iteration: model.model_iteration.ModelIteration
114+
Args:
115+
data: The data to apply efficiencies to.
116+
model_iteration: An instance of the ModelIteration class.
117+
118+
Returns:
119+
Tuple containing the updated data and step counts (None for A&E).
123120
"""
124121
# A&E doesn't have any efficiencies steps
125122
return data, None
126123

127124
@staticmethod
128125
def process_results(data: pd.DataFrame) -> pd.DataFrame:
129-
"""Processes the data into a format suitable for aggregation in results files.
126+
"""Process the data into a format suitable for aggregation in results files.
130127
131-
:param data: Data to be processed. Format should be similar to Model.data
132-
:type data: pd.DataFrame
128+
Args:
129+
data: Data to be processed. Format should be similar to Model.data.
130+
131+
Returns:
132+
Processed results.
133133
"""
134134
data["measure"] = "walk-in"
135135
data.loc[data["is_ambulance"], "measure"] = "ambulance"
@@ -161,10 +161,11 @@ def process_results(data: pd.DataFrame) -> pd.DataFrame:
161161
def specific_aggregations(self, model_results: pd.DataFrame) -> dict[str, pd.Series]:
162162
"""Create other aggregations specific to the model type.
163163
164-
:param model_results: the results of a model run
165-
:type model_results: pd.DataFrame
166-
:return: dictionary containing the specific aggregations
167-
:rtype: dict[str, pd.Series]
164+
Args:
165+
model_results: The results of a model run.
166+
167+
Returns:
168+
Dictionary containing the specific aggregations.
168169
"""
169170
return {
170171
"acuity": self.get_agg(model_results, "acuity"),
@@ -176,10 +177,12 @@ def calculate_avoided_activity(
176177
) -> pd.DataFrame:
177178
"""Calculate the rows that have been avoided.
178179
179-
:param data: The data before the binomial thinning step
180-
:type data: pd.DataFrame
181-
:return: The data that was avoided in the binomial thinning step
182-
:rtype: pd.DataFrame
180+
Args:
181+
data: The data before the binomial thinning step.
182+
data_resampled: The data after the binomial thinning step.
183+
184+
Returns:
185+
The data that was avoided in the binomial thinning step.
183186
"""
184187
avoided = data["arrivals"] - data_resampled["arrivals"]
185188
data["arrivals"] = avoided
@@ -192,10 +195,9 @@ def save_results(self, model_iteration: ModelIteration, path_fn: Callable[[str],
192195
It saves just the `rn` (row number) column and the `arrivals`, with the intention that
193196
you rejoin to the original data.
194197
195-
:param model_iteration: an instance of the `ModelIteration` class
196-
:type model_iteration: model.model_iteration.ModelIteration
197-
:param path_fn: a function which takes the activity type and returns a path
198-
:type path_fn: Callable[[str], str]
198+
Args:
199+
model_iteration: An instance of the ModelIteration class.
200+
path_fn: A function which takes the activity type and returns a path.
199201
"""
200202
model_iteration.get_model_results().set_index(["rn"])[["arrivals"]].to_parquet(
201203
f"{path_fn('aae')}/0.parquet"

src/nhp/model/activity_resampling.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
Methods for handling row resampling
44
"""
55

6+
from typing import TYPE_CHECKING
7+
68
import numpy as np
79
import pandas as pd
810

11+
if TYPE_CHECKING:
12+
from nhp.model.model_iteration import ModelIteration
13+
914

1015
class ActivityResampling:
1116
"""Activity Resampling.
@@ -24,16 +29,16 @@ class ActivityResampling:
2429
method.
2530
This updates the `model_iteration` which is passed in at initialisation.
2631
27-
:param model_iteration: the model iteration object, which contains all of the required
28-
values to run the model.
29-
:type model_iteration: ModelIteration
32+
Args:
33+
model_iteration: The model iteration object, which contains all of the required
34+
values to run the model.
3035
"""
3136

32-
def __init__(self, model_iteration) -> None:
37+
def __init__(self, model_iteration: "ModelIteration") -> None:
3338
"""Initialise ActivityResampling.
3439
35-
:param model_iteration: the current model iteration we are performing
36-
:type model_iteration: ModelIteration
40+
Args:
41+
model_iteration: The current model iteration we are performing.
3742
"""
3843
self._model_iteration = model_iteration
3944

0 commit comments

Comments
 (0)