Skip to content

Commit 97539e8

Browse files
authored
Merge pull request #56 from UCL-CORU/rewrite-notebooks
Rewrite notebooks
2 parents 3d4d2dc + d697829 commit 97539e8

21 files changed

Lines changed: 3560 additions & 1819 deletions

README.md

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,52 @@
3131

3232
## Summary
3333

34-
patientflow, a Python package, converts patient-level predictions into output that is useful for bed managers in hospitals.
34+
`patientflow`, a Python package, converts patient-level predictions into output that is useful for bed managers in hospitals.
3535

36-
We developed this code originally for University College London Hospitals (UCLH) NHS Trust to predict the number of emergency admissions within the next eight hours. The methods generalise to other aspects of patient flow in hospitals, including predictions of discharge numbers, within a group of patients. It can be applied to any problem where it is useful to convert patient-level predictions into outcomes for a whole cohort of patients at a point in time.
36+
We originally developed this code for University College London Hospitals (UCLH) to predict the number of emergency admissions they should expect within the next eight hours. Our method used real-time data from their Electronic Health Record (EHR) system. We wrote code to convert patient-level data, extracted from the EHR at a point in time, into predicted numbers of admissions in the following 4 or 8 hours. We also wrote code to help us evaluate the predictions.
3737

38-
If you have a predictive model of some outcome for a patient, like admission or discharge from hospital, you can use patientflow to create bed count distributions for a cohort of patients. We show how to prepare your data and train models for these kinds of problems. The repository includes a synthetic dataset and a series of notebooks demonstrating the use of the package.
38+
We have created the `patientflow` python package to make it convenient for others to adopt our approach. Its purpose is to predict bed demand for groups of hospital patients at a point in time. The package is organised around the following concepts:
3939

40-
## What patientflow is for:
40+
- Prediction time: A moment in the day at which predictions are to be made, for example 09:30.
41+
- Patient snapshot: A summary of data from the EHR capturing is known about a single patient at the prediction time. Each patient snapshot has a date and a prediction time associated with it.
42+
- Group snaphot: A set of patients snapshots. Each group snapshot has a date and a prediction time associated with it.
43+
- Prediction window: A period of hours that begins at the prediction time.
4144

