# 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
Get Started
A quick tutorial to learn the basics.
Step 1
Install
Download the code locally
Step 2
Process Data
Load time-series data into pandas
import pandas as pd
from datetime import datetime
# url path of the CSV file
= 'https://raw.githubusercontent.com/nhs-pycom/nhspy-plotthedots/main/nhspy_plotthedots/data/ae_attendances.csv'
url
# Read the CSV file and store it in a pandas DataFrame
= pd.read_csv(url)
df
# Convert 'period' column to datetime format
'period'] = pd.to_datetime(df['period'])
df[
# Create a subset of the DataFrame based on certain conditions
= df[(df['org_code'] == "RQM") & (df['type'] == "1") & (df['period'] < datetime(2018, 4, 1))]
sub_set
# It's important to sort values by date for time-series and reset index for spc calculations
= sub_set.sort_values(by='period').reset_index(drop=True)
sub_set 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
= pandas_spc_calculations.pandas_spc_x_calc(sub_set, 'breaches')
spc 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
= "plotly_mimetype+notebook"
pio.renderers.default
# plot SPC chart
'breaches', 'period', plot_title = 'Chelsea & Westminster Hospital NHS FT', x_lab = 'Month of attendance', y_lab = 'Number of 4-Hour Target Breaches') plotly_spc_chart.plotly_spc_chart(spc,