def plot_clustering(container,
                    in_dir,
                    isi_hist,
                    ac_hist,
                    dend,
                    cluster,
                    opt_end=""):
    dend_idxs = dend["leaves"][::-1]
    sorted_isi = isi_hist[dend_idxs]
    sorted_ac = ac_hist[dend_idxs]

    ax1, fig1 = nc_plot._make_ax_if_none(None)
    cmap = sns.cubehelix_palette(8,
                                 start=0.5,
                                 rot=-.75,
                                 dark=0,
                                 light=.95,
                                 reverse=True)
    # cmap = sns.color_palette("Blues")
    sns.heatmap(sorted_isi, ax=ax1, yticklabels=5, xticklabels=10, cmap=cmap)
    ax1.set_ylim([sorted_isi.shape[0], 0])
    ax1.axvline(x=16, c="r", ls="--")
    plot_loc = os.path.join(in_dir, "nc_plots",
                            "logisi_hist" + opt_end + ".png")
    fig1.savefig(plot_loc, dpi=400)
    fig1.clear()
    ax1, fig1 = nc_plot._make_ax_if_none(None)
    sns.heatmap(sorted_ac, ax=ax1, yticklabels=5, xticklabels=5, cmap=cmap)
    ax1.axvline(x=6, c="r", ls="--")
    ax1.set_ylim([sorted_ac.shape[0], 0])
    plot_loc = os.path.join(in_dir, "nc_plots", "ac_hist" + opt_end + ".png")
    fig1.savefig(plot_loc, dpi=400)
    fig1.clear()
def ward_clustering(data,
                    in_dir,
                    plot_dim1=0,
                    plot_dim2=1,
                    opt_end="",
                    s_color=False):
    """
    Perform heirarchical clustering using ward's method

    Params
    ------
    data - input data array
    in_dir - where to save the result to
    plot_dim1 - the PCA dimension to plot
    plot_dim2 - the other PCA dimesion to plot
    """
    ax, fig = nc_plot._make_ax_if_none(None)
    if s_color:
        shc.set_link_color_palette(
            ['#4A777A', "#6996AD", "#82CFFD", "#3579DC"])
    else:
        shc.set_link_color_palette(["k"])

    atc = '#bcbddc'
    dend = shc.dendrogram(shc.linkage(data,
                                      method="ward",
                                      optimal_ordering=True),
                          ax=ax,
                          above_threshold_color=atc,
                          orientation="right",
                          no_labels=True)
    plot_loc = os.path.join(in_dir, "nc_plots", "dendogram" + opt_end + ".png")
    fig.savefig(plot_loc, dpi=400)
    shc.set_link_color_palette(None)

    cluster = AgglomerativeClustering(n_clusters=2,
                                      affinity="euclidean",
                                      linkage="ward")
    cluster.fit_predict(data)

    ax, fig = nc_plot._make_ax_if_none(None)
    markers = list(map(lambda a: "Burst" if a else "Regular", cluster.labels_))
    # ax.scatter(data[:, plot_dim1], data[:, plot_dim2],
    #            c=markers)
    sns.scatterplot(data[:, plot_dim1],
                    data[:, plot_dim2],
                    ax=ax,
                    style=markers,
                    hue=markers)
    plot_loc = os.path.join(in_dir, "nc_plots", "PCAclust" + opt_end + ".png")
    fig.savefig(plot_loc, dpi=400)

    return cluster, dend
def calculate_isi_hist(container, in_dir, opt_end="", s_color=False):
    """Calculate a matrix of isi_hists for each unit in a container"""
    ax1, fig1 = nc_plot._make_ax_if_none(None)
    isi_hist_matrix = np.empty((len(container), 60), dtype=float)
    color = iter(cm.gray(np.linspace(0, 0.8, len(container))))
    for i, ndata in enumerate(container):
        res_isi, bins = log_isi(ndata)
        isi_hist_matrix[i] = res_isi
        bin_centres = bins[:-1] + np.mean(np.diff(bins)) / 2
        c = next(color) if s_color else "k"
        ax1.plot(bin_centres, res_isi, c=c)
        ax1.set_xlim([-3, 1])
        ax1.set_xticks([-3, -2, -1, 0])
    ax1.axvline(x=np.log10(0.006), c="r", ls="--")

    plot_loc = os.path.join(in_dir, "nc_plots", "logisi" + opt_end + ".png")
    fig1.savefig(plot_loc, dpi=400)

    return isi_hist_matrix
def calculate_auto_corr(container, in_dir, opt_end="", s_color=False):
    """Calculate a matrix of autocorrs for each unit in a container"""
    ax1, fig1 = nc_plot._make_ax_if_none(None)
    auto_corr_matrix = np.empty((len(container), 20), dtype=float)
    color = iter(cm.gray(np.linspace(0, 0.8, len(container))))
    for i, ndata in enumerate(container):
        auto_corr_data = ndata.isi_auto_corr(bins=1, bound=[0, 20])
        auto_corr_matrix[i] = (auto_corr_data["isiCorr"] /
                               ndata.spike.get_unit_stamp().size)
        bins = auto_corr_data['isiAllCorrBins']
        bin_centres = bins[:-1] + np.mean(np.diff(bins)) / 2
        c = next(color) if s_color else "k"
        ax1.plot(bin_centres / 1000, auto_corr_matrix[i], c=c)
        ax1.set_xlim([0.000, 0.02])
        ax1.set_xticks([0.000, 0.005, 0.01, 0.015, 0.02])
    ax1.axvline(x=0.006, c="r", ls="--")

    plot_loc = os.path.join(in_dir, "nc_plots", "autocorr" + opt_end + ".png")
    fig1.savefig(plot_loc, dpi=400)
    return auto_corr_matrix