Exemple #1
0
def StandardModelSetup(dimension, npoints):
    settings = KickedWindkesselModel.KickedWindkesselModelSettings()

    fireNotifier = FiringTimesNotifier()
    force = RespiratoryDelayedSmearedHeartActionForce()
    force.CoordinateNumber = 9  #Coordinate number, to  which the state will be written
    force.CoordinateNumberForInput = 6
    force.KickAmplitude = 1.0  #Kick amplitude
    force.DelayTau = 0.3  #Time from firing order to actual kick
    force.SamplingTime = 0.01  # required to normalize delay time.
    force.DecayTau = 0.3  # Time by which the drive decays
    force.Notify = fireNotifier.Notify

    iafResp = IntegrateAndFire()
    iafResp.SamplingTime = 0.01
    iafResp.SetPhaseVelocityFromBPM(20)
    iafResp.SetInitialPhase(0.0)
    iafResp.KickAmplitude = 0.1
    iafResp.CoordinateNumberForRate = -1
    iafResp.CoordinateNumberForPhase = 5
    iafResp.CoordinateNumberForForceInput = -1  #not used
    # Coordinate number, to  which the state will be written
    #this variable will be used for coupling.
    iafResp.CoordinateNumberForOutput = 6
    fireNotifierResp = FiringTimesNotifier()
    iafResp.Notify = fireNotifierResp.Notify

    iafHeart = IntegrateAndFire()
    iafHeart.SamplingTime = 0.01
    iafHeart.SetPhaseVelocityFromBPM(66)  #45 worked OK
    iafHeart.phaseEfectivenessCurve = phaseEfectivenessCurveSH
    iafHeart.CoordinateNumberForRate = 8
    iafHeart.CoordinateNumberForPhase = 7
    iafHeart.CoordinateNumberForForceInput = force.CoordinateNumber
    # Coordinate number, to  which the state will be written
    iafHeart.CoordinateNumberForOutput = 4
    fireNotifierHeart = FiringTimesNotifier()
    #iafHeart.Notify = fireNotifierHeart.Notify

    collector = AbpmFiducialPointsCollector(0)  #ABPM = 0
    iafHeart.NotifyFunctions = [
        fireNotifierHeart.Notify, collector.HeartOpenNotifier
    ]
    mmNotifier = MinMaxNotifier(0)
    mmNotifier.MinNotifier = collector.AbpmMinNotifier
    mmNotifier.MaxNotifier = collector.AbpmMaxNotifier

    seriesNotifier = SeriesNotifier(dimension, npoints)

    settings.heartActionForce = HeartActionForceChain(
        [iafResp, force, iafHeart])
    settings.CoordinateNumberForRespPhase = iafResp.CoordinateNumberForPhase

    model = KickedWindkesselModel(settings, dimension)
    model.param.Npoints = npoints

    chain = NotifierChain((mmNotifier, collector, seriesNotifier))
    model.Notify = chain.Notify

    return settings, model, force, iafResp, iafHeart, collector, seriesNotifier, fireNotifierResp, fireNotifierHeart, fireNotifier
    def test_fiducial_points_collector_rect(self):
        settings = KickedWindkesselModel.KickedWindkesselModelSettings()
        settings.heartActionForce = RectangularHeartActionForce()
        model = KickedWindkesselModel(settings)
        model.param.Npoints = 10000

        collector = AbpmFiducialPointsCollector(
            0)  #ABPM = 0. Other compartments have different numbers.
        settings.heartActionForce.Notify = collector.HeartOpenNotifier
        mmNotifier = MinMaxNotifier(0)
        mmNotifier.MinNotifier = collector.AbpmMinNotifier
        mmNotifier.MaxNotifier = collector.AbpmMaxNotifier
        seriesNotifier = SeriesNotifier(model.param.dimension,
                                        model.param.Npoints)

        chain = NotifierChain((mmNotifier, collector, seriesNotifier))
        model.Notify = chain.Notify

        model.IterateToNotifiers()
        allValues = seriesNotifier.GetVar(0)
        allTimes = np.linspace(0.0, model.param.dT * model.param.Npoints,
                               model.param.Npoints)
        allItems = collector.GetFiducialPointsList()
        fig = plt.figure()
        #plt.ylim(-12.0,12.0)
        plt.plot(allTimes, allValues)
        #pdb.set_trace()
        plt.plot(allItems[:, 0], allItems[:, 1], "ro")  # systolic
        plt.plot(allItems[:, 0], allItems[:, 2], "go")  # diastolic
        plt.plot(allItems[:, 0], allItems[:, 3], "bo")  # mean
        plt.savefig("test_fiducial_points_collector_rect.png")
        logging.warning("Result in test_fiducial_points_collector_rect.png")
    def test_min_max_notifier(self):
        data = RungeKutta45IntegratorData(2, 0.0)
        mmNotifier = MinMaxNotifier(0)  #first variable
        npoints = 1000
        seriesNotifier = SeriesNotifier(2, npoints)
        notifier = NotifierChain((mmNotifier, seriesNotifier))
        period = 1.0
        allTimes = np.linspace(0.0, 10.0, npoints)
        for t in allTimes:
            data.t = t
            data.y[0] = 10.0 * np.cos(2 * np.pi * t / period)
            data.y[1] = 10.0 * np.sin(2 * np.pi * t / period)

            notifier.Notify(data)
        maxTime, maxValue, maxIterator = mmNotifier.getMaxima()
        minTime, minValue, minIterator = mmNotifier.getMinima()
        allValues = seriesNotifier.GetVar(0)
        fig = plt.figure()
        plt.ylim(-12.0, 12.0)
        plt.plot(allTimes, allValues)
        plt.plot(minTime, minValue, "bo")
        plt.plot(maxTime, maxValue, "r*")
        plt.show()
