Skip to content

Commit 60ebb67

Browse files
committed
refactor(package): restructure repository, importing simulation/ as a local package
1 parent ca5fdb8 commit 60ebb67

18 files changed

+269
-178
lines changed

README.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Table of contents:
3232

3333
## 📌  Introduction
3434

35+
<!--TODO: Add links to STARS, relevant publication, and https://github.com/pythonhealthdatascience/rap_des/ -->
36+
3537
This repository provides a template for building discrete-event simulation (DES) models in Python.
3638

3739
😊 **Simple:** Easy-to-follow code structure using [SimPy](https://simpy.readthedocs.io/en/latest/). Implements a simple M/M/s queueing model in which patients arrive, wait to see a nurse, have a consultation with the nurse and then leave. Follows a modular structure: uses object-oriented programming, with the simulation implemented through classes.
@@ -52,9 +54,9 @@ This repository provides a template for building discrete-event simulation (DES)
5254

5355
For clarity, changes from the DES book in this template are explained in `docs/hsma_changes.md`.
5456

55-
**Style and structure:** The coding style is based on the [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html). Linting is implemented using `flake8` and `pylint` for `.py` files, and `pycodestyle` for `.ipynb` files. While this repository is designed to be used as a template—helping you create a new repository with all relevant metadata (e.g., `README.md`, `CITATION.cff`, `LICENSE`) and scripts—it also adheres to a package-like structure. This is aligned with the NHS Gold RAP tier, and key indicators of this structure include `__init__.py` marking directories to be treated like packages, and `pyproject.toml` specifying package metadata.
57+
**Style:** The coding style is based on the [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html). Linting is implemented using `flake8` and `pylint` for `.py` files, and `pycodestyle` for `.ipynb` files.
5658

57-
<!--TODO: Add links to STARS, relevant publication, and https://github.com/pythonhealthdatascience/rap_des/ -->
59+
🧱 **Package structure:** In Python, a package can simply be defined as a directory with an `__init__.py` file in it. In this repository, the scripts for model (within `simulation/`) are treated as a little local package. This keeps the code model code isolated from our experiments and analysis. It is installed as an editable (`-e`) local import - with `-e` meaning it will update with changes to the local files in `simulation/`. As it is installed in our environment, it can then easily be used anywhere else in the directory - here, in `notebooks/` and `tests/` - without needing any additional code (e.g. no need to modify `sys.path`, or have additional `__init__.py` files).
5860

5961
<br>
6062

@@ -125,7 +127,7 @@ As an alternative, a `requirements.txt` file is provided which can be used to se
125127

126128
🔎 Choose your desired licence (e.g. <https://choosealicense.com/>). If keeping an MIT licence, just modify the copyright holder in `LICENSE`.
127129

128-
🔎 Review the example DES implementation in `scripts`. Modify and extend the code as needed for your specific use case.
130+
🔎 Review the example DES implementation in `simulation` and `notebooks`. Modify and extend the code as needed for your specific use case.
129131

130132
🔎 Check you still fulfil the criteria in `docs/nhs_rap.md` and `docs/heather_2025.md`.
131133

@@ -156,8 +158,8 @@ If you have changed the model behaviour, you may wish to amend, remove or write
156158
You can lint the `.py` files by running either of this commands from the terminal:
157159

158160
```
159-
flake8 scripts/simulation/model.py
160-
pylint scripts/simulation/model.py
161+
flake8 simulation/model.py
162+
pylint simulation/model.py
161163
```
162164

163165
The first commands in the `.ipynb` files will lint the notebooks using `pycodestyle` when executed:
@@ -201,15 +203,18 @@ This section describes the purposes of each class in the simulation.
201203
repo/
202204
├── .github/workflows/ # GitHub actions
203205
├── docs/ # Documentation
204-
├── scripts/ # Code for DES model and analysis
205-
├── tests/ # Unit tests
206+
├── inputs/ # Folder to store any input data
207+
├── notebooks/ # Run DES model and analyse results
208+
├── outputs/ # Folder to save any outputs from model
209+
├── simulation/ # Local package containing code for the DES model
210+
├── tests/ # Unit and back testing of the DES model
206211
├── .gitignore # Untracked files
207212
├── CHANGELOG.md # Describes changes between releases
208213
├── CITATION.cff # How to cite the repository
209214
├── CONTRIBUTING.md # Contribution instructions
210215
├── environment.yaml # Conda environment (includes Python version)
211216
├── LICENSE # Licence file
212-
├── pyproject.toml # Project metadata and build system configuration
217+
├── pyproject.toml # Metadata for local `simulation/` package
213218
├── README.md # This file! Describes the repository
214219
└── requirements.txt # Virtual environment (used by GitHub actions)
215220
```
@@ -218,7 +223,7 @@ repo/
218223

219224
## ⏰ Run time and machine specification
220225

221-
As this a template, it is not possible to provide an overall run time, as this will vary depending on how the template model is used. However, this information is provided from some example implementations, within the `.ipynb` files in `scripts`. These times were obtained on an Intel Core i7-12700H with 32GB RAM running Ubuntu 24.04.1 Linux.
226+
As this a template, it is not possible to provide an overall run time, as this will vary depending on how the template model is used. However, this information is provided from some example implementations, within the `.ipynb` files in `notebooks`. These times were obtained on an Intel Core i7-12700H with 32GB RAM running Ubuntu 24.04.1 Linux.
222227

223228
<br>
224229

@@ -312,8 +317,8 @@ Provide step-by-step instructions and examples.
312317

313318
Clearly indicate which files will create each figure in the paper. Hypothetical example:
314319

315-
* To generate **Figures 1 and 2**, execute `scripts/simulation/base_case.ipynb`
316-
* To generate **Table 1** and **Figures 3 to 5**, execute `scripts/simulation/scenario_analysis.ipynb`
320+
* To generate **Figures 1 and 2**, execute `notebooks/base_case.ipynb`
321+
* To generate **Table 1** and **Figures 3 to 5**, execute `notebooks/scenario_analysis.ipynb`
317322

318323
<br>
319324

docs/nhs_rap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Meeting all of the above requirements, plus:
4444

4545
| Criteria | Completion | Further details |
4646
| - | - | - |
47-
| Code is fully [packaged](https://packaging.python.org/en/latest/). || `__init__.py` (shows directory should be treated like package) and `pyproject.toml` (specifies package details). Based on src/package layout described by [PyOpenSci](https://www.pyopensci.org/python-package-guide/package-structure-code/python-package-structure.html) |
47+
| Code is fully [packaged](https://packaging.python.org/en/latest/). || With thanks to [Joshua Cook](https://joshuacook.netlify.app/posts/2024-07-27_python-data-analysis-org/), whose blog post I used to help me structure this analysis as a package. |
4848
| Repository automatically runs tests etc. via CI/CD or a different integration/deployment tool e.g. [GitHub Actions](https://docs.github.com/en/actions). || `.github/workflows/tests.yaml` |
4949
| Process runs based on event-based triggers (e.g., new data in database) or on a schedule. | N/A | - |
5050
| Changes to the RAP are clearly signposted. E.g. a changelog in the package, releases etc. (See gov.uk info on [Semantic Versioning](https://github.com/alphagov/govuk-frontend/blob/main/docs/contributing/versioning.md)). || `CHANGELOG.md` and GitHub releases |

environment.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ dependencies:
1414
- python=3.13.0
1515
- simpy=4.1.1
1616
- pip:
17-
- pycodestyle_magic==0.5
17+
- pycodestyle_magic==0.5
18+
- -e .[dev]

0 commit comments

Comments
 (0)