42-
- Managing patient flow in hospitals: The package can be used to predict numbers of emergency admissions, discharges or transfers between units
45+
The modelling functions in `patientflow` are designed to receive a group snapshot as an input, and to predict something about that group's demand for beds between the prediction moment and the end of the prediction window. For example, that group could be the patients currently in the Emergency Department (ED), and the predictions could be the number of beds needed by those patients in the next 8 hours. The output is a probability distribution over the number of beds needed. The package includes functions to generate predictions at both patient and group level, to visualise predicted probability distributions, and to evaluate them.
46+
47+
This snapshot-based approach to predicting demand generalises to other aspects of patient flow in hospitals, such as predictions of how many patients from a clinical specialty will be discharged. A series of notebooks demonstrates the use of the package. We show how to prepare your data and train models based on a snapshot approach. The repository includes a synthetic dataset, and an anonymised patient dataset, based on real data from UCLH is available on [Zenodo](https://zenodo.org/records/14866057). Both the synthetic and the real dataset have been prepared in a snapshot structure.
48+
49+
## What `patientflow` is for:
50+
51+
- Predicting patient flow in hospitals: The package can be used by researchers or analysts who want to predict numbers of emergency admissions, discharges or transfers between units
4352
- Short-term operational planning: The predictions produced by this package are designed for bed managers who need to make decisions within an 4-16 hour timeframe.
4453
- Working with real-time data: The design assumes that data from an electronic health record (EHR) is available in real-time, or near to real-time
45-
- Point-in-time analysis: The packages works by taking "snapshots" of groups of patients at a particular moment, and making projections from those specific moments.
54+
- Point-in-time analysis: The package works by taking snapshots of groups of patients who are in the hospital at a particular moment, and making predictions about whether a non-clinical outcome like admission or discharge will occur with a short time horizon.
4655

47-
## What patientflow is NOT for:
56+
## What `patientflow` is NOT for:
4857

49-
- Long-term capacity planning: The package focuses on immediate operational needs (hours ahead), not strategic planning over weeks or months.
50-
- Making decisions about individual patients: The package is not designed for clinical decision-making about specific patients. It relies on data entered into the EHR by clinical staff looking after patients, but cannot and should not be use to influence their decision-making
51-
- General hospital analytics: It is specifically focused on short-term bed management, not broader hospital analytics like long-term demand and capacity planning.
52-
- Finished/historical patient analysis: While historical data might train underlying models, the package itself focuses on patients currently in the hospital or soon to arrive
53-
- Replacing human judgment: It augments the information available to bed managers, but isn't meant to automate bed management decisions completely.
58+
- Long-term capacity planning: The package focuses on immediate operational demand (hours ahead), not strategic planning over weeks or months.
59+
- Making decisions about individual patients: The package relies on data entered into the EHR by clinical staff looking after patients, but the patient-level predictions it generates should not be used to influence their decision-making
60+
- General hospital analytics: The package is designed for short-term bed management, not broader hospital analytics like long-term demand and capacity planning.
61+
- Predicting what happens _after_ a hospital visit: While historical data might train underlying models, the package itself focuses on patients currently in the hospital or soon to arrive
62+
- Replacing human judgment: The predictions are meant to augment the information available to bed managers, but not to automate bed management decisions.
5463

5564
## This package will help you if you want to:
5665

57-
- Convert individual patient predictions to cohort-level insights: Its core purpose is the creation of aggregate bed count distributions, because bed numbers are the currencly used by bed managers.
58-
- Make predictions for unfinished patient visits: It is designed for making predictions when outcome at the end of the visit are as yet unknown.
59-
- Develop your own predictive models of emergency demand: The package includes a fully worked example of how to convert data from A&E visits into the right structure, and use that data to train models that predict numbers of emergency beds.
66+
- Make predictions for unfinished patient visits: The package is designed for making predictions when outcomes at the end of the visit are as yet unknown, and evaluating those predictions against what actually happened.
67+
- Convert individual patient predictions to group-level insights: As bed numbers are the currency used by bed managers, the package generates bed count distributions; you may find this kind of output will help you interest hospital site and operations managers in your predictions.
68+
- Develop your own predictive models of emergency demand: The repository includes a fully worked example of how to convert historical data from Emergency Department visits into snapshots, and use the snapshots to train models that predict numbers of emergency beds.
6069

61-
## This package will not help you if:
70+
## This package will NOT help you if:
6271

63-
- You work with time series data: patientflow works with snapshots of a hospital visit summarising what is in the patient record up to that point in time
64-
- Your focus is on predicting clinical outcomes: the approach is designed
72+
- You work with time series data: `patientflow` works with snapshots of a hospital visit summarising what is in the patient record up to that point in time. It would need modification to accept time series data formats.
73+
- You want to predict clinical outcomes: the approach is designed for the management of hospital sites, not the management of patient care.
6574

66-
## Mathematical assumptions underlying the conversion from individual to cohort predictions:
75+
## Mathematical assumptions underlying the conversion from individual to group predictions:
6776

68-
- Independence of patient outcomes: The package assumes that individual patient outcomes are conditionally independent given the features used in prediction.
69-
- Symbolic probability generation: The conversion uses symbolic mathematics (via SymPy) to construct a probability generating function that represents the exact distribution of possible cohort outcomes.
70-
- Bernoulli outcome model: Each patient outcome is modeled as a Bernoulli trial with its own probability, and the package computes the exact probability distribution for the sum of these independent trials.
71-
- Coefficient extraction approach: The method works by expanding a symbolic expression and extracting coefficients corresponding to each possible cohort outcome count.
72-
- Optional weighted aggregation: When converting individual probabilities to cohort-level predictions, the package allows for weighted importance of individual predictions, modifying the contribution of each patient to the overall distribution in specific contexts (eg admissions to different specialties).
73-
- Discrete outcome space: The package assumes outcomes can be represented as discrete counts (e.g., number of admissions) rather than continuous values.
77+
- Independence of patient requirements: The package assumes that individual patient requirements (eg for admission) are conditionally independent.
78+
- Bernoulli outcome model: Each patient outcome is modeled as a Bernoulli trial with its own probability, and the package computes a probability distribution for the sum of these independent trials.
79+
- Different levels of aggregation: The package can calculate probability distributions for compound scenarios (such as the probability of a patient being admitted, assigned to a specific specialty if admitted, and being admitted within the prediction window) and for patient subgroups (like distributions by age or gender). In all cases, the independence assumption between patients is maintained.
7480

7581
## Getting started
7682

@@ -99,7 +105,7 @@ Navigate to the patientflow folder and run tests to confirm that the installatio
99105
pytest
100106
```
101107

102-
If you get errors running the pytest command, there may be other installations needed on your local machine. (We have found copying the error messages into ChatGPT or Claude very helpful for diagnosing and troubleshooting these errors.)
108+
If you get errors running the pytest command, there may be other installations needed on your local machine.
103109

104110
### Training models with data provided
105111

@@ -124,16 +130,22 @@ The data_folder_name specifies the name of the folder containing data. The funct
124130

125131
## About
126132

127-
This project was inspired by the [py-pi template](https://github.com/health-data-science-OR/pypi-template) developed by Tom Monks, and is based on a template developed by the
128-
[Centre for Advanced Research Computing](https://ucl.ac.uk/arc), University College London.
133+
This idea to create a Python package was inspired by , and
129134

130135
### Project Team
131136

132-
Dr Zella King, Clinical Operational Research Unit (CORU), University College London ([zella.king@ucl.ac.uk](mailto:zella.king@ucl.ac.uk))
133-
Jon Gillham, Institute of Health Informatics, UCL
134-
Professor Sonya Crowe, CORU
135-
Professor Martin Utley, CORU
137+
- [Dr Zella King](https://github.com/zmek), Clinical Operational Research Unit (CORU), University College London ([zella.king@ucl.ac.uk](mailto:zella.king@ucl.ac.uk))
138+
- [Jon Gillham](https://github.com/jongillham), Institute of Health Informatics, UCL
139+
- Professor Sonya Crowe, CORU
140+
- Professor Martin Utley, CORU
136141

137142
## Acknowledgements
138143

139-
This work was funded by a grant from the UCL Impact Funding. We are grateful to the Information Governance team and the Caldicott Guardian at UCLH for agreeing that we can release real patient data.
144+
The [py-pi template](https://github.com/health-data-science-OR/pypi-template) developed by [Tom Monks](https://github.com/TomMonks) inspired us to create a Python package. This repository is based on a template developed by the [Centre for Advanced Research Computing](https://ucl.ac.uk/arc), University College London. We are grateful to [Lawrence Lai](https://github.com/lawrencelai) for creation of the synthetic dataset. MAPS QR Policy Funding from by University College London contributed to the construction of the repository.
145+
146+
The underlying academic work was funded by grants from
147+
148+
- the Wellcome Institutional Strategic Support Fund (ISSF) UCL and Partner Hospitals: AI in Healthcare Funding Call 2019 (award number BRC717/HI/RW/101440),
149+
- the National Institute for Health Research UCLH Biomedical Research Centre HIGODS Theme (award number BRC824/HG/ZK/110420)
150+
- the National Institute for Health Research (Artificial Intelligence, Digitally adapted, hyper-local realtime bed forecasting to manage flow for NHS wards, AI_AWARD01786) and NHSX
151+
- University College London Hospitals NHS Trust (Zetetic Benefits-Enhancing Data Science)

notebooks/0_Background.ipynb

Whitespace-only changes.
File renamed without changes.

0 commit comments

Comments
 (0)