Plot discrete PDFs

    >>> df = pd.DataFrame(pd.np.random.randn(99,3), columns=list('ABC'))
    >>> df['Class'] = pd.np.array((pd.np.matrix([1,1,1])*pd.np.matrix(df).T).T > 0)
    >>> len(hist(df, verbosity=0, class_column='Class'))
    3
166041
def hist(table, field=-1, class_column=None,
         title='', verbosity=2, **kwargs):
    
    field = fuzzy_index_match(table, field)
    if not isinstance(table, (pd.DataFrame, basestring)):
        try:
            table = make_dataframe(table.objects.filter(**{field + '__isnull': False}))
        except:
            table = table
    # labels = get_column_labels(table)
    try:
        table = table[pd.notnull(table[field])]
    except:
        pass

    series_labels = []
    if class_column is not None:
        series_labels = sorted(set(table[class_column]))
    labels = [str(c) for c in series_labels] + ['all']

    default_kwargs = {
        'normed': False,
        'histtype': 'bar',
        'color': seaborn.color_palette(),
        'label': labels,
        'log': True,
        'bins': 10,
        }
    default_kwargs.update(kwargs)
    num_colors = len(default_kwargs['color'])
    num_labels = len(default_kwargs['label'])
    default_kwargs['color'] = [default_kwargs['color'][i % num_colors] for i in range(num_labels)]

    if not title:
        title = '{} vs. {}'.format(titlecase(str(field).replace('_', ' ')),
                                   titlecase(str(class_column).replace('_', ' ')))
    if verbosity > 0:
        print('Plotting histogram titled: {}'.format(title))
    if verbosity > 1:
        print('histogram configuration: {}'.format(default_kwargs))
    x = [table[(table[class_column].isnull() if pd.isnull(c) else table[class_column] == c)]
         [field].values for c in series_labels]
    x += [table[field].values]
    if not default_kwargs['normed']:
        default_kwargs['weights'] = [pd.np.ones_like(x_c) / float(len(x_c)) for x_c in x]
    elif isinstance(default_kwargs['normed'], int) and default_kwargs['normed'] < 0:
        default_kwargs['normed'] = 0

    bins = default_kwargs['bins']
    # FIXME: x log scaling doesn't work
    if False and default_kwargs['log'] and isinstance(bins, int):
        max_x = max(pd.np.max(x_c) for x_c in x)
        min_x = min(pd.np.min(x_c) for x_c in x)
        if pd.isnull(min_x) or not(min_x):
            min_x = max_x / 10.
        default_kwargs['bins'] = pd.np.logspace(min_x, max_x, bins)

    fig, ax = plt.subplots()
    ans = plt.hist(x, **default_kwargs)
    # FIXME: x log scaling doesn't work
    if False and default_kwargs['log'] and isinstance(bins, int):
        ax.set_xscale('log')
    if verbosity > 1:
        plt.legend(default_kwargs['label'])
        try:
            plt.show(block=False)
        except:
            plt.show()

    plt.title(title)
    plt.xlabel(titlecase(field.replace('_', ' ')))
    if 'weights' in default_kwargs:
        plt.ylabel('Normalized Frequency or Probability')
    elif default_kwargs['normed']:
        plt.ylabel('Normalized Count')
    else:
        plt.ylabel('Count')
    if verbosity > 2:
        plt.savefig(make_timestamp() + '--' + title.replace(' ', '-') + '.png', transparent=True)
    return ans
1102341
def describe(df, dtype=None):
    
    if dtype is None:
        dtype = {}
    print('Number of datapoints: {datapoints}'.format(datapoints=len(df)))
    column_info, column_info_meta = _get_column_info(df, dtype)

    if len(column_info['int']) > 0:
        _describe_int(df, column_info)

    if len(column_info['float']) > 0:
        _describe_float(df, column_info)

    if len(column_info['category']) > 0:
        _describe_category(df, column_info, column_info_meta)

    if len(column_info['time']) > 0:
        _describe_time(df, column_info, column_info_meta)

    if len(column_info['other']) > 0:
        _describe_other(df, column_info, column_info_meta)

    column_types = {}
    for column_type, columns in column_info.items():
        for column_name in columns:
            if column_type == 'other':
                column_type = 'str'
            column_types[column_name] = column_type
    return column_types
