def _filepath(self, name): """ File path to `name` morphology file. """ if self.file_ext is None: candidates = glob.glob(os.path.join(self.directory, name + ".*")) try: return next(filter(_is_morphology_file, candidates)) except StopIteration: raise NeuroMError("Can not find morphology file for '%s' " % name) else: return os.path.join(self.directory, name + self.file_ext)
def load_data(filename): '''Unpack data into a raw data wrapper''' ext = os.path.splitext(filename)[1].lower() if ext not in _READERS: raise NeuroMError('Do not have a loader for "%s" extension' % ext) try: return _READERS[ext](filename) except Exception: L.exception('Error reading file %s, using "%s" loader', filename, ext) raise RawDataError('Error reading file %s' % filename)
def load_data(handle, reader=None): '''Unpack data into a raw data wrapper''' if not reader: reader = os.path.splitext(handle)[1][1:].lower() if reader not in _READERS: raise NeuroMError('Do not have a loader for "%s" extension' % reader) filename = _get_file(handle) try: return _READERS[reader](filename) except Exception as e: L.exception('Error reading file %s, using "%s" loader', filename, reader) raise RawDataError('Error reading file %s:\n%s' % (filename, str(e)))
def has_no_dangling_branch(neuron): """Check if the neuron has dangling neurites. Are considered dangling - dendrites whose first point is too far from the soma center - axons whose first point is too far from the soma center AND from any point belonging to a dendrite Arguments: neuron(Neuron): The neuron object to test Returns: CheckResult with a list of all first segments of dangling neurites """ if len(neuron.soma.points) == 0: raise NeuroMError( 'Can\'t check for dangling neurites if there is no soma') soma_center = neuron.soma.points[:, COLS.XYZ].mean(axis=0) recentered_soma = neuron.soma.points[:, COLS.XYZ] - soma_center radius = np.linalg.norm(recentered_soma, axis=1) soma_max_radius = radius.max() dendritic_points = np.array( list( chain.from_iterable(n.points for n in iter_neurites(neuron) if n.type != NeuriteType.axon))) def is_dangling(neurite): """Is the neurite dangling ?.""" starting_point = neurite.points[0][COLS.XYZ] if np.linalg.norm(starting_point - soma_center) - soma_max_radius <= 12.: return False if neurite.type != NeuriteType.axon: return True distance_to_dendrites = np.linalg.norm(dendritic_points[:, COLS.XYZ] - starting_point, axis=1) return np.all( distance_to_dendrites >= 2 * dendritic_points[:, COLS.R] + 2) bad_ids = [(n.root_node.id, [n.root_node.points[0]]) for n in iter_neurites(neuron) if is_dangling(n)] return CheckResult(len(bad_ids) == 0, bad_ids)
def load_neurons(neurons, neuron_loader=load_neuron, name=None, population_class=Population, ignored_exceptions=()): """Create a population object. From all morphologies in a directory of from morphologies in a list of file names. Arguments: neurons: directory path or list of neuron file paths neuron_loader: function taking a filename and returning a neuron population_class: class representing populations name (str): optional name of population. By default 'Population' or\ filepath basename depending on whether neurons is list or\ directory path respectively. ignored_exceptions (tuple): NeuroM and MorphIO exceptions that you want to ignore when loading neurons. Returns: neuron population object """ if isinstance(neurons, str): neurons = Path(neurons) if isinstance(neurons, Path): files = get_files_by_path(neurons) name = name or neurons.name else: files = neurons name = name or 'Population' ignored_exceptions = tuple(ignored_exceptions) pop = [] for f in files: try: pop.append(neuron_loader(f)) except (NeuroMError, MorphioError) as e: if isinstance(e, ignored_exceptions): L.info('Ignoring exception "%s" for file %s', e, f.name) continue raise NeuroMError('`load_neurons` failed') from e return population_class(pop, name=name)
def _register_feature(namespace, name, func, shape): """Register a feature to be applied. Upon registration, an attribute 'shape' containing the expected shape of the function return is added to 'func'. Arguments: namespace(string): a namespace (must be 'NEURITEFEATURES' or 'NEURONFEATURES') name(string): name of the feature, used to access the feature via `neurom.features.get()`. func(callable): single parameter function of a neurite. shape(tuple): the expected shape of the feature values """ setattr(func, 'shape', shape) assert namespace in {'NEURITEFEATURES', 'NEURONFEATURES'} feature_dict = globals()[namespace] if name in feature_dict: raise NeuroMError('Attempt to hide registered feature %s' % name) feature_dict[name] = func
def _find_feature_func(feature_name): """Returns the python function used when getting a feature with `neurom.get(feature_name)`.""" for feature_dict in (NEURITEFEATURES, NEURONFEATURES): if feature_name in feature_dict: return feature_dict[feature_name] raise NeuroMError(f'Unable to find feature: {feature_name}')
def _raise_if_not_bifurcation(section): n_children = len(section.children) if n_children != 2: raise NeuroMError( 'A bifurcation point must have exactly 2 children, found {}'. format(n_children))