# nhsdigital/mental-health-monthly-statistics architecture

> Architecture diagram of the GitHub repository nhsdigital/mental-health-monthly-statistics, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nhsdigital/mental-health-monthly-statistics
- Repository: https://github.com/nhsdigital/mental-health-monthly-statistics

## Overview

This repository produces NHS England’s Mental Health Services Monthly Statistics from MHSDS data through several distinct publication pipelines. The main documented workflow prepares and aggregates measures, with additional pipelines for newer measures, publication-specific metrics, and data-quality outputs. The tree also includes restraint statistics and inpatient productivity outputs. The README establishes the pipelines’ purpose and contribution, but most sampled files do not name other modules; consequently, internal call relationships are not inferred. Some subsystem stages are represented by their exact directories because their individual files were not sampled.

Main entry points: [`Mental_Health_Monthly_Statistics/menh_analysis`](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_analysis) (Main measures), [`Mental_Health_Monthly_Statistics/menh_publications`](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_publications) (Publication measures), [`Mental_Health_Monthly_Statistics/menh_bbrb`](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_bbrb) (Newer measures), [`Mental_Health_Monthly_Statistics/menh_dq`](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_dq) (DQ outputs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_analysis["Main analysis"]
  node_analysis_pipeline["Main measures"]
  node_analysis_prep["Measure preparation"]
  node_analysis_lookup["Lookup data"]
  node_analysis_aggregate["Measure aggregation"]
  node_analysis_lda["LDA measures<br/>[6.LDA.py]"]
  node_analysis_extract["LDA extraction"]
end

subgraph group_publications["Publication measures"]
  node_pub_pipeline["Publication measures"]
  node_pub_ed_waits["CYP ED waits"]
  node_pub_eip["EIP measures"]
end

subgraph group_bbrb["Newer measures"]
  node_bbrb_pipeline["Newer measures"]
  node_bbrb_reference["MHSDS references"]
  node_bbrb_waits["Four-week waits<br/>[4w_waits_prep.py]"]
  node_bbrb_oaps["OAPS methodology<br/>[oaps_prep.py]"]
  node_bbrb_mha["MHA preparation<br/>[mha_prep.py]"]
  node_bbrb_metadata["Measure definitions"]
end

subgraph group_dq["Data quality"]
  node_dq_pipeline["DQ outputs"]
  node_dq_coverage["Coverage extraction"]
  node_dq_validity["Validity aggregation<br/>[validity_CDQA.py]"]
end

subgraph group_restraints["Restraints"]
  node_restraints_pipeline["Restraint statistics"]
  node_restraints_dashboard["Dashboard CSV build"]
  node_restraints_published["Published metrics"]
  node_restraints_suppression["Output suppression"]
end

subgraph group_productivity["Productivity"]
  node_productivity["Inpatient productivity"]
  node_productivity_outputs["CWA outputs"]
end

node_analyst(("Statistics producers"))
node_mhsds[("MHSDS data")]

node_analyst -->|"runs"| node_analysis_pipeline
node_analyst -->|"runs"| node_pub_pipeline
node_analyst -->|"runs"| node_bbrb_pipeline
node_analyst -->|"runs"| node_dq_pipeline
node_mhsds -->|"provides data"| node_analysis_pipeline
node_mhsds -->|"provides data"| node_pub_pipeline
node_mhsds -->|"provides data"| node_bbrb_pipeline
node_mhsds -->|"provides data"| node_dq_pipeline
node_analysis_pipeline -->|"contributes statistics"| node_analyst
node_pub_pipeline -->|"contributes statistics"| node_analyst
node_bbrb_pipeline -->|"contributes statistics"| node_analyst
node_dq_pipeline -->|"provides DQ files"| node_analyst

click node_analysis_pipeline "https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_analysis"
click node_analysis_prep "https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_analysis/notebooks/01_Prepare"
click node_analysis_lookup "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_analysis/notebooks/01_Prepare/0.Insert_lookup_data.py"
click node_analysis_aggregate "https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_analysis/notebooks/02_Aggregate"
click node_analysis_lda "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_analysis/notebooks/02_Aggregate/6.LDA.py"
click node_analysis_extract "https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_analysis/notebooks/03_Extract"
click node_pub_pipeline "https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_publications"
click node_pub_ed_waits "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_publications/notebooks/04a_CYP_ED_WaitingTimes/02_Aggregate/01_CYP_ED_WT_Aggregate.py"
click node_pub_eip "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_publications/notebooks/07_EIP/02_Aggregate/01_EIP_Aggregate_non_ethnicity.py"
click node_bbrb_pipeline "https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_bbrb"
click node_bbrb_reference "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/01_Generic_Prepare/04_mhsds_ref_tables.py"
click node_bbrb_waits "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/02_Specific_Prepare/02_4w_waits/4w_waits_prep.py"
click node_bbrb_oaps "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/02_Specific_Prepare/08_oaps/oaps_prep.py"
click node_bbrb_mha "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/02_Specific_Prepare/10_mha/mha_prep.py"
click node_bbrb_metadata "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/measure_metadata.py"
click node_dq_pipeline "https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_dq"
click node_dq_coverage "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_dq/notebooks/3.extract/3.3.coverage_csvs.py"
click node_dq_validity "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_dq/notebooks/2.aggregate/validity_CDQA.py"
click node_restraints_pipeline "https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/Restraints"
click node_restraints_dashboard "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/Restraints/Restraints_Dashboard_CSV_Build_v4.py"
click node_restraints_published "https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/Restraints/Restraints_Published_CSV_Build"
click node_restraints_suppression "https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/Restraints/Restraints_Suppression"
click node_productivity "https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/mental_health_productivity/MH%20Productivity%20Inpatients%20CWA"
click node_productivity_outputs "https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/mental_health_productivity/MH%20Productivity%20Inpatients%20CWA/AllOutputsV6_CWA.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_analysis_pipeline,node_analysis_prep,node_analysis_lookup,node_analysis_aggregate,node_analysis_lda,node_analysis_extract toneBlue
class node_pub_pipeline,node_pub_ed_waits,node_pub_eip,node_mhsds toneAmber
class node_bbrb_pipeline,node_bbrb_reference,node_bbrb_waits,node_bbrb_oaps,node_bbrb_mha,node_bbrb_metadata toneMint
class node_dq_pipeline,node_dq_coverage,node_dq_validity toneRose
class node_restraints_pipeline,node_restraints_dashboard,node_restraints_published,node_restraints_suppression,node_analyst toneIndigo
class node_productivity,node_productivity_outputs toneTeal
```

## Components

### Main analysis

- [Main measures](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_analysis): `Mental_Health_Monthly_Statistics/menh_analysis`
- [Measure preparation](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_analysis/notebooks/01_Prepare): `Mental_Health_Monthly_Statistics/menh_analysis/notebooks/01_Prepare`
- [Lookup data](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_analysis/notebooks/01_Prepare/0.Insert_lookup_data.py): `Mental_Health_Monthly_Statistics/menh_analysis/notebooks/01_Prepare/0.Insert_lookup_data.py`
- [Measure aggregation](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_analysis/notebooks/02_Aggregate): `Mental_Health_Monthly_Statistics/menh_analysis/notebooks/02_Aggregate`
- [LDA measures](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_analysis/notebooks/02_Aggregate/6.LDA.py): `Mental_Health_Monthly_Statistics/menh_analysis/notebooks/02_Aggregate/6.LDA.py`
- [LDA extraction](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_analysis/notebooks/03_Extract): `Mental_Health_Monthly_Statistics/menh_analysis/notebooks/03_Extract`

### Publication measures

- [Publication measures](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_publications): `Mental_Health_Monthly_Statistics/menh_publications`
- [CYP ED waits](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_publications/notebooks/04a_CYP_ED_WaitingTimes/02_Aggregate/01_CYP_ED_WT_Aggregate.py): `Mental_Health_Monthly_Statistics/menh_publications/notebooks/04a_CYP_ED_WaitingTimes/02_Aggregate/01_CYP_ED_WT_Aggregate.py`
- [EIP measures](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_publications/notebooks/07_EIP/02_Aggregate/01_EIP_Aggregate_non_ethnicity.py): `Mental_Health_Monthly_Statistics/menh_publications/notebooks/07_EIP/02_Aggregate/01_EIP_Aggregate_non_ethnicity.py`

### Newer measures

- [Newer measures](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_bbrb): `Mental_Health_Monthly_Statistics/menh_bbrb`
- [MHSDS references](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/01_Generic_Prepare/04_mhsds_ref_tables.py): `Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/01_Generic_Prepare/04_mhsds_ref_tables.py`
- [Four-week waits](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/02_Specific_Prepare/02_4w_waits/4w_waits_prep.py): `Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/02_Specific_Prepare/02_4w_waits/4w_waits_prep.py`
- [OAPS methodology](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/02_Specific_Prepare/08_oaps/oaps_prep.py): `Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/02_Specific_Prepare/08_oaps/oaps_prep.py`
- [MHA preparation](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/02_Specific_Prepare/10_mha/mha_prep.py): `Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/02_Specific_Prepare/10_mha/mha_prep.py`
- [Measure definitions](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/measure_metadata.py): `Mental_Health_Monthly_Statistics/menh_bbrb/notebooks/measure_metadata.py`

### Data quality

- [DQ outputs](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/menh_dq): `Mental_Health_Monthly_Statistics/menh_dq`
- [Coverage extraction](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_dq/notebooks/3.extract/3.3.coverage_csvs.py): `Mental_Health_Monthly_Statistics/menh_dq/notebooks/3.extract/3.3.coverage_csvs.py`
- [Validity aggregation](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/menh_dq/notebooks/2.aggregate/validity_CDQA.py): `Mental_Health_Monthly_Statistics/menh_dq/notebooks/2.aggregate/validity_CDQA.py`

### Restraints

- [Restraint statistics](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/Restraints): `Mental_Health_Monthly_Statistics/Restraints`
- [Dashboard CSV build](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/Restraints/Restraints_Dashboard_CSV_Build_v4.py): `Mental_Health_Monthly_Statistics/Restraints/Restraints_Dashboard_CSV_Build_v4.py`
- [Published metrics](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/Restraints/Restraints_Published_CSV_Build): `Mental_Health_Monthly_Statistics/Restraints/Restraints_Published_CSV_Build`
- [Output suppression](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/Restraints/Restraints_Suppression): `Mental_Health_Monthly_Statistics/Restraints/Restraints_Suppression`

### Productivity

- [Inpatient productivity](https://github.com/nhsdigital/mental-health-monthly-statistics/tree/main/Mental_Health_Monthly_Statistics/mental_health_productivity/MH%20Productivity%20Inpatients%20CWA): `Mental_Health_Monthly_Statistics/mental_health_productivity/MH Productivity Inpatients CWA`
- [CWA outputs](https://github.com/nhsdigital/mental-health-monthly-statistics/blob/main/Mental_Health_Monthly_Statistics/mental_health_productivity/MH%20Productivity%20Inpatients%20CWA/AllOutputsV6_CWA.py): `Mental_Health_Monthly_Statistics/mental_health_productivity/MH Productivity Inpatients CWA/AllOutputsV6_CWA.py`

### Other components

- **Statistics producers**
- **MHSDS data**

## Connections

- Statistics producers → Main measures: runs
- Statistics producers → Publication measures: runs
- Statistics producers → Newer measures: runs
- Statistics producers → DQ outputs: runs
- MHSDS data → Main measures: provides data
- MHSDS data → Publication measures: provides data
- MHSDS data → Newer measures: provides data
- MHSDS data → DQ outputs: provides data
- Main measures → Statistics producers: contributes statistics
- Publication measures → Statistics producers: contributes statistics
- Newer measures → Statistics producers: contributes statistics
- DQ outputs → Statistics producers: provides DQ files
