예제 #1
0
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)
예제 #2
0
    def get(self):
        """
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            GET
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        """

        # Info
        Logger.debug("Reading recent data...")

        # Define dates
        today = self.now.date()
        yesterday = today - datetime.timedelta(days=1)
        then = self.now - datetime.timedelta(hours=24)

        # Build net insulin profile for last 24 hours
        _net = net.Net()
        _net.build(then, self.now, False)

        # Format and store its data
        self.data["net"] = dict(
            zip([lib.formatTime(T) for T in _net.T],
                [round(y, 2) for y in _net.y]))

        # Get pump data
        self.data["pump"] = reporter.getPumpReport().get()

        # Get recent BGs
        self.data["bgs"] = reporter.getDatedEntries(reporter.BGReport,
                                                    [yesterday, today], [])

        # Get recent boluses
        self.data["boluses"] = reporter.getDatedEntries(
            reporter.TreatmentsReport, [yesterday, today], ["Boluses"])

        # Get recent IOBs
        self.data["iobs"] = reporter.getDatedEntries(reporter.TreatmentsReport,
                                                     [yesterday, today],
                                                     ["IOB"])

        # Get recent history
        self.data["history"] = reporter.getDatedEntries(
            reporter.HistoryReport, [yesterday, today], [])

        # Get recent sensor statuses (last session)
        # With n = 1, only today's history report would be considered, thus + 1
        self.data["statuses"] = reporter.getRecentDatedEntries(
            reporter.HistoryReport, self.now, ["CGM", "Sensor Statuses"],
            MAX_SENSOR_AGE + 1)

        # Get recent calibrations
        self.data["calibrations"] = reporter.getDatedEntries(
            reporter.HistoryReport, [yesterday, today],
            ["CGM", "Calibrations"])

        # Get recent errors
        self.data["errors"] = reporter.getDatedEntries(reporter.ErrorsReport,
                                                       [today], [])
예제 #3
0
    def buildProfiles(self, now, dt=5.0 / 60.0):
        """
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            BUILDPROFILES
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            dt: step size
        """

        # Get DIA
        DIA = reporter.getPumpReport().get(["Settings", "DIA"])

        # Define PIA (peak of insulin action)
        PIA = 1.25

        # Define past/future reference times
        past = now - datetime.timedelta(hours=DIA)
        future = now + datetime.timedelta(hours=DIA)

        # Instanciate profiles
        self.profiles = {
            "IDC": idc.ExponentialIDC(DIA, PIA),
            "Basal": basal.Basal(),
            "Net": net.Net(),
            "BGTargets": targets.BGTargets(),
            "FutureISF": isf.FutureISF(),
            "FutureCSF": csf.FutureCSF(),
            "PastIOB": iob.PastIOB(),
            "FutureIOB": iob.FutureIOB(),
            "PastBG": bg.PastBG(),
            "FutureBG": bg.FutureBG()
        }

        # Build net insulin profile
        self.profiles["Net"].build(past, now)

        # Build past profiles
        self.profiles["Basal"].build(past, now)
        self.profiles["PastIOB"].build(past, now)
        self.profiles["PastBG"].build(past, now)

        # Build daily profiles
        self.profiles["BGTargets"].build(now, future)
        self.profiles["FutureISF"].build(now, future)
        #self.profiles["FutureCSF"].build(now, future)

        # Build future profiles
        self.profiles["FutureIOB"].build(self.profiles["Net"],
                                         self.profiles["IDC"], dt)
        self.profiles["FutureBG"].build(self.profiles["PastBG"],
                                        self.profiles["Net"],
                                        self.profiles["IDC"],
                                        self.profiles["FutureISF"], dt)
예제 #4
0
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)
예제 #5
0
def compareExpectedVsObservedBGDeltas(now, t, IDC):
    """
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        COMPAREEXPECTEDVSOBSERVEDBGDELTAS
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        ...
    """

    # Define reference times
    past = now - datetime.timedelta(hours=t)

    # Instanciate profiles
    BGs = bg.PastBG()
    ISFs = isf.PastISF()
    Net = net.Net()

    # Build them
    BGs.build(past, now)
    ISFs.build(past, now)
    Net.build(past - datetime.timedelta(hours=IDC.DIA), now)

    # Compute expected and observed BGs
    expectedBGDeltas = computeExpectedBGDeltas(BGs.t, BGs.T, Net, IDC, ISFs)
    observedBGDeltas = computeObservedBGDeltas(BGs.y)

    # Compute difference between expectations and observations
    ddBGs = np.array(observedBGDeltas) - np.array(expectedBGDeltas)
    print "AVG ddBG: " + fmt.BG(np.mean(ddBGs))
    print "STD ddBG: " + fmt.BG(np.std(ddBGs))

    # Compute IOBs
    IOBs = computeIOBs(BGs.t, BGs.T, Net, IDC)

    # Plot results
    plot(BGs.t[:-1], expectedBGDeltas, observedBGDeltas, ddBGs, BGs.y[:-1],
         IOBs[:-1])