def __init__( self, file, # @ReservedAssignment distributed=False, safe=True, verbose=False): real_file = file opened_file = False if isinstance(file, basestring): real_file = files.StandardTextFile(file, mode="r") opened_file = True conn_list = None if distributed: directory = os.path.dirname(real_file.file) filename = "{}.".format(os.path.basename(real_file.file)) conns = list() for found_file in os.listdir(directory): if found_file.startswith(filename): file_reader = files.StandardTextFile(found_file, mode="r") conns.append(file_reader.read()) file_reader.close() conn_list = numpy.concatenate(conns) else: conn_list = real_file.read() if opened_file: real_file.close() FromListConnector.__init__(self, conn_list, safe, verbose)
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 = numpy.empty((len(self), 4)) synapses = [] for count, ranges in enumerate(self.connections): idx = range(self._ranges[count], self._ranges[count + 1]) lines[idx, 0] = simulator.state.sim.get_sources( xrange(ranges[0], ranges[1])) lines[idx, 1] = simulator.state.sim.get_targets( xrange(ranges[0], ranges[1])) lines[idx, 2] = simulator.state.sim.get_weights( xrange(ranges[0], ranges[1])) lines[idx, 3] = simulator.state.sim.get_delays( xrange(ranges[0], ranges[1])) 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 write(self, file=None, gather=False, compatible_output=True, filter=None): """Write recorded data to file.""" file = file or self.file if isinstance(file, basestring): filename = file #rename_existing(filename) if gather == False and simulator.state.num_processes > 1: filename += '.%d' % simulator.state.mpi_rank else: filename = file.name logger.debug( "Recorder is writing '%s' to file '%s' with gather=%s and compatible_output=%s" % (self.variable, filename, gather, compatible_output)) data = self.get(gather, compatible_output, filter) metadata = self.metadata logger.debug("data has size %s" % str(data.size)) if simulator.state.mpi_rank == 0 or gather == False: if compatible_output: data = self._make_compatible(data) # Open the output file, if necessary and write the data logger.debug("Writing data to file %s" % file) if isinstance(file, basestring): file = files.StandardTextFile(filename, mode='w') file.write(data, metadata) 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. """ lines = numpy.empty((len(self), 4)) padding = 0 for key in self._brian_connections.keys(): bc = self._brian_connections[key] size = bc.W.getnnz() lines[padding:padding + size, 0], lines[padding:padding + size, 1] = self._indices[key] lines[padding:padding + size, 2] = bc.W.alldata / bc.weight_units if isinstance(bc, brian.DelayConnection): lines[padding:padding + size, 3] = bc.delay.alldata / ms else: lines[padding:padding + size, 3] = bc.delay * bc.source.clock.dt / ms padding += size logger.debug("--- Projection[%s].__saveConnections__() ---" % self.label) if isinstance(file, basestring): file = files.StandardTextFile(file, mode='w') file.write(lines, {'pre': self.pre.label, 'post': self.post.label}) file.close()
def get_reader(self, file): """ get a filereader object using the pynn methods :return: A pynn StandardTextFile or similar """ return files.StandardTextFile(file, mode="r")
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 __init__(self, file, distributed=False, safe=True, callback=None): """ Create a new connector. """ Connector.__init__(self, safe=safe, callback=callback) if isinstance(file, str): file = files.StandardTextFile(file, mode='r') self.file = file self.distributed = distributed
def test_StandardTextFile_read(): files.open = Mock() stf = files.StandardTextFile("filename", "w") orig_loadtxt = numpy.loadtxt numpy.loadtxt = Mock() stf.read() numpy.loadtxt.assert_called_with(stf.fileobj) numpy.loadtxt = orig_loadtxt files.open = builtin_open
def test_StandardTextFile_write(): files.open = Mock() stf = files.StandardTextFile("filename", "w") data = [(0, 2.3), (1, 3.4), (2, 4.3)] metadata = {'a': 1, 'b': 9.99} target = [(('# a = 1\n# b = 9.99\n', ), {}), (('0.0\t2.3\n', ), {}), (('1.0\t3.4\n', ), {}), (('2.0\t4.3\n', ), {})] stf.write(data, metadata) assert_equal(stf.fileobj.write.call_args_list, target) files.open = builtin_open
def printDelays(self, file, format='list', gather=True): """ Print synaptic weights to file. In the array format, zeros are printed for non-existent connections. """ delays = self.getDelays(format=format, gather=gather) if isinstance(file, basestring): file = files.StandardTextFile(file, mode='w') if format == 'array': delays = numpy.where(numpy.isnan(delays), 0.0, delays) file.write(delays, {}) file.close()
def __init__(self, file, distributed=False, safe=True, verbose=False): """ Create a new connector. `file` -- file object containing a list of connections, in the format required by `FromListConnector`. `distributed` -- if this is True, then each node will read connections from a file called `filename.x`, where `x` is the MPI rank. This speeds up loading connections for distributed simulations. """ Connector.__init__(self, 0.0, None, safe=safe, verbose=verbose) if isinstance(file, basestring): file = files.StandardTextFile(file, mode='r') self.file = file self.distributed = distributed
def get_reader(self, file): # @ReservedAssignment """ Get a file reader object using the PyNN methods. :return: A pynn StandardTextFile or similar """ return files.StandardTextFile(file, mode="r")