labqc.control_charts¶
Internal quality control: Shewhart limits and Westgard multirules.
Rules are evaluated on z = (x - target) / sd for one control material:
1_2s warning one result beyond 2 SD 1_3s reject one result beyond 3 SD (random error) 2_2s reject two consecutive results beyond 2 SD on the same side (systematic) R_4s reject consecutive results differ by more than 4 SD, one above +2 SD and one below -2 SD (random error) 4_1s reject four consecutive results beyond 1 SD on the same side (systematic) 10_x reject ten consecutive results on the same side of the target (systematic)
limits_from_data ¶
limits_from_data(values: Sequence[float], method: str = 'sd') -> Limits
Estimate the centre line and SD from a baseline run.
method "sd" uses the sample standard deviation; "moving_range" uses the average moving range / 1.128, which is less inflated by drifts.
Source code in src/labqc/control_charts.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
westgard ¶
westgard(values: Sequence[float], limits: Limits) -> pd.DataFrame
Flag each result with the Westgard rules it violates.
Returns a table with the value, its z-score, one boolean column per rule, and a status: "accept", "warning" (1_2s only) or "reject".
Source code in src/labqc/control_charts.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
westgard_levels ¶
westgard_levels(values: Mapping[str, Sequence[float]], limits: Mapping[str, Limits]) -> pd.DataFrame
Westgard multirules across two or three control materials run together.
values maps each control level (e.g. "low", "high") to its results,
one per run; limits gives each level its own centre and SD. Rules use
z-scores, so levels with different concentrations can be compared.
Two levels: 1_3s, 2_2s (both levels in one run, or one level in two runs), R_4s (one level above +2 SD and the other below -2 SD in one run), 4_1s (two runs x two levels, or one level over four runs), 10_x (five runs x two levels, or one level over ten runs).
Three levels: 1_3s, 2of3_2s (two of the three in one run beyond 2 SD on the same side), R_4s, 3_1s (all three in one run, or one level over three runs), 12_x (four runs x three levels, or one level over twelve runs).
Returns one row per run: z per level, one boolean column per rule and a
status ("accept", "warning" for 1_2s only, or "reject"), like westgard.
Source code in src/labqc/control_charts.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
cusum ¶
cusum(values: Sequence[float], target: float, sd: float, k: float = 0.5, h: float = 5.0) -> pd.DataFrame
Tabular CUSUM in SD units (Montgomery, Introduction to Statistical Quality Control).
C+i = max(0, z_i - k + C+{i-1}) and C-i = max(0, -z_i - k + C-{i-1}), with z = (x - target) / sd. A signal when either exceeds h. k = 0.5 and h = 5 detect a 1 SD shift quickly; smaller shifts than Westgard catches.
Columns: value, z, c_plus, c_minus, signal_high, signal_low, signal.
Source code in src/labqc/control_charts.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
ewma ¶
ewma(values: Sequence[float], target: float, sd: float, lam: float = 0.2, L: float = 3.0) -> pd.DataFrame
EWMA chart (Montgomery, Introduction to Statistical Quality Control).
ewma_i = lam * x_i + (1 - lam) * ewma_{i-1}, starting at the target, with exact limits target +/- L * sd * sqrt(lam / (2 - lam) * (1 - (1 - lam)^(2i))).
Columns: value, ewma, lcl, ucl, signal.
Source code in src/labqc/control_charts.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
qc_overview ¶
qc_overview(data: DataFrame, baseline: int = 20, method: str = 'sd', date_col: str = 'date', value_col: str = 'value') -> pd.DataFrame
Westgard status of the latest run for every method and control level in one table.
data has one row per control result with columns method, level, date and value,
and optionally target and sd. A series with a target and sd is checked against them
from its first result; otherwise its first baseline results set the limits
(limits_from_data with method) and the rest are checked. Rules are applied to each
series on its own (see westgard_levels for rules across levels).
Returns one row per series, in order of first appearance: method, level, n, limits_from, baseline_n, centre, sd, last_date, last_value, last_z, status (of the latest run: accept, warning or reject), rules (broken at the latest run), and the number of warnings and rejects among the checked runs.
Source code in src/labqc/control_charts.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |