Skip to content

Instantly share code, notes, and snippets.

@SwadicalRag
Last active June 21, 2021 07:14
Show Gist options
  • Save SwadicalRag/9f34b5d52e642f358166bf79da43454a to your computer and use it in GitHub Desktop.
Save SwadicalRag/9f34b5d52e642f358166bf79da43454a to your computer and use it in GitHub Desktop.
SALHN Basal Bolus Insulin Protocol
-- for use by doctors only, always clinically correlate
-- don't use this for yourself
-- you shouldn't trust strangers on the internet with your blood sugars
print("insulin calculator 3000")
local function roundEven(n,noZero)
local frac = n % 1
local base = math.floor(n)
if base % 2 == 0 then
return base
elseif frac > 0.5 then
return base + 1
else
if noZero and base == 1 then return 2 end
return base - 1
end
end
local function promptNumber(prompt)
while true do
io.write(prompt,": ")
local input = io.read("*line")
if input == nil then os.exit() end
local out = tonumber(input)
if out ~= nil and out == out then return out end
print("incorrect number entered")
end
end
local function promptYesNo(prompt)
while true do
io.write(prompt," [y/N]: ")
local input = io.read("*line")
if input == nil then os.exit() end
input = input:lower()
if input == "y" then return true end
if input == "n" then return false end
end
end
local insulinUnitsTotal
local basalUnitsTotal,bolusUnitsTotal
local morningBolus,afternoonBolus,nightBolus
if promptYesNo("Already on insulin?") then
if promptYesNo("Is on IV insulin?") then
insulinUnitsTotal = promptNumber("Total insulin units in last 6 (SIX) hours")
insulinUnitsTotal = insulinUnitsTotal * 4
print("Splitting to 50/50 basal/bolus...")
basalUnitsTotal = roundEven(insulinUnitsTotal / 2)
bolusUnitsTotal = insulinUnitsTotal - basalUnitsTotal
print("Total basal: "..basalUnitsTotal.." units")
print("Total bolus: "..bolusUnitsTotal.." units")
elseif promptYesNo("Use charted insulin?") then
basalUnitsTotal = promptNumber("Total basal units")
morningBolus = promptNumber("Total morning bolus units")
afternoonBolus = promptNumber("Total afternoon bolus units")
nightBolus = promptNumber("Total night bolus units")
bolusUnitsTotal = morningBolus + afternoonBolus + nightBolus
insulinUnitsTotal = basalUnitsTotal + bolusUnitsTotal
else
insulinUnitsTotal = promptNumber("Total insulin units in last 24 (TWENTY FOUR) hours")
if promptYesNo("Is on oral hypoglycaemics?") then
print("Splitting to 50/50 + 10% basal/bolus...")
insulinUnitsTotal = insulinUnitsTotal * 1.1
else
print("Splitting to 50/50 basal/bolus...")
end
basalUnitsTotal = roundEven(insulinUnitsTotal / 2)
bolusUnitsTotal = insulinUnitsTotal - basalUnitsTotal
print("Total basal: "..basalUnitsTotal.." units")
print("Total bolus: "..bolusUnitsTotal.." units")
end
else
local bodyWeight = promptNumber("Body weight (in kg)")
if promptYesNo("Is on oral hypoglycaemics?") then
print("Using 0.4 units/kg...")
insulinUnitsTotal = bodyWeight * 0.4
else
print("Using 0.3 units/kg...")
insulinUnitsTotal = bodyWeight * 0.3
end
print("Splitting to 50/50 basal/bolus...")
basalUnitsTotal = roundEven(insulinUnitsTotal / 2)
bolusUnitsTotal = insulinUnitsTotal - basalUnitsTotal
print("Total basal: "..basalUnitsTotal.." units")
print("Total bolus: "..bolusUnitsTotal.." units")
end
if morningBolus == nil then
print("Splitting bolus into 3 parts...")
morningBolus = roundEven(bolusUnitsTotal / 3)
nightBolus = roundEven(bolusUnitsTotal / 3)
afternoonBolus = roundEven(bolusUnitsTotal - (morningBolus + nightBolus))
print("Morning Bolus: "..morningBolus.." units")
print("Afternoon Bolus: "..afternoonBolus.." units")
print("Night Bolus: "..nightBolus.." units")
end
local totalFastingBGL = 0
local numFastingBGL = 0
local totalMorningBGL = 0
local numMorningBGL = 0
local totalAfternoonBGL = 0
local numAfternoonBGL = 0
local totalNightBGL = 0
local numNightBGL = 0
local deltaBasal = 0
local deltaMorning = 0
local deltaAfternoon = 0
local deltaNight = 0
while true do
if promptYesNo("Add in a new measurement") then
local time = promptNumber("Measurement time (in 24 hour time)")
local bgl = promptNumber("Blood glucose level (BGL in mmol/L)")
if time <= 830 and time >= 200 then
if (bgl < 7) or promptYesNo("Was basal insulin given?") then
numFastingBGL = numFastingBGL + 1
totalFastingBGL = totalFastingBGL + bgl
end
else
if time <= 1400 then
if (bgl < 7) or promptYesNo("Was breakfast bolus insulin given?") then
numMorningBGL = numMorningBGL + 1
totalMorningBGL = totalMorningBGL + bgl
end
elseif time <= 1800 then
if (bgl < 7) or promptYesNo("Was lunch bolus insulin given?") then
numAfternoonBGL = numAfternoonBGL + 1
totalAfternoonBGL = totalAfternoonBGL + bgl
end
elseif (bgl < 7) or promptYesNo("Was dinner bolus insulin given?") then
numNightBGL = numNightBGL + 1
totalNightBGL = totalNightBGL + bgl
end
end
else
print("Calculating recommendations...")
local averageFastingBGL = totalFastingBGL / numFastingBGL
local averageMorningBGL = totalMorningBGL / numMorningBGL
local averageAfternoonBGL = totalAfternoonBGL / numAfternoonBGL
local averageNightBGL = totalNightBGL / numNightBGL
if numFastingBGL > 0 then
print("Average fasting BGL: "..averageFastingBGL.." mmol/L")
if averageFastingBGL > 7 then
print("High fasting glucose (> 7 mmol/L), correcting with 20%")
deltaBasal = roundEven(basalUnitsTotal * 0.2,true)
print("Increasing by +"..deltaBasal.." units")
elseif averageFastingBGL < 4 then
print("Low fasting glucose (< 4 mmol/L), correcting with 20%")
deltaBasal = -roundEven(basalUnitsTotal * 0.2,true)
print("Decreasing by "..deltaBasal.." units")
end
end
if numMorningBGL > 0 then
print("Average morning BGL: "..averageMorningBGL.." mmol/L")
if averageMorningBGL > 10 then
print("High post breakfast glucose (> 10 mmol/L), correcting with 20%")
deltaMorning = roundEven(morningBolus * 0.2,true)
print("Increasing by +"..deltaBasal.." units")
elseif averageMorningBGL < 4 then
print("Low post breakfast glucose (< 4 mmol/L), correcting with 20%")
print("Try checking if they actually ate their breakfast")
deltaMorning = -roundEven(morningBolus * 0.2,true)
print("Decreasing by "..deltaMorning.." units")
end
end
if numAfternoonBGL > 0 then
print("Average afternoon BGL: "..averageAfternoonBGL.." mmol/L")
if averageAfternoonBGL > 10 then
print("High post lunch glucose (> 10 mmol/L), correcting with 20%")
deltaAfternoon = roundEven(afternoonBolus * 0.2,true)
print("Increasing by +"..deltaAfternoon.." units")
elseif averageAfternoonBGL < 4 then
print("Low post lunch glucose (< 4 mmol/L), correcting with 20%")
print("Try checking if they actually ate their lunch")
deltaAfternoon = -roundEven(afternoonBolus * 0.2,true)
print("Decreasing by "..deltaAfternoon.." units")
end
end
if numNightBGL > 0 then
print("Average night BGL: "..averageNightBGL.." mmol/L")
if averageNightBGL > 10 then
print("High post dinner glucose (> 10 mmol/L), correcting with 20%")
deltaNight = roundEven(nightBolus * 0.2,true)
print("Increasing by +"..deltaNight.." units")
elseif averageNightBGL < 4 then
print("Low post dinner glucose (< 4 mmol/L), correcting with 20%")
print("Try checking if they actually ate their dinner")
deltaNight = -roundEven(nightBolus * 0.2,true)
print("Decreasing by "..deltaNight.." units")
end
end
print("New basal insulin: "..(basalUnitsTotal + deltaBasal).." units")
print("New morning bolus insulin: "..(morningBolus + deltaMorning).." units")
print("New afternoon bolus insulin: "..(afternoonBolus + deltaAfternoon).." units")
print("New night bolus insulin: "..(nightBolus + deltaNight).." units")
if promptYesNo("Accept changes") then
basalUnitsTotal = basalUnitsTotal + deltaBasal
morningBolus = morningBolus + deltaMorning
afternoonBolus = afternoonBolus + deltaAfternoon
nightBolus = nightBolus + deltaNight
bolusUnitsTotal = morningBolus + afternoonBolus + nightBolus
insulinUnitsTotal = basalUnitsTotal + bolusUnitsTotal
end
totalFastingBGL = 0
numFastingBGL = 0
totalMorningBGL = 0
numMorningBGL = 0
totalAfternoonBGL = 0
numAfternoonBGL = 0
totalNightBGL = 0
numNightBGL = 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment