def testValues(self): # load trees self.loadTTree() self.loadSOVTTree() # test Fourrier impedance matrix ft = ke.FourrierTools(np.arange(0.,100.,0.1)) # set the impedances self.tree.setImpedance(ft.s) print('!!! ', ft.ind_0s) # sets of location # sets of location locs = [(1, .5), (4, .5), (4, 1.), (5, .5), (6, .5), (7, .5), (8, .5)] self.tree.storeLocs(locs, 'locs') self.sovtree.storeLocs(locs, 'locs') # compute impedance matrices with both methods z_sov = self.sovtree.calcImpedanceMatrix(locarg='locs', eps=1e-10) z_gf = self.tree.calcImpedanceMatrix('locs')[ft.ind_0s].real assert np.allclose(z_gf, z_sov, atol=5e-1) z_gf2 = self.tree.calcImpedanceMatrix('locs', explicit_method=False)[ft.ind_0s].real print('> z_gf =\n', z_gf) print('> z_gf2 =\n', z_gf2) assert np.allclose(z_gf2, z_gf, atol=5e-6) zf_sov = self.sovtree.calcImpedanceMatrix(locarg='locs', eps=1e-10, freqs=ft.s) zf_gf = self.tree.calcImpedanceMatrix('locs') assert np.allclose(zf_gf, zf_sov, atol=5e-1) zf_gf2 = self.tree.calcImpedanceMatrix('locs', explicit_method=False) assert np.allclose(zf_gf2, zf_gf, atol=5e-6) # load trees self.loadValidationTree() self.loadSOVValidationTree() # test Fourrier impedance matrix ft = ke.FourrierTools(np.arange(0.,100.,0.1)) # set the impedances self.tree.setImpedance(ft.s) # set of locations locs = [(1, .5), (4, .5), (4, 1.), (5, .5), (5, 1.)] self.tree.storeLocs(locs, 'locs') self.sovtree.storeLocs(locs, 'locs') # compute impedance matrices with both methods z_sov = self.sovtree.calcImpedanceMatrix(locarg='locs', eps=1e-10) z_gf = self.tree.calcImpedanceMatrix('locs')[ft.ind_0s].real assert np.allclose(z_gf, z_sov, atol=5e-1) z_gf2 = self.tree.calcImpedanceMatrix('locs', explicit_method=False)[ft.ind_0s].real assert np.allclose(z_gf2, z_gf, atol=5e-6) zf_sov = self.sovtree.calcImpedanceMatrix(locarg='locs', eps=1e-10, freqs=ft.s) zf_gf = self.tree.calcImpedanceMatrix('locs') assert np.allclose(zf_gf, zf_sov, atol=5e-1) zf_gf2 = self.tree.calcImpedanceMatrix('locs', explicit_method=False) assert np.allclose(zf_gf2, zf_gf, atol=5e-6)
def loadTTreeTestChannel(self): """ Load the T-tree morphology in memory with h-current 6--5--4--7--8 | | 1 """ v_eq = -75. self.dt = 0.025 self.tmax = 100. # for frequency derivation self.ft = ke.FourrierTools(np.arange(0., self.tmax, self.dt)) # load the morphology test_chan = channelcollection.TestChannel2() fname = os.path.join(MORPHOLOGIES_PATH_PREFIX, 'Tsovtree.swc') self.greenstree = GreensTree(fname, types=[1, 3, 4]) self.greenstree.addCurrent(test_chan, 50., -23.) self.greenstree.fitLeakCurrent(v_eq, 10.) self.greenstree.setCompTree() self.greenstree.setImpedance(self.ft.s) # copy greenstree parameters into NEURON simulation tree self.neurontree = NeuronSimTree(dt=self.dt, t_calibrate=100., v_init=v_eq, factor_lambda=25.) self.greenstree.__copy__(self.neurontree) self.neurontree.treetype = 'computational'
def loadTTreeActive(self): """ Load the T-tree morphology in memory with h-current 6--5--4--7--8 | | 1 """ v_eq = -75. self.dt = 0.1 self.tmax = 100. # for frequency derivation self.ft = ke.FourrierTools(np.arange(0., self.tmax, self.dt)) # load the morphology print('>>> loading T-tree <<<') h_chan = channelcollection.h() fname = 'test_morphologies/Tsovtree.swc' self.greenstree = GreensTree(fname, types=[1, 3, 4]) self.greenstree.addCurrent(h_chan, 50., -43.) self.greenstree.fitLeakCurrent(v_eq, 10.) self.greenstree.setCompTree() self.greenstree.setImpedance(self.ft.s) # copy greenstree parameters into NEURON simulation tree self.neurontree = NeuronSimTree(dt=self.dt, t_calibrate=10., v_init=v_eq, factor_lambda=25.) self.greenstree.__copy__(self.neurontree) self.neurontree.treetype = 'computational'
def loadTTreeActive(self): ''' Load the T-tree morphology in memory with h-current 6--5--4--7--8 | | 1 ''' v_eq = -75. self.dt = 0.025 self.tmax = 100. # for frequency derivation self.ft = ke.FourrierTools(np.arange(0., self.tmax, self.dt)) # load the morphology print '>>> loading T-tree <<<' fname = 'test_morphologies/Tsovtree.swc' self.greenstree = GreensTree(fname, types=[1,3,4]) self.greenstree.addCurrent('h', 50., -43.) self.greenstree.fitLeakCurrent(e_eq_target=v_eq, tau_m_target=10.) # for node in self.greenstree: # print node.getGTot(channel_storage=self.greenstree.channel_storage) # print node.currents self.greenstree.setCompTree() self.greenstree.setImpedance(self.ft.s) # copy greenstree parameters into NEURON simulation tree self.neurontree = neurm.NeuronSimTree(dt=self.dt, t_calibrate=10., v_eq=v_eq, factor_lambda=25.) self.greenstree.__copy__(self.neurontree) self.neurontree.treetype = 'computational'
def testBasicProperties(self): self.loadTTree() # test Fourrier impedance matrix ft = ke.FourrierTools(np.arange(0.,100.,0.1)) # set the impedances self.tree.setImpedance(ft.s) # sets of location locs_0 = [(6, .5), (8, .5)] zf = self.tree.calcZF(*locs_0) locs_1 = [(1, .5), (4, .5), (4, 1.), (5, .5), (6, .5), (7, .5), (8, .5)] locs_2 = [(7, .5), (8, .5)] self.tree.storeLocs(locs_0, '0') self.tree.storeLocs(locs_1, '1') self.tree.storeLocs(locs_2, '2') # compute impedance matrices z_mat_0 = self.tree.calcImpedanceMatrix('0')[ft.ind_0s] z_mat_1 = self.tree.calcImpedanceMatrix('1')[ft.ind_0s] z_mat_2 = self.tree.calcImpedanceMatrix('2')[ft.ind_0s] # check complex steady state component zero assert np.allclose(z_mat_0.imag, np.zeros_like(z_mat_0.imag)) assert np.allclose(z_mat_1.imag, np.zeros_like(z_mat_1.imag)) assert np.allclose(z_mat_2.imag, np.zeros_like(z_mat_2.imag)) # check symmetry assert np.allclose(z_mat_0, z_mat_0.T) assert np.allclose(z_mat_1, z_mat_1.T) assert np.allclose(z_mat_2, z_mat_2.T) # check symmetry directly assert np.allclose(self.tree.calcZF(locs_0[0], locs_0[1]), self.tree.calcZF(locs_0[1], locs_0[0])) assert np.allclose(self.tree.calcZF(locs_1[0], locs_1[3]), self.tree.calcZF(locs_1[3], locs_1[0])) assert np.allclose(self.tree.calcZF(locs_1[2], locs_1[5]), self.tree.calcZF(locs_1[5], locs_1[2])) # check transitivity z_14_ = self.tree.calcZF(locs_1[1], locs_1[3]) * \ self.tree.calcZF(locs_1[3], locs_1[4]) / \ self.tree.calcZF(locs_1[3], locs_1[3]) z_14 = self.tree.calcZF(locs_1[1], locs_1[4]) assert np.allclose(z_14, z_14_) z_06_ = self.tree.calcZF(locs_1[0], locs_1[5]) * \ self.tree.calcZF(locs_1[5], locs_1[6]) / \ self.tree.calcZF(locs_1[5], locs_1[5]) z_06 = self.tree.calcZF(locs_1[0], locs_1[6]) assert np.allclose(z_06, z_06_) z_46_ = self.tree.calcZF(locs_1[4], locs_1[2]) * \ self.tree.calcZF(locs_1[2], locs_1[6]) / \ self.tree.calcZF(locs_1[2], locs_1[2]) z_46 = self.tree.calcZF(locs_1[4], locs_1[6]) assert np.allclose(z_46, z_46_) z_n15_ = self.tree.calcZF(locs_1[1], locs_1[3]) * \ self.tree.calcZF(locs_1[3], locs_1[5]) / \ self.tree.calcZF(locs_1[3], locs_1[3]) z_15 = self.tree.calcZF(locs_1[1], locs_1[5]) assert not np.allclose(z_15, z_n15_)
def loadTTreeTestChannelSoma(self): """ Load the T-tree morphology in memory with h-current 6--5--4--7--8 | | 1 """ v_eq = -75. self.dt = 0.025 self.tmax = 100. # for frequency derivation self.ft = ke.FourrierTools(np.arange(0., self.tmax, self.dt)) # load the morphology print('>>> loading T-tree <<<') test_chan = channelcollection.TestChannel2() fname = 'test_morphologies/Tsovtree.swc' self.greenstree = GreensTree(fname, types=[1, 3, 4]) self.greenstree.addCurrent(test_chan, 50., 23., node_arg=[self.greenstree[1]]) self.greenstree.fitLeakCurrent(v_eq, 10.) # for node in self.greenstree: # print node.getGTot(channel_storage=self.greenstree.channel_storage) # print node.currents self.greenstree.setCompTree() self.greenstree.setImpedance(self.ft.s) # copy greenstree parameters into NEURON simulation tree self.neurontree = NeuronSimTree(dt=self.dt, t_calibrate=100., v_init=v_eq, factor_lambda=25.) self.greenstree.__copy__(self.neurontree) self.neurontree.treetype = 'computational'
def testSOVCalculation(self): # validate the calculation on analytical model self.loadValidationTree() # do SOV calculation self.tree.calcSOVEquations() alphas, gammas = self.tree.getSOVMatrices([(1, 0.5)]) # compute time scales analytically self.tree.treetype = 'computational' lambda_m_test = np.sqrt(self.tree[4].R_sov / \ (2.*self.tree[4].g_m*self.tree[4].r_a)) tau_m_test = self.tree[4].c_m / self.tree[4].g_m * 1e3 alphas_test = \ (1. + \ (np.pi * np.arange(20) * lambda_m_test / \ (self.tree[4].L_sov + self.tree[5].L_sov))**2) / \ tau_m_test # compare analytical and computed time scales assert np.allclose(alphas[:20], alphas_test) # compute the spatial mode functions analytically # import matplotlib.pyplot as pl # self.tree.distributeLocsUniform(dx=4., name='NET_eval') # alphas, gammas = self.tree.getSOVMatrices(self.tree.getLocs(name='NET_eval')) # for kk in range(5): # print 'tau_' + str(kk) + ' =', -1./alphas[kk].real # pl.plot(range(gammas.shape[1]), gammas[kk,:]) # pl.plot(range(gammas.shape[1]), g) # pl.show() ## TODO # test basic identities self.loadTTree() self.tree.calcSOVEquations(maxspace_freq=500) # sets of location locs_0 = [(6, .5), (8, .5)] locs_1 = [(1, .5), (4, .5), (4, 1.), (5, .5), (6, .5), (7, .5), (8, .5)] locs_2 = [(7, .5), (8, .5)] self.tree.storeLocs(locs_0, '0') self.tree.storeLocs(locs_1, '1') self.tree.storeLocs(locs_2, '2') # test mode importance imp_a = self.tree.getModeImportance(locs=locs_0) imp_b = self.tree.getModeImportance(name='0') imp_c = self.tree.getModeImportance(sov_data=self.tree.getSOVMatrices( locs=locs_0)) imp_d = self.tree.getModeImportance(sov_data=self.tree.getSOVMatrices( name='0')) assert np.allclose(imp_a, imp_b) assert np.allclose(imp_a, imp_c) assert np.allclose(imp_a, imp_d) assert np.abs(1. - np.max(imp_a)) < 1e-12 with pytest.raises(IOError): self.tree.getModeImportance() # test important modes imp_2 = self.tree.getModeImportance(name='2') assert not np.allclose(imp_a, imp_2) # test impedance matrix z_mat_a = self.tree.calcImpedanceMatrix( sov_data=self.tree.getImportantModes(name='1', eps=1e-10)) z_mat_b = self.tree.calcImpedanceMatrix(name='1', eps=1e-10) assert np.allclose(z_mat_a, z_mat_b) assert np.allclose(z_mat_a - z_mat_a.T, np.zeros(z_mat_a.shape)) for ii, z_row in enumerate(z_mat_a): assert np.argmax(z_row) == ii # test Fourrier impedance matrix ft = ke.FourrierTools(np.arange(0., 100., 0.1)) z_mat_ft = self.tree.calcImpedanceMatrix(name='1', eps=1e-10, freqs=ft.s) print z_mat_ft[ft.ind_0s, :, :] print z_mat_a assert np.allclose(z_mat_ft[ft.ind_0s,:,:].real, \ z_mat_a, atol=1e-1) # check steady state assert np.allclose(z_mat_ft - np.transpose(z_mat_ft, axes=(0,2,1)), \ np.zeros(z_mat_ft.shape)) # check symmetry assert np.allclose(z_mat_ft[:ft.ind_0s,:,:].real, \ z_mat_ft[ft.ind_0s+1:,:,:][::-1,:,:].real) # check real part even assert np.allclose(z_mat_ft[:ft.ind_0s,:,:].imag, \ -z_mat_ft[ft.ind_0s+1:,:,:][::-1,:,:].imag) # check imaginary part odd
def testChannelFit(self): self.loadBall() locs = [(1, 0.5)] e_eqs = [-75., -55., -35., -15.] # create compartment tree ctree = self.greens_tree.createCompartmentTree(locs) ctree.addCurrent(channelcollection.Na_Ta(), 50.) ctree.addCurrent(channelcollection.Kv3_1(), -85.) # create tree with only leak greens_tree_pas = self.greens_tree.__copy__() greens_tree_pas[1].currents = {'L': greens_tree_pas[1].currents['L']} greens_tree_pas.setCompTree() greens_tree_pas.setImpedance(self.freqs) # compute the passive impedance matrix z_mat_pas = greens_tree_pas.calcImpedanceMatrix(locs)[0] # create tree with only potassium greens_tree_k = self.greens_tree.__copy__() greens_tree_k[1].currents = {key: val for key, val in greens_tree_k[1].currents.items() \ if key != 'Na_Ta'} # compute potassium impedance matrices z_mats_k = [] for e_eq in e_eqs: greens_tree_k.setEEq(e_eq) greens_tree_k.setCompTree() greens_tree_k.setImpedance(self.freqs) z_mats_k.append(greens_tree_k.calcImpedanceMatrix(locs)) # create tree with only sodium greens_tree_na = self.greens_tree.__copy__() greens_tree_na[1].currents = {key: val for key, val in greens_tree_na[1].currents.items() \ if key != 'Kv3_1'} # create state variable expansion points svs = [] e_eqs_ = [] na_chan = greens_tree_na.channel_storage['Na_Ta'] for e_eq1 in e_eqs: sv1 = na_chan.computeVarinf(e_eq1) for e_eq2 in e_eqs: e_eqs_.append(e_eq2) sv2 = na_chan.computeVarinf(e_eq2) svs.append({'m': sv2['m'], 'h': sv1['h']}) # compute sodium impedance matrices z_mats_na = [] for ii, sv in enumerate(svs): greens_tree_na.setEEq(e_eqs[ii % len(e_eqs)]) greens_tree_na[1].setExpansionPoint('Na_Ta', sv) greens_tree_na.setCompTree() greens_tree_na.setImpedance(self.freqs) z_mats_na.append(greens_tree_na.calcImpedanceMatrix(locs)) # compute combined impedance matrices z_mats_comb = [] for e_eq in e_eqs: self.greens_tree.setEEq(e_eq) self.greens_tree.setCompTree() self.greens_tree.setImpedance(self.freqs) z_mats_comb.append(self.greens_tree.calcImpedanceMatrix(locs)) # passive fit ctree.computeGMC(z_mat_pas) # get SOV constants for capacitance fit sov_tree = greens_tree_pas.__copy__(new_tree=SOVTree()) sov_tree.setCompTree() sov_tree.calcSOVEquations() alphas, phimat, importance = sov_tree.getImportantModes( locarg=locs, sort_type='importance', eps=1e-12, return_importance=True) # fit the capacitances from SOV time-scales ctree.computeC(-alphas[0:1].real * 1e3, phimat[0:1, :].real, weights=importance[0:1]) ctree1 = copy.deepcopy(ctree) ctree2 = copy.deepcopy(ctree) ctree3 = copy.deepcopy(ctree) ctree4 = copy.deepcopy(ctree) # fit paradigm 1 --> separate impedance matrices and separate fits # potassium channel fit for z_mat_k, e_eq in zip(z_mats_k, e_eqs): ctree1.computeGSingleChanFromImpedance('Kv3_1', z_mat_k, e_eq, self.freqs, other_channel_names=['L']) ctree1.runFit() # sodium channel fit for z_mat_na, e_eq, sv in zip(z_mats_na, e_eqs_, svs): ctree1.computeGSingleChanFromImpedance('Na_Ta', z_mat_na, e_eq, self.freqs, sv=sv, other_channel_names=['L']) ctree1.runFit() # fit paradigm 2 --> separate impedance matrices, same fit for z_mat_k, e_eq in zip(z_mats_k, e_eqs): ctree2.computeGSingleChanFromImpedance( 'Kv3_1', z_mat_k, e_eq, self.freqs, all_channel_names=['Kv3_1', 'Na_Ta']) for z_mat_na, e_eq, sv in zip(z_mats_na, e_eqs_, svs): ctree2.computeGSingleChanFromImpedance( 'Na_Ta', z_mat_na, e_eq, self.freqs, sv=sv, all_channel_names=['Kv3_1', 'Na_Ta']) ctree2.runFit() # fit paradigm 3 --> same impedance matrices for z_mat_comb, e_eq in zip(z_mats_comb, e_eqs): ctree3.computeGChanFromImpedance(['Kv3_1', 'Na_Ta'], z_mat_comb, e_eq, self.freqs) ctree3.runFit() # fit paradigm 4 --> fit incrementally for z_mat_na, e_eq, sv in zip(z_mats_na, e_eqs_, svs): ctree4.computeGSingleChanFromImpedance('Na_Ta', z_mat_na, e_eq, self.freqs, sv=sv) ctree4.runFit() for z_mat_comb, e_eq in zip(z_mats_comb, e_eqs): ctree4.computeGSingleChanFromImpedance( 'Kv3_1', z_mat_comb, e_eq, self.freqs, other_channel_names=['Na_Ta', 'L']) ctree4.runFit() # test if correct keys = ['L', 'Na_Ta', 'Kv3_1'] # soma surface (cm) for total conductance calculation a_soma = 4. * np.pi * (self.greens_tree[1].R * 1e-4)**2 conds = np.array( [self.greens_tree[1].currents[key][0] * a_soma for key in keys]) # compartment models conductances cconds1 = np.array([ctree1[0].currents[key][0] for key in keys]) cconds2 = np.array([ctree2[0].currents[key][0] for key in keys]) cconds3 = np.array([ctree3[0].currents[key][0] for key in keys]) cconds4 = np.array([ctree4[0].currents[key][0] for key in keys]) assert np.allclose(conds, cconds1) assert np.allclose(conds, cconds2) assert np.allclose(conds, cconds3) assert np.allclose(conds, cconds4) # rename for further testing ctree = ctree1 # frequency array ft = ke.FourrierTools(np.linspace(0., 50., 100)) freqs = ft.s # compute impedance matrix v_h = -42. # original self.greens_tree.setEEq(v_h) self.greens_tree.setCompTree() self.greens_tree.setImpedance(freqs) z_mat_orig = self.greens_tree.calcImpedanceMatrix([(1., .5)]) # potassium greens_tree_k.setEEq(v_h) greens_tree_k.setCompTree() greens_tree_k.setImpedance(freqs) z_mat_k = greens_tree_k.calcImpedanceMatrix([(1, .5)]) # sodium greens_tree_na.removeExpansionPoints() greens_tree_na.setEEq(v_h) greens_tree_na.setCompTree() greens_tree_na.setImpedance(freqs) z_mat_na = greens_tree_na.calcImpedanceMatrix([(1, .5)]) # passive greens_tree_pas.setCompTree() greens_tree_pas.setImpedance(freqs) z_mat_pas = greens_tree_pas.calcImpedanceMatrix([(1, .5)]) # reduced impedance matrices ctree.removeExpansionPoints() ctree.setEEq(v_h) z_mat_fit = ctree.calcImpedanceMatrix(freqs=freqs) z_mat_fit_k = ctree.calcImpedanceMatrix(channel_names=['L', 'Kv3_1'], freqs=freqs) z_mat_fit_na = ctree.calcImpedanceMatrix(channel_names=['L', 'Na_Ta'], freqs=freqs) z_mat_fit_pas = ctree.calcImpedanceMatrix(channel_names=['L'], freqs=freqs) assert np.allclose(z_mat_orig, z_mat_fit) assert np.allclose(z_mat_k, z_mat_fit_k) assert np.allclose(z_mat_na, z_mat_fit_na) assert np.allclose(z_mat_pas, z_mat_fit_pas) # test total current, conductance sv = svs[-1] p_open = sv['m']**3 * sv['h'] # with p_open given g1 = ctree[0].getGTot(ctree.channel_storage, channel_names=['L', 'Na_Ta'], p_open_channels={'Na_Ta': p_open}) i1 = ctree[0].getGTot(ctree.channel_storage, channel_names=['L', 'Na_Ta'], p_open_channels={'Na_Ta': p_open}) # with expansion point given ctree.setExpansionPoints({'Na_Ta': sv}) g2 = ctree[0].getGTot(ctree.channel_storage, channel_names=['L', 'Na_Ta']) i2 = ctree[0].getGTot(ctree.channel_storage, channel_names=['L', 'Na_Ta']) # with e_eq given g3 = ctree[0].getGTot(ctree.channel_storage, v=e_eqs[-1], channel_names=['L', 'Na_Ta']) i3 = ctree[0].getGTot(ctree.channel_storage, v=e_eqs[-1], channel_names=['L', 'Na_Ta']) # with e_eq stored ctree.setEEq(e_eqs[-1]) g4 = ctree[0].getGTot(ctree.channel_storage, channel_names=['L', 'Na_Ta']) i4 = ctree[0].getGTot(ctree.channel_storage, channel_names=['L', 'Na_Ta']) # check if correct assert np.abs(g1 - g2) < 1e-10 assert np.abs(g1 - g3) < 1e-10 assert np.abs(g1 - g4) < 1e-10 assert np.abs(i1 - i2) < 1e-10 assert np.abs(i1 - i3) < 1e-10 assert np.abs(i1 - i4) < 1e-10 # compare current, conductance g_ = ctree[0].getGTot(ctree.channel_storage, channel_names=['Na_Ta']) i_ = ctree[0].getITot(ctree.channel_storage, channel_names=['Na_Ta']) assert np.abs(g_ * (e_eqs[-1] - ctree[0].currents['Na_Ta'][1]) - i_) < 1e-10 # test leak fitting self.greens_tree.setEEq(-75.) self.greens_tree.setCompTree() ctree.setEEq(-75.) ctree.removeExpansionPoints() ctree.fitEL() assert np.abs(ctree[0].currents['L'][1] - self.greens_tree[1].currents['L'][1]) < 1e-10