def load_neuron(input_file, line_delimiter='\n', soma_type=None, tree_types=None, remove_duplicates=True): ''' Io method to load an swc or h5 file into a Neuron object. TODO: Check if tree is connected to soma, otherwise do not include it in neuron structure and warn the user that there are disconnected components ''' if tree_types is not None: td.update(tree_types) # Definition of swc types from type_dict function if soma_type is None: soma_index = TYPE_DCT['soma'] else: soma_index = soma_type # Make neuron with correct filename and load data if os.path.splitext(input_file)[-1] == '.swc': data = swc_to_data( read_swc(input_file=input_file, line_delimiter=line_delimiter)) neuron = Neuron.Neuron(name=input_file.replace('.swc', '')) elif os.path.splitext(input_file)[-1] == '.h5': data = read_h5(input_file=input_file, remove_duplicates=remove_duplicates) neuron = Neuron.Neuron(name=input_file.replace('.h5', '')) try: soma_ids = _np.where(_np.transpose(data)[1] == soma_index)[0] except IndexError: raise LoadNeuronError('Soma points not in the expected format') # Extract soma information from swc soma = Soma.Soma(x=_np.transpose(data)[SWC_DCT['x']][soma_ids], y=_np.transpose(data)[SWC_DCT['y']][soma_ids], z=_np.transpose(data)[SWC_DCT['z']][soma_ids], d=_np.transpose(data)[SWC_DCT['radius']][soma_ids]) # Save soma in Neuron neuron.set_soma(soma) p = _np.array(_np.transpose(data)[6], dtype=int) - _np.transpose(data)[0][0] # return p, soma_ids try: dA = sp.csr_matrix((_np.ones(len(p) - len(soma_ids)), (range(len(soma_ids), len(p)), p[len(soma_ids):])), shape=(len(p), len(p))) except Exception: raise LoadNeuronError( 'Cannot create connectivity, nodes not connected correctly.') # assuming soma points are in the beginning of the file. comp = cs.connected_components(dA[len(soma_ids):, len(soma_ids):]) # Extract trees for i in range(comp[0]): tree_ids = _np.where(comp[1] == i)[0] + len(soma_ids) tree = make_tree(data[tree_ids]) neuron.append_tree(tree, td=td) return neuron
def test_neuron_set_soma(): neu1 = Neuron.Neuron() neu1.set_soma(soma_test) nt.ok_(neu1.soma.is_equal(soma_test))
def test_neuron_rename(): neu1 = Neuron.Neuron() neu1.rename('test') nt.ok_(neu1.name == 'test')
def test_copy_neuron(): neu1 = Neuron.Neuron() neu2 = neu1.copy_neuron() nt.ok_(neu1.is_equal(neu2)) nt.ok_(neu1 != neu2)
import numpy as np from tmd.Neuron import Neuron from tmd.Soma import Soma from tmd.Tree import Tree from tmd.utils import TREE_TYPE_DICT as td soma_test = Soma.Soma([0.],[0.],[0.],[12.]) soma_test1 = Soma.Soma([0.],[0.],[0.],[6.]) apical_test = Tree.Tree(x=np.array([5., 5.]), y=np.array([6., 6.]), z=np.array([ 7., 7.]), d=np.array([16., 16.]), t=np.array([4, 4]), p=np.array([-1, 0])) basal_test = Tree.Tree(x=np.array([4.]), y=np.array([5.]), z=np.array([6.]), d=np.array([14.]), t=np.array([3]), p=np.array([-1])) axon_test = Tree.Tree(x=np.array([ 3.]), y=np.array([4.]), z=np.array([5.]), d=np.array([12.]), t=np.array([2 ]), p=np.array([-1])) neu_test = Neuron.Neuron() neu_test.set_soma(soma_test) neu_test.append_tree(apical_test, td) def test_neuron_init_(): neu1 = Neuron.Neuron() nt.ok_(neu1.name == 'Neuron') nt.ok_(isinstance(neu1.soma, Soma.Soma)) nt.ok_(neu1.axon == []) nt.ok_(neu1.basal == []) nt.ok_(neu1.apical == []) nt.ok_(neu1.neurites == []) neu1 = Neuron.Neuron(name='test') nt.ok_(neu1.name == 'test') def test_neuron_rename():