def morphology_to_dict(sections, outfile=None): section_map = {sec: i for i, sec in enumerate(sections)} result = [] h.define_shape() for sec in sections: my_parent = parent(sec) my_parent_loc = -1 if my_parent is None else parent_loc(sec, my_parent) my_parent = -1 if my_parent is None else section_map[my_parent] n3d = int(h.n3d(sec=sec)) result.append({ 'section_orientation': h.section_orientation(sec=sec), 'parent': my_parent, 'parent_loc': my_parent_loc, 'x': [h.x3d(i, sec=sec) for i in xrange(n3d)], 'y': [h.y3d(i, sec=sec) for i in xrange(n3d)], 'z': [h.z3d(i, sec=sec) for i in xrange(n3d)], 'diam': [h.diam3d(i, sec=sec) for i in xrange(n3d)], 'name': sec.hname() }) if outfile is not None: with open(outfile, 'w') as f: json.dump(result, f) return result