示例#1
0
def linlinheatmap(x, y, z, n_bins_x=30, n_bins_y=30, stat='mean', xlabel=r'$w$ (calls)', ylabel=r'$B$', title='Overlap as a function of Number of Calls and Burstiness\n' + r'$\langle O | w, B \rangle$', exp_f=1):
    """
    exp_f contains an "expansion factor". The linear part is ploted [0-1], but we use this expansion factor to depict different things (for instance, =25 for 24 hours)
    """

    bins_x = binner.Bins(float, min(x), max(x), 'lin', n_bins_x)
    bins_y = binner.Bins(float, min(y), max(y), 'lin', n_bins_y)
    bin_means, _, _ = binned_statistic_2d(x, y, z, statistic=stat, bins=[bins_x.bin_limits, bins_y.bin_limits])
    bin_means = np.nan_to_num(bin_means.T)
    extent = [bins_x.bin_limits[0], bins_x.bin_limits[-1], bins_y.bin_limits[0], bins_y.bin_limits[-1]]
    fig, ax = plt.subplots(1)
    cax = ax.imshow(bin_means, extent=extent, origin="lower")
    x_ticks = np.linspace(bins_x.bin_limits[0], bins_x.bin_limits[-1],
            len(bins_x.bin_limits))
    ax.set_xticks(x_ticks)
    ax.set_xticklabels([round(xb, 2) for xb in bins_x.bin_limits])
    y_ticks = np.linspace(bins_y.bin_limits[0], bins_y.bin_limits[-1],
            len(bins_y.bin_limits))
    ax.set_yticks(y_ticks)
    ax.set_yticklabels([round(yb, 2) for yb in bins_y.bin_limits])
    ax.set_ylabel(ylabel)
    ax.set_xlabel(xlabel)
    ax.set_title(title)
    fig.colorbar(cax)
    return fig, ax
示例#2
0
def loglogheatmap(x, y, z, factor_x=1.5, factor_y=1.45, stat='mean', xlabel=r'$w$ (calls)', ylabel=r'$\bar{\tau}$ (days)', title='Overlap as a function of and Number of Calls and Inter-event Time\n' + r'$\langle O | w, \bar{\tau} \rangle$'):
    x = x+1
    y = y+1
    bins_x = binner.Bins(float, min(x), max(x), 'log', factor_x)
    bins_y = binner.Bins(float, min(y), max(y), 'log', factor_y)
    bin_means, _, _, _ = binned_statistic_2d(x, y, z, statistic=stat, bins=[bins_x.bin_limits, bins_y.bin_limits])
    print(bin_means.shape)
    bin_means = np.nan_to_num(bin_means.T)
    extent = [bins_x.bin_limits[0], bins_x.bin_limits[-1], bins_y.bin_limits[0], bins_y.bin_limits[-1]]
    fig, ax = plt.subplots(1)
    cax = ax.imshow(bin_means, origin="lower") #extent=extent
    #x_ticks = np.linspace(bins_x.bin_limits[0], bins_x.bin_limits[-1],
    #        len(bins_x.bin_limits))
    #ax.set_xticks(x_ticks)
    ax.set_xticklabels(np.int16(bins_x.bin_limits))
    #y_ticks = np.linspace(bins_y.bin_limits[0], bins_y.bin_limits[-1],
    #        len(bins_y.bin_limits))
    #ax.set_yticks(y_ticks)
    ax.set_yticklabels(np.round(bins_y.bin_limits, 1))
    ax.set_ylabel(ylabel)
    ax.set_xlabel(xlabel)
    ax.set_title(title)
    fig.colorbar(cax)

    return fig, ax
    def _get_bins(self, x, bin_params, transfs):
        bins, bin_means = [], []
        for column, t in zip(x.iteritems(), transfs):
            col_type = bin_params[column[0]][0]
            if col_type == 'log' and t not in ['rank', 'raw']:
                b = binner.Bins(float, max(1, min(column[1])), max(column[1]), 'log', bin_params.get(column[0], 1.5)[1])
            else:
                if col_type != 'log':
                    n_bin = bin_params[column[0]][1]
                else:
                    n_bin = bin_params[column[0]][2]
                b = binner.Bins(float, min(column[1]), max(column[1]), 'lin', n_bin)
            bins.append(b.bin_limits)
            bin_means.append(b.centers)

        return bins, bin_means
