コード例 #1
0
ファイル: events.py プロジェクト: rkdarst/dynsnap
def main_analyze(argv=sys.argv):
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("input")
    parser.add_argument("output")


    parser.add_argument("--uw", action='store_true', help="Weighted analysis")
    parser.add_argument("--timescale", type=float, help="time scale")
    parser.add_argument("--nitems", type=int, help="number of items to analyze")
    args = parser.parse_args(argv[1:])

    time_scale = 1.
    if args.timescale:
        time_scale = args.timescale
    weighted = not args.uw

    evs = Events(args.input)
    e_last_time = { }
    inter_event_times = [ ]
    t_min = evs.t_min()
    nitems = args.nitems

    for i, (t, e, w) in enumerate(evs.iter_ordered()):
        if nitems and i > nitems:
            break
        #print t, e, w
        if e not in e_last_time:
            e_last_time[e] = t
            inter_event_times.append(t-t_min)
            continue
        dt = t - e_last_time[e]
        inter_event_times.append(dt)

    for t_lastseen in e_last_time.itervalues():
        inter_event_times.append(t-t_lastseen)


    if weighted:
        weights = inter_event_times
    else:
        weights = None
    import numpy
    hist, bin_edges = numpy.histogram(inter_event_times,
                                      weights=weights,
                                      bins=50, normed=True)
    #print hist
    #print bin_edges

    import pcd.support.matplotlibutil as mplutil

    bin_edges /= time_scale

    ax, extra = mplutil.get_axes(args.output+'.[png,pdf]')
    ax.plot(bin_edges[:-1], hist)
    ax.set_xlabel('$\Delta t$')
    ax.set_ylabel('PDF')
    ax.set_title('total t: [%s, %s], Dt=%4.2f'%(t_min, t, (t-t_min)/time_scale))
    mplutil.save_axes(ax, extra)
コード例 #2
0
ファイル: viz.py プロジェクト: rkdarst/dynsnap
    def __init__(self):
        print("==", self.__class__.__name__)
        evs_list = list(self.model())
        evs = events.Events()
        evs.add_events((t,e,1) for t,e in evs_list)

        _, results = dynsnap.main([None,
                                   #'--dtmode=linear',
                                   #'--dtmax=10',
                                   #'--dtextra=5',
                                   #'--dont-merge-first',
                                   #'--peakfinder=greedy',
                                  ]+self.dsargs,
                                  evs=evs)
        results = results['results']
        if results.thighs[-1] > evs.t_max():
            results.thighs[-1] = evs.t_max()+1

        import pcd.support.matplotlibutil as mplutil

        ax, extra = mplutil.get_axes(self.fname,
                                     figsize=self.figsize)
        ts, es = zip(*evs_list)
        ax.scatter(ts, es, s=self.ps, facecolor='b', edgecolor='b')
        #for thigh in results.thighs[:-1]:
        #    ax.axvline(x=thigh-0.5)
        results.plot_intervals_patches(ax, shift=-.5)

        from matplotlib.font_manager import FontProperties
        font = FontProperties()
        font.set_family('serif')
        ax.set_xlabel('time', fontproperties=font)
        ax.set_ylabel('event ID', fontproperties=font)
        ax.set_xticks([])
        ax.set_yticks([])
        ax.autoscale(tight=True)
        if hasattr(self, 'xlim'):  ax.set_xlim(*self.xlim)
        if hasattr(self, 'ylim'):  ax.set_ylim(*self.ylim)

        #axB = ax.twinx()
        #results.plot_Jfinding(axB)

        mplutil.save_axes(ax, extra)
