示例#1
0
def fit_dbscan(data,
               eps,
               min_samples,
               normalize=True,
               show=True,
               juxta_cluster_indices_grouped=None,
               threshold_legend=None):
    X = np.transpose(data)

    if normalize:
        from sklearn.preprocessing import minmax_scale
        minmax_scale(X, feature_range=(-1, 1), axis=0, copy=False)

    db = DBSCAN(eps=eps, min_samples=min_samples).fit(X)
    core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
    core_samples_mask[db.core_sample_indices_] = True
    labels = db.labels_

    # Number of clusters in labels, ignoring noise if present.
    n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
    score = metrics.silhouette_score(X, labels, sample_size=5000)
    print('For eps={}, min_samples={}, estimated number of clusters={}'.format(
        eps, min_samples, n_clusters_))
    print("Silhouette Coefficient: {}".format(score))

    if show:
        pf.show_clustered_tsne(db, X, juxta_cluster_indices_grouped,
                               threshold_legend)

    return db, n_clusters_, labels, core_samples_mask, score
def fit_dbscan(data,
               eps,
               min_samples,
               show=True,
               juxta_cluster_indices_grouped=None,
               threshold_legend=None):
    X = np.transpose(data)

    db = DBSCAN(eps=eps, min_samples=min_samples).fit(X)
    core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
    core_samples_mask[db.core_sample_indices_] = True
    db_labels = db.labels_

    # Number of clusters in db_labels, ignoring noise if present.
    n_clusters_ = len(set(db_labels)) - (1 if -1 in db_labels else 0)
    score = metrics.silhouette_score(X, db_labels, sample_size=5000)
    print('For eps={}, min_samples={}, estimated number of clusters={}'.format(
        eps, min_samples, n_clusters_))
    print("Silhouette Coefficient: {}".format(score))

    if show:
        pf.show_clustered_tsne(db, X, juxta_cluster_indices_grouped,
                               threshold_legend)

    return db, n_clusters_, db_labels, core_samples_mask, score
    pf.plot_tsne(tsne,
                 juxta_cluster_indices_grouped,
                 cm=plt.cm.brg,
                 label_name='Peak size in uV',
                 label_array=(spike_thresholds_groups * 1e6).astype(int),
                 subtitle='T-sne of ' + str(channel_number[code]) +
                 'channels, ' + str(geometry_descriptions[code]))

# ----------------------------------------------------------------------------------------------------------------------
# CLUSTERING AND SCORING

db, n_clusters, labels, core_samples_mask, score = taf.fit_dbscan(
    tsne, 0.019, 40, normalize=True, show=True)

pf.show_clustered_tsne(db,
                       tsne,
                       juxta_cluster_indices_grouped=None,
                       threshold_legend=None)

taf.calculate_precision_recall_for_single_label_grouped(
    tsne,
    juxta_cluster_indices_grouped,
    n_clusters,
    labels,
    core_samples_mask,
    show_means=False)

# ----------------------------------------------------------------------------------------------------------------------
# Comparing tsnes with the manual clustering of the 128 channels

base_dir = r'D:\Data\George\Projects\SpikeSorting\Joana_Paired_128ch\2015-09-03'
tsne_dir = r'Analysis\klustakwik\threshold_6_5std'
f = plt.figure(3)
ax = f.add_subplot(111)


def show_frame(ind, *nothing):
    ax.clear()
    ax.imshow(grayscale_resized_video_array_frame_smoothed[ind, :].reshape(
        (80, 80)))


plt.figure(1)
pf.show_clustered_tsne(db,
                       tsne_result,
                       juxta_cluster_indices_grouped=None,
                       threshold_legend=None,
                       func_to_exec_on_pick=show_frame,
                       args_of_func_on_pick=[],
                       labels_on=True)

plt.figure(2)
pf.show_clustered_tsne(db,
                       tsne_result,
                       juxta_cluster_indices_grouped=None,
                       threshold_legend=None,
                       func_to_exec_on_pick=None,
                       args_of_func_on_pick=None,
                       labels_on=False)

jet = cm.jet
colors = jet(np.squeeze(preprocessing.normalize([labels], 'max')))
示例#5
0
    ax_pcs.clear()
    capture_cropped.set(cv2.CAP_PROP_POS_FRAMES, int(i * frames_to_bin))
    _, video_frame = capture_cropped.read()
    ax_video.imshow(video_frame)
    ax_pcs.imshow(reversed_pcs_video[i, :, :])


f_video = plt.figure(4)
ax_video = f_video.add_subplot(111)
f_pcs = plt.figure(5)
ax_pcs = f_pcs.add_subplot(111)

arg = [ax_video, ax_pcs]
pf.show_clustered_tsne(
    dbscan_result,
    tsne_video,
    func_to_exec_on_pick=show_video_and_pc_reconstructed_frames,
    args_of_func_on_pick=arg)


def show_video_label_on_spikes_tsne(label, ax2):
    ax2.clear()
    ax2.set_xlim(ax1.get_xlim())
    ax2.set_ylim(ax1.get_ylim())
    indices = np.squeeze(np.argwhere(video_tsne_labels == label))
    ax2.scatter(tsne_spikes[indices, 0],
                tsne_spikes[indices, 1],
                c=[label_colors[label]])
    return None