示例#4
0
def plot_counts(values,
                ax=None,
                xscale='lin',
                yscale='lin',
                xParam=None,
                label=None,
                **kwargs):
    """
    Plots the probability density function of given values.

    Parameters
    ----------

    values : numpy ndarray
        the values for which the experimental pdf is computed
    ax : matplotlib axes object, optional
        axes to plot the figure in
    xscale : str
        'lin' or 'log', or ... (see binner.Bins for more details)
    yscale : str
        'lin' or 'log'
    xParam : different things, optional
        see binner.Bins for more details
    **kwargs : kwargs
        keyword arguments that will be passed to matplotlib hist
        function

    Returns
    -------
    fig : matplotlib Figure
        the parent figure of the axes
    ax : matplotlib Axes object
        the axes in which the pdf is plotted
    """
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    else:
        fig = ax.get_figure()

    indices = bins.get_reasonable_data_indices_for_binning(values,
                                                           xscale=xscale)

    prop_vals = values[indices]

    if xParam is None:
        if xscale == 'log':
            xParam = np.sqrt(2)  # just getting a nice factor of two...
        if xscale == 'lin':
            xParam = 50
    xbins = bins.Bins(float, np.min(prop_vals), np.max(prop_vals), xscale,
                      xParam)
    ax.hist(values, bins=xbins.bin_limits, normed=False, label=label, **kwargs)

    if 'log' in xscale:
        ax.set_xscale('log')
    if 'log' in yscale:
        ax.set_yscale('log')

    return fig, ax
示例#5
0
def loglinsbmap(x, y, z, x_factor=1.2, y_bins=40, stat='mean', fig=None, ax=None):
    """
    Heatmap using seaborn

    """
    bins_x = binner.Bins(float, min(x), max(x), 'log', x_factor)
    bins_y = binner.Bins(float, min(y), max(y), 'lin', y_bins)
    bin_means, x_edge, y_edge, _ = binned_statistic_2d(x, y, z, statistic=stat, bins=[bins_x.bin_limits, bins_y.bin_limits])
    bin_means = np.nan_to_num(bin_means.T)
    ker = np.ones((5, 5)); ker[3, 3] = 10.; ker = ker/34.
    bin_means = convolve(bin_means, ker) #Add weights
    bin_means = np.flip(bin_means, 0)
    if fig is not None:
        fig, ax = plt.subplots(1)
    y_ticks = np.flip([str(round(a, 2)) for a in y_edge], 0)
    x_ticks = np.array([str(int(a)) for a in x_edge])
    y_ticks[1:y_bins:2] = ''
    x_ticks[1:len(x_edge):2] = ''
    sb.heatmap(bin_means, ax=ax, xticklabels=x_ticks, yticklabels=y_ticks, robust=True)

    return fig, ax
示例#6
0
def powerlaw(values, main='', xlabel='', ylabel='', label='', fig=None, ax=None, factor=1.2, fit=True):

    """
Plot powerlaw distribution by binning values into log-bins defined by factor. This version also fits a powerlaw distribution of the form:
    P(x) = a*exp(-x/xc)(x+x0)**(-alpha)

Parameters:
    (values): list or 1D array of values to obtain a density from
    (main): matplotlib title
    (xlabel): x-axis label
    (ylabel): y-axis label
    (label): label to appear on the graph
    (fig): matplotlib figure
    (ax): matplotlib axis
    (factor): factor > 1 to log-bin values using verkko.

Returns:
    (fig, ax): matplotlib figure and axis
    (p1): vector with fit coefficientes: [log(a), alpha, x0, xc]

    """
    if not fig:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)
        ax.set_title(main)

    n_val = len(values)
    start = np.min(values)
    end = np.max(values)
    bins = binner.Bins(float, int(min(values)) + 1, max(values) + 1, 'log', factor)

    ind = bins.widths > 1
    counts, _,  _ = binned_statistic(values, values, statistic='count', bins=bins.bin_limits)
    counts = counts[ind]/float(n_val)
    bins_c = bins.centers[ind]
    ax.loglog(bins_c, counts, '.',  label=label)

    try:
        if fit:
            p1, succ = powerlaw_coeff(bins_c, counts)
            y_fit = 10**(powerlaw_func(p1, bins_c))
            ax.loglog(bins_c, y_fit, label= 'Fit: ' + r'$\gamma = $' + str(round(p1[1], 2)), color='blue')
        else:
            p1 = None
    except:
        p1 = None

    ax.legend(loc=0)
    return fig, ax, p1
