Get Started

A quick tutorial to learn the basics.

Step 1

Install

Download the code locally

# Install libs from the test Python Package Index (pypi) repo
%pip install --index-url https://test.pypi.org/simple/ --no-deps nhspy-plotthedots-test

# Import plotthedots to your environment
from nhspy_plotthedots import pandas_spc_calculations
from nhspy_plotthedots import plotly_spc_chart

Step 2

Process Data

Load time-series data into pandas

import pandas as pd
from datetime import datetime

# url path of the CSV file
url = 'https://raw.githubusercontent.com/nhs-pycom/nhspy-plotthedots/main/nhspy_plotthedots/data/ae_attendances.csv'

# Read the CSV file and store it in a pandas DataFrame
df = pd.read_csv(url)

# Convert 'period' column to datetime format
df['period'] = pd.to_datetime(df['period'])

# Create a subset of the DataFrame based on certain conditions
sub_set = df[(df['org_code'] == "RQM") & (df['type'] == "1") & (df['period'] < datetime(2018, 4, 1))]

# It's important to sort values by date for time-series and reset index for spc calculations
sub_set = sub_set.sort_values(by='period').reset_index(drop=True)
sub_set.head()
period org_code type attendances breaches admissions
0 2016-04-01 RQM 1 15154 1199 3415
1 2016-05-01 RQM 1 16705 929 3590
2 2016-06-01 RQM 1 16021 970 3398
3 2016-07-01 RQM 1 16761 1178 3321
4 2016-08-01 RQM 1 15084 1110 3198

Step 3

Calculation

Calculate the control limits

# calculate control limits
spc = pandas_spc_calculations.pandas_spc_x_calc(sub_set, 'breaches')
spc.head()
period org_code type attendances breaches admissions mean lpl upl outside_limits relative_to_mean close_to_limits special_cause_flag
0 2016-04-01 RQM 1 15154 1199 3415 1545.166667 740.395758 2349.937576 False -1.0 False False
1 2016-05-01 RQM 1 16705 929 3590 1545.166667 740.395758 2349.937576 False -1.0 True True
2 2016-06-01 RQM 1 16021 970 3398 1545.166667 740.395758 2349.937576 False -1.0 True True
3 2016-07-01 RQM 1 16761 1178 3321 1545.166667 740.395758 2349.937576 False -1.0 False False
4 2016-08-01 RQM 1 15084 1110 3198 1545.166667 740.395758 2349.937576 False -1.0 False False

Step 4

Visualisation

Plot the statistical process control chart

import plotly.io as pio
pio.renderers.default = "plotly_mimetype+notebook"

# plot SPC chart
plotly_spc_chart.plotly_spc_chart(spc, 'breaches', 'period', plot_title = 'Chelsea & Westminster Hospital NHS FT', x_lab = 'Month of attendance', y_lab = 'Number of 4-Hour Target Breaches')

Figure 1: Number of A&E attendance 4-Hour Breaches