raw_data = ioep.load_raw_data(filename=filename_raw_data, numchannels=num_ivm_channels, dtype=amp_dtype)
filename_kl_data = join(analysis_folder, r'klustakwik_cell{}\raw_data_klusta.dat'.format(cell))
iokl.make_dat_file(raw_data=raw_data.dataMatrix, num_channels=num_ivm_channels, filename=filename_kl_data)



# Run t-sne
kwx_file_path = join(analysis_folder, 'klustakwik_cell{}'.format(cell),
                     r'threshold_6_5std/threshold_6_5std.kwx')
perplexity = 100
theta = 0.2
iterations = 2000
gpu_mem = 0.8
eta = 200
early_exaggeration = 4.0
indices_of_spikes_to_tsne = None#range(spikes_to_do)
seed = 100000
verbose = 2
tsne = tsne_spikes.t_sne_spikes(kwx_file_path, hdf5_dir_to_pca=r'channel_groups/0/features_masks',
                                mask_data=True, perplexity=perplexity, theta=theta, iterations=iterations,
                                gpu_mem=gpu_mem, seed=seed, eta=eta, early_exaggeration=early_exaggeration,
                                indices_of_spikes_to_tsne=indices_of_spikes_to_tsne, verbose=verbose)


# Load t-sne
filename = 't_sne_results_100per_200lr_02theta_2000its_100kseed.npy'
tsne = np.load(join(analysis_folder, 'klustakwik_cell{}'.format(cell), 'threshold_6_5std', filename))


fig, ax = pf.plot_tsne(tsne[:, :seed], color='b')
pf.plot_tsne(tsne[:, seed:(5*seed)], color='g', axes=ax)
pc_features_tsne_selected_indices = TSNE.t_sne(pc_features_sparse_flatten[indices_all[:-10]],
                              perplexity=20, theta=0.01,
                              files_dir=os.path.join(kilosort_path, 'tsne'),
                              results_filename='tsne_pc_features_blob.npy',
                              gpu_mem=0.8, iterations=2000, randseed=1)


pc_and_template_features_sparse = np.concatenate((pc_features_sparse_flatten, template_features_sparse), axis=1)

pc_and_template_features_tsne_selected_indices = TSNE.t_sne(pc_and_template_features_sparse[indices_all],
                              perplexity=100, theta=0.2,
                              files_dir=os.path.join(kilosort_path, 'tsne'),
                              results_filename='tsne_pc_template_features_blob.npy',
                              gpu_mem=0.8, iterations=2000, randseed=1)

pf.plot_tsne(np.transpose(pc_and_template_features_tsne_selected_indices), legend_on=False,
             subtitle='T-sne', cm=plt.cm.coolwarm, sizes=[2, 8])

plt.bar(edges[:-1], hist, width=30)

result = plt.hist(diffs, bins=autocor_bin_number)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.scatter(tsne_indices_all[:, 0],
            tsne_indices_all[:, 1],
           color='blue')
ax.scatter(tsne_indices_all[local_indices_sorted_to_distance[len(middle_bin_size):], 0],
            tsne_indices_all[local_indices_sorted_to_distance[len(middle_bin_size):], 1],
           color='red')
tsne = tsne_spikes.t_sne_spikes(kwx_file_path, path_to_save_tmp_data=path_to_save_tmp_data,
                                hdf5_dir_to_pca=r'channel_groups/1/features_masks',
                                mask_data=True, perplexity=perplexity, theta=theta, iterations=iterations,
                                gpu_mem=gpu_mem, seed=seed, eta=eta, early_exaggeration=early_exaggeration,
                                verbose=verbose, indices_of_spikes_to_tsne=range(spikes_used), randseed=randseed)



# Load t-sne results
tsne = TSNE.load_tsne_result(results_dir, 'result_tsne40K_com46k_p500_it1k_th05_eta200.dat')
tsne = np.transpose(tsne)
tsne = np.load(join(results_dir, 't_sne_results_s130k_100per_200lr_02theta.npy'))


# 2D plot
pf.plot_tsne(tsne, labels_dict=spikes_labeled_dict, subtitle='T-sne of first 130k spikes from Synthetic Data',
             label_name='"Cell" No', cm=plt.cm.jet, markers=['.', '^'], sizes=[3, 20])
