Skip to content

Commit

Permalink
Merge pull request #98 from NeuralEnsemble/fix/update-py3
Browse files Browse the repository at this point in the history
fix: update for py3
  • Loading branch information
pgleeson authored Mar 18, 2021
2 parents 18361d2 + 9deb151 commit a0b3080
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 62 deletions.
7 changes: 6 additions & 1 deletion neuroml/arraymorph_load_time_benchmark.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import numpy as np
import neuroml
import neuroml.arraymorph as am


class Benchmark:
def __init__(num_segments):
def __init__(self, num_segments):
self.num_segments = num_segments

def set_up(self):
Expand Down
110 changes: 52 additions & 58 deletions neuroml/benchmarks/arraymorph_benchmarks.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""
Benchmarks for reading and writing arraymorphs.
"""
from __future__ import print_function
import numpy as np
from neuroml import arraymorph as am
import neuroml
import neuroml.writers
import tempfile
from matplotlib import pyplot as plt
import time
import time
import os
from os import path


def timeit(method):

Expand All @@ -18,12 +19,12 @@ def timed(*args, **kw):
result = method(*args, **kw)
te = time.time()

print '%r (%r, %r) %2.2f sec' % \
(method.__name__, args, kw, te-ts)
print('%r (%r, %r) %2.2f sec' % (method.__name__, args, kw, te-ts))
return te-ts

return timed


class AMWriteBenchmark(object):
"""
TODO: Get rid of methods which are not used
Expand All @@ -38,11 +39,11 @@ def __init__(self,num_segments=1e6):
d = np.linspace(1,0.01,num_vertices)

vertices = np.array([x,y,z,d]).T

connectivity = range(-1,num_segments)

big_arraymorph = am.ArrayMorphology(vertices = vertices,
connectivity = connectivity)
big_arraymorph = am.ArrayMorphology(vertices=vertices,
connectivity=connectivity)

self.big_arraymorph = big_arraymorph

Expand All @@ -57,10 +58,10 @@ def __init__(self,num_segments=1e6):
self.__write_time = None

self.num_segments = num_segments

@property
def write_time(self):
if self.__write_time == None:
if self.__write_time is None:
print("Benchmark has not been executed")
else:
return self.__write_time
Expand All @@ -77,7 +78,6 @@ def run_json(self):
def run_hdf5(self):
self.test_write_big_arraymorph_hdf5()


def test_write_big_arraymorph_json(self):
writer_method = neuroml.writers.JSONWriter.write
fh,filename = tempfile.mkstemp()
Expand All @@ -87,10 +87,10 @@ def test_write_big_arraymorph_json(self):
except:
self.fail("Exception raised!")

print 'JSON Number of segments:'
print self.num_segments
print 'JSON size in bytes:'
print self.file_size(filename)
print('JSON Number of segments:')
print(self.num_segments)
print('JSON size in bytes:')
print(self.file_size(filename))

os.close(fh)

Expand All @@ -103,17 +103,16 @@ def test_write_big_arraymorph_neuroml(self):
except:
self.fail("Exception raised!")

print 'NeuroML (XML) Number of segments:'
print self.num_segments
print 'NeuroML (XML) size in bytes:'
print self.file_size(filename)
print('NeuroML (XML) Number of segments:')
print(self.num_segments)
print('NeuroML (XML) size in bytes:')
print(self.file_size(filename))

os.close(fh)

def file_size(self,path):
return os.path.getsize(path)


def test_write_big_arraymorph_hdf5(self):
writer_method = neuroml.writers.ArrayMorphWriter.write
fh,filename = tempfile.mkstemp()
Expand All @@ -123,10 +122,10 @@ def test_write_big_arraymorph_hdf5(self):
except:
self.fail("Exception raised!")

print 'HDF5 Number of segments:'
print self.num_segments
print 'HDF5 size in bytes:'
print self.file_size(filename)
print('HDF5 Number of segments:')
print(self.num_segments)
print('HDF5 size in bytes:')
print(self.file_size(filename))

os.close(fh)

Expand All @@ -148,10 +147,9 @@ def test_write_expected(self):
physical_masks_equal = np.testing.assert_array_equal(array_morph.physical_mask,self.big_arraymorph.physical_mask)
vertices_equal = np.testing.assert_array_equal(array_morph.vertices,self.big_arraymorph.vertices)


self.assertEqual(connectivity_equal,None) #None when equal
self.assertEqual(physical_masks_equal,None) #None when equal
self.assertEqual(vertices_equal,None) #None when equal
self.assertEqual(connectivity_equal,None) # None when equal
self.assertEqual(physical_masks_equal,None) # None when equal
self.assertEqual(vertices_equal,None)# None when equal

def test_write_multiple_morphologies(self):
filename = tempfile.mkstemp()[1]
Expand All @@ -173,9 +171,6 @@ def test_write_multiple_morphologies(self):
self.assertIsInstance(document,neuroml.NeuroMLDocument)





