Example #1
0
    def astep(self, q0):
        """Perform a single NUTS iteration."""
        p0 = self.potential.random()
        start = self.integrator.compute_state(q0, p0)

        if not np.isfinite(start.energy):
            raise ValueError('Bad initial energy: %s. The model '
                             'might be misspecified.' % start.energy)

        if not self.adapt_step_size:
            step_size = self.step_size
        elif self.tune:
            step_size = np.exp(self.log_step_size)
        else:
            step_size = np.exp(self.log_step_size_bar)

        if self.tune and self.m < 200:
            max_treedepth = self.early_max_treedepth
        else:
            max_treedepth = self.max_treedepth

        tree = _Tree(len(p0), self.integrator, start, step_size, self.Emax)

        for _ in range(max_treedepth):
            direction = logbern(np.log(0.5)) * 2 - 1
            diverging, turning = tree.extend(direction)
            q, q_grad = tree.proposal.q, tree.proposal.q_grad

            if diverging or turning:
                if diverging:
                    self.report._add_divergence(self.tune, *diverging)
                break

        w = 1. / (self.m + self.t0)
        self.h_bar = (
            (1 - w) * self.h_bar + w *
            (self.target_accept - tree.accept_sum * 1. / tree.n_proposals))

        if self.tune:
            self.log_step_size = self.mu - self.h_bar * np.sqrt(
                self.m) / self.gamma
            mk = self.m**-self.k
            self.log_step_size_bar = mk * self.log_step_size + (
                1 - mk) * self.log_step_size_bar

        self.m += 1

        if self.tune:
            self.potential.adapt(q, q_grad)

        stats = {
            'step_size': step_size,
            'tune': self.tune,
            'step_size_bar': np.exp(self.log_step_size_bar),
            'diverging': diverging,
        }

        stats.update(tree.stats())

        return q, [stats]
Example #2
0
    def astep(self, q0):
        """Perform a single NUTS iteration."""
        p0 = self.potential.random()
        start = self.integrator.compute_state(q0, p0)

        if not np.isfinite(start.energy):
            raise ValueError('Bad initial energy: %s. The model '
                             'might be misspecified.' % start.energy)

        if not self.adapt_step_size:
            step_size = self.step_size
        elif self.tune:
            step_size = np.exp(self.log_step_size)
        else:
            step_size = np.exp(self.log_step_size_bar)

        if self.tune and self.m < 200:
            max_treedepth = self.early_max_treedepth
        else:
            max_treedepth = self.max_treedepth

        tree = _Tree(len(p0), self.integrator, start, step_size, self.Emax)

        for _ in range(max_treedepth):
            direction = logbern(np.log(0.5)) * 2 - 1
            diverging, turning = tree.extend(direction)
            q, q_grad = tree.proposal.q, tree.proposal.q_grad

            if diverging or turning:
                if diverging:
                    self.report._add_divergence(self.tune, *diverging)
                break

        w = 1. / (self.m + self.t0)
        self.h_bar = ((1 - w) * self.h_bar +
                      w * (self.target_accept - tree.accept_sum * 1. / tree.n_proposals))

        if self.tune:
            self.log_step_size = self.mu - self.h_bar * np.sqrt(self.m) / self.gamma
            mk = self.m ** -self.k
            self.log_step_size_bar = mk * self.log_step_size + (1 - mk) * self.log_step_size_bar

        self.m += 1

        if self.tune:
            self.potential.adapt(q, q_grad)

        stats = {
            'step_size': step_size,
            'tune': self.tune,
            'step_size_bar': np.exp(self.log_step_size_bar),
            'diverging': diverging,
        }

        stats.update(tree.stats())

        return q, [stats]