pf.plot_tsne(tsne, labels_dict=None, subtitle='T-sne of 86000 spikes from Synthetic Data, not labeled', label_name=None)



#--------------------------------------------------------------------------------------
# CHECK QUALITY OF FIT
# 1) DBSCAN
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_
perplexity = 1000.0
theta = 0.2
learning_rate = 200.0
iterations = 2000
gpu_mem = 0.8
t_tsne = tsne_bhcuda.t_sne(data_for_tsne,
                           files_dir=r'D:\Data\George\Projects\SpikeSorting\Joana_Paired_128ch\2015-09-03\Analysis\tsne_results',
                           no_dims=2, perplexity=perplexity, eta=learning_rate, theta=theta,
                           iterations=iterations, gpu_mem=gpu_mem, randseed=-1, verbose=3)
t_tsne = np.transpose(t_tsne)
t1 = time.time()
print("CUDA t-sne took {} seconds, ({} minutes), for {} spikes".format(t1-t0, (t1-t0)/60, up_to_extra_spike))


#  2D plot
pf.plot_tsne(t_tsne, juxta_cluster_indices_grouped, subtitle='T-sne', cm=plt.cm.coolwarm, label_name='Peak size in uV',
             label_array=(spike_thresholds_groups*1e6).astype(int), sizes=[2, 15])
pf.plot_tsne(t_tsne, subtitle='T-sne of 129000 spikes from Juxta Paired recordings, not labeled',
             label_name=None,
             label_array=None)



#  3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
s = 10
c = ['r', 'g', 'c', 'm', 'y', 'k', 'w', 'b']
ax.scatter(t_tsne[0], t_tsne[1], t_tsne[2], s=3)
for g in np.arange(1, num_of_spike_groups+1):
    ax.scatter(t_tsne[0][juxta_cluster_indices_grouped[g]], t_tsne[1][juxta_cluster_indices_grouped[g]],
               t_tsne[2][juxta_cluster_indices_grouped[g]], s=s, color=c[g-1])
kwik_file = join(basic_dir, geometry_dir.format(channel_number[code], geometry_descriptions[code]),
                 'threshold_6_5std.kwik')
juxta_cluster_indices_grouped, spike_thresholds_groups = taf.create_juxta_label(kwik_file,
                                                                                spike_thresholds=spike_thresholds,
                                                                                adc_channel_used=adc_channel_used,
                                                                                adc_dtype=adc_dtype,
                                                                                inter_spike_time_distance=inter_spike_time_distance,
                                                                                amp_gain=amp_gain,
                                                                                num_of_raw_data_channels=None,
                                                                                spike_channels=None,
                                                                                verbose=True)
# ----------------------------------------------------------------------------------------------------------------------
# PLOTTING

pf.plot_tsne(tsne)


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))



# Show all t-snes
for code in geometry_codes:
    print('---'+code+'---')
    tsne = load_tsne(code)
    kwik_file = join(basic_dir, geometry_dir.format(channel_number[code], geometry_descriptions[code]),
                 'threshold_6_5std.kwik')
    juxta_cluster_indices_grouped, spike_thresholds_groups = taf.create_juxta_label(kwik_file,
# Run t-sne
kwx_file_path = join(
    analysis_folder, "klustakwik_cell{}".format(good_cells[0]), r"threshold_6_5std/threshold_6_5std.kwx"
)
perplexity = 100
theta = 0.2
iterations = 2000
gpu_mem = 0.8
eta = 200
early_exaggeration = 4.0
indices_of_spikes_to_tsne = range(34000)
seed = 0
verbose = 2
tsne = tsne_spikes.t_sne_spikes(
    kwx_file_path,
    hdf5_dir_to_pca=r"channel_groups/0/features_masks",
    mask_data=True,
    perplexity=perplexity,
    theta=theta,
    iterations=iterations,
    gpu_mem=gpu_mem,
    seed=seed,
    eta=eta,
    early_exaggeration=early_exaggeration,
    indices_of_spikes_to_tsne=indices_of_spikes_to_tsne,
    verbose=verbose,
)


pf.plot_tsne(tsne)