1142082
def toPandas(self):
        
        assert pd is not None
        nindices = self.getNumIndices()
        headers = self.getHeaders()
        columns = {
            header: list(self.getColumn(header))
            for header in headers[nindices:]
        }
        index = zip(*[
            list(self.getColumn(header))
            for header in headers[:nindices]
        ])
        index = [key if len(key) > 1 else key[0] for key in index]
        if index == []:
            return pd.DataFrame(columns, index=None)
        else:
            return pd.DataFrame(columns, index=index)
733039
def sample_discrete_from_log(p_log,axis=0,dtype=np.int32):
    ''
    cumvals = np.exp(p_log - np.expand_dims(p_log.max(axis),axis)).cumsum(axis) # cumlogaddexp
    thesize = np.array(p_log.shape)
    thesize[axis] = 1
    randvals = random(size=thesize) * \
            np.reshape(cumvals[[slice(None) if i is not axis else -1
                for i in range(p_log.ndim)]],thesize)
    return np.sum(randvals > cumvals,axis=axis,dtype=dtype)
988210
def print_matrix(X, decimals=1):
    """"""
    for row in np.round(X, decimals=decimals):
        print(row)
26298
def _pdf(self, **kwargs):
        
        if kwargs not in self:
            return 0.
        return self._norm * \
            self._dfunc(numpy.array([kwargs[p] for p in self._params])).prod()
1236232
def plot(data: Dict[str, np.array], fields: List[str] = None, *args, **kwargs):
    
    if plt is None:
        return

    if fields is None:
        fields = ['x', 'y', 'm', 'c']
    labels = []
    lines = []
    for field in fields:
        if min(data[field].shape) > 0:
            f_lines = plt.plot(data['t'], data[field], *args, **kwargs)
            lines.extend(f_lines)
            labels.extend(data['labels'][field])
    plt.legend(lines, labels, ncol=2, loc='best')
    plt.xlabel('t, sec')
    plt.grid()
552780
def create_histogram(df):
    
    fig = Figure("/mg/histogram/", "mg_histogram")
    fig.layout.set_size(width=450, height=200)
    fig.layout.set_margin(left=40, right=40)
    fig.graphics.animate_on_load()

    # Make a histogram with 20 bins
    return Histogram(df, fig, "value", 20, init_params={"Data": "Steps"})
25092
def plot_all(*args, **kwargs):
    '''
    
    '''
    dfs = do_all(*args, **kwargs)
    ps = []
    for line in dfs:
        f, df, config = line
        df.plot(title=config['name'])
        ps.append(df)
    return ps
1060889
def histogram(data, bins=None, binsize=1., min=None, max=None, rev=False, use_weave=True, verbose=0):
    

    if not have_scipy:
        use_weave=False
     
    dowhere=False
    s = data.argsort()
    if min is not None:
        dmin = min
        dowhere=True
    else:
        dmin = data[s[0]]

    if max is not None:
        dmax = max
        dowhere=True
    else:
        dmax = data[s[-1]]

    bsize = float(binsize)
#    except TypeError :
#        print '[Warning] TypeError reached - type {0}'.format(type(binsize[0]))
#        bsize = binsize
#    finally : pass

    if dowhere:
        # where will preserve order, so subscript with s
        w,=numpy.where( (data[s] >= dmin) & (data[s] <= dmax) )
        if w.size == 0:
            raise ValueError("No data in specified min/max range\n")
        s = s[w]

    nbin = numpy.int64( (dmax-dmin)/bsize ) + 1

    if bins is not None :
        s = bins
        nbin = len(s)

    if rev:
        revsize = s.size + nbin+1
        revind = numpy.zeros(revsize, dtype='i8')
    else:
        # this is just a dummy variable
        revind=numpy.zeros(1, dtype='i8')
    hist = numpy.zeros(nbin, dtype='i8')

    # populate the array from nbin+1:nbin+1+s.size
    # with the sort indices.  Simultaneosly record bin
    # edges at the beginning of reverse indices

    if use_weave:
        _weave_dohist(data, s, bsize, hist, revind, dorev=rev,verbose=verbose)
    else:
        _dohist(data, s, bsize, hist, revind, dorev=rev)

    if rev:
        return hist, revind
    else:
        return hist
