Make a light-weight stat figure
429471
def plotstat(data, circleinds=None, crossinds=None, edgeinds=None, url_path=None, fileroot=None, 
             tools="hover,tap,pan,box_select,wheel_zoom,reset", plot_width=450, plot_height=400):
    

    fields = ['imkur', 'specstd', 'sizes', 'colors', 'snrs', 'key']

    if not circleinds: circleinds = range(len(data['snrs']))

    # set ranges
    datalen = len(data['dm'])
    inds = circleinds + crossinds + edgeinds
    specstd = [data['specstd'][i] for i in inds]
    specstd_min = min(specstd)
    specstd_max = max(specstd)
    imkur = [data['imkur'][i] for i in inds]
    imkur_min = min(imkur)
    imkur_max = max(imkur)

    source = ColumnDataSource(data = dict({(key, tuple([value[i] for i in circleinds if i not in edgeinds])) 
                                           for (key, value) in data.iteritems() if key in fields}))
    stat = Figure(plot_width=plot_width, plot_height=plot_height, toolbar_location="left", x_axis_label='Spectral std',
                  y_axis_label='Image kurtosis', x_range=(specstd_min, specstd_max), 
                  y_range=(imkur_min, imkur_max), tools=tools, output_backend='webgl')
    stat.circle('specstd', 'imkur', size='sizes', line_color=None, fill_color='colors', fill_alpha=0.2, source=source)

    if crossinds:
        sourceneg = ColumnDataSource(data = dict({(key, tuple([value[i] for i in crossinds]))
                                                  for (key, value) in data.iteritems() if key in fields}))
        stat.cross('specstd', 'imkur', size='sizes', line_color='colors', line_alpha=0.3, source=sourceneg)

    if edgeinds:
        sourceedge = ColumnDataSource(data = dict({(key, tuple([value[i] for i in edgeinds]))
                                                   for (key, value) in data.iteritems() if key in fields}))
        stat.circle('specstd', 'imkur', size='sizes', line_color='colors', fill_color='colors', source=sourceedge, line_alpha=0.5, fill_alpha=0.2)

    hover = stat.select(dict(type=HoverTool))
    hover.tooltips = OrderedDict([('SNR', '@snrs'), ('key', '@key')])

    if url_path and fileroot:
        url = '{}/cands_{}_@key.png'.format(url_path, fileroot)
        taptool = stat.select(type=TapTool)
        taptool.callback = OpenURL(url=url)

    return stat
828701
def _float_values(self, shape):
        
        return self.state.uniform(low=0.0, high=100.0, size=shape)
103059
def plot_stats(statistics, ylog=False, view=False, filename='avg_fitness.svg'):
    
    if plt is None:
        warnings.warn("This display is not available due to a missing optional dependency (matplotlib)")
        return

    generation = range(len(statistics.most_fit_genomes))
    best_fitness = [c.fitness for c in statistics.most_fit_genomes]
    avg_fitness = np.array(statistics.get_fitness_mean())
    stdev_fitness = np.array(statistics.get_fitness_stdev())

    plt.plot(generation, avg_fitness, 'b-', label="average")
    plt.plot(generation, avg_fitness - stdev_fitness, 'g-.', label="-1 sd")
    plt.plot(generation, avg_fitness + stdev_fitness, 'g-.', label="+1 sd")
    plt.plot(generation, best_fitness, 'r-', label="best")

    plt.title("Population's average and best fitness")
    plt.xlabel("Generations")
    plt.ylabel("Fitness")
    plt.grid()
    plt.legend(loc="best")
    if ylog:
        plt.gca().set_yscale('symlog')

    plt.savefig(filename)
    if view:
        plt.show()

    plt.close()
805168
def document_stat(stat):
    
    # Dedented so that it lineups (in sphinx) with the part
    # generated parts when put together
    docstring = dedent(stat.__doc__)

    # usage:
    signature = make_signature(stat.__name__,
                               stat.DEFAULT_PARAMS,
                               common_stat_params,
                               common_stat_param_values)
    usage = STAT_SIGNATURE_TPL.format(signature=signature)

    # aesthetics
    contents = OrderedDict(('**{}**'.format(ae), '')
                           for ae in sorted(stat.REQUIRED_AES))
    contents.update(sorted(stat.DEFAULT_AES.items()))
    table = dict_to_table(('Aesthetic', 'Default value'), contents)
    aesthetics_table = AESTHETICS_TABLE_TPL.format(table=table)
    tpl = dedent(stat._aesthetics_doc.lstrip('\n'))
    aesthetics_doc = tpl.format(aesthetics_table=aesthetics_table)
    aesthetics_doc = indent(aesthetics_doc, ' '*4)

    # common_parameters
    d = stat.DEFAULT_PARAMS
    common_parameters = STAT_PARAMS_TPL.format(
            default_geom=d['geom'],
            default_position=d['position'],
            default_na_rm=d['na_rm'],
            _aesthetics_doc=aesthetics_doc,
            **common_params_doc)

    docstring = docstring.replace('{usage}', usage)
    docstring = docstring.replace('{common_parameters}',
                                  common_parameters)
    stat.__doc__ = docstring
    return stat
