# Run t-sne
path_to_save_tmp_data = tsne_video_path
perplexity = 200
theta = 0.2
iterations = 5000
gpu_mem = 0.9
eta = 200
early_exaggeration = 4.0
seed = 400000
verbose = 3
randseed = 0
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)
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)
# T-sne with my conda package
path = r'D:\Data\George\Projects\SpikeSorting\Joana_Paired_128ch\2015-09-03\Analysis\klustakwik\threshold_6_5std'
kwx_file_path = os.path.join(path, r'threshold_6_5std.kwx')
video = os.path.join(path, r'video')
indices_of_data_for_tsne = None #range(40000)
seed = 0
perplexity = 100.0
theta = 0.2
learning_rate = 200.0
iterations = 5000
gpu_mem = 0.2
no_dims = 2
tsne = tsne_spikes.t_sne_spikes(kwx_file_path=kwx_file_path, hdf5_dir_to_pca=r'channel_groups/0/features_masks',
                                  mask_data=True, path_to_save_tmp_data=path,
                                  indices_of_spikes_to_tsne=indices_of_data_for_tsne, use_scikit=False,
                                  perplexity=perplexity, theta=theta, no_dims=no_dims, eta=learning_rate,
                                  iterations=iterations, seed=seed, verbose=2, gpu_mem=gpu_mem)


# C++ wrapper t-sne using CPU
t0 = time.time()
perplexity = 50.0
theta = 0.2
learning_rate = 200.0
iterations = 5000
gpu_mem = 0
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)