435966
def output_pdf_plot(core_result, spid, models, options, fit_results):
    
    # PDF/PMF
    hist_bins = 11
    emp_hist, edges = np.histogram(core_result['y'].values, hist_bins,
                                   normed=True)
    x = (np.array(edges[:-1]) + np.array(edges[1:])) / 2
    df = pd.DataFrame({'x': x, 'empirical': emp_hist})

    def calc_func(model, df, shapes):
        try:
            return eval("mod.%s.pmf(np.floor(df['x']), *shapes)" % model)
        except:
            return eval("mod.%s.pdf(df['x'], *shapes)" % model)

    plot_exec_str = "ax.bar(df['x']-width/2, emp, width=width, color='gray')"

    _save_table_and_plot(spid, models, options, fit_results, 'data_pred_pdf',
                         df, calc_func, plot_exec_str)
1357494
def plot_discrete(self, show=False):
        
        import matplotlib.pyplot as plt
        from mpl_toolkits.mplot3d import Axes3D  # NOQA
        fig = plt.figure()
        axis = fig.add_subplot(111, projection='3d')
        for discrete in self.discrete:
            axis.plot(*discrete.T)
        if show:
            plt.show()
138178
def hist2d(self, da, **kwargs):
        """"""
        if self.value is None or self.value == 'counts':
            normed = False
        else:
            normed = True
        y = da.values
        x = da.coords[da.dims[0]].values
        counts, xedges, yedges = np.histogram2d(
            x, y, normed=normed, **kwargs)
        if self.value == 'counts':
            counts = counts / counts.sum().astype(float)
        return counts, xedges, yedges
935677
def diri(table):
    
    t = []
    for i in table:
        a = [j + 1 for j in i]
        t.append(np.ndarray.tolist(np.random.mtrand.dirichlet(a)))
    return t
30649
def to_df(figure):
    
    dfs=[]
    for trace in figure['data']:
        if 'scatter' in trace['type']:
            try:
                if type(trace['x'][0])==float:
                    index=trace['x']
                else:
                    index=pd.to_datetime(trace['x'])
            except:
                index=trace['x']
            if 'marker' in trace:
                d={}
                if 'size' in trace['marker']:
                    size=trace['marker']['size']
                    if type(size)!=list:
                        size=[size]*len(index)
                    d['size']=size
                if 'text' in trace:
                    d['text']=trace['text']
                if 'name' in trace:
                    name=trace['name']
                    if type(name)!=list:
                        name=[name]*len(index)
                    d['categories']=name
                d['y']=trace['y']
                d['x']=trace['x']
                if 'z' in trace:
                    d['z']=trace['z']
                df=pd.DataFrame(d)
            else:
                df=pd.Series(trace['y'],index=index,name=trace['name'])
            dfs.append(df)
        elif trace['type'] in ('heatmap','surface'):
            df=pd.DataFrame(trace['z'].transpose(),index=trace['x'],columns=trace['y'])
            dfs.append(df)
        elif trace['type'] in ('box','histogram'):
            vals=trace['x'] if 'x' in trace else trace['y']
            df=pd.DataFrame({trace['name']:vals})
            dfs.append(df)
    if max(list(map(len,dfs)))==min(list(map(len,dfs))):
        if len(dfs)==1:
            return dfs[0]
        else:
            if type(dfs[0])==pd.core.series.Series:
                return pd.concat(dfs,axis=1)                
            if all(dfs[0].columns==dfs[1].columns):
                    return pd.concat(dfs,axis=0)
            else:
                return pd.concat(dfs,axis=1)
    else:
        try:
            return pd.concat(dfs)
        except:
            return dfs