1118567
def scoreatpercentile(data,per,axis=0):
    ''
    a = np.sort(data,axis=axis)
    idx = per/100. * (data.shape[axis]-1)

    if (idx % 1 == 0):
        return a[[slice(None) if ii != axis else idx for ii in range(a.ndim)]]
    else:
        lowerweight = 1-(idx % 1)
        upperweight = (idx % 1)
        idx = int(np.floor(idx))
        return lowerweight * a[[slice(None) if ii != axis else idx for ii in range(a.ndim)]] \
                + upperweight * a[[slice(None) if ii != axis else idx+1 for ii in range(a.ndim)]]
540859
def roughsize(size, above=20, mod=10):
    """"""
    if size < above:
        return str(size)

    return "{:d}+".format(size - size % mod)
31838
def timeseries(X, **kwargs):
    """"""
    pl.figure(figsize=(2*rcParams['figure.figsize'][0], rcParams['figure.figsize'][1]),
              subplotpars=sppars(left=0.12, right=0.98, bottom=0.13))
    timeseries_subplot(X, **kwargs)
650500
def make_figure_uhs(extractors, what):
    
    import matplotlib.pyplot as plt
    fig = plt.figure()
    got = {}  # (calc_id, kind) -> curves
    for i, ex in enumerate(extractors):
        uhs = ex.get(what)
        for kind in uhs.kind:
            got[ex.calc_id, kind] = uhs[kind]
    oq = ex.oqparam
    n_poes = len(oq.poes)
    periods = [imt.period for imt in oq.imt_periods()]
    [site] = uhs.site_id
    for j, poe in enumerate(oq.poes):
        ax = fig.add_subplot(n_poes, 1, j + 1)
        ax.set_xlabel('UHS on site %s, poe=%s, inv_time=%dy' %
                      (site, poe, oq.investigation_time))
        ax.set_ylabel('SA')
        for ck, arr in got.items():
            ax.plot(periods, arr[0, :, j], '-', label='%s_%s' % ck)
            ax.plot(periods, arr[0, :, j], '.')
        ax.grid(True)
        ax.legend()
    return plt
1112960
def _init_fld2val_stats(self, name, vals):
        """"""
        vals_stats = stats.describe(vals)
        stddev = math.sqrt(vals_stats[3]) # stats variance
        p25 = np.percentile(vals, 25)
        p50 = np.percentile(vals, 50) # median
        p75 = np.percentile(vals, 75)
        fld2val = {
            'name':name,
            'qty'.format(ITEMS=self.desc):vals_stats[0], # stats nobs
            'range':self._get_str_range(vals_stats),
            '25th percentile':p25,
            'median':p50,
            '75th percentile':p75,
            'mean':vals_stats[2], # stats mean
            'stddev':stddev}
        fmtflds = set(['25th percentile', 'median', '75th percentile', 'mean', 'stddev'])
        mkint = "," in self.fmtstr
        for key, val in fld2val.items():
            if key in fmtflds:
                if mkint:
                    val = int(round(val))
                fld2val[key] = self.fmtstr.format(val)
        return fld2val
