def generate_signal(go_onset=2, ss_onset=12, fs_onset=22, go_pwr=1, ss_pwr=2, fs_pwr=3, noise=0, design_resolution=0.1, duration=40, stim_duration=1, tr=1): rho = 0.12 cond_order = [0, 1, 2] betas = np.array([go_pwr, ss_pwr, fs_pwr]) onsets = np.array([go_onset, ss_onset, fs_onset]) onsets_res = onsets / design_resolution onsets_res = onsets_res.astype(int) duration_res = int(duration / design_resolution) stim_duration_res = int(stim_duration / design_resolution) sampling_rate = int(tr / design_resolution) X = np.zeros((duration_res, onsets.shape[0])) B = np.zeros((onsets.shape[0], 1)) for idx, (cond, onset) in enumerate(zip(cond_order, onsets_res)): # set the design matrix X[onset:onset+stim_duration_res, idx] = 1 X[:, idx] = np.convolve( X[:, idx], hemodynamic_models._gamma_difference_hrf( tr, oversampling=sampling_rate))[0:X.shape[0]] # set the beta for the trial depending on condition B[idx, :] = betas[cond] # downsample X so it's back to TR resolution X = X[::sampling_rate, :] signal = X @ B signal = np.squeeze(signal) if noise > 0.0: np.random.seed(12345) # make the noise component n_trs = int(duration / tr) ar = np.array([1, -rho]) # statmodels says to invert rho ap = ArmaProcess(ar) err = ap.generate_sample(n_trs, scale=noise, axis=0) Y = signal + err else: Y = signal return Y
def update_data(attrname, old, new): # Get the current slider values dy = delay.value tl = time_length.value on = onset.value un = undershoot.value di = dispersion.value ud = u_dispersion.value ra = ratio.value # Generate the new curve model = hemodynamic_models._gamma_difference_hrf(tr=2, time_length=tl, onset=on, delay=dy, undershoot=un, dispersion=di, u_dispersion=ud, ratio=ra) * scale.value x = np.arange(0, len(model)) source.data = dict(x=x, y=model)
def _get_hrf_model(hrf_model=None, hrf_length=25., dt=1., normalize=False): """Returns HRF created with model hrf_model. If hrf_model is None, then a vector of 0 is returned Parameters ---------- hrf_model: str hrf_length: float dt: float normalize: bool Returns ------- hrf_0: hrf """ if hrf_model == 'glover': hrf_0 = glover_hrf(tr=1., oversampling=1./dt, time_length=hrf_length) elif hrf_model == 'spm': hrf_0 = spm_hrf(tr=1., oversampling=1./dt, time_length=hrf_length) elif hrf_model == 'gamma': hrf_0 = _gamma_difference_hrf(1., oversampling=1./dt, time_length=hrf_length, onset=0., delay=6, undershoot=16., dispersion=1., u_dispersion=1., ratio=0.167) elif hrf_model == 'bezier': # Bezier curves. We can indicate where is the undershoot and the peak etc hrf_0 = bezier_hrf(hrf_length=hrf_length, dt=dt, pic=[6,1], picw=2, ushoot=[15,-0.2], ushootw=3, normalize=normalize) elif hrf_model == 'physio': # Balloon model. By default uses the parameters of Khalidov11 hrf_0 = physio_hrf(hrf_length=hrf_length, dt=dt, normalize=normalize) else: # Mean 0 if no hrf_model is specified hrf_0 = np.zeros(hrf_length/dt) warnings.warn("The HRF model is not recognized, setting it to None") if normalize and hrf_model is not None: hrf_0 = hrf_0 / np.linalg.norm(hrf_0) return hrf_0
#HRF peak peak_range_sim = np.arange(3, 11) peak_range = np.arange(3, 11) hrf_ushoot = 16. norm_resid = np.zeros((len(peak_range), len(peak_range))) i = 0 for sigma_noise in np.array([2., 10., 0.01]): for isim, hrf_peak_sim in enumerate(peak_range_sim): # Simulate with different hrf peaks hrf_sim = _gamma_difference_hrf(1., oversampling=1./dt, time_length=hrf_length + dt, onset=0., delay=hrf_peak_sim, undershoot=hrf_ushoot, dispersion=1., u_dispersion=1., ratio=0.167) f_hrf_sim = interp1d(x_0, hrf_sim) #plt.plot(hrf_sim) #plt.show() fmri, paradigm, design_sim, masks = generate_fmri( n_x=n_x, n_y=n_y, n_z=n_y, modulation=None, n_events=n_events, event_types=event_types, n_blank_events=n_blank_events, event_spacing=event_spacing, t_r=t_r, smoothing_fwhm=smoothing_fwhm, sigma=sigma, sigma_noise=sigma_noise, threshold=threshold, seed=seed, f_hrf=f_hrf_sim, hrf_length=hrf_length, period_cut=period_cut, drift_order=drift_order) #fmri = fmri / fmri.mean() * 100 niimgs = nb.Nifti1Image(fmri, affine=np.eye(4))
bokeh serve sliders.py at your command prompt. Then navigate to the URL http://localhost:5006/sliders in your browser. ''' import numpy as np from bokeh.io import curdoc from bokeh.layouts import row, column from bokeh.models import ColumnDataSource from bokeh.models.widgets import Slider, TextInput from bokeh.plotting import figure from nistats import hemodynamic_models # Set up data model = hemodynamic_models._gamma_difference_hrf(tr=2) x = np.arange(0, len(model)) source = ColumnDataSource(data=dict(x=x, y=model)) # Set up plot thr = 0.01 plot = figure(plot_height=400, plot_width=400, title="my hrf wave", tools="crosshair,pan,reset,save,wheel_zoom", x_range=[0, np.max(x)], y_range=[np.min(model) - thr, np.max(model) + thr]) plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
def get_values(simulation_peak, estimation_peak, held_out_index, noise_level, noise_vector_list, paradigms=paradigms, frame_times_run=frame_times_run, beta=beta, new_betas=new_betas): simulation_hrf = _gamma_difference_hrf(tr=1., oversampling=20, time_length=hrf_length, undershoot=16., delay=simulation_peak) xs = np.linspace(0., hrf_length + 1, len(simulation_hrf), endpoint=False) f_sim_hrf = interp1d(xs, simulation_hrf) shifted_paradigms = [paradigm.copy() for i, paradigm in enumerate(paradigms) if i != held_out_index] shifted_frame_times = [] offset = 0 # shift paradigms to concatenate them for paradigm in shifted_paradigms: paradigm_length = paradigm['onset'].max() paradigm['onset'] += offset shifted_frame_times.append(frame_times_run + offset) offset += paradigm_length + time_offset shifted_frame_times = np.concatenate(shifted_frame_times) train_paradigm = pandas.concat(shifted_paradigms) test_paradigm = paradigms[held_out_index] train_noise = np.concatenate( [noise for i, noise in enumerate(noise_vector_list) if i != held_out_index]) scaled_train_noise = train_noise[:, np.newaxis] * noise_level #test_noise = noise_vectors[held_out_index] # design matrix dataframes train_design_gen_df = make_design_matrix_hrf(shifted_frame_times, train_paradigm, f_hrf=f_sim_hrf) test_design_gen_df = make_design_matrix_hrf(frame_times_run, test_paradigm, f_hrf=f_sim_hrf) # design matrix without drifts train_design_gen = train_design_gen_df[event_types].values test_design_gen = test_design_gen_df[event_types].values y_train_clean = train_design_gen.dot(beta) y_train_norm = np.linalg.norm(y_train_clean) ** 2 y_train_noisy = y_train_clean[:, np.newaxis] + np.linalg.norm(y_train_clean) * scaled_train_noise y_train_noisy_norm = np.linalg.norm(y_train_noisy, axis=0) ** 2 #train_signal_norm[i_sim, held_out_index, :] = y_train_noisy_norm y_test = test_design_gen.dot(beta) y_test_new = test_design_gen.dot(new_betas) y_test_norm = np.linalg.norm(y_test) ** 2 y_test_new_norm = np.linalg.norm(y_test_new, axis=0) ** 2 #test_signal_norm[i_sim, held_out_index, :] = y_test_norm #new_test_signal_norm[i_sim, held_out_index, :] = y_test_new_norm beta_hat_gen = np.linalg.pinv(train_design_gen).dot(y_train_noisy) train_gen_resid = np.linalg.norm(y_train_noisy - train_design_gen.dot(beta_hat_gen), axis=0) ** 2 #train_gen_train_gen[i_sim, held_out_index, :] = train_gen_resid y_pred_gen = test_design_gen.dot(beta_hat_gen) test_gen_resid = np.linalg.norm(y_test[:, np.newaxis] - y_pred_gen) ** 2 #train_gen_test_gen[i_sim, held_out_index, :] = test_gen_resid #print("Generation peak {} Estimation peak {} Fold {}".format(simulation_peak, estimation_peak, held_out_index)) estimation_hrf = _gamma_difference_hrf(tr=1., oversampling=20, time_length=hrf_length, undershoot=16, delay=estimation_peak) f_est_hrf = interp1d(xs, estimation_hrf) # design matrix dataframes train_design_est_df = make_design_matrix_hrf(shifted_frame_times, train_paradigm, f_hrf=f_est_hrf) test_design_est_df = make_design_matrix_hrf(frame_times_run, test_paradigm, f_hrf=f_est_hrf) # design matrix without drifts train_design_est = train_design_est_df[event_types].values test_design_est = test_design_est_df[event_types].values beta_hat_est = np.linalg.pinv(train_design_est).dot(y_train_noisy) train_est_resid = np.linalg.norm(y_train_noisy - train_design_est.dot(beta_hat_est), axis=0) ** 2 #train_gen_train_est[i_sim, i_est, held_out_index, :] = train_est_resid y_pred_est = test_design_est.dot(beta_hat_est) test_est_resid = np.linalg.norm(y_test[:, np.newaxis] - y_pred_est, axis=0) ** 2 #train_gen_test_est[i_sim, i_est, held_out_index, :] = test_est_resid y_test_squashed = test_design_est.dot(np.linalg.pinv(test_design_est).dot(y_test)) test_squashed_resid = np.linalg.norm(y_test - y_test_squashed, axis=0) ** 2 #test_est_test_est[i_sim, i_est, held_out_index, :] = test_squashed_resid # now for some crazy hrf fitting output = alternating_optimization( train_paradigm, y_train_noisy, hrf_length, frame_times=shifted_frame_times, mean=f_est_hrf, n_alternations=10, sigma_squared=1, rescale_hrf=False, optimize_kernel=True, optimize_sigma_squared=False) (betas, (hrf_measurement_points, hrf_measures), residuals, hrfs, lls, grads, looes, thetas, sigmas_squared) = output hrf_measurement_points = np.concatenate(hrf_measurement_points) order = np.argsort(hrf_measurement_points) hrf_measurement_points = hrf_measurement_points[order] hrf_measures = hrf_measures[order] extra_points = np.array([0., -.1, hrf_length, hrf_length + 1.]) extra_values = np.zeros_like(extra_points) hrf_func = interp1d(np.concatenate([hrf_measurement_points, extra_points]), np.concatenate([hrf_measures, extra_points])) fitted_train_resid = residuals[-1] fitted_train_design_ = make_design_matrix_hrf(frame_times_run, train_paradigm, f_hrf=hrf_func) fitted_test_design_ = make_design_matrix_hrf(frame_times_run, test_paradigm, f_hrf=hrf_func) fitted_train_design = fitted_train_design_[event_types].values fitted_test_design = fitted_test_design_[event_types].values ftd_sparsity = np.abs(fitted_test_design).sum(axis=0) / np.sqrt((fitted_test_design ** 2).sum(axis=0)) if (ftd_sparsity < 2.).any(): #print('Spike at sim {} est {} ho {} noise {}'.format(simulation_peak, estimation_peak, held_out_index, noise_level)) #print('Removing strongest entry') spiking = ftd_sparsity < 2. d = fitted_test_design[:, spiking] location = np.abs(d).argmax(0) d[location, np.arange(len(location))] = .5 * (d[location - 1, np.arange(len(location))] + d[location + 1, np.arange(len(location))]) fitted_test_design[:, spiking] = d reest_betas = np.linalg.pinv(fitted_train_design).dot(y_train_noisy) # fitted_test_pred = fitted_test_design.dot(reest_betas) fitted_test_pred = fitted_test_design.dot(betas) fitted_test_resid = np.linalg.norm(y_test - fitted_test_pred) ** 2 fitted_test_squashed_pred = fitted_test_design.dot( np.linalg.pinv(fitted_test_design).dot(y_test_new)) fitted_test_squashed_resid = np.linalg.norm( y_test_new - fitted_test_squashed_pred, axis=0) ** 2 # now do exactly the same thing again for 0 mean ... # I know it is redundant output = alternating_optimization( train_paradigm, y_train_noisy, hrf_length, frame_times=shifted_frame_times, mean=zero_mean, n_alternations=10, sigma_squared=1, rescale_hrf=False, optimize_kernel=True, optimize_sigma_squared=False) (zm_betas, (zm_hrf_measurement_points, zm_hrf_measures), zm_residuals, zm_hrfs, zm_lls, zm_grads, zm_looes, zm_thetas, zm_sigmas_squared) = output zm_hrf_measurement_points = np.concatenate(zm_hrf_measurement_points) order = np.argsort(zm_hrf_measurement_points) zm_hrf_measurement_points = zm_hrf_measurement_points[order] zm_hrf_measures = zm_hrf_measures[order] extra_points = np.array([0., -.1, hrf_length, hrf_length + 1.]) extra_values = np.zeros_like(extra_points) zm_hrf_func = interp1d(np.concatenate([zm_hrf_measurement_points, extra_points]), np.concatenate([zm_hrf_measures, extra_points])) zm_fitted_train_resid = zm_residuals[-1] zm_fitted_train_design_ = make_design_matrix_hrf(frame_times_run, train_paradigm, f_hrf=zm_hrf_func) zm_fitted_test_design_ = make_design_matrix_hrf(frame_times_run, test_paradigm, f_hrf=zm_hrf_func) zm_fitted_train_design = zm_fitted_train_design_[event_types].values zm_fitted_test_design = zm_fitted_test_design_[event_types].values ftd_sparsity = np.abs(zm_fitted_test_design).sum(axis=0) / np.sqrt((zm_fitted_test_design ** 2).sum(axis=0)) if (ftd_sparsity < 2.).any(): #print('Spike at sim {} est {} ho {} noise {}'.format(simulation_peak, estimation_peak, held_out_index, noise_level)) #print('Removing strongest entry') spiking = ftd_sparsity < 2. d = zm_fitted_test_design[:, spiking] location = np.abs(d).argmax(0) d[location, np.arange(len(location))] = .5 * (d[location - 1, np.arange(len(location))] + d[location + 1, np.arange(len(location))]) zm_fitted_test_design[:, spiking] = d zm_reest_betas = np.linalg.pinv(zm_fitted_train_design).dot(y_train_noisy) # zm_fitted_test_pred = zm_fitted_test_design.dot(zm_reest_betas) zm_fitted_test_pred = zm_fitted_test_design.dot(zm_betas) zm_fitted_test_resid = np.linalg.norm(y_test - zm_fitted_test_pred) ** 2 zm_fitted_test_squashed_pred = zm_fitted_test_design.dot( np.linalg.pinv(zm_fitted_test_design).dot(y_test_new)) zm_fitted_test_squashed_resid = np.linalg.norm( y_test_new - zm_fitted_test_squashed_pred, axis=0) ** 2 return (train_paradigm, test_paradigm, beta, betas, zm_betas, reest_betas, zm_reest_betas, y_train_noisy, y_test, y_train_norm, y_train_noisy_norm, y_test_norm, y_test_new_norm, train_gen_resid, test_gen_resid, train_est_resid, test_est_resid, test_squashed_resid, fitted_train_resid, fitted_test_resid, fitted_test_squashed_resid, hrf_measurement_points, hrf_measures, zm_fitted_train_resid, zm_fitted_test_resid, zm_fitted_test_squashed_resid, zm_hrf_measurement_points, zm_hrf_measures, train_design_gen, test_design_gen, train_design_est, test_design_est, fitted_train_design, fitted_test_design, zm_fitted_train_design, zm_fitted_test_design)
def generate_2voxels_signal(tr=1, corr=0, n_trials=15, initial_guess=None, design_resolution=0.1, onset_offset=0, timepoints=200): np.random.seed(12345) corr_mat = np.array([[1, corr], [corr, 1]]) trial_interval = int(timepoints / n_trials) onsets = np.array(range(onset_offset, timepoints-10, trial_interval)) # want the mean betas of each voxel to be one gnd_means = np.ones(2) # continue while loop while the target correlations # are more than 0.1 off c_wrong = True c_tol = 0.1 while c_wrong: # generate betas if initial_guess is None: initial_guess = np.random.multivariate_normal( gnd_means, corr_mat, size=(onsets.shape[0]), tol=0.00005 ) sim_betas = minimize( _check_data, initial_guess, args=(corr_mat,), method='BFGS', tol=1e-10 ).x # reshape the output (comes out 1-dimensional) sim_betas = sim_betas.reshape(initial_guess.shape) corr_error = _check_data( sim_betas, corr_mat, ) c_wrong = c_tol < corr_error initial_guess = None mean_fix = 1 - sim_betas.mean(axis=0) # ensure each beta series has average of 1. betas = sim_betas + mean_fix onsets_res = (onsets // design_resolution).astype(int) duration_res = int(timepoints / design_resolution) stim_duration_res = int(0.5 / design_resolution) sampling_rate = int(tr / design_resolution) X = np.zeros((duration_res, onsets.shape[0])) for idx, onset in enumerate(onsets_res): # set the design matrix X[onset:onset+stim_duration_res, idx] = 1 X[:, idx] = np.convolve( X[:, idx], hemodynamic_models._gamma_difference_hrf( tr, oversampling=sampling_rate))[0:X.shape[0]] # downsample X so it's back to TR resolution X = X[::sampling_rate, :] region1 = np.squeeze(X @ betas[:, 0]) region2 = np.squeeze(X @ betas[:, 1]) return onsets, betas, region1, region2
generate_spikes_time_series as generate_experiment) n_events=100 n_blank_events=25 event_spacing=6 t_r=2 hrf_length=32. event_types=['ev1', 'ev2'] jitter_min=-1 jitter_max=1 time_offset = 20 modulation=None seed = 42 from nistats.hemodynamic_models import _gamma_difference_hrf simulation_hrf = _gamma_difference_hrf(tr=1., oversampling=20, time_length=hrf_length + 1, undershoot=16., delay=11.) xs = np.linspace(0., hrf_length + 1, len(simulation_hrf), endpoint=False) f_sim_hrf = interp1d(xs, simulation_hrf) from data_generator import make_design_matrix_hrf paradigm, design_, modulation, measurement_times = generate_experiment( n_events=n_events, n_blank_events=n_blank_events, event_spacing=event_spacing, t_r=t_r, hrf_length=hrf_length, event_types=event_types, jitter_min=jitter_min, jitter_max=jitter_max, time_offset=time_offset,