params = gpcsd_model.extract_model_params() with open('%s/results/gpcsd_model_%s.pkl' % (root_path, probe_name), 'wb') as f: pickle.dump(params, f) # %% Predict during baseline period gpcsd_model.update_lfp(lfp_baseline, t) print(gpcsd_model) gpcsd_model.predict(x, t) tcsd = predictcsd_trad_1d(lfp_baseline) # %% Single trial image plots trial = 0 plt.figure(figsize=(14, 5)) plt.subplot(151) plt.imshow(normalize(gpcsd_model.csd_pred_list[0][:, :, trial]), aspect='auto', cmap='bwr', vmin=-1, vmax=1) plt.title('Slow CSD') plt.subplot(152) plt.imshow(normalize(gpcsd_model.csd_pred_list[1][:, :, trial]), aspect='auto', cmap='bwr', vmin=-1, vmax=1) plt.title('Fast CSD') plt.subplot(153) plt.imshow(normalize(gpcsd_model.csd_pred[:, :, trial]), aspect='auto',
csd3_interior_electrodes = np.zeros((nx - 2, nt, ntrials)) for trial in range(ntrials): csdinterp = scipy.interpolate.RectBivariateSpline(z, t, csd2[:, :, trial]) csd2_interior_electrodes[:, :, trial] = csdinterp(xshort, t) csdinterp = scipy.interpolate.RectBivariateSpline(z, t, csd3[:, :, trial]) csd3_interior_electrodes[:, :, trial] = csdinterp(xshort, t) # %% Pass through forward model, add white noise lfp2 = np.zeros((nx, nt, ntrials)) lfp3 = np.zeros((nx, nt, ntrials)) for trial in range(ntrials): lfp2[:, :, trial] = fwd_model_1d(csd2[:, :, trial], z, x, 100) lfp3[:, :, trial] = fwd_model_1d(csd3[:, :, trial], z, x, 100) lfp2 = lfp2 + np.random.normal(0, np.sqrt(0.0001), size=(nx, nt, ntrials)) lfp3 = lfp3 + np.random.normal(0, np.sqrt(0.0001), size=(nx, nt, ntrials)) lfp2 = normalize(lfp2) lfp3 = normalize(lfp3) # %% plt.figure(figsize=(8, 5)) plt.subplot(121) plt.imshow(normalize(csd2[:, :, 0]), vmin=-1, vmax=1, cmap='bwr', aspect='auto') plt.xlabel('Time') plt.ylabel('Depth') plt.title('Two-component CSD') plt.colorbar() plt.subplot(122)
gpcsd_model.fit(n_restarts=10, verbose=True) # %% predict print(gpcsd_model) gpcsd_model.predict(z_grid, t) csd_pred_rect = gpcsd_model.csd_pred.reshape((nz1, nz2, nt, -1)) # %% trad CSD on test data tcsd_pred = predictcsd_trad_2d(lfp_sparse_rect)[:, 1:-1, :, :] # %% tinds = [0, 1, 2, 3] plt.figure(figsize=(16, 16)) for ti in tinds: plt.subplot(4, 4, ti + 1) plt.imshow(normalize(tcsd_pred[:, :, ti, 0]).T, aspect='auto', cmap='bwr') for ti in tinds: plt.subplot(4, 4, ti + 1 + 4) plt.imshow(normalize(csd_pred_rect[:, :, ti, 0]).T, aspect='auto', cmap='bwr') for ti in tinds: plt.subplot(4, 4, ti + 1 + 8) plt.imshow(normalize(lfp_sparse_rect[:, :, ti, 0]).T, aspect='auto', cmap='bwr') # %% Compute RMSE and R2 rmse = np.sqrt(np.mean(np.square(csd_pred_rect - csd_dense_rect)))
evoked_probe['kcsd'] = kcsd_evoked_model.values() with open('%s/results/gpcsd_evoked_%s.pkl' % (root_path, probe_name), 'wb') as f: pickle.dump(evoked_probe, f) evoked[probe_name] = evoked_probe with open('%s/results/gpcsd_cov_%s.pkl' % (root_path, probe_name), 'wb') as f: pickle.dump(cov_probe, f) cov[probe_name] = cov_probe # Supplemental figure: comparing kCSD on evoked and GPCSD evoked plt.figure(figsize=(14, 6)) plt.subplot(131) plt.imshow(normalize(evoked[probe_name]['lfp']), vmin=-1, vmax=1, aspect='auto', cmap='bwr') plt.title('%s evoked LFP' % probe_name) plt.subplot(132) plt.imshow(normalize(evoked[probe_name]['gpcsd']), vmin=-1, vmax=1, aspect='auto', cmap='bwr') plt.title('GPCSD evoked') plt.subplot(133) plt.imshow(normalize(evoked[probe_name]['kcsd']), cmap='bwr',
gpcsd_gen.temporal_cov_list[1].params['sigma2']['value'] = sig2tM_true # %% Generate CSD and sample at interior electrode positions for comparing to tCSD csd = gpcsd_gen.sample_prior(2 * ntrials) csd_interior_electrodes = np.zeros((nx - 2, nt, 2 * ntrials)) for trial in range(2 * ntrials): csdinterp = scipy.interpolate.RectBivariateSpline(z, t, csd[:, :, trial]) csd_interior_electrodes[:, :, trial] = csdinterp(xshort, t) # %% Pass through forward model, add white noise lfp = np.zeros((nx, nt, 2 * ntrials)) for trial in range(2 * ntrials): lfp[:, :, trial] = fwd_model_1d(csd[:, :, trial], z, x, R_true) lfp = lfp + np.random.normal( 0, np.sqrt(sig2n_true), size=(nx, nt, 2 * ntrials)) lfp = normalize(lfp) # %% Visualize one trial plt.figure() plt.subplot(121) plt.imshow(csd[:, :, 0], vmin=-1, vmax=1, cmap='bwr', aspect='auto') plt.title('CSD') plt.xlabel('Time') plt.ylabel('depth') plt.colorbar() plt.subplot(122) plt.imshow(lfp[:, :, 0], cmap='bwr', aspect='auto') plt.title('LFP') plt.xlabel('Time') plt.colorbar() plt.tight_layout()
nx = 24 # Number of observed spatial locations for LFP snr = 30 # noise characteristics t = np.linspace(0, 50, nt)[:, None] x = np.linspace(a, b, nx)[:, None] nz = int(np.rint(b - a) / deltaz) + 1 z = np.linspace(a, b, nz)[:, None] # %% Create true CSD csd_true = csd_true_f(z, t) print('generated CSD of shape') print(csd_true.shape) # %% Create observed LFP lfp = {} lfp['noiseless'] = normalize( forward_models.fwd_model_1d(csd_true, z, x, R_true)) print('generated LFP of shape') print(lfp['noiseless'].shape) # Add white noise to LFP sig2n_true = np.square(np.std(lfp['noiseless']) / snr) lfp['white_noise'] = lfp['noiseless'] + np.random.normal( 0, np.sqrt(sig2n_true), size=(nx, nt)) plt.figure(figsize=(12, 6)) plt.subplot(131) plt.imshow(csd_true, aspect='auto', cmap='bwr') plt.title('True CSD') plt.ylabel('Depth (microns)') plt.xlabel('Time (ms)') plt.subplot(132)