def _calc_indices(self, item): ''' Convert the neuron indices to indices into the stored values. For example, if neurons [0, 5, 10] have been recorded, [5, 10] is converted to [1, 2]. ''' dtype = get_dtype(item) # scalar value if np.issubdtype( dtype, np.signedinteger) and not isinstance(item, np.ndarray): indices = np.nonzero(self.monitor.record == item)[0] if len(indices) == 0: raise IndexError('Index number %d has not been recorded' % item) return indices[0] if self.monitor.record_all: return item indices = [] for index in item: if index in self.monitor.record: indices.append(np.nonzero(self.monitor.record == index)[0][0]) else: raise IndexError('Index number %d has not been recorded' % index) return np.array(indices)
def __getitem__(self, item): dtype = get_dtype(item) if np.issubdtype(dtype, np.int): return StateMonitorView(self, item) elif isinstance(item, collections.Sequence): index_array = np.array(item) if not np.issubdtype(index_array.dtype, np.int): raise TypeError('Index has to be an integer or a sequence ' 'of integers') return StateMonitorView(self, item) else: raise TypeError('Cannot use object of type %s as an index' % type(item))
def __getitem__(self, item): dtype = get_dtype(item) if np.issubdtype(dtype, np.signedinteger): return StateMonitorView(self, item) elif isinstance(item, Sequence): index_array = np.array(item) if not np.issubdtype(index_array.dtype, np.signedinteger): raise TypeError("Index has to be an integer or a sequence " "of integers") return StateMonitorView(self, item) elif hasattr(item, '_indices'): # objects that support the indexing interface will return absolute # indices but here we need relative ones # TODO: How to we prevent the use of completely unrelated objects here? source_offset = getattr(self.source, '_offset', 0) return StateMonitorView(self, item._indices() - source_offset) else: raise TypeError(f'Cannot use object of type {type(item)} as an index')
def __getitem__(self, item): dtype = get_dtype(item) if np.issubdtype(dtype, np.int): return StateMonitorView(self, item) elif isinstance(item, collections.Sequence): index_array = np.array(item) if not np.issubdtype(index_array.dtype, np.int): raise TypeError('Index has to be an integer or a sequence ' 'of integers') return StateMonitorView(self, item) elif hasattr(item, '_indices'): # objects that support the indexing interface will return absolute # indices but here we need relative ones # TODO: How to we prevent the use of completely unrelated objects here? source_offset = getattr(self.source, '_offset', 0) return StateMonitorView(self, item._indices() - source_offset) else: raise TypeError('Cannot use object of type %s as an index' % type(item))
def _calc_indices(self, item): ''' Convert the neuron indices to indices into the stored values. For example, if neurons [0, 5, 10] have been recorded, [5, 10] is converted to [1, 2]. ''' dtype = get_dtype(item) # scalar value if np.issubdtype(dtype, np.int) and not isinstance(item, np.ndarray): indices = np.nonzero(self.monitor.record == item)[0] if len(indices) == 0: raise IndexError('Index number %d has not been recorded' % item) return indices[0] if self.monitor.record_all: return item indices = [] for index in item: if index in self.monitor.record: indices.append(np.nonzero(self.monitor.record == index)[0][0]) else: raise IndexError('Index number %d has not been recorded' % index) return np.array(indices)