Example #3
0
    def astep(self, q0):
        """Perform a single NUTS iteration."""
        p0 = self.potential.random()
        v0 = self.compute_velocity(p0)
        start_energy = self.compute_energy(q0, p0)
        if not np.isfinite(start_energy):
            raise ValueError('Bad initial energy: %s. The model '
                             'might be misspecified.' % start_energy)

        if not self.adapt_step_size:
            step_size = self.step_size
        elif self.tune:
            step_size = np.exp(self.log_step_size)
        else:
            step_size = np.exp(self.log_step_size_bar)

        start = Edge(q0, p0, v0, self.dlogp(q0), start_energy)
        tree = _Tree(len(p0), self.leapfrog, start, step_size, self.Emax)

        for _ in range(self.max_treedepth):
            direction = logbern(np.log(0.5)) * 2 - 1
            diverging, turning = tree.extend(direction)
            q = tree.proposal.q

            if diverging or turning:
                if diverging:
                    self.report._add_divergence(self.tune, *diverging)
                break
        else:
            self._reached_max_treedepth += 1

        w = 1. / (self.m + self.t0)
        self.h_bar = (
            (1 - w) * self.h_bar + w *
            (self.target_accept - tree.accept_sum * 1. / tree.n_proposals))

        if self.tune:
            self.log_step_size = self.mu - self.h_bar * np.sqrt(
                self.m) / self.gamma
            mk = self.m**-self.k
            self.log_step_size_bar = mk * self.log_step_size + (
                1 - mk) * self.log_step_size_bar

        self.m += 1

        stats = {
            'step_size': step_size,
            'tune': self.tune,
            'step_size_bar': np.exp(self.log_step_size_bar),
            'diverging': diverging,
        }

        stats.update(tree.stats())

        return q, [stats]
Example #4
0
def aggregate(df, fce):
    n = 50
    list_df = [df[i:i + n] for i in range(0, df.shape[0] - n - 1)]
    # print(list_df[0])

    stats = dict()
    for f in fce:
        stats.update({f: list()})
    # means = []
    # stds = []

    for line in list_df:
        for f in fce:
            stats.get(f).append(line.agg(f)['value'])
    return pd.DataFrame({name: stats.get(name) for name in fce})
Example #5
0
    def astep(self, q0):
        p0 = self.potential.random()
        v0 = self.compute_velocity(p0)
        start_energy = self.compute_energy(q0, p0)

        if not self.adapt_step_size:
            step_size = self.step_size
        elif self.tune:
            step_size = np.exp(self.log_step_size)
        else:
            step_size = np.exp(self.log_step_size_bar)

        start = Edge(q0, p0, v0, self.dlogp(q0), start_energy)
        tree = Tree(len(p0), self.leapfrog, start, step_size, self.Emax)

        for _ in range(self.max_treedepth):
            direction = logbern(np.log(0.5)) * 2 - 1
            diverging, turning = tree.extend(direction)
            q = tree.proposal.q

            if diverging or turning:
                break

        w = 1. / (self.m + self.t0)
        self.h_bar = (
            (1 - w) * self.h_bar + w *
            (self.target_accept - tree.accept_sum * 1. / tree.n_proposals))

        if self.tune:
            self.log_step_size = self.mu - self.h_bar * np.sqrt(
                self.m) / self.gamma
            mk = self.m**-self.k
            self.log_step_size_bar = mk * self.log_step_size + (
                1 - mk) * self.log_step_size_bar

        self.m += 1

        stats = {
            'step_size': step_size,
            'tune': self.tune,
            'step_size_bar': np.exp(self.log_step_size_bar),
            'diverging': diverging,
        }

        stats.update(tree.stats())

        return q, [stats]
Example #6
0
File: nuts.py Project: taku-y/pymc3
    def astep(self, q0):
        p0 = self.potential.random()
        v0 = self.compute_velocity(p0)
        start_energy = self.compute_energy(q0, p0)

        if not self.adapt_step_size:
            step_size = self.step_size
        elif self.tune:
            step_size = np.exp(self.log_step_size)
        else:
            step_size = np.exp(self.log_step_size_bar)

        start = Edge(q0, p0, v0, self.dlogp(q0), start_energy)
        tree = Tree(len(p0), self.leapfrog, start, step_size, self.Emax)

        for _ in range(self.max_treedepth):
            direction = logbern(np.log(0.5)) * 2 - 1
            diverging, turning = tree.extend(direction)
            q = tree.proposal.q

            if diverging or turning:
                break

        w = 1. / (self.m + self.t0)
        self.h_bar = ((1 - w) * self.h_bar +
                      w * (self.target_accept - tree.accept_sum * 1. / tree.n_proposals))

        if self.tune:
            self.log_step_size = self.mu - self.h_bar * np.sqrt(self.m) / self.gamma
            mk = self.m ** -self.k
            self.log_step_size_bar = mk * self.log_step_size + (1 - mk) * self.log_step_size_bar

        self.m += 1

        stats = {
            'step_size': step_size,
            'tune': self.tune,
            'step_size_bar': np.exp(self.log_step_size_bar),
            'diverging': diverging,
        }

        stats.update(tree.stats())

        return q, [stats]
