def test_KCSD_1D_zero_csd(self): """if measured potential is 0, the calculated CSD should be 0""" params = {'n_sources': 20, 'dist_density': 20, 'source_type': 'gauss_lim'} k_zero = KCSD(elec_pos=np.array([[1.0], [2.0], [3.0], [4.0], [5.0]]), sampled_pots=np.array([[0.0], [0.0], [0.0], [0.0], [0.0]]), params=params) k_zero.estimate_csd() self.assertAlmostEqual(norm(k_zero.solver.estimated_csd), 0.0, places=10)
def csd_est_using_kCSD(probe_loc, lfp_bar, lambd=0.0, sigma = sigma): elec_pos = np.expand_dims(probe_loc, axis=1) pots = np.expand_dims(lfp_bar, axis=1) params = {'gdX': np.diff(probe_loc)[0], 'gdY': np.diff(probe_loc)[0], sigma:sigma, lambd:lambd } k = KCSD(elec_pos, pots, params) k.solver.lambd = lambd k.estimate_pots() k.estimate_csd() return k.solver.space_X.ravel(), k.solver.estimated_csd.ravel()*sigma
def test_KCSD_1D_csd_reconstruction(self): """reconstructed csd should be similar to model csd""" params = {'sigma': self.sigma, 'source_type': 'gauss_lim', 'x_min': self.xmin, 'x_max': self.xmax, 'h': self.h} k = KCSD(self.elec_pos, self.meas_pot, params) k.estimate_csd() """print np.max(k.estimated_csd) plot(k.estimated_csd) plot(self.true_csd) show() print k.X_src""" for estimated_csd, true_csd in zip(k.solver.estimated_csd, self.true_csd): self.assertAlmostEqual(estimated_csd, true_csd, places=1)
def test_KCSD_1D_zero_pot(self): """if measured potential is 0, the calculated potential should be 0""" params = { 'n_sources': 20, 'dist_density': 20, 'source_type': 'gauss_lim' } k_zero = KCSD(elec_pos=np.array([[1.0], [2.0], [3.0], [4.0], [5.0]]), sampled_pots=np.array([[0.0, 0.0, 0.0, 0.0, 0.0]]).T, params=params) k_zero.estimate_pots() self.assertAlmostEqual(norm(k_zero.solver.estimated_pots), 0.0, places=10)
def test_KCSD_1D_pot_reconstruction(self): """reconstructed pots should be similar to model pots""" params = {'sigma': self.sigma, 'source_type': 'gauss_lim', 'x_min': self.xmin, 'x_max': self.xmax, 'h': self.h, 'n_src': 20} k = KCSD(self.elec_pos, self.meas_pot, params) k.estimate_pots() """print np.max(k.estimated_pots) plot(k.space_X, k.estimated_pots) plot(self.x, self.true_pots) scatter(k.elec_pos, k.sampled_pots) show()""" for estimated_pot, true_pot in zip(k.solver.estimated_pots, self.true_pots): self.assertAlmostEqual(estimated_pot, true_pot, places=1)
def test_KCSD2D_zero_csd(self): elec_pos = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) pots = np.array([[0], [0], [0], [0]]) k = KCSD(elec_pos, pots) k.solver.estimate_csd() for csd in k.solver.estimated_csd.flatten(): self.assertAlmostEqual(csd, 0.0, places=5)
def test_KCSD_2D_zero_pot(self): elec_pos = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) pots = np.array([[0], [0], [0], [0]]) k = KCSD(elec_pos, pots) k.solver.estimate_pots() for pot in k.solver.estimated_pots.flatten(): self.assertAlmostEqual(pot, 0.0, places=5)
def test_KCSD_1D_cross_validation_two_electrodes(self): """cross validation should promote high lambdas in this case""" params = {'x_min': 0.0, 'x_max': 1.0, 'gdX': 0.1, 'source_type': 'gauss_lim'} k = KCSD(elec_pos=np.array([[0.2], [0.7]]), sampled_pots=np.array([[1.0, 0.5]]).T, params=params) k.estimate_pots() lambdas = np.array([0.1, 0.5, 1.0]) n_elec = k.solver.elec_pos.shape[0] index_generator = LeaveOneOut(n_elec, indices=True) k.solver.lambd = cv.choose_lambda(lambdas, k.solver.sampled_pots, k.solver.k_pot, k.solver.elec_pos, index_generator) self.assertEquals(k.solver.lambd, 1.0)
def test_KCSD_3D_zero_pot(self): """if the input pots are zero, estimated pots and csd should be zero""" elec_pos = np.array([(0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0), (0, 1, 1), (1, 1, 0), (1, 0, 1), (1, 1, 1)]) pots = np.array([[0], [0], [0], [0], [0], [0], [0], [0]]) params = { 'gdX': 0.2, 'gdY': 0.2, 'gdZ': 0.2, 'n_src': 10 } k = KCSD(elec_pos, pots, params) k.estimate_pots() k.estimate_csd() for pot in k.solver.estimated_pots.flatten(): self.assertAlmostEqual(pot, 0.0, places=5) for csd in k.solver.estimated_csd.flatten(): self.assertAlmostEqual(csd, 0.0, places=5)
def test_KCSD_1D_cross_validation_two_electrodes(self): """cross validation should promote high lambdas in this case""" params = { 'x_min': 0.0, 'x_max': 1.0, 'gdX': 0.1, 'source_type': 'gauss_lim' } k = KCSD(elec_pos=np.array([[0.2], [0.7]]), sampled_pots=np.array([[1.0, 0.5]]).T, params=params) k.estimate_pots() lambdas = np.array([0.1, 0.5, 1.0]) n_elec = k.solver.elec_pos.shape[0] index_generator = LeaveOneOut(n_elec) #, indices=True) k.solver.lambd = cv.choose_lambda(lambdas, k.solver.sampled_pots, k.solver.k_pot, k.solver.elec_pos, index_generator) self.assertEqual(k.solver.lambd, 1.0)
def test_KCSD_1D_csd_reconstruction(self): """reconstructed csd should be similar to model csd""" params = { 'sigma': self.sigma, 'source_type': 'gauss_lim', 'x_min': self.xmin, 'x_max': self.xmax, 'h': self.h } k = KCSD(self.elec_pos, self.meas_pot, params) k.estimate_csd() """print np.max(k.estimated_csd) plot(k.estimated_csd) plot(self.true_csd) show() print k.X_src""" for estimated_csd, true_csd in zip(k.solver.estimated_csd, self.true_csd): self.assertAlmostEqual(estimated_csd, true_csd, places=1)
def test_KCSD_1D_pot_reconstruction(self): """reconstructed pots should be similar to model pots""" params = { 'sigma': self.sigma, 'source_type': 'gauss_lim', 'x_min': self.xmin, 'x_max': self.xmax, 'h': self.h, 'n_src': 20 } k = KCSD(self.elec_pos, self.meas_pot, params) k.estimate_pots() """print np.max(k.estimated_pots) plot(k.space_X, k.estimated_pots) plot(self.x, self.true_pots) scatter(k.elec_pos, k.sampled_pots) show()""" for estimated_pot, true_pot in zip(k.solver.estimated_pots, self.true_pots): self.assertAlmostEqual(estimated_pot, true_pot, places=1)
def test_KCSD_1D_lambda_choice(self): """for potentials calculated from model, lambda < 1.0""" params = { 'sigma': self.sigma, 'source_type': 'gauss_lim', 'x_min': -5.0, 'x_max': 10.0, 'h': self.h } k = KCSD(self.elec_pos, self.meas_pot, params) lambdas = np.array([100.0 / 2**n for n in range(1, 20)]) n_elec = k.solver.elec_pos.shape[0] index_generator = LeaveOneOut(n_elec) #, indices=True) k.solver.lambd = cv.choose_lambda(lambdas, k.solver.sampled_pots, k.solver.k_pot, k.solver.elec_pos, index_generator) self.assertLess(k.solver.lambd, 1.0)
def test_KCSD_3D_zero_pot(self): """if the input pots are zero, estimated pots and csd should be zero""" elec_pos = np.array([(0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0), (0, 1, 1), (1, 1, 0), (1, 0, 1), (1, 1, 1)]) pots = np.array([[0], [0], [0], [0], [0], [0], [0], [0]]) params = {'gdX': 0.2, 'gdY': 0.2, 'gdZ': 0.2, 'n_src': 10} k = KCSD(elec_pos, pots, params) k.estimate_pots() k.estimate_csd() for pot in k.solver.estimated_pots.flatten(): self.assertAlmostEqual(pot, 0.0, places=5) for csd in k.solver.estimated_csd.flatten(): self.assertAlmostEqual(csd, 0.0, places=5)
def test_KCSD_1D_duplicated_electrode(self): """if two electrodes are at the same spot, it should raise exception""" with self.assertRaises(Exception): k = KCSD(elec_pos=np.array([[0], [0]]), sampled_pots=[[0], [0]])
def test_KCSD_1D_incorrect_electrode_number(self): """if there are more electrodes than pots, it should raise exception""" with self.assertRaises(Exception): k = KCSD(elec_pos=np.array([[0], [1], [2]]), sampled_pots=[[0], [1]])