| | 435 | def saveConnections(self, filename, gather=True, compatible_output=True): |
| | 436 | """ |
| | 437 | Save connections to file in a format suitable for reading in with a |
| | 438 | FromFileConnector. |
| | 439 | """ |
| | 440 | import operator |
| | 441 | if compatible_output: |
| | 442 | fmt = "%s%s\t%s%s\t%s\t%s\n" % (self.pre.label, "%s", self.post.label, |
| | 443 | "%s", "%g", "%g") |
| | 444 | else: |
| | 445 | fmt = "%s\t%s\t%s\t%s\n" % ("%d", "%d", "%g", "%g") |
| | 446 | lines = [] |
| | 447 | connections = nest.FindConnections(self.connection_manager.sources, synapse_type=self.connection_manager.synapse_model) |
| | 448 | res = nest.GetStatus(connections, ('source', 'target', 'weight', 'delay')) |
| | 449 | |
| | 450 | if not compatible_output: |
| | 451 | for c in res: |
| | 452 | line = fmt % (c[0], c[1], 0.001*c[2], c[3]) |
| | 453 | lines.append(line) |
| | 454 | else: |
| | 455 | for c in res: |
| | 456 | line = fmt % (self.pre.locate(c[0]), self.post.locate(c[1]), 0.001*c[2], c[3]) |
| | 457 | line = line.replace('(','[').replace(')',']') |
| | 458 | lines.append(line) |
| | 459 | if gather == True and num_processes() > 1: |
| | 460 | all_lines = { rank(): lines } |
| | 461 | all_lines = recording.gather_dict(all_lines) |
| | 462 | if rank() == 0: |
| | 463 | lines = reduce(operator.add, all_lines.values()) |
| | 464 | elif num_processes() > 1: |
| | 465 | filename += '.%d' % rank() |
| | 466 | logger.debug("--- Projection[%s].__saveConnections__() ---" % self.label) |
| | 467 | if gather == False or rank() == 0: |
| | 468 | f = open(filename, 'w') |
| | 469 | f.writelines(lines) |
| | 470 | f.close() |