##-----------------------------------------------------------------------
## training
##-----------------------------------------------------------------------
for screen in instructions['training']:
    show_instruction(screen)

for i, num in enumerate(stimuli_training):
    stats = {'condition': 'train', 'trial': i + 1}
    if num < 0:
        # probe
        res = show_probe()

    else:
        res = show_trial(num)
    stats.update(res)
    logdata(**stats)

##-----------------------------------------------------------------------
## full experiment
##-----------------------------------------------------------------------
for screen in instructions['experiment']:
    show_instruction(screen)

iprobes = 0  # number of probes already shown
for i, num in enumerate(stimuli):

    stats = {'condition': 'exp', 'trial': i + 1}
    if i == ntotal / 2:
        for screen in instructions['halfway']:
            show_instruction(screen)
##-----------------------------------------------------------------------
## training
##-----------------------------------------------------------------------
for screen in instructions['training']:
    show_instruction(screen)


for i,num in enumerate(stimuli_training):
    stats={'condition':'train', 'trial':i+1}
    if num<0:
        # probe
        res=show_probe()

    else:
        res=show_trial(num)
    stats.update(res)
    logdata(**stats)


##-----------------------------------------------------------------------
## full experiment
##-----------------------------------------------------------------------
for screen in instructions['experiment']:
    show_instruction(screen)

iprobes=0 # number of probes already shown
for i,num in enumerate(stimuli):

    stats={'condition':'exp', 'trial':i+1}
    if i==ntotal/2: 
        for screen in instructions['halfway']:
Example #9
0
 def _DoPostProcessing(self):
     # profile = cProfile.Profile()
     print "_DoPostProcessing"
     if self._processFramesThread.progress < 0.9999:
         print "Stats: processing was stopped"
         return
         
     # PostProcessing
     # profile.enable()        
     postProcessedLocs = DuplicateLocsList(self._resultsDock._allLocsNotPostprocessed)
     for postprocessor in self.main._postprocessors:
         postProcessedLocs = postprocessor.Apply(self.main._fseries, self.main._curPSF, self.main._curGroundTruth, postProcessedLocs)
     self._postProcessedLocs = postProcessedLocs
     # profile.disable()
     
     # Stats
     if self.main._fseries.HasGroundTruth():
         originalStats = self._processFramesThread.stats
         postprocessedStats = GroundTruthStats(self.main._fseries, self.main._curGroundTruth, \
                                               originalStats._nrGroundTruthMolecules)
         postprocessedStats.AddLocations(postProcessedLocs)
         
         # Results history
         thread = self._processFramesThread
         stats = postprocessedStats.GetStats()
         stats.update({"framesFrom": int(thread.frameFrom), \
              "framesTo": int(thread.frameTo), \
              "process": thread.progress})
         self._resHistory.append((stats["jac"], stats["recall"], stats["precision"], 1e9 * stats["rmsXY"], 1e9 * stats["rmsZ"], 1e9 * stats["averageDx"], 1e9 * stats["averageDy"], 1e9 * stats["averageDz"], stats["avgPhotonsScale"], stats["avgPhotonsDelta"]))
     
         print "Res history:"
         print "\t".join(["jac", "recall", "pres", "rmsXY", "rmsZ", "avgeDx", "avgDy", "avgDz", "phDelta", "phScale"])
         for hist in self._resHistory:
             print "\t".join(["%.3f" % (v) for v in hist])
             
         print "Without post-processing:"
         s = originalStats.GetStats()
         print "\t".join(["%.3f" % (v) for v in [s["jac"], s["recall"], s["precision"], 1e9 * s["rmsXY"], 1e9 * s["rmsZ"], 1e9 * s["averageDx"], 1e9 * s["averageDy"], 1e9 * s["averageDz"], s["avgPhotonsScale"], s["avgPhotonsDelta"]]])
         print
    
         # Real statistics
         statisticsReal = GroundTruthStats(self.main._fseries, self.main._curGroundTruth)
         _, self._sortingResReal = statisticsReal.SortedStatistics(postProcessedLocs, \
             lambda loc: loc.goodnessValue, alreadySorted = True)
    
         # Optimal statistics
         statOptimalXY = GroundTruthStats(self.main._fseries, self.main._curGroundTruth)
         _, self._sortingResOptimalXY = statOptimalXY.SortedStatistics(postProcessedLocs, lambda loc:-loc.distXY)
    
         statOptimalZ = GroundTruthStats(self.main._fseries, self.main._curGroundTruth)
         _, self._sortingResOptimalZ = statOptimalZ.SortedStatistics(postProcessedLocs, lambda loc:-loc.distZ)
 
         
         
         # Publish to toplist thread.frameFrom 
         toPublish = {"userName": getuser(),
                      "datasetName": self.main._fseries.name,
                      "testName": str(self._resultsDock._studyName.text()),
                      "framesFrom": thread.frameFrom,
                      "framesTo": thread.frameTo,
                      "goodnessFunc": "None",
                      "conf": self.main.GetConf(removeDockingStates = True),
                      "results": {"originalStats": originalStats.GetStats(),
                                  "postprocessedStats": postprocessedStats.GetStats(),
                                  "goodness": self._sortingResReal}}
     
         try:
             PublishToToplist(toPublish)
         except:
             traceback.print_exc()
     
         self.main.ui.statusbar.showMessage("Postprocessed stats: %s" % (postprocessedStats.GetStatsStr()))
     else:
         # No ground-truth
         self.main.ui.statusbar.showMessage("Processing done.")
