Skip to content

Commit 7515f35

Browse files
committed
Fix connectors_benchmark.py [RFC/WIP]
PyNN: * use file mode `wb` in save_positions as we write binary data * zipfile now only accepts `w` (no `wb`) connectors_benchmark.py * `3` AllToAllConnector should not need delays? * `7` is somehow broken? Wrong save/load format? * `8` SmallWorldConnector doesn't seem to be supported by nest/mock
1 parent ab1b31f commit 7515f35

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

pyNN/common/populations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ def save_positions(self, file):
579579
Save positions to file. The output format is ``index x y z``
580580
"""
581581
if isinstance(file, basestring):
582-
file = recording.files.StandardTextFile(file, mode='w')
582+
file = recording.files.StandardTextFile(file, mode='wb')
583583
cells = self.all_cells
584584
result = numpy.empty((len(cells), 4))
585585
result[:, 0] = numpy.array([self.id_to_index(id) for id in cells])
@@ -1310,7 +1310,7 @@ def save_positions(self, file):
13101310
Save positions to file. The output format is id x y z
13111311
"""
13121312
if isinstance(file, basestring):
1313-
file = files.StandardTextFile(file, mode='w')
1313+
file = files.StandardTextFile(file, mode='wb')
13141314
cells = self.all_cells
13151315
result = numpy.empty((len(cells), 4))
13161316
result[:, 0] = numpy.array([self.id_to_index(id) for id in cells])

pyNN/recording/files.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
have_hdf5 = False
3030
from pyNN.core import iteritems
3131

32+
try:
33+
basestring
34+
except NameError:
35+
basestring = str
3236

3337
DEFAULT_BUFFER_SIZE = 10000
3438

@@ -61,7 +65,7 @@ def savez(file, *args, **kwds):
6165
raise ValueError("Cannot use un-named variables and keyword %s" % key)
6266
namedict[key] = val
6367

64-
zip = zipfile.ZipFile(file, mode="wb")
68+
zip = zipfile.ZipFile(file, mode="w")
6569

6670
# Place to write temporary .npy files
6771
# before storing them in the zip. We need to path this to have a working

test/benchmarks/connectors_benchmark.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def draw_rf(cell, positions, connections, color='k'):
1818
idx = numpy.where(connections[:, 1] == cell)[0]
1919
sources = connections[idx, 0]
2020
for src in sources:
21-
plot([positions[cell, 1], positions[src, 1]], [positions[cell, 2], positions[src, 2]], c=color)
21+
plot([positions[cell, 1], positions[int(src), 1]], [positions[cell, 2], positions[int(src), 2]], c=color)
2222

2323

2424
def distances(pos_1, pos_2, N):
@@ -73,28 +73,28 @@ def test(cases=[1]):
7373
synapse = StaticSynapse(weight=w, delay=delay)
7474
rng = NumpyRNG(23434, parallel_safe=parallel_safe)
7575

76-
if case is 1:
76+
if case == 1:
7777
conn = DistanceDependentProbabilityConnector(d_expression, safe=safe, callback=callback, allow_self_connections=autapse, rng=rng)
7878
fig_name = "DistanceDependent_%s_np_%d.png" % (simulator_name, np)
79-
elif case is 2:
79+
elif case == 2:
8080
conn = FixedProbabilityConnector(0.02, safe=safe, callback=callback, allow_self_connections=autapse, rng=rng)
8181
fig_name = "FixedProbability_%s_np_%d.png" % (simulator_name, np)
82-
elif case is 3:
83-
conn = AllToAllConnector(delays=delay, safe=safe, callback=callback, allow_self_connections=autapse)
82+
elif case == 3:
83+
conn = AllToAllConnector(safe=safe, callback=callback, allow_self_connections=autapse)
8484
fig_name = "AllToAll_%s_np_%d.png" % (simulator_name, np)
85-
elif case is 4:
85+
elif case == 4:
8686
conn = FixedNumberPostConnector(50, safe=safe, callback=callback, allow_self_connections=autapse, rng=rng)
8787
fig_name = "FixedNumberPost_%s_np_%d.png" % (simulator_name, np)
88-
elif case is 5:
88+
elif case == 5:
8989
conn = FixedNumberPreConnector(50, safe=safe, callback=callback, allow_self_connections=autapse, rng=rng)
9090
fig_name = "FixedNumberPre_%s_np_%d.png" % (simulator_name, np)
91-
elif case is 6:
91+
elif case == 6:
9292
conn = OneToOneConnector(safe=safe, callback=callback)
9393
fig_name = "OneToOne_%s_np_%d.png" % (simulator_name, np)
94-
elif case is 7:
95-
conn = FromFileConnector(files.NumpyBinaryFile('Results/connections.dat', mode='r'), safe=safe, callback=callback, distributed=True)
94+
elif case == 7:
95+
conn = FromFileConnector(files.NumpyBinaryFile('Results/connections.dat', mode='rb'), safe=safe, callback=callback, distributed=False)
9696
fig_name = "FromFile_%s_np_%d.png" % (simulator_name, np)
97-
elif case is 8:
97+
elif case == 8:
9898
conn = SmallWorldConnector(degree=0.1, rewiring=0., safe=safe, callback=callback, allow_self_connections=autapse)
9999
fig_name = "SmallWorld_%s_np_%d.png" % (simulator_name, np)
100100

@@ -110,7 +110,7 @@ def test(cases=[1]):
110110
if not(os.path.isdir('Results')):
111111
os.mkdir('Results')
112112
print("Saving Connections....")
113-
prj.save('all', files.NumpyBinaryFile('Results/connections.dat', mode='w'), gather=True)
113+
prj.save('all', files.NumpyBinaryFile('Results/connections.dat', mode='wb'), gather=True)
114114

115115
mytime = timer.diff()
116116
print("Time to save the projection:", mytime, 's')
@@ -126,7 +126,7 @@ def test(cases=[1]):
126126
positions = numpy.loadtxt('Results/positions.dat')
127127

128128
positions[:, 0] -= positions[:, 0].min()
129-
connections = files.NumpyBinaryFile('Results/connections.dat', mode='r').read()
129+
connections = files.NumpyBinaryFile('Results/connections.dat', mode='rb').read()
130130
print(positions.shape, connections.shape)
131131
connections[:, 0] -= connections[:, 0].min()
132132
connections[:, 1] -= connections[:, 1].min()
@@ -147,7 +147,7 @@ def test(cases=[1]):
147147
ids = numpy.random.permutation(positions[:, 0])[0:6]
148148
colors = ['k', 'r', 'b', 'g', 'c', 'y']
149149
for count, cell in enumerate(ids):
150-
draw_rf(cell, positions, connections, colors[count])
150+
draw_rf(int(cell), positions, connections, colors[int(count)])
151151
subplot(235)
152152
plot(d, connections[:, 2], '.')
153153

0 commit comments

Comments
 (0)