def do_kcsd_evd(pot, ele_pos, xmin, xmax, ymin, ymax, n_src_init=1000, R_init=30): k = KCSD2D(ele_pos, pot, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, h=1, sigma=1, n_src_init=n_src_init, gdx=4, gdy=4, R_init=R_init) try: eigenvalue, eigenvector = np.linalg.eigh(k.k_pot + k.lambd * np.identity(k.k_pot.shape[0])) except LinAlgError: raise LinAlgError('EVD is failing - try moving the electrodes' 'slightly') idx = eigenvalue.argsort()[::-1] eigenvalues = eigenvalue[idx] eigenvectors = eigenvector[:, idx] return k, eigenvalues, eigenvectors
def do_kcsd(ele_pos, pots): pots = pots.reshape((len(ele_pos), 1)) # first time point return KCSD2D(ele_pos, pots, h=h, sigma=conductivity, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, n_src_init=n_src_init, src_type='gauss', R_init=R_init)
def do_kcsd(ele_pos_for_csd, pots_for_csd, ele_limit): ele_position = ele_pos_for_csd[:ele_limit[1]][0::1] csd_pots = pots_for_csd[:ele_limit[1]][0::1] k = KCSD2D(ele_position, csd_pots, h=1, sigma=1, R_init=32, lambd=1e-9, xmin= -42, xmax=42, gdx=4, ymin=0, ymax=4000, gdy=4) # k.L_curve(Rs=np.linspace(16, 48, 3), lambdas=np.logspace(-9, -3, 20)) return k, k.values('CSD'), k.values('POT'), ele_position
def stability_M(n_src, total_ele, ele_pos, pots, R_init=0.23): """ Investigates stability of reconstruction for different number of basis sources Parameters ---------- n_src: int Number of basis sources. total_ele: int Number of electrodes. ele_pos: numpy array Electrodes positions. pots: numpy array Values of potentials at ele_pos. R_init: float Initial value of R parameter - width of basis source Default: 0.23. Returns ------- obj_all: class object eigenvalues: numpy array Eigenvalues of k_pot matrix. eigenvectors: numpy array Eigen vectors of k_pot matrix. """ obj_all = [] eigenvectors = np.zeros((len(n_src), total_ele, total_ele)) eigenvalues = np.zeros((len(n_src), total_ele)) for i, value in enumerate(n_src): pots = pots.reshape((len(ele_pos), 1)) obj = KCSD2D(ele_pos, pots, src_type='gauss', sigma=1., h=50., gdx=0.01, gdy=0.01, n_src_init=n_src[i], xmin=0, xmax=1, ymax=1, ymin=0, R_init=R_init) try: eigenvalue, eigenvector = np.linalg.eigh( obj.k_pot + obj.lambd * np.identity(obj.k_pot.shape[0])) except LinAlgError: raise LinAlgError('EVD is failing - try moving the electrodes' 'slightly') idx = eigenvalue.argsort()[::-1] eigenvalues[i] = eigenvalue[idx] eigenvectors[i] = eigenvector[:, idx] obj_all.append(obj) return obj_all, eigenvalues, eigenvectors
def do_kcsd(CSD_PROFILE, csd_seed, noise_level): if CSD_PROFILE.__name__ == 'gauss_2d_small': R_init = 1. # R_final = np.linspace(0.01, 0.15, 15) R_final = np.linspace(0.05, 1., 20) elif CSD_PROFILE.__name__ == 'gauss_2d_large': R_init = 0.1 # R_final = np.linspace(0.1, 1.5, 15) R_final = np.linspace(0.05, 1., 20) # True CSD_PROFILE csd_at = np.mgrid[0.:1.:100j, 0.:1.:100j] csd_x, csd_y = csd_at # Small source true_csd = CSD_PROFILE(csd_at, seed=csd_seed) # Electrode positions ele_x, ele_y = np.mgrid[0.05: 0.95: 10j, 0.05: 0.95: 10j] ele_pos = np.vstack((ele_x.flatten(), ele_y.flatten())).T # Potentials generated pots = np.zeros(ele_pos.shape[0]) xlin = csd_at[0, :, 0] ylin = csd_at[1, 0, :] h = 50. sigma = 0.3 for ii in range(ele_pos.shape[0]): pots[ii] = integrate_2d(csd_at, true_csd, [ele_pos[ii][0], ele_pos[ii][1]], h, [xlin, ylin]) pots /= 2 * np.pi * sigma pot_X, pot_Y, pot_Z = grid(ele_pos[:, 0], ele_pos[:, 1], pots) pots = pots.reshape((len(ele_pos), 1)) # Add noise to potentials rstate = np.random.RandomState(23) noise = noise_level*0.01*(rstate.normal(np.mean(pots), np.std(pots), size=(len(pots), 1))) pots += noise # KCSD2D k = KCSD2D(ele_pos, pots, h=h, sigma=sigma, xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0, gdx=0.01, gdy=0.01, R_init=R_init, n_src_init=1000, src_type='gauss') # rest of the parameters are set at default k.cross_validate(lambdas=None, Rs=R_final) est_csd_post_cv = k.values('CSD') err = point_errors(true_csd, est_csd_post_cv) return k.estm_x, k.estm_y, err, ele_pos
def animate_spike(spike, shape, X, Y, Z): print("animate spike") fig1 = plt.figure() plt.subplot(1, 2, 1) print("animate spike") ax1 = plt.gca() print("imshow!!!") im = ax1.imshow(spike[:, 0].reshape(shape)) fig1.colorbar(im, ax=ax1) ele_pos = np.vstack((Y.flatten()[0::100], Z.flatten()[::100])).T print(spike[:, 0][::100]) print(ele_pos) k = KCSD2D(ele_pos, spike[:, 0][::100].reshape(len(spike[:, 0].flatten()[::100]), 1), h=50, n_src_init=10000) print('k', k) plt.subplot(1, 2, 2) ax3 = plt.gca() print("imshow!!!") im3 = ax3.imshow(k.values('CSD')[:, :, 0]) fig1.colorbar(im3, ax=ax3) plt.figure() ax2 = plt.axes(xlim=(0, spike.shape[1]), ylim=(np.min(spike.flatten()), np.max(spike.flatten()))) ax2.plot(spike[200, :]) ln, = ax2.plot([], []) update_func = lambda frame, spike=spike, shape=shape, im=im, im3=im3, ln=ln, kcsd = k: \ update(frame, spike, shape, im, im3, ln, k) print("trying animation...") # Set up formatting for the movie files Writer = animation.writers['ffmpeg'] writer = Writer(fps=15, metadata=dict(artist='Liam Long'), bitrate=1800) anim = animation.FuncAnimation(fig1, update_func, init_func=lambda: update_func(0), frames=3000, interval=5, blit=True) anim.save('basic_animation.mp4', writer=writer) plt.show()
def test_kcsd2d_estimate(self, cv_params={}): self.test_params.update(cv_params) result = KCSD2D(self.ele_pos, self.pots, **self.test_params) result.cross_validate() vals = result.values() true_csd = self.csd_profile(result.estm_pos, 43) # print(true_csd.shape, vals.shape) # Meh here! rms = np.linalg.norm(np.array(vals[:, :, 0]) - true_csd) rms /= np.linalg.norm(true_csd) self.assertLess(rms, 0.5, msg='RMS ' + str(rms) + 'between trueCSD and estimate > 0.5')
def do_kcsd(CSD_PROFILE, csd_seed): if CSD_PROFILE.__name__ == 'gauss_2d_small': R_init = 1. R_final = 0.1 elif CSD_PROFILE.__name__ == 'gauss_2d_large': R_init = 0.1 R_final = 0.3 # True CSD_PROFILE csd_at = np.mgrid[0.:1.:100j, 0.:1.:100j] csd_x, csd_y = csd_at # Small source true_csd = CSD_PROFILE(csd_at, seed=csd_seed) # Electrode positions ele_x, ele_y = np.mgrid[0.05:0.95:10j, 0.05:0.95:10j] ele_pos = np.vstack((ele_x.flatten(), ele_y.flatten())).T # Potentials generated pots = np.zeros(ele_pos.shape[0]) xlin = csd_at[0, :, 0] ylin = csd_at[1, 0, :] h = 50. sigma = 0.3 for ii in range(ele_pos.shape[0]): pots[ii] = integrate_2d(csd_at, true_csd, [ele_pos[ii][0], ele_pos[ii][1]], h, [xlin, ylin]) pots /= 2 * np.pi * sigma pot_X, pot_Y, pot_Z = grid(ele_pos[:, 0], ele_pos[:, 1], pots) pots = pots.reshape((len(ele_pos), 1)) # KCSD2D k = KCSD2D(ele_pos, pots, h=h, sigma=sigma, xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0, gdx=0.01, gdy=0.01, R_init=R_init, n_src_init=1000, src_type='gauss') # rest of the parameters are set at default est_csd_pre_cv = k.values('CSD') #k.cross_validate(Rs=np.linspace(0.03, 0.12, 10)) #k.cross_validate() # k.cross_validate(lambdas=None, Rs=np.array(R_final).reshape(1)) k.cross_validate(lambdas=None, Rs=np.linspace(0.05, 1., 20)) est_csd_post_cv = k.values('CSD') return csd_x, csd_y, true_csd, ele_pos, pot_X, pot_Y, pot_Z, k, est_csd_pre_cv, est_csd_post_cv
def do_kcsd(pot, ele_pos, xmin, xmax, ymin, ymax, n_src_init=1000): k = KCSD2D(ele_pos, pot, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, h=1, sigma=1, n_src_init=n_src_init, gdx=4, gdy=4, R_init=30) k.L_curve(lambdas=np.array(1e-07), Rs=np.array([30])) # k.cross_validate(lambdas=np.array(6.455e-10), # Rs=np.array([80])) kcsd = k.values('CSD') est_pot = k.values('POT') return kcsd, est_pot, k.estm_x, k.estm_y, k
ylin = csd_at[1, 0, :] h = 50. sigma = 0.3 for ii in range(ele_pos.shape[0]): pots[ii] = integrate_2d(csd_at, true_csd, [ele_pos[ii][0], ele_pos[ii][1]], h, [xlin, ylin]) pots /= 2 * np.pi * sigma pot_X, pot_Y, pot_Z = grid(ele_pos[:, 0], ele_pos[:, 1], pots) pots = pots.reshape((len(ele_pos), 1)) # KCSD2D k = KCSD2D(ele_pos, pots, h=h, sigma=sigma, xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0, n_src_init=1000, src_type='gauss') # rest of the parameters are set at default est_csd_pre_cv = k.values('CSD') k.cross_validate(lambdas=None, Rs=np.arange(0.1, 0.13, 0.005)) est_csd_post_cv = k.values('CSD') fig = plt.figure(figsize=(20, 5)) ax = plt.subplot(141) ax.set_aspect('equal') t_max = np.max(np.abs(true_csd)) levels = np.linspace(-1 * t_max, t_max, 16) im = ax.contourf(csd_x, csd_y, true_csd, levels=levels, cmap=cm.bwr) ax.set_xlabel('X [mm]')
def make_reconstruction(self, csd_profile, csd_seed, noise=0, nr_broken_ele=None, Rs=None, lambdas=None, method='cross-validation'): """ Makes the whole kCSD reconstruction. Parameters ---------- csd_profile: function function to produce csd profile csd_seed: int Seed for random generator to choose random CSD profile. noise: float Determines the level of noise in the data. Default: 0. nr_broken_ele: int How many electrodes are broken (excluded from analysis) Default: None. Rs: numpy 1D array Basis source parameter for crossvalidation. Default: None. lambdas: numpy 1D array Regularization parameter for crossvalidation. Default: None. method: string Determines the method of regularization. Default: cross-validation. Returns ------- List of [rms, kcsd] and point_error rms: float Error of reconstruction. kcsd: object of a class Object of a class. point_error: numpy array Error of reconstruction calculated at every point of reconstruction space. """ ele_pos, pots = self.electrode_config(csd_profile, csd_seed, self.total_ele, self.ele_lims, self.h, self.sigma, noise, nr_broken_ele) k = KCSD2D(ele_pos, pots, h=self.h, sigma=self.sigma, xmax=np.max(self.kcsd_xlims), xmin=np.min(self.kcsd_xlims), ymax=np.max(self.kcsd_ylims), ymin=np.min(self.kcsd_ylims), n_src_init=self.n_src_init, gdx=self.est_xres, gdy=self.est_yres) if method == 'cross-validation': k.cross_validate(Rs=Rs, lambdas=lambdas) elif method == 'L-curve': k.L_curve(Rs=Rs, lambdas=lambdas) else: raise ValueError('Invalid value of reconstruction method,' 'pass either cross-validation or L-curve') est_csd = k.values('CSD') test_csd = csd_profile([k.estm_x, k.estm_y], csd_seed) rms = self.calculate_rms(test_csd, est_csd[:, :, 0]) point_error = self.calculate_point_error(test_csd, est_csd[:, :, 0]) return rms, point_error
def do_kcsd(CSD_PROFILE, csd_seed, prefix, missing_ele): # True CSD_PROFILE csd_at = np.mgrid[0.:1.:100j, 0.:1.:100j] csd_x, csd_y = csd_at true_csd = CSD_PROFILE(csd_at, seed=csd_seed) # Electrode positions ele_x, ele_y = np.mgrid[0.05:0.95:10j, 0.05:0.95:10j] ele_pos = np.vstack((ele_x.flatten(), ele_y.flatten())).T #Remove some electrodes remove_num = missing_ele rstate = np.random.RandomState(42) # just a random seed rmv = rstate.choice(ele_pos.shape[0], remove_num, replace=False) ele_pos = np.delete(ele_pos, rmv, 0) # Potentials generated pots = np.zeros(ele_pos.shape[0]) xlin = csd_at[0, :, 0] ylin = csd_at[1, 0, :] h = 50. sigma = 0.3 for ii in range(ele_pos.shape[0]): pots[ii] = integrate_2d(csd_at, true_csd, [ele_pos[ii][0], ele_pos[ii][1]], h, [xlin, ylin]) pots /= 2 * np.pi * sigma pot_X, pot_Y, pot_Z = grid(ele_pos[:, 0], ele_pos[:, 1], pots) pots = pots.reshape((len(ele_pos), 1)) # KCSD2D k = KCSD2D(ele_pos, pots, h=h, sigma=sigma, xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0, gdx=0.01, gdy=0.01, R_init=0.1, n_src_init=1000, src_type='gauss') # rest of the parameters are set at default est_csd_pre_cv = k.values('CSD') R_range = np.linspace(0.1, 1.0, 10) #R_range = np.linspace(0.03, 0.12, 10) #R_range = np.linspace(0.1, 1.0, 10) k.cross_validate(Rs=R_range) #k.cross_validate() #k.cross_validate(lambdas=None, Rs=np.array(0.08).reshape(1)) est_csd_post_cv = k.values('CSD') fig = plt.figure(figsize=(20, 5)) ax = plt.subplot(141) ax.set_aspect('equal') t_max = np.max(np.abs(true_csd)) levels = np.linspace(-1 * t_max, t_max, 16) im = ax.contourf(csd_x, csd_y, true_csd, levels=levels, cmap=cm.bwr) ax.set_xlabel('X [mm]') ax.set_ylabel('Y [mm]') ax.set_title('True CSD') ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ticks = np.linspace(-1 * t_max, t_max, 5, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) ax = plt.subplot(142) ax.set_aspect('equal') v_max = np.max(np.abs(pots)) levels_pot = np.linspace(-1 * v_max, v_max, 16) im = ax.contourf(pot_X, pot_Y, pot_Z, levels=levels_pot, cmap=cm.PRGn) ax.scatter(ele_pos[:, 0], ele_pos[:, 1], 10, c='k') ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ax.set_xlabel('X [mm]') ax.set_title('Interpolated potentials') ticks = np.linspace(-1 * v_max, v_max, 5, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) ax = plt.subplot(143) ax.set_aspect('equal') t_max = np.max(np.abs(est_csd_pre_cv[:, :, 0])) levels_kcsd = np.linspace(-1 * t_max, t_max, 16, endpoint=True) im = ax.contourf(k.estm_x, k.estm_y, est_csd_pre_cv[:, :, 0], levels=levels_kcsd, cmap=cm.bwr) ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ax.set_xlabel('X [mm]') ax.set_title('Estimated CSD without CV') ticks = np.linspace(-1 * t_max, t_max, 5, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) ax = plt.subplot(144) ax.set_aspect('equal') t_max = np.max(np.abs(est_csd_post_cv[:, :, 0])) levels_kcsd = np.linspace(-1 * t_max, t_max, 16, endpoint=True) im = ax.contourf(k.estm_x, k.estm_y, est_csd_post_cv[:, :, 0], levels=levels_kcsd, cmap=cm.bwr) ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ax.set_xlabel('X [mm]') ax.set_title('Estimated CSD with CV') ticks = np.linspace(-1 * t_max, t_max, 5, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) plt.savefig(os.path.join(prefix, str(csd_seed) + '.pdf')) plt.close() #plt.show() np.savez(os.path.join(prefix, str(csd_seed) + '.npz'), true_csd=true_csd, pots=pots, post_cv=est_csd_post_cv, R=k.R)
def do_kcsd(CSD_PROFILE, data, csd_seed, prefix, missing_ele): # True CSD_PROFILE csd_at = np.mgrid[0.:1.:100j, 0.:1.:100j] csd_x, csd_y = csd_at true_csd = data['true_csd'] # Electrode positions ele_x, ele_y = np.mgrid[0.05:0.95:10j, 0.05:0.95:10j] ele_pos = np.vstack((ele_x.flatten(), ele_y.flatten())).T #Remove some electrodes remove_num = missing_ele rstate = np.random.RandomState(42) # just a random seed rmv = rstate.choice(ele_pos.shape[0], remove_num, replace=False) ele_pos = np.delete(ele_pos, rmv, 0) # Potentials generated pots = np.zeros(ele_pos.shape[0]) pots = data['pots'] h = 50. sigma = 0.3 pot_X, pot_Y, pot_Z = grid(ele_pos[:, 0], ele_pos[:, 1], pots) # KCSD2D k = KCSD2D(ele_pos, pots, h=h, sigma=sigma, xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0, gdx=0.01, gdy=0.01, R_init=0.1, n_src_init=1000, src_type='gauss') # rest of the parameters are set at default est_csd_pre_cv = k.values('CSD') est_csd_post_cv = data['post_cv'] fig = plt.figure(figsize=(20, 12)) ax = plt.subplot(241) ax.set_aspect('equal') t_max = np.max(np.abs(true_csd)) levels = np.linspace(-1 * t_max, t_max, 16) im = ax.contourf(csd_x, csd_y, true_csd, levels=levels, cmap=cm.bwr) ax.set_xlabel('X [mm]') ax.set_ylabel('Y [mm]') ax.set_title('True CSD') ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ticks = np.linspace(-1 * t_max, t_max, 5, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) ax = plt.subplot(242) ax.set_aspect('equal') v_max = np.max(np.abs(pots)) levels_pot = np.linspace(-1 * v_max, v_max, 16) im = ax.contourf(pot_X, pot_Y, pot_Z, levels=levels_pot, cmap=cm.PRGn) ax.scatter(ele_pos[:, 0], ele_pos[:, 1], 10, c='k') ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ax.set_xlabel('X [mm]') ax.set_title('Interpolated potentials') ticks = np.linspace(-1 * v_max, v_max, 5, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) ax = plt.subplot(243) ax.set_aspect('equal') t_max = np.max(np.abs(est_csd_pre_cv[:, :, 0])) levels_kcsd = np.linspace(-1 * t_max, t_max, 16, endpoint=True) im = ax.contourf(k.estm_x, k.estm_y, est_csd_pre_cv[:, :, 0], levels=levels_kcsd, cmap=cm.bwr) ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ax.set_xlabel('X [mm]') ax.set_title('Estimated CSD without CV') ticks = np.linspace(-1 * t_max, t_max, 5, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) ax = plt.subplot(244) ax.set_aspect('equal') t_max = np.max(np.abs(est_csd_post_cv[:, :, 0])) levels_kcsd = np.linspace(-1 * t_max, t_max, 16, endpoint=True) im = ax.contourf(k.estm_x, k.estm_y, est_csd_post_cv[:, :, 0], levels=levels_kcsd, cmap=cm.bwr) ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ax.set_xlabel('X [mm]') ax.set_title('Estimated CSD with CV') ticks = np.linspace(-1 * t_max, t_max, 5, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) ax = plt.subplot(245) error1 = point_errors(true_csd, est_csd_post_cv) print(error1.shape) ax.set_aspect('equal') t_max = np.max(abs(error1)) levels_kcsd = np.linspace(0, t_max, 16, endpoint=True) im = ax.contourf(k.estm_x, k.estm_y, error1, levels=levels_kcsd, cmap=cm.Greys) ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ax.set_xlabel('X [mm]') ax.set_ylabel('Y [mm]') ax.set_title('Sigmoid error') ticks = np.linspace(0, t_max, 3, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) ax = plt.subplot(246) error2 = point_errors_Ch(true_csd, est_csd_post_cv) ax.set_aspect('equal') t_max = np.max(abs(error2)) levels_kcsd = np.linspace(0, t_max, 16, endpoint=True) im = ax.contourf(k.estm_x, k.estm_y, error2, levels=levels_kcsd, cmap=cm.Greys) ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ax.set_xlabel('X [mm]') ax.set_title('Normalized difference') ticks = np.linspace(0, t_max, 3, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) ax = plt.subplot(247) error3 = calculate_rdm(true_csd, est_csd_post_cv[:, :, 0]) print(error3.shape) ax.set_aspect('equal') t_max = np.max(abs(error3)) levels_kcsd = np.linspace(0, t_max, 16, endpoint=True) im = ax.contourf(k.estm_x, k.estm_y, error3, levels=levels_kcsd, cmap=cm.Greys) ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ax.set_xlabel('X [mm]') ax.set_title('Relative difference measure') ticks = np.linspace(0, t_max, 3, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) ax = plt.subplot(248) error4 = calculate_mag(true_csd, est_csd_post_cv[:, :, 0]) print(error4.shape) ax.set_aspect('equal') t_max = np.max(abs(error4)) levels_kcsd = np.linspace(0, t_max, 16, endpoint=True) im = ax.contourf(k.estm_x, k.estm_y, error4, levels=levels_kcsd, cmap=cm.Greys) ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_xticks([0, 0.5, 1]) ax.set_yticks([0, 0.5, 1]) ax.set_xlabel('X [mm]') ax.set_title('Magnitude ratio') ticks = np.linspace(0, t_max, 3, endpoint=True) plt.colorbar(im, orientation='horizontal', format='%.2f', ticks=ticks, pad=0.25) plt.savefig(os.path.join(prefix, str(csd_seed) + '.pdf')) plt.close() #plt.show() np.savez(os.path.join(prefix, str(csd_seed) + '.npz'), true_csd=true_csd, pots=pots, post_cv=est_csd_post_cv, R=k.R)
for ii, chann in enumerate(chanList): pots.append(convData[ii, int(sRate * csd_at_time)]) pots = np.array(pots) # print(pots.shape) pots = pots.reshape((len(chanList), 1)) R_init = 0.3 h = 50. sigma = 0.3 k = KCSD2D(ele_pos, pots, h=h, sigma=sigma, xmin=-35, xmax=35, ymin=1100, ymax=2000, gdx=10, gdy=10, R_init=R_init, n_src_init=1000, src_type='gauss') # rest of the parameters are set at default k.cross_validate(Rs=np.linspace(0.1, 1.001, 20), lambdas=None) # # ax = plt.subplot(121) # # for ii, chan in enumerate(chanList): # # ax.plot(tDat, convData[ii, :], label=str(chan)+' Ele'+str(chan_dict[chan])) # # plt.legend() # # ax = plt.subplot(122) # # for i in range(0, len(dLineList)): # # ax.plot(tDat, digArray[i, :])
csd_at = np.mgrid[0.:1.:100j, 0.:1.:100j] csd_x, csd_y = csd_at D = 2 - 1.618 ele_x, ele_y = np.mgrid[0.5 - D:0.5 + D:3j, 0.5 - D:0.5 + D:3j] ele_pos = np.vstack((ele_x.flatten(), ele_y.flatten())).T pots = np.eye(9) kcsd = KCSD2D(ele_pos, pots, h=h, sigma=conductivity, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, n_src_init=n_src_init, src_type='gauss', R_init=R_init) csd = kcsd.values('CSD') E = np.array([csd[:, :, i].flatten() for i in range(csd.shape[-1])]) VAR = np.dot(E.T, E)[np.eye(E.shape[1], dtype=bool)].reshape(int(np.sqrt(E.shape[1])), -1) FIG_WIDTH = 17.0 FIG_HEIGHT = 6.0 FIG_WSPACE = 2.0