Example #10
0
    def analyse_data(self, data, energies, t_max, dt):
        neuron_types = ["PM", "LP", "PY"]
        ref_neuron = neuron_types[0]

        # Percentage of triphasic periods for system to be considered triphasic.
        triphasic_thresh = 0.9
        nan = float("nan")

        t = np.arange(0, t_max, dt)
        v = data

        assert len(v) == len(neuron_types)

        stats = {
            neutype: self.analyse_neuron(t, np.asarray(V), energy)
            for V, neutype, energy in zip(v, neuron_types, energies)
        }

        # if one neuron does not have a periodic rhythm, the whole system is not
        # considered triphasic
        if np.isnan(stats[ref_neuron]["avg_cycle_length"]):
            cycle_period = nan
            period_times = []

            triphasic = False
            period_data = []
        else:
            # The system period is determined by the periods of a fixed neuron (PM)
            ref_stats = stats[ref_neuron]
            period_times = ref_stats["burst_start_times"]
            cycle_period = np.mean(np.diff(period_times))

            # Analyse the periods, store useful data and check if the neuron is
            # triphasic
            n_periods = len(period_times)
            period_data = []
            period_triphasic = np.zeros(n_periods - 1)
            for i in range(n_periods - 1):
                # The start and end times of the given period, and the starts of the
                # neurons' bursts
                # within this period
                pst, pet = period_times[i], period_times[i + 1]
                burst_starts = {}
                burst_ends = {}

                for nt in neuron_types:
                    bs_nt = stats[nt]["burst_start_times"]
                    be_nt = stats[nt]["burst_end_times"]

                    if len(bs_nt) == 0:
                        burst_starts[nt] = []
                        burst_ends[nt] = []
                    else:
                        cond = (pst <= bs_nt) & (bs_nt < pet)
                        burst_starts[nt] = bs_nt[cond]
                        burst_ends[nt] = be_nt[cond]

                # A period is classified as triphasic if all neurons start to burst
                # once within the period
                if np.all([len(burst_starts[nt]) == 1 for nt in neuron_types]):
                    period_triphasic[i] = 1
                    period_data.append({
                        nt: (burst_starts[nt], burst_ends[nt])
                        for nt in neuron_types
                    })

            # if we have at least two periods and most of them are triphasic, classify
            # the system as triphasic
            if n_periods >= 2:
                triphasic = np.mean(period_triphasic) >= triphasic_thresh
            else:
                triphasic = False

        stats.update({
            "cycle_period": cycle_period,
            "period_times": period_times,
            "triphasic": triphasic,
            "period_data": period_data,
        })

        return stats