示例#7
0
def linlinsbmap(x, y, z, x_bins=40, y_bins=40, stat='mean', z_label=r'$O_{ij}$'):
    """
    Heatmap using seaborn
    """

    bins_x = binner.Bins(float, min(x), max(x), 'lin', x_bins)
    bins_y = binner.Bins(float, min(y), max(y), 'lin', y_bins)
    bin_means, x_edge, y_edge, _ = binned_statistic_2d(x, y, z, statistic=stat, bins=[bins_x.bin_limits, bins_y.bin_limits])

    bin_means, x_edge, y_edge = remove_empty_bins(bin_means.T, x_edge, y_edge)

    bin_means = np.nan_to_num(bin_means)
    ker = np.ones((5, 5)); ker[3, 3] = 10.; ker = ker/34.
    bin_means = convolve(bin_means, ker) #Add weights
    bin_means = np.flip(bin_means, 0)

    y_ticks = np.flip([str(round(a, 2)) for a in y_edge], 0)
    x_ticks = np.array([str(round(a, 2)) for a in x_edge])
    x_ticks[1:x_bins:2] = ''
    y_ticks[1:y_bins:2] = ''
    g = sb.heatmap(bin_means, xticklabels=x_ticks, yticklabels=y_ticks, robust=True, cbar_kws={'label': z_label}, square=True)

    return g
示例#8
0
def lin_bin_plot(x, y, x_bins=20, xlabel=r'$B$', ylabel=r'$\langle O | B \rangle$', title='Overlap as a function of Burstiness', fig=None, ax=None, label=None, arg='-'):
    """
    Used for burstiness VS overlap
    """
    bins = binner.Bins(float, int(np.floor(min(x))), max(x), 'lin', x_bins)
    bin_means, _, _ = binned_statistic(x, y, bins=bins.bin_limits)
    bin_cts, _, _ = binned_statistic(x, y, bins=bins.bin_limits, statistic='count')
    bin_means[bin_cts < 5] = np.nan
    if not fig:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    ax.plot(bins.centers, bin_means, arg, label=label)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_title(title)
    return fig, ax
示例#9
0
def log_bin_plot(x, y, factor=1.82, col=None, fig=None, ax=None, xlabel=r'$\bar{\tau}$' + ' (days)', ylabel=r'$\langle O | \bar{\tau} \rangle$', title='Overlap as a Function of Mean Inter-Event time', label=None):
    """
    Used for mean waiting time and weight vs Overlap
    """
    bins = binner.Bins(float, min(x)+1, max(x), 'log', factor)
    bin_means, _, _ = binned_statistic(x, y, bins=bins.bin_limits)
    if not fig:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    ax.semilogx(bins.centers, bin_means, label=label)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_title(title)

    if col:
        cols, _, _ = binned_statistic(x, col, bins=bins.bin_limits)
        ax.scatter(bins.centers, bin_means, c=cols)

    return fig, ax
