def saveConnections(self, file, gather=True, compatible_output=True): """ Save connections to file in a format suitable for reading in with a FromFileConnector. """ if isinstance(file, basestring): file = files.StandardTextFile(file, mode='w') lines = [] if not compatible_output: for c in self.connections: lines.append([c.source, c.target, c.weight, c.delay]) else: for c in self.connections: lines.append([self.pre.id_to_index(c.source), self.post.id_to_index(c.target), c.weight, c.delay]) if gather == True and self._simulator.state.num_processes > 1: all_lines = { self._simulator.state.mpi_rank: lines } all_lines = recording.gather_dict(all_lines) if self._simulator.state.mpi_rank == 0: lines = reduce(operator.add, all_lines.values()) elif self._simulator.state.num_processes > 1: file.rename('%s.%d' % (file.name, self._simulator.state.mpi_rank)) logger.debug("--- Projection[%s].__saveConnections__() ---" % self.label) if gather == False or self._simulator.state.mpi_rank == 0: file.write(lines, {'pre' : self.pre.label, 'post' : self.post.label}) file.close()
def saveConnections(self, file, gather=True, compatible_output=True): """ Save connections to file in a format suitable for reading in with a FromFileConnector. """ if isinstance(file, basestring): file = files.StandardTextFile(file, mode='w') lines = [] if not compatible_output: for c in self.connections: lines.append([c.source, c.target, c.weight, c.delay]) else: for c in self.connections: lines.append([ self.pre.id_to_index(c.source), self.post.id_to_index(c.target), c.weight, c.delay ]) if gather == True and self._simulator.state.num_processes > 1: all_lines = {self._simulator.state.mpi_rank: lines} all_lines = recording.gather_dict(all_lines) if self._simulator.state.mpi_rank == 0: lines = reduce(operator.add, all_lines.values()) elif self._simulator.state.num_processes > 1: file.rename('%s.%d' % (file.name, self._simulator.state.mpi_rank)) logger.debug("--- Projection[%s].__saveConnections__() ---" % self.label) if gather == False or self._simulator.state.mpi_rank == 0: file.write(lines, {'pre': self.pre.label, 'post': self.post.label}) file.close()
def saveConnections(self, file, gather=True, compatible_output=True): """ Save connections to file in a format suitable for reading in with a FromFileConnector. """ import operator if isinstance(file, basestring): file = files.StandardTextFile(file, mode='w') lines = nest.GetStatus(self.connections, ('source', 'target', 'weight', 'delay')) if gather == True and num_processes() > 1: all_lines = {rank(): lines} all_lines = recording.gather_dict(all_lines) if rank() == 0: lines = reduce(operator.add, all_lines.values()) elif num_processes() > 1: file.rename('%s.%d' % (file.name, rank())) logger.debug("--- Projection[%s].__saveConnections__() ---" % self.label) if gather == False or rank() == 0: lines = numpy.array(lines, dtype=float) lines[:, 2] *= 0.001 if compatible_output: lines[:, 0] = self.pre.id_to_index(lines[:, 0]) lines[:, 1] = self.post.id_to_index(lines[:, 1]) file.write(lines, {'pre': self.pre.label, 'post': self.post.label}) file.close()
def saveConnections(self, file, gather=True, compatible_output=True): """ Save connections to file in a format suitable for reading in with a FromFileConnector. """ import operator if isinstance(file, basestring): file = files.StandardTextFile(file, mode='w') lines = nest.GetStatus(self.connections, ('source', 'target', 'weight', 'delay')) if gather == True and num_processes() > 1: all_lines = { rank(): lines } all_lines = recording.gather_dict(all_lines) if rank() == 0: lines = reduce(operator.add, all_lines.values()) elif num_processes() > 1: file.rename('%s.%d' % (file.name, rank())) logger.debug("--- Projection[%s].__saveConnections__() ---" % self.label) if gather == False or rank() == 0: lines = numpy.array(lines, dtype=float) lines[:,2] *= 0.001 if compatible_output: lines[:,0] = self.pre.id_to_index(lines[:,0]) lines[:,1] = self.post.id_to_index(lines[:,1]) file.write(lines, {'pre' : self.pre.label, 'post' : self.post.label}) file.close()
def count(self, gather=False, filter=None): """ Return the number of data points for each cell, as a dict. This is mainly useful for spike counts or for variable-time-step integration methods. """ N = {} if self.variable == 'spikes': for id in self.filter_recorded(filter): N[id] = simulator.net.object(self.recorders[id]).spikeCount() else: raise Exception("Only implemented for spikes.") if gather and simulator.state.num_processes > 1: N = recording.gather_dict(N) return N
def get(self, attribute_names, format, gather=True, with_address=True): """ Get the values of a given attribute (weight or delay) for all connections in this Projection. `attribute_names`: name of the attributes whose values are wanted, or a list of such names. `format`: "list" or "array". With list format, returns a list of tuples. Each tuple contains the indices of the pre- and post-synaptic cell followed by the attribute values in the order given in `attribute_names`. Example:: >>> prj.get(["weight", "delay"], format="list")[:5] [(TODO)] With array format, returns a tuple of 2D NumPy arrays, one for each name in `attribute_names`. The array element X_ij contains the attribute value for the connection from the ith neuron in the pre- synaptic Population to the jth neuron in the post-synaptic Population, if a single such connection exists. If there are no such connections, X_ij will be NaN. If there are multiple such connections, the summed value will be given, which makes some sense for weights, but is pretty meaningless for delays. Example:: >>> weights, delays = prj.get(["weight", "delay"], format="array") >>> weights.shape TODO TODO: document "with_address" Values will be expressed in the standard PyNN units (i.e. millivolts, nanoamps, milliseconds, microsiemens, nanofarads, event per second). """ if isinstance(attribute_names, basestring): attribute_names = (attribute_names,) return_single = True else: return_single = False if isinstance(self.synapse_type, StandardSynapseType): attribute_names = self.synapse_type.get_native_names(*attribute_names) if format == 'list': names = list(attribute_names) if with_address: names = ["presynaptic_index", "postsynaptic_index"] + names values = self._get_attributes_as_list(*names) if gather and self._simulator.state.num_processes > 1: all_values = {self._simulator.state.mpi_rank: values} all_values = recording.gather_dict(all_values, all=(gather == 'all')) if gather == 'all' or self._simulator.state.mpi_rank == 0: values = reduce(operator.add, all_values.values()) if not with_address and return_single: values = [val[0] for val in values] return values elif format == 'array': if gather and self._simulator.state.num_processes > 1: # Node 0 is the only one creating a full connection matrix, and returning it (saving memory) # Slaves nodes are returning list of connections, so this may be inconsistent... names = list(attribute_names) names = ["presynaptic_index", "postsynaptic_index"] + names values = self._get_attributes_as_list(*names) all_values = {self._simulator.state.mpi_rank: values} all_values = recording.gather_dict(all_values, all=(gather == 'all')) if gather == 'all' or self._simulator.state.mpi_rank == 0: tmp_values = reduce(operator.add, all_values.values()) values = self._get_attributes_as_arrays(*attribute_names) tmp_values = numpy.array(tmp_values) for i in xrange(len(values)): values[i][tmp_values[:, 0].astype(int), tmp_values[:, 1].astype(int)] = tmp_values[:, 2 + i] else: values = self._get_attributes_as_arrays(*attribute_names) if return_single: if gather == 'all' or self._simulator.state.mpi_rank == 0: assert len(values) == 1, values return values[0] else: return values else: raise Exception("format must be 'list' or 'array'")
def get(self, attribute_names, format, gather=True, with_address=True, multiple_synapses='sum'): """ Get the values of a given attribute (weight or delay) for all connections in this Projection. `attribute_names`: name of the attributes whose values are wanted, or a list of such names. `format`: "list" or "array". `gather`: if True, get connection information from all MPI nodes, otherwise only from connections that exist in this node. With list format, returns a list of tuples. By default, each tuple contains the indices of the pre- and post-synaptic cell followed by the attribute values in the order given in `attribute_names`. Example:: >>> prj.get(["weight", "delay"], format="list")[:5] [(0.0, 0.0, 0.3401892507507171, 0.1), (0.0, 1.0, 0.7990713166233654, 0.30000000000000004), (0.0, 2.0, 0.6180841812877726, 0.5), (0.0, 3.0, 0.6758149775627305, 0.7000000000000001), (0.0, 4.0, 0.7166906726862953, 0.9)] If `with_address` is set to False, then the tuples will contain only the attribute values, not the cell indices. With array format, returns a tuple of 2D NumPy arrays, one for each name in `attribute_names`. The array element X_ij contains the attribute value for the connection from the ith neuron in the pre- synaptic Population to the jth neuron in the post-synaptic Population, if a single such connection exists. If there are no such connections, X_ij will be NaN. Example:: >>> weights, delays = prj.get(["weight", "delay"], format="array") >>> weights array([[ 0.66210438, nan, 0.10744555, 0.54557088], [ 0.3676134 , nan, 0.41463193, nan], [ 0.57434871, 0.4329354 , 0.58482943, 0.42863916]]) If there are multiple such connections, the action to take is controlled by the `multiple_synapses` argument, which must be one of {'last', 'first', 'sum', 'min', 'max'}. Values will be expressed in the standard PyNN units (i.e. millivolts, nanoamps, milliseconds, microsiemens, nanofarads, event per second). """ if isinstance(attribute_names, basestring): attribute_names = (attribute_names, ) return_single = True else: return_single = False if isinstance(self.synapse_type, StandardSynapseType): attribute_names = self.synapse_type.get_native_names( *attribute_names) if format == 'list': names = list(attribute_names) if with_address: names = ["presynaptic_index", "postsynaptic_index"] + names values = self._get_attributes_as_list(names) if gather and self._simulator.state.num_processes > 1: all_values = {self._simulator.state.mpi_rank: values} all_values = recording.gather_dict(all_values, all=(gather == 'all')) if gather == 'all' or self._simulator.state.mpi_rank == 0: values = reduce(operator.add, all_values.values()) if not with_address and return_single: values = [val[0] for val in values] return values elif format == 'array': if multiple_synapses not in Projection.MULTI_SYNAPSE_OPERATIONS: raise ValueError( "`multiple_synapses` argument must be one of {}".format( list(Projection.MULTI_SYNAPSE_OPERATIONS))) if gather and self._simulator.state.num_processes > 1: # Node 0 is the only one creating a full connection matrix, and returning it (saving memory) # Slaves nodes are returning list of connections, so this may be inconsistent... names = list(attribute_names) names = ["presynaptic_index", "postsynaptic_index"] + names values = self._get_attributes_as_list(names) all_values = {self._simulator.state.mpi_rank: values} all_values = recording.gather_dict(all_values, all=(gather == 'all')) if gather == 'all' or self._simulator.state.mpi_rank == 0: tmp_values = reduce(operator.add, all_values.values()) values = self._get_attributes_as_arrays( attribute_names, multiple_synapses=multiple_synapses) tmp_values = numpy.array(tmp_values) for i in xrange(len(values)): values[i][tmp_values[:, 0].astype(int), tmp_values[:, 1].astype(int)] = tmp_values[:, 2 + i] else: values = self._get_attributes_as_arrays( attribute_names, multiple_synapses=multiple_synapses) if return_single: if gather == 'all' or self._simulator.state.mpi_rank == 0: assert len(values) == 1, values return values[0] else: return values else: raise Exception("format must be 'list' or 'array'")
def get(self, attribute_names, format, gather=True, with_address=True, multiple_synapses='sum'): """ Get the values of a given attribute (weight or delay) for all connections in this Projection. `attribute_names`: name of the attributes whose values are wanted, or a list of such names. `format`: "list" or "array". `gather`: if True, get connection information from all MPI nodes, otherwise only from connections that exist in this node. With list format, returns a list of tuples. By default, each tuple contains the indices of the pre- and post-synaptic cell followed by the attribute values in the order given in `attribute_names`. Example:: >>> prj.get(["weight", "delay"], format="list")[:5] [(0.0, 0.0, 0.3401892507507171, 0.1), (0.0, 1.0, 0.7990713166233654, 0.30000000000000004), (0.0, 2.0, 0.6180841812877726, 0.5), (0.0, 3.0, 0.6758149775627305, 0.7000000000000001), (0.0, 4.0, 0.7166906726862953, 0.9)] If `with_address` is set to False, then the tuples will contain only the attribute values, not the cell indices. With array format, returns a tuple of 2D NumPy arrays, one for each name in `attribute_names`. The array element X_ij contains the attribute value for the connection from the ith neuron in the pre- synaptic Population to the jth neuron in the post-synaptic Population, if a single such connection exists. If there are no such connections, X_ij will be NaN. Example:: >>> weights, delays = prj.get(["weight", "delay"], format="array") >>> weights array([[ 0.66210438, nan, 0.10744555, 0.54557088], [ 0.3676134 , nan, 0.41463193, nan], [ 0.57434871, 0.4329354 , 0.58482943, 0.42863916]]) If there are multiple such connections, the action to take is controlled by the `multiple_synapses` argument, which must be one of {'last', 'first', 'sum', 'min', 'max'}. Values will be expressed in the standard PyNN units (i.e. millivolts, nanoamps, milliseconds, microsiemens, nanofarads, event per second). """ if isinstance(attribute_names, basestring): attribute_names = (attribute_names,) return_single = True else: return_single = False if isinstance(self.synapse_type, StandardSynapseType): attribute_names = self.synapse_type.get_native_names(*attribute_names) if format == 'list': names = list(attribute_names) if with_address: names = ["presynaptic_index", "postsynaptic_index"] + names values = self._get_attributes_as_list(names) if gather and self._simulator.state.num_processes > 1: all_values = {self._simulator.state.mpi_rank: values} all_values = recording.gather_dict(all_values, all=(gather == 'all')) if gather == 'all' or self._simulator.state.mpi_rank == 0: values = reduce(operator.add, all_values.values()) if not with_address and return_single: values = [val[0] for val in values] return values elif format == 'array': if multiple_synapses not in Projection.MULTI_SYNAPSE_OPERATIONS: raise ValueError("`multiple_synapses` argument must be one of {}".format(list(Projection.MULTI_SYNAPSE_OPERATIONS))) if gather and self._simulator.state.num_processes > 1: # Node 0 is the only one creating a full connection matrix, and returning it (saving memory) # Slaves nodes are returning list of connections, so this may be inconsistent... names = list(attribute_names) names = ["presynaptic_index", "postsynaptic_index"] + names values = self._get_attributes_as_list(names) all_values = {self._simulator.state.mpi_rank: values} all_values = recording.gather_dict(all_values, all=(gather == 'all')) if gather == 'all' or self._simulator.state.mpi_rank == 0: tmp_values = reduce(operator.add, all_values.values()) values = self._get_attributes_as_arrays(attribute_names, multiple_synapses=multiple_synapses) tmp_values = numpy.array(tmp_values) for i in xrange(len(values)): values[i][tmp_values[:, 0].astype(int), tmp_values[:, 1].astype(int)] = tmp_values[:, 2 + i] else: values = self._get_attributes_as_arrays(attribute_names, multiple_synapses=multiple_synapses) if return_single: if gather == 'all' or self._simulator.state.mpi_rank == 0: assert len(values) == 1, values return values[0] else: return values else: raise Exception("format must be 'list' or 'array'")