def benchmark_arraymorph_writer():
"""
TODO: Add NeuroML Document benchmark
Expand All @@ -197,12 +192,12 @@ def benchmark_arraymorph_writer():
neuroml_num_segments_list = []

for i in range(30):
print "test %d" % (i)
print("test %d" % (i))

neuroml_num_segments_factor = 4e2
json_num_segments_factor = 4e2
hdf5_num_segments_factor = 4e2

neuroml_num_segments = i * neuroml_num_segments_factor
json_num_segments = i * json_num_segments_factor
hdf5_num_segments = i * hdf5_num_segments_factor
Expand Down Expand Up @@ -231,7 +226,6 @@ def benchmark_arraymorph_writer():
np.savetxt("json_results.csv", json_results, delimiter=",")
np.savetxt("hdf5_results.csv", hdf5_results, delimiter=",")


neuroml_runtimes_averaged = np.mean(neuroml_results,axis=0)
json_runtimes_averaged = np.mean(json_results,axis=0)
hdf5_runtimes_averaged = np.mean(hdf5_results,axis=0)
Expand All @@ -244,44 +238,43 @@ def benchmark_arraymorph_writer():
json_num_segments_list = np.array(json_num_segments_list)
hdf5_num_segments_list = np.array(hdf5_num_segments_list)


plt_neuroml = plt.errorbar(neuroml_num_segments_list,
neuroml_runtimes_averaged,
yerr=hdf5_errors,
marker='o',
color='k',
ecolor='k',
markerfacecolor='r',
label="series 2",
capsize=5,)
neuroml_runtimes_averaged,
yerr=hdf5_errors,
marker='o',
color='k',
ecolor='k',
markerfacecolor='r',
label="series 2",
capsize=5,)
plt.title("ArrayMorph write to disk benchmark (NeuroML (XML) serialization)")
plt.xlabel("Number of segments in morphology (Units of 1000 segments)")
plt.ylabel("Time to write to disk (s)")

plt_hdf5 = plt.errorbar(hdf5_num_segments_list,
hdf5_runtimes_averaged,
yerr=hdf5_errors,
marker='o',
color='k',
ecolor='k',
markerfacecolor='g',
label="series 2",
capsize=5,)
hdf5_runtimes_averaged,
yerr=hdf5_errors,
marker='o',
color='k',
ecolor='k',
markerfacecolor='g',
label="series 2",
capsize=5,)
plt.title("ArrayMorph write to disk benchmark (HDF5 serialization)")
plt.xlabel("Number of segments in morphology (Units of 1000 segments)")
plt.ylabel("Time to write to disk (s)")

# plt.show()

plt_json = plt.errorbar(json_num_segments_list,
json_runtimes_averaged,
yerr=json_errors,
marker='o',
color='k',
ecolor='k',
markerfacecolor='b',
label="series 2",
capsize=5,)
json_runtimes_averaged,
yerr=json_errors,
marker='o',
color='k',
ecolor='k',
markerfacecolor='b',
label="series 2",
capsize=5,)

plt.title("ArrayMorph write to disk benchmarks for JSON, HDF5 and NeuroML serialization formats")
plt.xlabel("Number of segments in morphology")
Expand All @@ -293,6 +286,7 @@ def benchmark_arraymorph_writer():
plt.xscale('log')
plt.show()


#prototype:
if __name__ == "__main__":
benchmark_arraymorph_writer()
3 changes: 2 additions & 1 deletion neuroml/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def load_swc_single(cls, src, name=None):
if len(id_to_index_dict) != len(index_to_id):
s = "Internal Error Loading SWC: Index and ID map are different lengths."
s += " [ID:%d, Index:%d]"%( len(index_to_id), len(id_to_index_dict) )
raise MorphologyImportError(s)
# TODO: this is undefined!!
raise MorphologyImportError(s) # noqa: F821

# Vertices and section types are easy:
vertices = d[ ['x','y','z','r'] ]
Expand Down
4 changes: 2 additions & 2 deletions neuroml/nml/nml.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

Validate_simpletypes_ = True
if sys.version_info.major == 2:
BaseStrType_ = basestring
BaseStrType_ = basestring # noqa: F821
else:
BaseStrType_ = str

Expand Down Expand Up @@ -415,7 +415,7 @@ def gds_encode(instring):
def convert_unicode(instring):
if isinstance(instring, str):
result = quote_xml(instring)
elif sys.version_info.major == 2 and isinstance(instring, unicode):
elif sys.version_info.major == 2 and isinstance(instring, unicode): # noqa: F821
result = quote_xml(instring).encode('utf8')
else:
result = GeneratedsSuper.gds_encode(str(instring))
Expand Down

0 comments on commit a0b3080

Please sign in to comment.