def test_convert(tmpdir): for in_ext, out_ext in it.product(['asc', 'h5', 'swc'], repeat=2): # A simple morphology inname = DATA / f'simple.{in_ext}' outname = Path(tmpdir, f'test.{out_ext}') convert(inname, outname) assert not diff(inname, outname) # A more complex one inname = DATA / f'tkb061126a4_ch0_cc2_h_zk_60x_1.{in_ext}' outname = Path(tmpdir, f'test.{out_ext}') convert(inname, outname) diff_result = diff(inname, outname, rtol=1e-5, atol=1e-5) assert not bool(diff_result), diff_result.info
def test_convert(): with setup_tempdir('test-convert') as tmp_dir: for in_ext, out_ext in it.product(['asc', 'h5', 'swc'], repeat=2): # A simple morphology inname = os.path.join(DATA, 'simple.' + in_ext) outname = os.path.join(tmp_dir, 'test.' + out_ext) convert(inname, outname) ok_(not diff(inname, outname)) # A more complex one inname = os.path.join(DATA, 'tkb061126a4_ch0_cc2_h_zk_60x_1.' + in_ext) outname = os.path.join(tmp_dir, 'test.' + out_ext) convert(inname, outname) diff_result = diff(inname, outname, rtol=1e-5, atol=1e-5) ok_(not bool(diff_result), diff_result.info)
def test_same_conversion_as_asciitoh5(): '''Check that the morph-tool conversion to produces the same h5 files as asciitoh5 converter. repo_base/02-morphology-repository-sanitized contains valid morphologies under ASC format repo_base/03-morphology-repository-sanitized-asciitoh5ed contains the expected conversion output Note: asciitoh5 also reorder the neurites but not 'morph-tool convert' so we compare the reordered files ''' repo_base = '/gpfs/bbp.cscs.ch/project/proj30/jenkins/morph-tool/converter' with tempfile.TemporaryDirectory() as folder: sanitized = Path(repo_base, '02-morphology-repository-sanitized') for path in tqdm(list(_walk(sanitized))): morphio_morph = Path(folder, path.stem + '.h5') convert(path, str(morphio_morph)) asciitoh5_morph = Path( repo_base, '03-morphology-repository-sanitized-asciitoh5ed', path.stem + '.h5') diff_result = diff(Morphology(morphio_morph, Option.nrn_order), asciitoh5_morph) ok_( not diff_result, 'mismatch:\n{}\n{}\n{}'.format(path, asciitoh5_morph, diff_result.info))
def assert_conversion_works(input_file): ext_in = input_file.lower().split('.')[-1] name = os.path.basename(input_file).split('.')[0] for ext_out in ['asc', 'h5', 'swc']: output_file = os.path.join('/tmp', name + '.' + ext_out) convert(input_file, output_file) input = Morphology(input_file) output = Morphology(output_file) diff_result = diff(input, output) ok_( not diff_result, 'Difference between {} and {}: {}'.format(input_file, output_file, diff_result.info)) try: if ext_in == ext_out or {ext_in, ext_out} == {'asc', 'h5'}: assert_array_almost_equal( Morphology(input_file).soma.points, Morphology(output_file).soma.points) else: surf1 = _get_surface(input_file, ext_in) surf2 = _get_surface(output_file, ext_out) try: assert_allclose(surf1, surf2, 5e-1) except Exception as e: raise e except NotImplementedError: pass
def test__repair(): neuron = load_neuron(Path(DATA_PATH, 'valid.h5')) axon = neuron.root_sections[0] assert_equal(axon.type, SectionType.axon) test_module.repair(neuron, axon, [axon], [axon], set(), y_extent=10000) assert_equal(len(axon.children), 1) assert_array_equal(axon.children[0].points[0], axon.points[-1]) ok_(not diff(neuron, DATA_PATH / 'axon-repair.h5'))
def test_self_graft(): '''Grafting a neuron with its own neuron''' filename = DATA / 'neuron.asc' new_axon = graft.find_axon(ImmutMorphology(filename)) neuron = Morphology(filename) graft.graft_axon(neuron, new_axon) expected = Morphology(filename) assert not diff(expected, neuron)
def test__repair_no_intact_axon(): filename = Path(DATA_PATH, 'valid.h5') neuron = load_neuron(filename) axon = neuron.root_sections[0] used_axon_branches = set() test_module.repair(neuron, axon, [], [axon], used_axon_branches, y_extent=10000) # There should not be any repair ok_(not diff(neuron, filename))
def test_convert_recenter(): with setup_tempdir('test-convert_recenter') as tmp_dir: simple = os.path.join(DATA, 'simple.swc') outname = os.path.join(tmp_dir, 'test.swc') convert(simple, outname, recenter=True) ok_(not diff(simple, outname)) #simple.swc is already centered mut = morphio.Morphology(simple).as_mutable() mut.soma.points = [[1, 1, 1], ] inname = os.path.join(tmp_dir, 'moved.swc') mut.write(inname) convert(inname, outname, recenter=True) simple = morphio.Morphology(simple) centered_morph = morphio.Morphology(outname) ok_(np.all((simple.points - centered_morph.points) == 1))
def test_convert_recenter(tmpdir): simple = DATA / 'simple.swc' outname = Path(tmpdir, 'test.swc') convert(simple, outname, recenter=True) assert not diff(simple, outname) #simple.swc is already centered mut = morphio.Morphology(simple).as_mutable() mut.soma.points = [ [1, 1, 1], ] inname = Path(tmpdir, 'moved.swc') mut.write(inname) convert(inname, outname, recenter=True) simple = morphio.Morphology(simple) centered_morph = morphio.Morphology(outname) assert np.all((simple.points - centered_morph.points) == 1)
def diff(morph1, morph2, quiet): ''' Compare two morphology files. Return exit code 0 if morphology objects stored in `file1` and `file2` are deemed identical by MorphIO; and exit code 1 otherwise. Morphologies with different formats can be compared. MORPH1 and MORPH2 can be either filenames or morphio.Morpholgy objects ''' if quiet: L.setLevel(logging.WARNING) result = morph_tool.diff(morph1, morph2) if result: L.info("Morphologies not identical") L.info(result.info) sys.exit(1)