Skip to content

Commit 25e28ed

Browse files
authored
Merge pull request #160 from The-Strategy-Unit/151_153_refactor-time-until-death-calculations
Refactor time until death calculations
2 parents 6f1ac91 + 201e46b commit 25e28ed

File tree

6 files changed

+312
-339
lines changed

6 files changed

+312
-339
lines changed

renal_capacity_model/config_values.py

Lines changed: 96 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -264,151 +264,151 @@
264264
"initialisation": {
265265
"not_listed": {
266266
"early": {
267-
1: [0.926963321, 2127.231053],
268-
2: [1.102888443, 2360.960064],
269-
3: [1.042024424, 1757.881403],
270-
4: [1.102673686, 1641.614239],
271-
5: [1.149855608, 1561.636782],
272-
6: [1.151814597, 1190.40286],
267+
1: {"shape": 0.926963321, "scale": 2127.231053},
268+
2: {"shape": 1.102888443, "scale": 2360.960064},
269+
3: {"shape": 1.042024424, "scale": 1757.881403},
270+
4: {"shape": 1.102673686, "scale": 1641.614239},
271+
5: {"shape": 1.149855608, "scale": 1561.636782},
272+
6: {"shape": 1.151814597, "scale": 1190.40286},
273273
},
274274
"late": {
275-
1: [1.099732869, 4314.8445],
276-
2: [1.239811029, 2204.356655],
277-
3: [1.025879638, 1940.947327],
278-
4: [1.17279732, 1700.235305],
279-
5: [1.168619527, 1512.312581],
280-
6: [1.156286321, 1253.912137],
275+
1: {"shape": 1.099732869, "scale": 4314.8445},
276+
2: {"shape": 1.239811029, "scale": 2204.356655},
277+
3: {"shape": 1.025879638, "scale": 1940.947327},
278+
4: {"shape": 1.17279732, "scale": 1700.235305},
279+
5: {"shape": 1.168619527, "scale": 1512.312581},
280+
6: {"shape": 1.156286321, "scale": 1253.912137},
281281
},
282282
},
283283
"listed": {
284284
"early": {
285-
1: [1.426589565, 13064.21921],
286-
2: [1.501319694, 8229.734667],
287-
3: [1.475457685, 6126.819295],
288-
4: [1.388640715, 4221.17834],
289-
5: [1.381583619, 2805.868318],
290-
6: [1.262968437, 1862.925138],
285+
1: {"shape": 1.426589565, "scale": 13064.21921},
286+
2: {"shape": 1.501319694, "scale": 8229.734667},
287+
3: {"shape": 1.475457685, "scale": 6126.819295},
288+
4: {"shape": 1.388640715, "scale": 4221.17834},
289+
5: {"shape": 1.381583619, "scale": 2805.868318},
290+
6: {"shape": 1.262968437, "scale": 1862.925138},
291291
},
292292
"late": {
293-
1: [1.403897083, 13250.96392],
294-
2: [1.359190822, 8393.578341],
295-
3: [1.435707658, 5981.108405],
296-
4: [1.326446673, 4096.723314],
297-
5: [1.469351223, 2905.94038],
298-
6: [1.244780918, 1800.673553],
293+
1: {"shape": 1.403897083, "scale": 13250.96392},
294+
2: {"shape": 1.359190822, "scale": 8393.578341},
295+
3: {"shape": 1.435707658, "scale": 5981.108405},
296+
4: {"shape": 1.326446673, "scale": 4096.723314},
297+
5: {"shape": 1.469351223, "scale": 2905.94038},
298+
6: {"shape": 1.244780918, "scale": 1800.673553},
299299
},
300300
},
301301
"received_Tx": {
302302
"early": {
303-
1: [1.945083614, 11291.09043],
304-
2: [1.765333669, 8449.099418],
305-
3: [1.750669869, 6535.672038],
306-
4: [1.61142907, 4693.44361],
307-
5: [1.51886545, 3189.62546],
308-
6: [1.315655704, 2078.132622],
303+
1: {"shape": 1.945083614, "scale": 11291.09043},
304+
2: {"shape": 1.765333669, "scale": 8449.099418},
305+
3: {"shape": 1.750669869, "scale": 6535.672038},
306+
4: {"shape": 1.61142907, "scale": 4693.44361},
307+
5: {"shape": 1.51886545, "scale": 3189.62546},
308+
6: {"shape": 1.315655704, "scale": 2078.132622},
309309
},
310310
"late": {
311-
1: [1.982364823, 10573.47681],
312-
2: [1.658109448, 8611.590105],
313-
3: [1.699130074, 6499.195977],
314-
4: [1.491659213, 4669.1818],
315-
5: [1.678178577, 3296.367133],
316-
6: [1.306592647, 1990.733317],
311+
1: {"shape": 1.982364823, "scale": 10573.47681},
312+
2: {"shape": 1.658109448, "scale": 8611.590105},
313+
3: {"shape": 1.699130074, "scale": 6499.195977},
314+
4: {"shape": 1.491659213, "scale": 4669.1818},
315+
5: {"shape": 1.678178577, "scale": 3296.367133},
316+
6: {"shape": 1.306592647, "scale": 1990.733317},
317317
},
318318
},
319319
},
320320
"incidence": {
321321
"not_listed": {
322322
"early": {
323-
1: [1.021124756, 1630.450226],
324-
2: [1.033558934, 1601.726618],
325-
3: [1.175150289, 1788.324784],
326-
4: [1.171027698, 1671.61871],
327-
5: [1.182843607, 1604.030346],
328-
6: [1.111932566, 1324.708399],
323+
1: {"shape": 1.021124756, "scale": 1630.450226},
324+
2: {"shape": 1.033558934, "scale": 1601.726618},
325+
3: {"shape": 1.175150289, "scale": 1788.324784},
326+
4: {"shape": 1.171027698, "scale": 1671.61871},
327+
5: {"shape": 1.182843607, "scale": 1604.030346},
328+
6: {"shape": 1.111932566, "scale": 1324.708399},
329329
},
330330
"late": {
331-
1: [1.025446743, 2098.5119],
332-
2: [0.86558945, 2071.301577],
333-
3: [0.787953011, 1428.187242],
334-
4: [1.093861294, 1533.652413],
335-
5: [0.817406992, 1170.501979],
336-
6: [0.829891, 841.5931435],
331+
1: {"shape": 1.025446743, "scale": 2098.5119},
332+
2: {"shape": 0.86558945, "scale": 2071.301577},
333+
3: {"shape": 0.787953011, "scale": 1428.187242},
334+
4: {"shape": 1.093861294, "scale": 1533.652413},
335+
5: {"shape": 0.817406992, "scale": 1170.501979},
336+
6: {"shape": 0.829891, "scale": 841.5931435},
337337
},
338338
},
339339
"listed": {
340340
"early": {
341-
1: [1.76722409, 7565.454427],
342-
2: [2.071602421, 5267.578147],
343-
3: [1.842843082, 5294.787645],
344-
4: [2.276835063, 3862.063689],
345-
5: [1.990617668, 3148.737229],
346-
6: [2.435166079, 2479.412332],
341+
1: {"shape": 1.76722409, "scale": 7565.454427},
342+
2: {"shape": 2.071602421, "scale": 5267.578147},
343+
3: {"shape": 1.842843082, "scale": 5294.787645},
344+
4: {"shape": 2.276835063, "scale": 3862.063689},
345+
5: {"shape": 1.990617668, "scale": 3148.737229},
346+
6: {"shape": 2.435166079, "scale": 2479.412332},
347347
},
348348
"late": {
349-
1: [2.732803839, 5100.306997],
350-
2: [1.7523112, 6999.685955],
351-
3: [2.667855187, 3817.522327],
352-
4: [2.39938038, 3728.91519],
353-
5: [2.195794462, 2550.374846],
354-
6: [2.435166079, 2479.412332],
349+
1: {"shape": 2.732803839, "scale": 5100.306997},
350+
2: {"shape": 1.7523112, "scale": 6999.685955},
351+
3: {"shape": 2.667855187, "scale": 3817.522327},
352+
4: {"shape": 2.39938038, "scale": 3728.91519},
353+
5: {"shape": 2.195794462, "scale": 2550.374846},
354+
6: {"shape": 2.435166079, "scale": 2479.412332},
355355
},
356356
},
357357
"received_Tx": {
358358
"early": {
359-
1: [2.51486622, 6903.239543],
360-
2: [3.159509376, 5391.985192],
361-
3: [2.70710626, 5381.509481],
362-
4: [2.893707823, 4282.49379],
363-
5: [2.35276178, 3550.906294],
364-
6: [4.316362639, 3118.52084],
359+
1: {"shape": 2.51486622, "scale": 6903.239543},
360+
2: {"shape": 3.159509376, "scale": 5391.985192},
361+
3: {"shape": 2.70710626, "scale": 5381.509481},
362+
4: {"shape": 2.893707823, "scale": 4282.49379},
363+
5: {"shape": 2.35276178, "scale": 3550.906294},
364+
6: {"shape": 4.316362639, "scale": 3118.52084},
365365
},
366366
"late": {
367-
1: [3.160772244, 5169.606112],
368-
2: [2.323615011, 7944.757158],
369-
3: [3.8964386, 4637.1169],
370-
4: [5.45663996, 3913.875032],
371-
5: [3.071596253, 3120.115725],
372-
6: [4.316362639, 3118.52084],
367+
1: {"shape": 3.160772244, "scale": 5169.606112},
368+
2: {"shape": 2.323615011, "scale": 7944.757158},
369+
3: {"shape": 3.8964386, "scale": 4637.1169},
370+
4: {"shape": 5.45663996, "scale": 3913.875032},
371+
5: {"shape": 3.071596253, "scale": 3120.115725},
372+
6: {"shape": 4.316362639, "scale": 3118.52084},
373373
},
374374
},
375375
},
376376
}
377377

