def __init__(self, name='Neuron'): '''Creates an empty Neuron object. ''' self.soma = Soma.Soma() self.axon = list() self.apical = list() self.basal = list() self.undefined = list() self.name = name
def load_neuron(input_file, line_delimiter='\n', soma_type=None, user_tree_types=None, remove_duplicates=True): """ Io method to load an swc or h5 file into a Neuron object. """ tree_types = redefine_types(user_tree_types) # Definition of swc types from type_dict function if soma_type is None: soma_index = SOMA_TYPE 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 as exc: raise LoadNeuronError('Soma points not in the expected format') from exc # 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 as exc: raise LoadNeuronError('Cannot create connectivity, nodes not connected correctly.') from exc # 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, tree_types) return neuron
POP_PATH = os.path.join(_path, '../../../test_data/valid') # Filenames for testing basic_file = os.path.join(DATA_PATH, 'basic.swc') nosecids_file = os.path.join(DATA_PATH, 'basic_no_sec_ids.swc') sample_file = os.path.join(DATA_PATH, 'sample.swc') sample_h5_v1_file = os.path.join(DATA_PATH, 'sample_v1.h5') sample_h5_v2_file = os.path.join(DATA_PATH, 'sample_v2.h5') sample_h5_v0_file = os.path.join(DATA_PATH, 'sample_v0.h5') neuron_v1 = io.load_neuron(sample_h5_v1_file) neuron_v2 = io.load_neuron(sample_h5_v2_file) neuron1 = io.load_neuron(sample_file) 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])) def test_type_dict(): nt.ok_(io.TYPE_DCT == {'soma': 1, 'basal': 3, 'apical': 4, 'axon': 2})
def test_copy_soma(): soma1 = Soma.Soma(x=x1, y=y1, z=z1, d=d1) soma2 = soma1.copy_soma() nt.ok_(soma1.is_equal(soma2)) nt.ok_(soma1 != soma2)
def test_soma_is_equal(): soma1 = Soma.Soma(x=x1, y=y1, z=z1, d=d1) soma2 = Soma.Soma(x=x1, y=y1, z=z1, d=d1) nt.ok_(soma1.is_equal(soma2)) soma2 = Soma.Soma(x=x2, y=y1, z=z1, d=d1) nt.ok_(not soma1.is_equal(soma2))
def test_soma_init_(): soma1 = Soma.Soma(x=x1, y=y1, z=z1, d=d1) nt.ok_(np.allclose(soma1.x, x1)) nt.ok_(np.allclose(soma1.y, y1)) nt.ok_(np.allclose(soma1.z, z1)) nt.ok_(np.allclose(soma1.d, d1))
'''Test tmd.soma''' from nose import tools as nt import numpy as np from tmd.Soma import Soma soma = Soma.Soma([0.], [0.], [0.], [12.]) soma1 = Soma.Soma([0., 0., 0.], [0., 1., 0.], [0., 0., 1.], [0., 0., 0.]) def test_get_center(): nt.ok_(np.allclose(soma.get_center(), [0., 0., 0.])) nt.ok_(np.allclose(soma1.get_center(), [0., 1. / 3., 1. / 3.])) def test_get_diameter(): nt.ok_(soma.get_diameter() == 12.) nt.ok_(soma1.get_diameter() == 0.65403883526363049)
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 ''' import os from scipy import sparse as sp from scipy.sparse import csgraph as cs from tmd.utils import tree_type as td 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', '')) soma_ids = _np.where(_np.transpose(data)[1] == soma_index)[0] # 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 # 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