436247
def plot_lightness(saveplot=False):
    '''

    '''

    from colorspacious import cspace_converter

    dc = 1.
    x = np.linspace(0.0, 1.0, 256)
    locs = []  # locations for text labels

    fig = plt.figure(figsize=(16, 5))
    ax = fig.add_subplot(111)
    fig.subplots_adjust(left=0.03, right=0.97)
    ax.set_xlim(-0.1, len(cm.cmap_d)/2. + 0.1)
    ax.set_ylim(0, 100)
    ax.set_xlabel('Lightness for each colormap', fontsize=14)

    for j, cmapname in enumerate(cm.cmapnames):

        if '_r' in cmapname:  # skip reversed versions for plot
            continue

        cmap = cm.cmap_d[cmapname]  # get the colormap instance
        rgb = cmap(x)[np.newaxis, :, :3]
        lab = cspace_converter("sRGB1", "CAM02-UCS")(rgb)
        L = lab[0, :, 0]
        if L[-1] > L[0]:
            ax.scatter(x+j*dc, L, c=x, cmap=cmap, s=200, linewidths=0.)
        else:
            ax.scatter(x+j*dc, L[::-1], c=x[::-1], cmap=cmap, s=200, linewidths=0.)
        locs.append(x[-1]+j*dc)  # store locations for colormap labels

    # Set up labels for colormaps
    ax.xaxis.set_ticks_position('top')
    ticker = mpl.ticker.FixedLocator(locs)
    ax.xaxis.set_major_locator(ticker)
    formatter = mpl.ticker.FixedFormatter([cmapname for cmapname in cm.cmapnames])
    ax.xaxis.set_major_formatter(formatter)
    labels = ax.get_xticklabels()
    for label in labels:
        label.set_rotation(60)

    if saveplot:
        fig.savefig('figures/lightness.png', bbox_inches='tight')
        fig.savefig('figures/lightness.pdf', bbox_inches='tight')

    plt.show()
743589
def __display_stats(self):
        """"""

        self.display('unify.tmpl', processed=self.total,
                     matched=self.matched,
                     unified=self.total - self.matched)
1048914
def stats():
    ''''''
    import re
    import sys
    import math
    values = []
    for line in sys.stdin:
        values.extend(map(float, re.findall(r'\d+\.?\d+', line)))

    mean = sum(values) / len(values)
    variance = sum((val - mean) ** 2 for val in values) / len(values)
    print '%3i items; mean: %10.5f; std-dev: %10.5f' % (
        len(values), mean, math.sqrt(variance))
130796
def reweight(self, data):
        

        """

        print(' - Reweighting: {}'.format(self._rw_num))
        self._rw_num += 1

        data = check_float(data)

        if data.shape != self.weights.shape:
            raise ValueError('Input data must have the same shape as the '
                             'initial weights.')

        self.weights *= (1.0 / (1.0 + np.abs(data) / (self.thresh_factor *
                         self.original_weights)))
794645
def add_stats(self, args):
        """"""
        bg_name, stats = args
        logger.debug("Stats: %s %s", bg_name, stats)
        
        for motif_id in stats.keys():
            if motif_id not in self.stats:
                self.stats[motif_id] = {}
        
            self.stats[motif_id][bg_name] = stats[motif_id]
660674
def toGray(img):
    '''
    
    '''
    return np.average(img, axis=-1, weights=(0.299,  # red
                                             0.587,  # green
                                             0.114)  # blue
                      ).astype(img.dtype)
476215
def display_stats(self):
        """"""
        for i, one_stage in enumerate(STAGE_NAME):
            second_in_stage = self.annot.time_in_stage(one_stage)
            time_in_stage = str(timedelta(seconds=second_in_stage))

            label = self.idx_stage_stats.itemAt(i,
                                                QFormLayout.FieldRole).widget()
            label.setText(time_in_stage)

        for i, one_qual in enumerate(QUALIFIERS):
            second_in_qual = self.annot.time_in_stage(one_qual, attr='quality')
            time_in_qual = str(timedelta(seconds=second_in_qual))

            label = self.idx_qual_stats.itemAt(i,
                                               QFormLayout.FieldRole).widget()
            label.setText(time_in_qual)
1313413
def analyzeWeightDistribution(weights,base):
  """"""
  weights = weights.numpy()
  weights = weights.reshape(28*28*500)
  bins = np.linspace(-0.1, 0.1, 50)
  nz = weights.nonzero()[0]
  nzw = weights[nz]
  print("zero before and after:", (weights==0).sum(), (nzw==0).sum())
  print("Small weights:", (abs(nzw)>0.0).sum(), (abs(nzw)<0.01).sum(), (abs(nzw)<0.02).sum(), (abs(nzw)<0.03).sum())
  plt.hist(nzw, bins, alpha=0.5, label='All cols')
  plt.legend(loc='upper right')
  plt.xlabel("Weight values")
  plt.ylabel("Frequency")
  plt.title("Histogram of non-zero weight values")
  plt.savefig(base+"_weights")
  plt.close()