def test_walsh(): """ Specific tests for Walsh IDC. """ # Define DIAs DIAs = [3, 4, 5, 6] badDIAs = [1, 2, 2.5, 3.5, 4.5, 5.5, 6.5, 7, 8] # Define theoretical remaining active insulin after 1h for all valid DIAs remainingInsulin = [0.67282, 0.79694, 0.866427, 0.916375] # Test bad DIAs for dia in badDIAs: with pytest.raises(ValueError): idc.WalshIDC(dia) # Test fractions of active insulin remaining for dia, ri in dict(zip(DIAs, remainingInsulin)).items(): walsh = idc.WalshIDC(dia) # Test Walsh IDC isValid(walsh) # Test after one hour assert isEqual(walsh.f(-1), ri)
def test_compute_iob_single_step_net(): """ Test IOB computing for a net insulin profile with a single step. """ # Define a DIA DIA = 3.0 # Get an IDC walsh = idc.WalshIDC(DIA) # Create net insulin profile netInsulin = net.Net() netInsulin.t = np.array([-DIA, 0]) netInsulin.y = np.array([1, 1]) # Define integral over single step walshIntegrals = np.array([1.45532, 0]) expectedIOB = np.sum(netInsulin.y * walshIntegrals) IOB = calculator.computeIOB(netInsulin, walsh) assert isEqual(IOB, expectedIOB) # Redefine net insulin profile that corresponds to only scheduled basals # (there should be no insulin on board) netInsulin.t = np.array([-DIA, 0]) netInsulin.y = [0, 0] expectedIOB = 0 IOB = calculator.computeIOB(netInsulin, walsh) assert isEqual(IOB, expectedIOB)
def test_compute_iob_multiple_steps_net(): """ Test IOB computing for a net insulin profile with multiple steps. """ # Define a DIA DIA = 3.0 # Get an IDC walsh = idc.WalshIDC(DIA) # Create net insulin profile netInsulin = net.Net() netInsulin.t = np.array([-DIA, -2, -1, 0]) netInsulin.y = np.array([2, -0.5, 3, 1]) # Define part integrals (one for each step) walshIntegrals = np.array([0.129461, 0.444841, 0.881021, 0]) expectedIOB = np.sum(netInsulin.y * walshIntegrals) IOB = calculator.computeIOB(netInsulin, walsh) assert isEqual(IOB, expectedIOB)
def plotInsulinActivity(): """ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PLOTINSULINACTIVITY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ # Get current time now = datetime.datetime.now() # Read DIA DIA = reporter.getPumpReport().get(["Settings", "DIA"]) # Define timestep (h) dt = 5 / 60. # Compute number of steps n = int(DIA / dt) # Generate time axis for all IOBs t = np.linspace(DIA * 3600, 0, 500) T = np.linspace(dt, DIA, n) # Convert time axis to hours t /= 3600.0 # FIXME # Build profiles manually # Build net insulin profile walsh = idc.WalshIDC(DIA) iob = FutureIOB() bg = FutureBG() net = Net() # Initialize plot mpl.rc("font", size=11, family="Ubuntu") plt.figure(0, figsize=(10, 8)) plt.subplot(111) # Define plot title plt.title("Insulin Decay Over Time (DIA = " + str(DIA) + ")", weight="semibold") # Define plot axis plt.xlabel("Time (h)", weight="semibold") plt.ylabel("Insulin Activity (-)", weight="semibold") # Add Walsh IDC to plot plt.plot(-t, walsh.f(t), ls="-", lw=1.5, c="red", label="Walsh IDC") # Add insulin net profile to plot plt.step(-net.T, np.append(0, net.y[:-1]), ls="-", lw=1.5, c="black", label="Net Profile") # Add future IOBs to plot plt.plot(T, iob.y, ls="-", lw=1.5, c="purple", label="Future IOB") # Add eventual BGs to plot plt.plot(T, bg.y, ls="-", lw=1.5, c="blue", label="Eventual BG") # Define plot legend legend = plt.legend(title="Legend", loc=0, borderaxespad=1.5, numpoints=1, markerscale=2) # Style legend plt.setp(legend.get_title(), fontweight="semibold") # Tighten up plt.tight_layout() # Show plot plt.show()
def modelInsulinActivity(t, args, PIA, DIA, MID): """ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MODELINSULINACTIVITY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ # Define IDC of Animas x0 = 1.0104 x1 = -0.02203 x2 = -0.2479 x3 = 0.07493 x4 = -0.006623 tAnimas = t[0:(4.5 * len(t) / DIA)] IDCAnimas = (x0 + x1 * tAnimas + x2 * tAnimas**2 + x3 * tAnimas**3 + x4 * tAnimas**4) # Extract optimized parameters a = args[0] b = args[1] c = args[2] # Initialize plot mpl.rc("font", size=11, family="Ubuntu") fig = plt.figure(0, figsize=(10, 8)) sub = plt.subplot(111) # Define plot title plt.title("Insulin activity and decay over time for " + "PIA = " + str(PIA) + ", DIA = " + str(DIA) + ", and MID = " + str(MID), weight="semibold") # Define plot axis plt.xlabel("Time (h)", weight="semibold") plt.ylabel("Insulin Activity (-)", weight="semibold") # Add IAC and its integral to plot plt.plot(t, IAC(t=t, args=args), ls="-", lw=1.5, c="grey", label="IAC: " + r"$f(t) = a \cdot x^b \cdot e^{-c \cdot t}$, " + "with $[a, b, c]$ = [" + str(round(a, 1)) + ", " + str(round(b, 1)) + ", " + str(round(c, 1)) + "]") plt.plot(t, IDC(t=t, args=args), ls="-", lw=1.5, c="blue", label="IDC: " + r"$F(t) = \int$" + " " + r"$f(t) \cdot dt$") plt.plot(tAnimas, IDCAnimas, ls="-", lw=1.5, c="purple", label="Animas IDC") walshIDC = idc.WalshIDC(3) plt.plot(t, walshIDC.f(t=t), ls="-", lw=1.5, c="red", label="Walsh IDC") plt.plot(t, walshIDC.F(t=t), ls="-", lw=1.5, c="orange", label="Walsh IDC") # Define plot legend legend = plt.legend(title="Insulin activity and decay curves", loc=1, borderaxespad=1.5, numpoints=1, markerscale=2) plt.setp(legend.get_title(), fontweight="semibold") # Tighten up plt.tight_layout() # Show plot plt.show()