コード例 #3
0
ファイル: commstats.py プロジェクト: zjminglove/pcd
    def write(self, fname, axopts={}, title=None):
        from pcd.support import matplotlibutil
        ax, extra = matplotlibutil.get_axes(fname, **axopts)

        data = self._data

        # Do the actual plotting
        import matplotlib.cm as cm
        import matplotlib.colors as mcolors
        colormap = cm.get_cmap('jet')
        normmap = mcolors.Normalize(vmin=0, vmax=len(self.label_order))

        from functools import partial

        if self.bin and self.log_y:
            binparams = {'ints': self.bin_ints}
            binfunc = log_bin
            binwidth = log_bin_width
        elif self.bin:
            binparams = {'bins':20, 'ints': self.bin_ints}
            binfunc = lin_bin
            binwidth = lin_bin_width
        else:
            binparams = {'ints': self.bin_ints}
            binfunc = lambda x: x
            binwidth = lambda x: 1
        if hasattr(self, 'binparams'):
            binparams.update(self.binparams)

        label_order = self.label_order
        if not label_order:
            label_order = sorted(data.keys())
        for i, label in enumerate(label_order):
            points = data[label]
            if self.colormap and label in self.colormap:
                color = self.colormap[label]
            else:
                color = colormap(normmap(i))
            # Line type
            if label in self.plotstyle:
                plotstyle = self.plotstyle[label]
            elif self.markers:
                plotstyle = '-%s'%(self.markers[i%len(self.markers)])
            else:
                plotstyle = self.plotstyle[label]
            #
            vals = []
            [vals.extend(x) for x in points.values() ]
            vals = map(partial(binfunc, **binparams), vals)
            # Set a domain of all values to include
            if self.domain is not None:
                domain = set(binfunc(x, **binparams) for x in self.domain)
            else:
                domain = set()
            vals, vals_counts = counts(vals, domain=domain)
            if self.dist_is_counts:
                lines = ax.plot(vals, vals_counts, plotstyle, color=color,
                                label=label)
            # Complimentary cumulative distribution function plotting.
            elif self.dist_is_ccdf or self.dist_is_cccf:
                vals, vals_counts = \
                      zip(*sorted(zip(vals, vals_counts), key=lambda x: x[0])
                          )
                total = float(sum(vals_counts))
                import pcd.util
                counts_cumul = list(pcd.util._accumulate(vals_counts))
                counts_cumul = [counts_cumul[-1]-x for x in counts_cumul]
                if self.dist_is_ccdf:
                    counts_cumul = [ x/total for x in counts_cumul ]
                lines = ax.plot(vals, counts_cumul, plotstyle,
                                color=color, label=label)

            else:
                vals_counts = [ c/binwidth(s, **binparams)
                                for s,c in zip(vals, vals_counts) ]
                p_vals = norm(vals_counts)
                lines = ax.plot(vals, p_vals, plotstyle, color=color,
                                label=label)

            if hasattr(self, '_hook_lines'):
                self._hook_lines(locals())

        if self.log_y:
            ax.set_xscale('log')
        if self.dist_xlim: ax.set_xlim(*self.dist_xlim)
        if self.dist_ylim: ax.set_ylim(*self.dist_ylim)
        if self.log_p:
            ax.minorticks_on()
            ax.set_yscale('log', basey=10)
        if self.decorate:
            if self.ylabel: ax.set_xlabel(self.ylabel)
            if self.ylabel_dist:
                ax.set_ylabel(self.ylabel_dist)
            elif self.dist_is_counts:
                if self.ylabel: ax.set_ylabel("Count(%s)"%self.ylabel)
            elif self.dist_is_ccdf:
                if self.ylabel:
                    ax.set_ylabel("CCDF(%s)"%self.ylabel)
            elif self.dist_is_cccf:
                if self.ylabel:
                    ax.set_ylabel("Complimentary cumulative count(%s)"%self.ylabel)
            else:
                if self.ylabel: ax.set_ylabel("P(%s)"%self.ylabel)
            if   title:      ax.set_title(self.title)
            elif self.title: ax.set_title(self.title)
            if len(data) > 1 and self.legend_make:
                ax.legend(loc=self.legend_loc, **self.legend_kwargs)

        self._hook_write_setup_plot(locals())
        matplotlibutil.save_axes(ax, extra)
コード例 #4
0
ファイル: commstats.py プロジェクト: zjminglove/pcd
    def write(self, fname, axopts={}, title=None):
        """Write data.

        `fname` can be an matplotlib.axes.Axes object, in which case
        no new files are created and lines are added to this axis.
        """
        from pcd.support import matplotlibutil
        ax, extra = matplotlibutil.get_axes(fname, **axopts)

        data = self._data

        # Do the actual plotting
        # Get colors and styles.
        import matplotlib.cm as cm
        import matplotlib.colors as mcolors
        colormap = cm.get_cmap('jet')
        normmap = mcolors.Normalize(vmin=0, vmax=len(self.label_order))

        label_order = self.label_order
        if not label_order:
            label_order = sorted(data.keys())
        for i, label in enumerate(label_order):
            # Colors
            if self.colormap and label in self.colormap:
                color = self.colormap[label]
            else:
                color = colormap(normmap(i))
            # Line type
            if label in self.plotstyle:
                plotstyle = self.plotstyle[label]
            elif self.markers:
                plotstyle = '-%s'%(self.markers[i%len(self.markers)])
            else:
                plotstyle = self.plotstyle[label]
            # Make all points data.  `points` is a list of (cmty_size,
            # [list of <measure> values]) pairs and `xy` is a list of
            # (cmty_size, <mean_measure>) pairs.
            points = data[label]
            if len(points) == 0:
                print "skipping %s with no data"%label
                continue
            xy = sorted((a, numpy.mean(b)) for a,b in points.iteritems())
            xvals, y = zip(*xy)

            # Plot the mean:
            ax.plot(xvals, y, plotstyle, lw=2, color=color, label=label)

            # If we are a subclass of QuantileStatter, plot quantiles,
            # too.
            if self.quantiles is not None and len(self.quantiles):
                quantiles = [ ]
                for x in xvals:
                    values = sorted(points[x])
                    quantiles.append(tuple(quantile(values, p)
                                           for p in self.quantiles))
                p_quantiles = zip(*quantiles)
                # This does a little bit of "offset" to make the
                # quantiles more visible.
                #min_v = min(p_quantiles[0])
                #max_v = max(p_quantiles[-1])
                #epsilon = (max_v - min_v)*.001
                #def adjust(i):
                #    return epsilon * (i-len(p_quantiles)/2.)
                adjust = lambda i: 0
                # Do the actual plotting
                for i, qtl in enumerate(p_quantiles):
                    #print colormap(normmap(i))
                    qtl = numpy.asarray(qtl) + adjust(i)
                    ax.plot(xvals, qtl, color=color,
                            lw=.5)

        # General plot layout and configuration.
        ylims = ax.get_ylim()
        if self.log_x:
            ax.set_xscale('log')
        if self.log_y:
            ax.minorticks_on()
            ax.set_yscale('log', basey=10)
        if self.xlim: ax.set_xlim(*self.xlim)
        if self.ylim: ax.set_ylim(*self.ylim)
        if self.decorate:
            if self.xlabel: ax.set_xlabel(self.xlabel)
            if self.ylabel: ax.set_ylabel(self.ylabel)
            if   title:      ax.set_title(self.title)
            elif self.title: ax.set_title(self.title)
            if len(data) > 1 and self.legend_make:
                ax.legend(loc=self.legend_loc, **self.legend_kwargs)


        self._hook_write_setup_plot(locals())
        matplotlibutil.save_axes(ax, extra)