// A model fitted by hand: the concentration of a drug in the blood after a single dose, measured every hour, and a model of // first-order elimination, c(t) = a * exp(-k * t) + b, drawn over it. Three sliders set the amount above the floor, the rate of // elimination and the floor; the curve, the residuals and the half-life follow. The measurements are a table of the host, so the // same figure can be fitted to other rows. Everything is drawn from the Parameters: the plot area, the grid, the curve in 120 // pieces, a residual for every measurement. sfl 1.12 Style tpl_canvas { fill: "#ffffff" } DataSource plasma { mode: table fields { hour: number level: number } } Parameters fit { a: 78 // 40..120 step 1, Amount above the floor k: 0.24 // 0.05..0.8 step 0.01, Rate of elimination b: 6 // 0..20 step 0.5, Floor originX: 64 originY: 36 plotW: 680 plotH: 330 tMax: 12 cMax: 110 half: 0.693 / k } Style tick { fontSize: 10.5 textColor: "#6f6a86" } Style gridLine { stroke: "#eceaf2" strokeWidth: 1 fill: none } Model elimination { // The grid: a line every 10 mg/L and every hour, with their numbers. Repeat levels { count: 12 Path levelLine : gridLine { points: [[originX, originY + plotH - index * 10 / cMax * plotH], [originX + plotW, originY + plotH - index * 10 / cMax * plotH]] } Text levelNumber : tick { text: "" + index * 10 layout { position: originX - 40, originY + plotH - index * 10 / cMax * plotH - 7 size: 32, 14 } style { textAlign: right } } } Repeat hours { count: 13 Path hourLine : gridLine { points: [[originX + index / tMax * plotW, originY], [originX + index / tMax * plotW, originY + plotH]] } Text hourNumber : tick { text: "" + index layout { position: originX + index / tMax * plotW - 16, originY + plotH + 8 size: 32, 14 } style { textAlign: center } } } Path axisBase { points: [[originX, originY + plotH], [originX + plotW, originY + plotH]] style { stroke: "#4b4760" strokeWidth: 1.2 fill: none } } Text axisX : tick { text: "Hours after the dose" layout { position: originX + plotW / 2 - 120, originY + plotH + 30 size: 240, 14 } style { textAlign: center } } Text axisY : tick { text: "Concentration, mg / L" layout { position: originX - 40, originY - 26 size: 220, 14 } style { textAlign: left } } // The residuals: from every measurement to the model at its hour. Repeat residuals { each: p in plasma m: a * exp(-k * p.hour) + b Path residual { points: [[originX + p.hour / tMax * plotW, originY + plotH - p.level / cMax * plotH], [originX + p.hour / tMax * plotW, originY + plotH - m / cMax * plotH]] style { stroke: "#9d8fd6" strokeWidth: 1.4 fill: none dash: "[3,3]" } } } // The model, in 120 short pieces. Repeat curve { count: 120 t0: index * tMax / 120 t1: (index + 1) * tMax / 120 c0: a * exp(-k * t0) + b c1: a * exp(-k * t1) + b Path piece { points: [[originX + t0 / tMax * plotW, originY + plotH - min(c0, cMax) / cMax * plotH], [originX + t1 / tMax * plotW, originY + plotH - min(c1, cMax) / cMax * plotH]] style { stroke: "#c2338f" strokeWidth: 3 fill: none } } } // The half-life, where the part above the floor has fallen to half. Path halfLine { points: [[originX + min(half, tMax) / tMax * plotW, originY + plotH], [originX + min(half, tMax) / tMax * plotW, originY + plotH - (a / 2 + b) / cMax * plotH]] style { stroke: "#6a3fb5" strokeWidth: 1.2 fill: none dash: "[6,4]" } } Text halfNote { text: "half-life " + round(half * 10) / 10 + " h" layout { position: originX + min(half, tMax) / tMax * plotW + 6, originY + plotH - (a / 2 + b) / cMax * plotH - 18 size: 140, 14 } style { fontSize: 11 fontWeight: 600 textColor: "#6a3fb5" textAlign: left } } // The measurements. Repeat points { each: p in plasma Shape point { layout { position: originX + p.hour / tMax * plotW - 4.5, originY + plotH - p.level / cMax * plotH - 4.5 size: 9, 9 } style { shape: circle fill: "#ffffff" stroke: "#17142b" strokeWidth: 1.8 } } } // The legend. Shape legendPoint { layout { position: originX + plotW - 214, originY + 6 size: 9, 9 } style { shape: circle fill: "#ffffff" stroke: "#17142b" strokeWidth: 1.8 } } Text legendMeasured : tick { text: "Measured" layout { position: originX + plotW - 198, originY + 3 size: 80, 14 } style { textAlign: left textColor: "#17142b" } } Path legendCurve { points: [[originX + plotW - 112, originY + 10.5], [originX + plotW - 88, originY + 10.5]] style { stroke: "#c2338f" strokeWidth: 3 fill: none } } Text legendModel : tick { text: "Model" layout { position: originX + plotW - 80, originY + 3 size: 80, 14 } style { textAlign: left textColor: "#17142b" } } Text equation { text: "c(t) = " + a + " · exp(−" + k + " · t) + " + b layout { position: originX, originY + plotH + 58 size: plotW, 20 } style { fontSize: 14 fontWeight: 600 textColor: "#17142b" textAlign: left } } // The sliders, which write the Parameters. Text aLabel { text: <- "Amount above the floor " + fit.a + " mg / L" layout { position: originX, originY + plotH + 96 size: 230, 16 } style { fontSize: 11 textColor: "#17142b" textAlign: left } } Component aControl { type: slider value: <-> fit.a min: 40 max: 120 layout { position: originX, originY + plotH + 114 size: 210, 32 } style { accent: "#c2338f" track: "#e4e1ee" step: 1 } } Text kLabel { text: <- "Rate of elimination " + round(fit.k * 100) + " % per hour" layout { position: originX + 235, originY + plotH + 96 size: 230, 16 } style { fontSize: 11 textColor: "#17142b" textAlign: left } } Component kControl { type: slider value: <-> fit.k min: 0.05 max: 0.8 layout { position: originX + 235, originY + plotH + 114 size: 210, 32 } style { accent: "#6a3fb5" track: "#e4e1ee" step: 0.01 } } Text bLabel { text: <- "Floor " + fit.b + " mg / L" layout { position: originX + 470, originY + plotH + 96 size: 230, 16 } style { fontSize: 11 textColor: "#17142b" textAlign: left } } Component bControl { type: slider value: <-> fit.b min: 0 max: 20 layout { position: originX + 470, originY + plotH + 114 size: 210, 32 } style { accent: "#17142b" track: "#e4e1ee" step: 0.5 } } }