Skip to content

Commit

Permalink
Fix perimeters in converter (#124)
Browse files Browse the repository at this point in the history
* Fix perimeters in converter
  • Loading branch information
adrien-berchet committed Dec 21, 2023
1 parent ac23f03 commit f060afe
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
18 changes: 11 additions & 7 deletions morph_tool/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,15 @@ def from_h5_or_asc(neuron, output_ext):
mean, new_xyz)
neuron.soma.type = SomaType.SOMA_CYLINDERS

if output_ext != "h5":
for sec in neuron.iter():
sec.perimeters = []

return neuron


def convert(input_file,
outputfile,
output_file,
recenter=False,
nrn_order=False,
single_point_soma=False,
Expand All @@ -249,7 +253,7 @@ def convert(input_file,
Args:
input_file(str): path to input file
outputfile(str): path to output file
output_file(str): path to output file
recenter(bool): whether to recenter the morphology based on the
center of gravity of the soma
nrn_order(bool): whether to traverse the neuron in the NEURON fashion
Expand All @@ -264,12 +268,12 @@ def convert(input_file,
if sanitize:
neuron.remove_unifurcations()

output_ext = Path(outputfile).suffix
output_ext = Path(output_file).suffix.lower()

if single_point_soma and output_ext.lower() != '.swc':
if single_point_soma and output_ext != '.swc':
raise MorphToolException('Single point soma is only applicable for swc output')

if output_ext.lower() not in ('.swc', '.asc', '.h5', ):
if output_ext not in ('.swc', '.asc', '.h5', ):
raise MorphToolException('Output file format should be one swc, asc or h5')

output_ext = output_ext[1:] # Remove the dot
Expand All @@ -293,7 +297,7 @@ def convert(input_file,
transform.translate(new, -1 * new.soma.center)

try:
new.write(outputfile)
new.write(output_file)
except WriterError as e:
raise MorphToolException('Use `sanitize` option for converting') from e

Expand All @@ -304,7 +308,7 @@ def convert(input_file,
'before conversion: %s\n'
'after conversion: %s',
get_NEURON_surface(input_file),
get_NEURON_surface(outputfile))
get_NEURON_surface(output_file))
except: # noqa pylint: disable=bare-except
L.info('Final NEURON soma surface check was skipped probably because BluePyOpt'
' or NEURON is not installed')
9 changes: 7 additions & 2 deletions morph_tool/morphio_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __nonzero__(self):
return self.__bool__()


def diff(morph1, morph2, rtol=1.e-5, atol=1.e-8):
def diff(morph1, morph2, rtol=1.e-5, atol=1.e-8, *, skip_perimeters=False):
"""Returns a DiffResult object that is equivalent to True when morphologies differ.
Additional information about why they differ is stored in DiffResult.info
Expand All @@ -47,6 +47,7 @@ def diff(morph1, morph2, rtol=1.e-5, atol=1.e-8):
morph2 (str|morphio.Morphology|morphio.mut.Morphology): a morphology
rtol (float): the relative tolerance used for comparing points (see numpy.isclose help)
atol (float): the absolute tolerance used for comparing points (see numpy.isclose help)
skip_perimeters (bool): do not check the perimeters if set to True
"""
if not isinstance(morph1, Morphology):
morph1 = Morphology(morph1)
Expand All @@ -57,8 +58,12 @@ def diff(morph1, morph2, rtol=1.e-5, atol=1.e-8):
return DiffResult(True,
'Both morphologies have a different number of root sections')

attr_list = ['points', 'diameters']
if not skip_perimeters:
attr_list.append('perimeters')

for section1, section2 in zip(morph1.iter(), morph2.iter()):
for attrib in ['points', 'diameters', 'perimeters']:
for attrib in attr_list:
val1, val2 = getattr(section1, attrib), getattr(section2, attrib)
if val1.shape != val2.shape:
return DiffResult(True,
Expand Down
Binary file modified tests/data/simple.h5
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_convert(tmpdir):
inname = DATA / f'simple.{in_ext}'
outname = Path(tmpdir, f'test.{out_ext}')
convert(inname, outname, single_point_soma=(out_ext == 'swc'))
assert not diff(inname, outname)
assert not diff(inname, outname, skip_perimeters=in_ext == 'h5')

# A more complex one
inname = DATA / f'tkb061126a4_ch0_cc2_h_zk_60x_1.{in_ext}'
Expand Down

0 comments on commit f060afe

Please sign in to comment.