def __init__(self, source, start, stop, morphology, name=None): self.morphology = morphology if isinstance(source, SpatialSubgroup): source = source.source start += source.start stop += source.start Subgroup.__init__(self, source, start, stop, name)
def spatialneuron_segment(neuron, x): ''' Selects a segment from `SpatialNeuron` neuron, where x is a slice of either compartment indexes or distances. Note a: segment is not a `SpatialNeuron`, only a `Group`. ''' if not isinstance(x, slice): raise TypeError( 'Subgroups can only be constructed using slicing syntax') start, stop, step = x.start, x.stop, x.step if step is None: step = 1 if step != 1: raise IndexError('Subgroups have to be contiguous') if type(start) == type(1 * cm): # e.g. 10*um:20*um # Convert to integers (compartment numbers) morpho = neuron.morphology[x] start = morpho._origin stop = morpho._origin + len(morpho) if start >= stop: raise IndexError('Illegal start/end values for subgroup, %d>=%d' % (start, stop)) return Subgroup(neuron, start, stop)
def spatialneuron_segment(neuron, item): ''' Selects a segment from `SpatialNeuron` neuron, where item is a slice of either compartment indexes or distances. Note a: segment is not a `SpatialNeuron`, only a `Group`. ''' if not isinstance(item, slice): raise TypeError( 'Subgroups can only be constructed using slicing syntax') start, stop, step = item.start, item.stop, item.step if step is None: step = 1 if step != 1: raise IndexError('Subgroups have to be contiguous') if isinstance(start, Quantity): if not have_same_dimensions( start, meter) or not have_same_dimensions(stop, meter): raise DimensionMismatchError( 'Start and stop should have units of meter', start, stop) # Convert to integers (compartment numbers) indices = neuron.morphology.indices[item] start, stop = indices[0], indices[-1] + 1 if start >= stop: raise IndexError('Illegal start/end values for subgroup, %d>=%d' % (start, stop)) return Subgroup(neuron, start, stop)
def spatialneuron_segment(neuron, item): ''' Selects a segment from `SpatialNeuron` neuron, where item is a slice of either compartment indexes or distances. Note a: segment is not a `SpatialNeuron`, only a `Group`. ''' if isinstance(item, slice) and isinstance(item.start, Quantity): if item.step is not None: raise ValueError('Cannot specify a step size for slicing based' 'on length.') start, stop = item.start, item.stop if (not have_same_dimensions(start, meter) or not have_same_dimensions(stop, meter)): raise DimensionMismatchError('Start and stop should have units ' 'of meter', start, stop) # Convert to integers (compartment numbers) indices = neuron.morphology.indices[item] start, stop = indices[0], indices[-1] + 1 elif not isinstance(item, slice) and hasattr(item, 'indices'): start, stop = to_start_stop(item.indices[:], neuron._N) else: start, stop = to_start_stop(item, neuron._N) if start >= stop: raise IndexError('Illegal start/end values for subgroup, %d>=%d' % (start, stop)) return Subgroup(neuron, start, stop)
def spatialneuron_segment(neuron, item): """ Selects a segment from `SpatialNeuron` neuron, where item is a slice of either compartment indexes or distances. Note a: segment is not a `SpatialNeuron`, only a `Group`. """ if isinstance(item, slice) and isinstance(item.start, Quantity): if item.step is not None: raise ValueError("Cannot specify a step size for slicing based" "on length.") start, stop = item.start, item.stop if (not have_same_dimensions(start, meter) or not have_same_dimensions(stop, meter)): raise DimensionMismatchError("Start and stop should have units " "of meter", start, stop) # Convert to integers (compartment numbers) indices = neuron.morphology.indices[item] start, stop = indices[0], indices[-1] + 1 elif not isinstance(item, slice) and hasattr(item, 'indices'): start, stop = to_start_stop(item.indices[:], neuron._N) else: start, stop = to_start_stop(item, neuron._N) if isinstance(neuron, SpatialSubgroup): start += neuron.start stop += neuron.start if start >= stop: raise IndexError(f'Illegal start/end values for subgroup, {int(start)}>={int(stop)}') if isinstance(neuron, SpatialSubgroup): # Note that the start/stop values calculated above are always # absolute values, even for subgroups neuron = neuron.source return Subgroup(neuron, start, stop)
def __getitem__(self, item): if not isinstance(item, slice): raise TypeError('Subgroups can only be constructed using slicing syntax') start, stop, step = item.indices(self._N) if step != 1: raise IndexError('Subgroups have to be contiguous') if start >= stop: raise IndexError('Illegal start/end values for subgroup, %d>=%d' % (start, stop)) return Subgroup(self, start, stop)
def __getitem__(self, item): if not isinstance(item, slice): raise TypeError( "Subgroups can only be constructed using slicing syntax") start, stop, step = item.indices(self._N) if step != 1: raise IndexError("Subgroups have to be contiguous") if start >= stop: raise IndexError( f"Illegal start/end values for subgroup, {int(start)}>={int(stop)}" ) return Subgroup(self, start, stop)
def spatialneuron_attribute(neuron, x): ''' Selects a subtree from `SpatialNeuron` neuron and returns a `SpatialSubgroup`. If it does not exist, returns the `Group` attribute. ''' if x == 'main': # Main segment, without the subtrees origin = neuron.morphology._origin return Subgroup(neuron, origin, origin + len(neuron.morphology.x)) elif (x != 'morphology') and ((x in neuron.morphology._namedkid) or all([c in 'LR123456789' for c in x])): # subtree morpho = neuron.morphology[x] return SpatialSubgroup(neuron, morpho._origin, morpho._origin + len(morpho), morphology=morpho) else: return Group.__getattr__(neuron, x)
def __init__(self, source, start, stop, morphology, name=None): self.morphology = morphology Subgroup.__init__(self, source, start, stop, name)