378378
tw_cadTx_values = {
379-
1: [1.12, 719],
380-
2: [1.23, 734],
381-
3: [1.31, 794],
382-
4: [1.31, 762],
383-
5: [1.23, 619],
384-
6: [1.02, 385],
379+
1: {"shape": 1.12, "scale": 719},
380+
2: {"shape": 1.23, "scale": 734},
381+
3: {"shape": 1.31, "scale": 794},
382+
4: {"shape": 1.31, "scale": 762},
383+
5: {"shape": 1.23, "scale": 619},
384+
6: {"shape": 1.02, "scale": 385},
385385
}
386386

387387
tw_liveTx_values = {
388-
1: [1.03, 399],
389-
2: [1.06, 464],
390-
3: [1.08, 417],
391-
4: [1.09, 420],
392-
5: [1.22, 391],
393-
6: [1.01, 342],
388+
1: {"shape": 1.03, "scale": 399},
389+
2: {"shape": 1.06, "scale": 464},
390+
3: {"shape": 1.08, "scale": 417},
391+
4: {"shape": 1.09, "scale": 420},
392+
5: {"shape": 1.22, "scale": 391},
393+
6: {"shape": 1.01, "scale": 342},
394394
}
395395

396396
tw_cadTx_initialisation_values = {
397-
1: [1.12, 1336],
398-
2: [1.13, 1163],
399-
3: [1.22, 1194],
400-
4: [1.26, 1023],
401-
5: [1.28, 839],
402-
6: [1.28, 409],
397+
1: {"shape": 1.12, "scale": 1336},
398+
2: {"shape": 1.13, "scale": 1163},
399+
3: {"shape": 1.22, "scale": 1194},
400+
4: {"shape": 1.26, "scale": 1023},
401+
5: {"shape": 1.28, "scale": 839},
402+
6: {"shape": 1.28, "scale": 409},
403403
}
404404