Exemple #4
0
def PhaseShiftProcessorOld():
    logging.basicConfig(level=logging.ERROR)
    stepShiftLinspace = np.linspace(0, 1.25, 10)
    stepShiftLinspace = np.linspace(0, 2.0 * np.pi, 100)
    Sav = []
    Ssd = []
    Dav = []
    Dsd = []
    Mav = []
    Msd = []

    for stepShift in stepShiftLinspace:
        settings = KickedWindkesselModel.KickedWindkesselModelSettings()
        settings.breathingPhi0 = stepShift
        settings.heartActionForce = RectangularHeartActionForce()
        settings.heartActionForce.StepShift = stepShift
        model = KickedWindkesselModel(settings)
        model.param.Npoints = 1000
        collector = AbpmFiducialPointsCollector(
            0)  #ABPM = 0. Other compartments have different numbers.
        settings.heartActionForce.Notify = collector.HeartOpenNotifier
        mmNotifier = MinMaxNotifier(0)
        mmNotifier.MinNotifier = collector.AbpmMinNotifier
        mmNotifier.MaxNotifier = collector.AbpmMaxNotifier
        seriesNotifier = SeriesNotifier(model.param.dimension,
                                        model.param.Npoints)

        chain = NotifierChain((mmNotifier, collector, seriesNotifier))
        model.Notify = chain.Notify

        model.IterateToNotifiers()

        allItems = collector.GetFiducialPointsList()
        averages = []
        standard_deviations = []

        for channel in [1, 2, 3]:  # 1-systloic 2-diastolic 3-mean
            averages.append(np.mean(allItems[:, channel]))
            standard_deviations.append(np.std(allItems[:, channel]))

        Sav.append(averages[0])
        Ssd.append(standard_deviations[0])
        Dav.append(averages[1])
        Dsd.append(standard_deviations[1])
        Mav.append(averages[2])
        Msd.append(standard_deviations[2])
        print("%f\t%f\t%f\t%f\t%f\t%f\t%f" %
              (settings.heartActionForce.StepShift, averages[0],
               standard_deviations[0], averages[1], standard_deviations[1],
               averages[2], standard_deviations[2]))

    #normalize values
    basalValue = Ssd[0]
    for i, v in enumerate(Ssd):
        Ssd[i] = 100.0 * (v / basalValue)
    basalValue = Dsd[0]
    for i, v in enumerate(Dsd):
        Dsd[i] = 100.0 * (v / basalValue)
    basalValue = Msd[0]
    for i, v in enumerate(Msd):
        Msd[i] = 100.0 * (v / basalValue)

    #pdb.set_trace()
    fig, ax = plt.subplots()
    ax.ticklabel_format(useOffset=False)
    #plt.subplot(3,1,1)
    ax.plot(stepShiftLinspace, Ssd, "r", linestyle="-",
            linewidth=1)  # SAP thin curve
    #plt.subplot(3,1,2)
    ax.plot(stepShiftLinspace, Dsd, "g", linestyle="--",
            linewidth=3)  # DAP thick dashed
    #plt.subplot(3,1,3)
    ax.plot(stepShiftLinspace, Msd, "b", linestyle="-",
            linewidth=3)  # MAP thick solid
    for y in (100.0, ):  # 80.0,100.0,120.0):
        plt.axhline(y=y, color='k', linestyle='-')
    #ax.fill(30, 30, fill=False, hatch='\\')
    #pdb.set_trace()
    ylim = copy.deepcopy(ax.get_ylim())  # keep original limits.\
    xlim = copy.deepcopy(ax.get_xlim())  # keep original limits.\

    ax.set_ylim([ylim[0], ylim[1]])
    ax.set_xlim([xlim[0], xlim[1]])
    plt.savefig("phaseShiftFunction100.png")
    plt.show()
    return (Sav, Ssd, Dav, Dsd, Mav,
            Msd), (settings, model, force, iafResp, iafHeart, collector,
                   seriesNotifier, fireNotifierResp, fireNotifierHeart,
                   fireNotifier)
    def test_FullCoupling(self):
        """
        This test handles the majority of physiological control of heart
        rate. Respiratory IAF periodically forces the heart via the
        RespiratoryDelayedSmearedHeartActionForce. The force is applied to heart
        drive. Initially the heart is in phase with respiration, as both
        periods are commensurate (3:1) and initial phase of both is 0.
        """
        dimension = 10
        npoints = 1800
        settings = KickedWindkesselModel.KickedWindkesselModelSettings()

        fireNotifier = FiringTimesNotifier()
        force = RespiratoryDelayedSmearedHeartActionForce()
        force.CoordinateNumber = 9  #Coordinate number, to  which the state will be written
        force.CoordinateNumberForInput = 6
        force.KickAmplitude = 1.0  #Kick amplitude
        force.DelayTau = 0.1  #Time from firing order to actual kick
        force.SamplingTime = 0.01  # required to normalize delay time.
        force.DecayTau = 0.9  # Time by which the drive decays
        force.Notify = fireNotifier.Notify

        iafResp = IntegrateAndFire()
        iafResp.SamplingTime = 0.01
        iafResp.SetPhaseVelocityFromBPM(20)
        iafResp.SetInitialPhase(0.0)
        iafResp.KickAmplitude = 0.1
        iafResp.CoordinateNumberForRate = -1
        iafResp.CoordinateNumberForPhase = 5
        iafResp.CoordinateNumberForForceInput = -1  #not used
        # Coordinate number, to  which the state will be written
        #this variable will be used for coupling.
        iafResp.CoordinateNumberForOutput = 6
        fireNotifierResp = FiringTimesNotifier()
        iafResp.Notify = fireNotifierResp.Notify

        iafHeart = IntegrateAndFire()
        iafHeart.SamplingTime = 0.01
        iafHeart.SetPhaseVelocityFromBPM(67)
        iafHeart.phaseEfectivenessCurve = phaseEfectivenessCurveSH
        iafHeart.CoordinateNumberForRate = 8
        iafHeart.CoordinateNumberForPhase = 7
        iafHeart.CoordinateNumberForForceInput = force.CoordinateNumber
        # Coordinate number, to  which the state will be written
        iafHeart.CoordinateNumberForOutput = 4
        fireNotifierHeart = FiringTimesNotifier()
        iafHeart.Notify = fireNotifierHeart.Notify

        collector = AbpmFiducialPointsCollector(0)  #ABPM = 0
        seriesNotifier = SeriesNotifier(dimension, npoints)
        allTimes = np.linspace(0.0, npoints * force.SamplingTime, npoints)

        settings.heartActionForce = HeartActionForceChain(
            [iafResp, force, iafHeart])
        model = KickedWindkesselModel(settings, dimension)
        model.param.Npoints = npoints

        chain = NotifierChain((collector, seriesNotifier))
        model.Notify = chain.Notify

        model.IterateToNotifiers()

        #print("Firing times: %s" % str(fireNotifier.firingTimes))
        fig = plt.figure()
        gs = gridspec.GridSpec(6, 1, height_ratios=[1, 2, 2, 2, 2, 4])
        ax = plt.subplot(gs[0])
        plt.plot(allTimes,
                 seriesNotifier.GetVar(iafResp.CoordinateNumberForPhase), "b")
        plt.plot(fireNotifierResp.firingTimes,
                 fireNotifierResp.firingTimesSpikes(), "bo")
        plt.yticks([0.0, 1.0])
        plt.ylabel(r"$\Phi(t)$")
        #plt.xlabel("Time [s]")
        plt.ylim(0.0, 1.2)
        plt.setp(ax.get_xticklabels(), visible=False)
        ax = plt.subplot(gs[1])
        #todo: the guy below is all flat.
        #plt.plot(allTimes,seriesNotifier.GetVar(iafResp.CoordinateNumberForOutput),"g",linewidth=2)
        plt.plot(fireNotifier.firingTimes, fireNotifier.firingTimesSpikes(),
                 "go")
        plt.plot(allTimes,
                 seriesNotifier.GetVar(force.CoordinateNumber),
                 "g",
                 linewidth=2)
        plt.yticks([])
        plt.setp(ax.get_xticklabels(), visible=False)
        #plt.xlabel("Time [s]")
        plt.ylabel(r"$r_{n}(t)$")
        #plt.ylim(0.0,5.0 * iafResp.KickAmplitude)
        ax = plt.subplot(gs[2])
        plt.yticks([])
        plt.ylabel(r"$r(t)$")
        plt.plot(allTimes,
                 seriesNotifier.GetVar(iafHeart.CoordinateNumberForRate), "v")
        plt.setp(ax.get_xticklabels(), visible=False)

        ax = plt.subplot(gs[3])
        plt.plot(allTimes,
                 seriesNotifier.GetVar(iafHeart.CoordinateNumberForPhase), "r")
        #plt.plot(allTimes,seriesNotifier.GetVar(2),"r")
        plt.plot(fireNotifierHeart.firingTimes,
                 fireNotifierHeart.firingTimesSpikes(), "ro")
        plt.ylim(0.0, 1.2)
        plt.yticks([0.0, 1.0])
        #plt.xlabel("Time [s]")
        plt.ylabel(r"$\varphi(t)$")
        #plt.ylim(0.0,5.0 * iafResp.KickAmplitude)
        plt.setp(ax.get_xticklabels(), visible=False)

        ax = plt.subplot(gs[4])
        plt.ylabel("ISI")
        plt.plot(fireNotifierHeart.firingTimes[1:],
                 fireNotifierHeart.ISI()[1:], "b+-")
        plt.setp(ax.get_xticklabels(), visible=False)

        plt.subplot(gs[5])
        for varNumber in (0, 1, 2, 3):
            plt.plot(allTimes, seriesNotifier.GetVar(varNumber))
        plt.ylim(0.0, 300.0)
        plt.xlabel("Time [s]")
        plt.ylabel("BP [mmHg]")
        fname = sys._getframe().f_code.co_name + ".png"
        print("Test result in %s" % fname)
        plt.savefig(fname)
        plt.close(fig)
    def test_HighAmplitude(self):
        """
        Is the influence of high intrathoracic pressure amplitude visible
        at disabled heart control
        """
        dimension = 10
        npoints = 2400
        settings = KickedWindkesselModel.KickedWindkesselModelSettings()

        fireNotifier = FiringTimesNotifier()
        force = RespiratoryDelayedSmearedHeartActionForce()
        force.CoordinateNumber = 9  #Coordinate number, to  which the state will be written
        force.CoordinateNumberForInput = 6
        force.KickAmplitude = 0.0  #Kick amplitude
        force.DelayTau = 0.3  #Time from firing order to actual kick
        force.SamplingTime = 0.01  # required to normalize delay time.
        force.DecayTau = 0.3  # Time by which the drive decays
        force.Notify = fireNotifier.Notify

        iafResp = IntegrateAndFire()
        iafResp.SamplingTime = 0.01
        iafResp.SetPhaseVelocityFromBPM(6)
        iafResp.SetInitialPhase(0.0)
        iafResp.KickAmplitude = 0.1
        iafResp.CoordinateNumberForRate = -1
        iafResp.CoordinateNumberForPhase = 5
        iafResp.CoordinateNumberForForceInput = -1  #not used
        # Coordinate number, to  which the state will be written
        #this variable will be used for coupling.
        iafResp.CoordinateNumberForOutput = 6
        fireNotifierResp = FiringTimesNotifier()
        iafResp.Notify = fireNotifierResp.Notify

        iafHeart = IntegrateAndFire()
        iafHeart.SamplingTime = 0.01
        iafHeart.SetPhaseVelocityFromBPM(67)
        iafHeart.phaseEfectivenessCurve = phaseEfectivenessCurveSH
        iafHeart.CoordinateNumberForRate = 8
        iafHeart.CoordinateNumberForPhase = 7
        iafHeart.CoordinateNumberForForceInput = force.CoordinateNumber
        # Coordinate number, to  which the state will be written
        iafHeart.CoordinateNumberForOutput = 4
        fireNotifierHeart = FiringTimesNotifier()
        #iafHeart.Notify = fireNotifierHeart.Notify

        collector = AbpmFiducialPointsCollector(0)  #ABPM = 0
        iafHeart.NotifyFunctions = [
            fireNotifierHeart.Notify, collector.HeartOpenNotifier
        ]
        mmNotifier = MinMaxNotifier(0)
        mmNotifier.MinNotifier = collector.AbpmMinNotifier
        mmNotifier.MaxNotifier = collector.AbpmMaxNotifier

        seriesNotifier = SeriesNotifier(dimension, npoints)
        allTimes = np.linspace(0.0, npoints * force.SamplingTime, npoints)

        settings.heartActionForce = HeartActionForceChain(
            [iafResp, force, iafHeart])
        settings.CoordinateNumberForRespPhase = iafResp.CoordinateNumberForPhase
        settings.p_I1 = 3.0  # default is 0.1
        model = KickedWindkesselModel(settings, dimension)
        model.param.Npoints = npoints

        chain = NotifierChain((mmNotifier, collector, seriesNotifier))
        model.Notify = chain.Notify

        model.IterateToNotifiers()
        allItems = collector.GetFiducialPointsList()
        series = {}
        series["systolicABP"] = np.array(allItems[:, 1])
        series["diastolicABP"] = np.array(allItems[:, 2])
        series["meanABP"] = np.array(allItems[:, 3])
        for key in series.keys():
            av = np.average(series[key])
            sd = np.std(series[key])
            logging.info("%s: av: %.2lf sd: %.2lf, npoints: %d" %
                         (key, av, sd, len(series[key])))

        print(allItems)

        fname = sys._getframe().f_code.co_name + ".png"
        KickedWindkesselModelVisualization(
            fname, allTimes, allItems, seriesNotifier, fireNotifierResp,
            fireNotifier, fireNotifierHeart, iafResp.CoordinateNumberForPhase,
            force.CoordinateNumber, iafHeart.CoordinateNumberForRate,
            iafHeart.CoordinateNumberForPhase)