results['h1'] = barcodes[1]

# H2. Need to subsample points for computational tractability if
# number of points is large (can go higher but very slow)
if len(rates) > 1500:
    idx = np.random.choice(np.arange(len(rates)), 1500, replace=False)
    H2_rates = rates[idx]
else:
    H2_rates = rates

barcodes = tda(H2_rates, maxdim=2, coeff=2)['dgms']
results['h2'] = barcodes[2]

# save
gff.save_pickle_file(
    results, save_dir + '%s_%s%s_ph_barcodes.p' % (session, state,
                                                   ('_thresholded' * thrsh)))

# If plotting from a saved file, uncomment this and replace with appropriate file.
# results = gff.load_pickle_file(gen_params['results_dir'] + '2019_03_22_tda/Mouse28-140313_Wake_ph_barcodes.p')

if plot_barcode:
    col_list = ['r', 'g', 'm', 'c']
    h0, h1, h2 = results['h0'], results['h1'], results['h2']
    # replace the infinity bar (-1) in H0 by a really large number
    h0[~np.isfinite(h0)] = 100
    # Plot the longest barcodes only
    plot_prcnt = [99, 98, 90]  # order is h0, h1, h2
    to_plot = []
    for curr_h, cutoff in zip([h0, h1, h2], plot_prcnt):
        bar_lens = curr_h[:, 1] - curr_h[:, 0]
Exemple #2
0
# Some commonly used paths
base_dir = '/Users/rchaudhuri/data/2019_03_hd/'
# Location where Peyrache et al data is unzipped
param_dict['raw_data_dir'] = base_dir + 'raw_data/'

# Contains the Peyrache et al data accumulated into single files.
# These files are created by read_in_data/preprocess_raw_data.py. Note that
# preprocess_raw_data will make this directory if it doesn't exist.
param_dict['processed_data_dir'] = base_dir + 'processed/'

# Spike counts in bins. Currently not used.
# param_dict['spike_counts_dir'] = base_dir + 'analyses/2016_06_spike_matrix_new/'

# Firing rates estimated by convolution with Gaussian kernel. Again, generated
# by read_in_data/preprocess_raw_data.py and directory created if needed.
param_dict['kernel_rates_dir'] = base_dir + 'analyses/2019_03_kernel_rates/'

# Analyses and results will be saved under this directory (and loaded from here too).
param_dict['results_dir'] = base_dir + 'analyses/'

# Colors for the plots.
param_dict['cols'] = {
    'REM': (0.392, 0.549, 0.0784),
    'SWS': (0.824, 0.627, 0.0392),
    'Wake': (0.0118, 0.235, 0.392),
    'measured': (0.3, 0.3, 0.3),
    'fit': (0.490, 0.961, 0.961)
}

save_pickle_file(param_dict, 'general_params.p')
session = 'Mouse28-140313'

make_processed_files = True
make_rates = True

if make_processed_files:
    data_path = gen_params['raw_data_dir'] + session + '/'
    params = {
        'session': 'Mouse28-140313',
        'data_path': data_path,
        'eeg_sampling_rate': 1250.,
        'spike_sampling_interval': 1.0 / (20e3)
    }
    data = drf.gather_session_spike_info(params)
    save_dir = gff.return_dir(gen_params['processed_data_dir'])
    gff.save_pickle_file(data, save_dir + '%s.p' % session)

if make_rates:
    print 'Getting kernel rates'
    t0 = time.time()
    sigma = 0.1
    params = {'dt': 0.05, 'method': 'gaussian', 'sigma': sigma}
    inp_data = gff.load_pickle_file(gen_params['processed_data_dir'] +
                                    '%s.p' % session)
    rates = rf.get_rates_and_angles_by_interval(inp_data,
                                                params,
                                                smooth_type='kernel',
                                                just_wake=True)
    save_dir = gff.return_dir(gen_params['kernel_rates_dir'] +
                              '%0.0fms_sigma/' % (sigma * 1000))
    gff.save_pickle_file(rates, save_dir + '%s.p' % session)
    print('Counts for each ', len(counts1), len(counts2))
    nSamples = min(len(counts1), len(counts2), desired_nSamples)
    print('nSamples = ', nSamples)
    sel1 = counts1[:nSamples]
    sel2 = counts2[:nSamples]
    concat_counts = np.concatenate((sel1, sel2), 0)
    proj = run_dim_red(concat_counts, params=dim_red_params, method=method)
    to_save = {
        'seed': sd,
        state: proj[:nSamples].copy(),
        state2: proj[nSamples:].copy()
    }
    fname = '%s_%s_kern_%dms_sigma_%dms_binsep_%s_%s_embed_%s_%ddims_%dneighbors_%d.p' % (
        session, area, sigma * 1000, dt_kernel * 1000, state, state2, method,
        target_dim, n_neighbors, sd)
gff.save_pickle_file(to_save, dir_to_save + fname)
print('Time ', time.time() - t0)

if to_plot:
    fig = plt.figure(figsize=(8, 8))
    if target_dim == 2:
        ax = fig.add_subplot(111)
        ax.scatter(to_save[state][:, 0],
                   to_save[state][:, 1],
                   s=10,
                   alpha=0.4,
                   color=cols[state])
        if condition == 'joint':
            ax.scatter(to_save[state2][:, 0],
                       to_save[state2][:, 1],
                       s=10,
Exemple #5
0
    else:
        results[k] = [[
            mse, curr_fit_result['fit_err'],
            np.array(curr_fit_result['final_knots'])
        ]]
print 'Time ', time.time() - tic

to_save = {
    'fit_results': results,
    'session': session,
    'area': area,
    'state': state,
    'embed_file': embed_fname
}
gff.save_pickle_file(
    to_save, dir_to_save + '%s_%s_dim%d_trainfrac%.2f_decode_errors_sd%d.p' %
    (session, state, fit_dim, train_frac, sd))

rmse_to_plot = np.sqrt([x[0] for x in results[k]])

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111)
if nTests < 20:
    ax.scatter(np.ones_like(rmse_to_plot), rmse_to_plot)
    ax.set_xticks([1])
    # ax.set_xticklabels(['Samples'])
else:
    vp = ax.violinplot([rmse_to_plot],
                       positions=[1],
                       points=100,
                       widths=0.75,
           c=(0.5, 0.5, 0.5),
           s=5)
ax.plot(fit_results['loop_init_knots'][:, i0],
        fit_results['loop_init_knots'][:, i1],
        fit_results['loop_init_knots'][:, i2],
        c='b',
        lw=2)
ax.plot(fit_results['loop_final_knots'][:, i0],
        fit_results['loop_final_knots'][:, i1],
        fit_results['loop_final_knots'][:, i2],
        c='r',
        lw=2)
plt.show()

dec_angle, mse = mff.decode_from_passed_fit(data_to_decode,
                                            fit_results['tt'][:-1],
                                            fit_results['curve'][:-1],
                                            ref_angles[test_idx])

to_save = {
    'fit_results': fit_results,
    'session': session,
    'area': area,
    'state': state,
    'embed_file': fname if not run_dim_red_here else None
}
gff.save_pickle_file(
    to_save,
    dir_to_save + '%s_%s_dim%d_trainfrac%.2f_interactive_fits_sd%d.p' %
    (session, state, fit_dim, train_frac, sd))