405405
tw_liveTx_initialisation_values = {
406-
1: [0.96, 752],
407-
2: [1.05, 1006],
408-
3: [0.96, 903],
409-
4: [1.01, 665],
410-
5: [1.27, 765],
411-
6: [1.27, 765],
406+
1: {"shape": 0.96, "scale": 752},
407+
2: {"shape": 1.05, "scale": 1006},
408+
3: {"shape": 0.96, "scale": 903},
409+
4: {"shape": 1.01, "scale": 665},
410+
5: {"shape": 1.27, "scale": 765},
411+
6: {"shape": 1.27, "scale": 765},
412412
}
413413

414414
# Time waiting before dialysis

renal_capacity_model/entity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ def __init__(
4040
self.time_living_with_live_transplant: float | None = None
4141
self.time_living_with_cadaver_transplant: float | None = None
4242
self.transplant_count = 0
43-
self.time_on_waiting_list = 0
43+
self.time_on_waiting_list: float = 0
4444
self.time_enters_waiting_list: float | None = None
4545
self.time_of_transplant: float | None = None

renal_capacity_model/helpers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,21 @@ def check_time_to_event_curve_dfs(tte_df_name: str, tte_df: pd.DataFrame):
315315
assert list(tte_df.columns) == cols
316316
except AssertionError:
317317
raise ValueError(f"Check {tte_df_name} csv - incorrect indices/columns")
318+
319+
320+
def calculate_time_to_event(
321+
rng: np.random.Generator, scale: float, shape: float, multiplier: float = 1
322+
) -> float:
323+
"""Calculate time to event, sampling from a Weibull distribution and multiplying
324+
it with a scale and optional multiplier
325+
326+
Args:
327+
rng (np.random.Generator): Random Number Generator
328+
scale (float): Scale to be used for the calculation
329+
shape (float): Shape parameter for Weibull distribution
330+
multiplier (float, optional): Optional multiplier. Defaults to 1.
331+
332+
Returns:
333+
float: Sampled time to event
334+
"""
335+
return (scale * rng.weibull(shape)) * multiplier

renal_capacity_model/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ def main(
6060
)
6161
for excel_file in [path_to_inputs_file, path_to_outputs_file]:
6262
filepaths.append(copy_excel_files(excel_file, run_start_time))
63-
write_results_to_excel(filepaths[1], combined_results, trial.costs_dfs)
63+
write_results_to_excel(
64+
filepaths[1],
65+
combined_results,
66+
trial.costs_dfs,
67+
sim_years=int(config.sim_duration / 365),
68+
)
6469

6570

6671
if __name__ == "__main__":

0 commit comments

Comments
 (0)