def raveler_to_labeled_volume(rav_export_dir, get_glia=False, use_watershed=True, **kwargs): """Import a raveler export stack into a labeled segmented volume.""" import morpho spmap = read_image_stack( os.path.join(rav_export_dir, 'superpixel_maps', '*.png'), **kwargs) sp2seg_list = numpy.loadtxt( os.path.join(rav_export_dir, 'superpixel_to_segment_map.txt'), uint32) seg2bod_list = numpy.loadtxt( os.path.join(rav_export_dir, 'segment_to_body_map.txt'), uint32) sp2seg = {} max_sp = sp2seg_list[:,1].max() start_plane = sp2seg_list[:,0].min() for z, sp, seg in sp2seg_list: if not sp2seg.has_key(z): sp2seg[z] = zeros(max_sp+1, uint32) sp2seg[z][sp] = seg max_seg = seg2bod_list[:,0].max() seg2bod = zeros(max_seg+1, uint32) seg2bod[seg2bod_list[:,0]] = seg2bod_list[:,1] initial_output_volume = zeros_like(spmap) for i, m in enumerate(spmap): j = start_plane + i initial_output_volume[i] = seg2bod[sp2seg[j][m]] probs = kwargs.get('probability_map', ones_like(spmap)) output_volume = morpho.watershed(probs, seeds=initial_output_volume) \ if use_watershed else initial_output_volume if get_glia: annots = json.load( open(os.path.join(rav_export_dir, 'annotations-body.json'), 'r')) glia = [a['body ID'] for a in annots['data'] if a.get('comment', None) == 'glia'] return output_volume, glia else: return output_volume
def raveler_to_labeled_volume(rav_export_dir, get_glia=False, use_watershed=False, **kwargs): """Import a raveler export stack into a labeled segmented volume.""" import morpho spmap = read_image_stack( os.path.join(rav_export_dir, 'superpixel_maps', '*.png'), **kwargs) sp2seg_list = numpy.loadtxt( os.path.join(rav_export_dir, 'superpixel_to_segment_map.txt'), uint32) seg2bod_list = numpy.loadtxt( os.path.join(rav_export_dir, 'segment_to_body_map.txt'), uint32) sp2seg = {} max_sp = sp2seg_list[:, 1].max() start_plane = sp2seg_list[:, 0].min() for z, sp, seg in sp2seg_list: if not sp2seg.has_key(z): sp2seg[z] = zeros(max_sp + 1, uint32) sp2seg[z][sp] = seg max_seg = seg2bod_list[:, 0].max() seg2bod = zeros(max_seg + 1, uint32) seg2bod[seg2bod_list[:, 0]] = seg2bod_list[:, 1] initial_output_volume = zeros_like(spmap) for i, m in enumerate(spmap): j = start_plane + i initial_output_volume[i] = seg2bod[sp2seg[j][m]] probs = kwargs.get('probability_map', ones_like(spmap)) output_volume = morpho.watershed(probs, seeds=initial_output_volume) \ if use_watershed else initial_output_volume if (output_volume[:, 0, 0] == 0).all() and \ (output_volume == 0).sum() == output_volume.shape[0]: output_volume[:, 0, 0] = output_volume[:, 0, 1] if get_glia: annots = json.load( open(os.path.join(rav_export_dir, 'annotations-body.json'), 'r')) glia = [ a['body ID'] for a in annots['data'] if a.get('comment', None) == 'glia' ] return output_volume, glia else: return output_volume
def raveler_to_labeled_volume(rav_export_dir, get_glia=False, use_watershed=False, probability_map=None, crop=None): """Import a raveler export stack into a labeled segmented volume. Parameters ---------- rav_export_dir : string The directory containing the Raveler stack. get_glia : bool (optional, default False) Return the segment numbers corresponding to glia, if available. use_watershed : bool (optional, default False) Fill in 0-labeled voxels using watershed. probability_map : np.ndarray, same shape as volume to be read (optional) If `use_watershed` is True, use `probability_map` as the landscape. If this is not provided, it uses a flat landscape. crop : tuple of int (optional, default None) A 6-tuple of [xmin, xmax, ymin, ymax, zmin, zmax]. Returns ------- output_volume : np.ndarray, shape (Z, X, Y) The segmentation in the Raveler volume. glia : list of int (optional, only returned if `get_glia` is True) The IDs in the segmentation corresponding to glial cells. """ import morpho spmap = read_image_stack( os.path.join(rav_export_dir, 'superpixel_maps', '*.png'), crop=crop) spmap = raveler_rgba_to_int(spmap) sp2seg_list = np.loadtxt( os.path.join(rav_export_dir, 'superpixel_to_segment_map.txt'), uint32) seg2bod_list = np.loadtxt( os.path.join(rav_export_dir, 'segment_to_body_map.txt'), uint32) sp2seg = {} max_sp = sp2seg_list[:,1].max() start_plane = sp2seg_list[:,0].min() for z, sp, seg in sp2seg_list: if not sp2seg.has_key(z): sp2seg[z] = zeros(max_sp+1, uint32) sp2seg[z][sp] = seg max_seg = seg2bod_list[:,0].max() seg2bod = zeros(max_seg+1, uint32) seg2bod[seg2bod_list[:,0]] = seg2bod_list[:,1] initial_output_volume = zeros_like(spmap) for i, m in enumerate(spmap): j = start_plane + i initial_output_volume[i] = seg2bod[sp2seg[j][m]] if use_watershed: probs = np.ones_like(spmap) if probability_map is None \ else probability_map output_volume = morpho.watershed(probs, seeds=initial_output_volume) else: output_volume = initial_output_volume if (output_volume[:, 0, 0] == 0).all() and \ (output_volume == 0).sum() == output_volume.shape[0]: output_volume[:, 0, 0] = output_volume[:, 0, 1] if get_glia: annots = json.load( open(os.path.join(rav_export_dir, 'annotations-body.json'), 'r')) glia = [a['body ID'] for a in annots['data'] if a.get('comment', None) == 'glia'] return output_volume, glia else: return output_volume
def raveler_to_labeled_volume(rav_export_dir, get_glia=False, use_watershed=False, probability_map=None, crop=None): """Import a raveler export stack into a labeled segmented volume. Parameters ---------- rav_export_dir : string The directory containing the Raveler stack. get_glia : bool (optional, default False) Return the segment numbers corresponding to glia, if available. use_watershed : bool (optional, default False) Fill in 0-labeled voxels using watershed. probability_map : np.ndarray, same shape as volume to be read (optional) If `use_watershed` is True, use `probability_map` as the landscape. If this is not provided, it uses a flat landscape. crop : tuple of int (optional, default None) A 6-tuple of [xmin, xmax, ymin, ymax, zmin, zmax]. Returns ------- output_volume : np.ndarray, shape (Z, X, Y) The segmentation in the Raveler volume. glia : list of int (optional, only returned if `get_glia` is True) The IDs in the segmentation corresponding to glial cells. """ import morpho spmap = read_image_stack(os.path.join(rav_export_dir, 'superpixel_maps', '*.png'), crop=crop) spmap = raveler_rgba_to_int(spmap) sp2seg_list = np.loadtxt( os.path.join(rav_export_dir, 'superpixel_to_segment_map.txt'), uint32) seg2bod_list = np.loadtxt( os.path.join(rav_export_dir, 'segment_to_body_map.txt'), uint32) sp2seg = {} max_sp = sp2seg_list[:, 1].max() start_plane = sp2seg_list[:, 0].min() for z, sp, seg in sp2seg_list: if not sp2seg.has_key(z): sp2seg[z] = zeros(max_sp + 1, uint32) sp2seg[z][sp] = seg max_seg = seg2bod_list[:, 0].max() seg2bod = zeros(max_seg + 1, uint32) seg2bod[seg2bod_list[:, 0]] = seg2bod_list[:, 1] initial_output_volume = zeros_like(spmap) for i, m in enumerate(spmap): j = start_plane + i initial_output_volume[i] = seg2bod[sp2seg[j][m]] if use_watershed: probs = np.ones_like(spmap) if probability_map is None \ else probability_map output_volume = morpho.watershed(probs, seeds=initial_output_volume) else: output_volume = initial_output_volume if (output_volume[:, 0, 0] == 0).all() and \ (output_volume == 0).sum() == output_volume.shape[0]: output_volume[:, 0, 0] = output_volume[:, 0, 1] if get_glia: annots = json.load( open(os.path.join(rav_export_dir, 'annotations-body.json'), 'r')) glia = [ a['body ID'] for a in annots['data'] if a.get('comment', None) == 'glia' ] return output_volume, glia else: return output_volume