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 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_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_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_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_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)