示例#10
0
def paper_heatmaps(x1, x2, x3, y, z, x1_bins=40, x2_bins=30, x3_bins=30, y_bins=40, stat='mean', cbar_kws={}):
    fig, axn = plt.subplots(1, 3, sharey=True)
    cbar_ax = fig.add_axes([.9, .2, .015, .7])

    bins_x1 = binner.Bins(float, min(x1), max(x1), 'lin', x1_bins)
    bins_x2 = binner.Bins(float, min(x2), max(x2), 'lin', x2_bins)
    bins_x3 = binner.Bins(float, min(x3), max(x3), 'lin', x3_bins)
    bins_y = binner.Bins(float, min(y), max(y), 'lin', y_bins)

    bin_means, x_edge, y_edge, _ = binned_statistic_2d(x1, y, z, statistic=stat, bins=[bins_x1.bin_limits, bins_y.bin_limits])
    bin_means, x_edge, y_edge = remove_empty_bins(bin_means.T, x_edge, y_edge)
    bin_means = np.nan_to_num(bin_means)
    ker = np.ones((5, 5)); ker[3, 3] = 10.; ker = ker/34.
    bin_means = convolve(bin_means, ker) #Add weights
    bin_means = np.flip(bin_means, 0)
    y_ticks = np.flip([str(round(a, 2)) for a in y_edge], 0)
    x_ticks_h = np.array([str(round(a, 1)) for a in x_edge])
    xl = len(x_edge)
    x_ticks = ['']*xl
    x_ticks[1:xl:4] = x_ticks_h[1:xl:4]
    y_ticks[1:y_bins:2] = ''
    y_ticks[1:y_bins:3] = ''
    g = sb.heatmap(bin_means, xticklabels=x_ticks, yticklabels=y_ticks, robust=True, square=True, ax=axn[0], cbar=False, vmin=0, vmax=.12)
    g.set(xlabel='(a)    ' + r'$Rank(N^E_{ij})$', ylabel=r'$Rank(w_{ij})$')
    #g.set_tickxlabels(rotation=40)

    bin_means, x_edge, y_edge, _ = binned_statistic_2d(x2, y, z, statistic=stat, bins=[bins_x2.bin_limits, bins_y.bin_limits])
    bin_means, x_edge, y_edge = remove_empty_bins(bin_means.T, x_edge, y_edge)
    bin_means = np.nan_to_num(bin_means)
    ker = np.ones((5, 5)); ker[3, 3] = 10.; ker = ker/34.
    bin_means = convolve(bin_means, ker) #Add weights
    bin_means = np.flip(bin_means, 0)
    y_ticks = np.flip([str(round(a, 2)) for a in y_edge], 0)
    x_ticks_h = np.array([str(round(a, 1)) for a in x_edge])
    xl = len(x_edge)
    x_ticks = ['']*xl
    x_ticks[1:xl:4] = x_ticks_h[1:xl:4]
    y_ticks[1:y_bins:2] = ''
    y_ticks[1:y_bins:3] = ''
    g = sb.heatmap(bin_means, xticklabels=x_ticks, yticklabels=y_ticks, robust=True, square=True, ax=axn[1], cbar=False, vmin=0, vmax=.12)
    g.set(xlabel='(b)    ' + r'$Rank(JSD_{ij})$')

    bin_means, x_edge, y_edge, _ = binned_statistic_2d(x3, y, z, statistic=stat, bins=[bins_x3.bin_limits, bins_y.bin_limits])
    bin_means, x_edge, y_edge = remove_empty_bins(bin_means.T, x_edge, y_edge)
    bin_means = np.nan_to_num(bin_means)
    ker = np.ones((5, 5)); ker[3, 3] = 10.; ker = ker/34.
    bin_means = convolve(bin_means, ker) #Add weights
    bin_means = np.flip(bin_means, 0)
    y_ticks_h = np.flip([str(round(a, 2)) for a in y_edge], 0)
    x_ticks_h = np.array([str(round(a, 1)) for a in x_edge])
    xl, yl = len(x_edge), len(y_edge)
    x_ticks = ['']*xl
    y_ticks = ['']*yl
    x_ticks[1:xl:4] = x_ticks_h[1:xl:4]
    y_ticks[1:yl:3] = y_ticks_h[1:yl:3]
    g = sb.heatmap(bin_means, xticklabels=x_ticks, yticklabels=y_ticks, robust=True, square=True, ax=axn[2], cbar=True, vmin=0, vmax=.12, cbar_ax=cbar_ax, cbar_kws={'label': r'$O_{ij}$'})
    g.set(xlabel='(c)    ' + r'$Rank(B_{ij})$')
    fig.tight_layout(rect=[0, .03, .9, .98])
    fig.savefig('full_run/figs/abstract1.pdf')

    return g