11371
def _construct_df(pdb_lines):
        """"""
        valids = tuple(pdb_records.keys())
        line_lists = {r: [] for r in valids}
        line_lists['OTHERS'] = []
        for line_num, line in enumerate(pdb_lines):
            if line.strip():
                if line.startswith(valids):
                    record = line[:6].rstrip()
                    line_ele = ['' for _ in range(len(
                        pdb_records[record]) + 1)]
                    for idx, ele in enumerate(pdb_records[record]):
                        line_ele[idx] = (line[ele['line'][0]:ele['line'][1]]
                                         .strip())
                    line_ele[-1] = line_num
                    line_lists[record].append(line_ele)
                else:
                    line_lists['OTHERS'].append([line[:6].rstrip(),
                                                line[6:-1].rstrip(), line_num])

        dfs = {}
        for r in line_lists.items():
            df = pd.DataFrame(r[1], columns=[c['id'] for c in
                                             pdb_records[r[0]]] + ['line_idx'])
            for c in pdb_records[r[0]]:
                try:
                    df[c['id']] = df[c['id']].astype(c['type'])
                except ValueError:
                    # expect ValueError if float/int columns are empty strings
                    df[c['id']] = pd.Series(np.nan, index=df.index)

            dfs[r[0]] = df
        return dfs
720065
def plot(self, axis, ith_plot, total_plots, limits):
        
        print(self.plot_type_str.upper() + " plot")
        print("%5s %9s  %s" % ("id", " #points", "group"))

        for idx, group in enumerate(self.groups):
            print("%5s %9s  %s" % (idx + 1, len(self.groups[group]), group))

        print('')

        datasets = []
        colors = []
        minx = np.inf
        maxx = -np.inf

        for idx, group in enumerate(self.groups):
            x = date2num([logevent.datetime
                          for logevent in self.groups[group]])
            minx = min(minx, min(x))
            maxx = max(maxx, max(x))
            datasets.append(x)
            color, marker = self.color_map(group)
            colors.append(color)

        if total_plots > 1:
            # if more than one plot, move histogram to twin axis on the right
            twin_axis = axis.twinx()
            twin_axis.set_ylabel(self.ylabel)
            axis.set_zorder(twin_axis.get_zorder() + 1)  # put ax ahead of ax2
            axis.patch.set_visible(False)  # hide the 'canvas'
            axis = twin_axis

        n_bins = max(1, int((maxx - minx) * 24. * 60. * 60. / self.bucketsize))
        if n_bins > 1000:
            # warning for too many buckets
            print("warning: %i buckets, will take a while to render. "
                  "consider increasing --bucketsize." % n_bins)

        n, bins, artists = axis.hist(datasets, bins=n_bins, align='mid',
                                     log=self.logscale,
                                     histtype="barstacked"
                                     if self.barstacked else "bar",
                                     color=colors, edgecolor="none",
                                     linewidth=0, alpha=0.8, picker=True,
                                     label=map(str, self.groups.keys()))

        # scale current y-axis to match min and max values
        axis.set_ylim(np.min(n), np.max(n))

        # add meta-data for picking
        if len(self.groups) > 1:
            for g, group in enumerate(self.groups.keys()):
                for i in range(len(artists[g])):
                    artists[g][i]._mt_plot_type = self
                    artists[g][i]._mt_group = group
                    artists[g][i]._mt_n = n[g][i]
                    if self.barstacked:
                        artists[g][i]._mt_n -= (n[g - 1][i] if g > 0 else 0)

                    artists[g][i]._mt_bin = bins[i]
        else:
            for i in range(len(artists)):
                artists[i]._mt_plot_type = self
                artists[i]._mt_group = group
                artists[i]._mt_n = n[i]
                artists[i]._mt_bin = bins[i]

        return artists