Example #1
0
def mc_segmentation (bb, mc_blocks, filename_raw, filename_mem, filename_sv): 
	f = open_file(filename_raw, 'r')
	data_raw = f['data'][bb].astype(np.float32) 
	shape = data_raw.shape

	f = open_file(filename_mem, 'r') 
	data_mem = f['data'][bb].astype(np.float32).reshape(data_raw.shape)
	assert data_mem.shape == shape

	f = open_file(filename_sv, 'r')
	data_sv = f['data'][bb].astype('uint64')
	assert data_sv.shape == shape

	print("Final shape:", shape)

	# run blockwise segmentation
	print("Start segmentation")
	segmentation = elf_workflow.multicut_segmentation(raw=data_raw,
                                                  boundaries=data_mem,
                                                  rf=rf, use_2dws=False,
                                                  watershed=data_sv,
                                                  multicut_solver='blockwise-multicut',
                                                  solver_kwargs={'internal_solver': 'kernighan-lin',
                                                                 'block_shape': mc_blocks},
                                                  n_threads=8)  # multicut_solver = 'kernighan-lin')
	print('segmentation is done')
	return segmentation
Example #2
0
def mc_segmentation(bb, mc_blocks, filename_raw, filename_mem, filename_sv):
    f = open_file(filename_raw, 'r')
    data_raw = f['/t00000/s00/0/cells'][bb].astype(np.float32)
    shape = data_raw.shape

    if np.min(data_raw) == np.max(data_raw):
        # print('no raw data ')
        # print(np.min(data_raw))
        return np.zeros(shape)

    f = open_file(filename_mem, 'r')
    data_mem = f['data'][bb].astype(np.float32).reshape(data_raw.shape)
    assert data_mem.shape == shape

    f = open_file(filename_sv, 'r')
    data_sv = f['data'][bb].astype('uint32')
    assert data_sv.shape == shape

    data_sv, maxlabel, mapping = vigra.analysis.relabelConsecutive(data_sv)

    # print("Final shape:", shape)

    # print('sv', np.min(data_sv), np.max(data_sv))

    if np.min(data_sv) == np.max(data_sv):
        # print('no superpixels')
        return np.zeros(shape)
    try:
        # run blockwise segmentation
        # print("Start segmentation")
        segmentation = elf_workflow.multicut_segmentation(
            raw=data_raw,
            boundaries=data_mem,
            rf=rf,
            use_2dws=False,
            watershed=data_sv,
            multicut_solver='blockwise-multicut',
            solver_kwargs={
                'internal_solver': 'kernighan-lin',
                'block_shape': mc_blocks
            },
            n_threads=16,
            beta=0.6)
        # print('segmentation is done')
        return segmentation
    except RuntimeError:
        error_cubes.append(bb)
        # print('runtime error in segmentation')
        return np.zeros(shape)
f = open_file(filename_mem, 'r')
data_mem = f['data'][bb].astype(np.float32).reshape(data_raw.shape)
assert data_mem.shape == shape

f = open_file(filename_sv, 'r')
data_sv = f['data'][bb].astype('uint64')
assert data_sv.shape == shape

print("Final shape:", shape)

# run blockwise segmentation
print("Start segmentation")
segmentation = elf_workflow.multicut_segmentation(
    raw=data_raw,
    boundaries=data_mem,
    rf=rf,
    use_2dws=False,
    watershed=data_sv,
    multicut_solver='blockwise-multicut',
    solver_kwargs={
        'internal_solver': 'kernighan-lin',
        'block_shape': mc_blocks
    },
    n_threads=8)  # multicut_solver = 'kernighan-lin')
print('segmentation is done')
# save segmentation to h5 file
# res_path = '/g/schwab/Viktoriia/src/source/upscaling_results/' + 's4a2_mc_blockwise_3.h5'
res_path = './res_full.h5'
with open_file(res_path, 'w') as f:
    f.create_dataset('data', data=segmentation, compression="gzip")
Example #4
0
def mc_segmentation(bb,
                    mc_blocks=[256, 256, 256],
                    filename_raw=None,
                    dataset_raw='/t00000/s00/0/cells',
                    filename_mem=None,
                    filename_sv=None,
                    rf=None,
                    n_threads=4,
                    beta=0.5,
                    error_file=None):
    """

    :param bb: numpy slice that needs to be segmented
    :param mc_blocks: size of block for blockwise segmentation, list of size 3
    :param filename_raw: path to file with raw data
    :param dataset_raw: internal dataset
    :param filename_mem: path to file with membrane prediction
    :param filename_sv: path to file with supervoxels (stitched)
    :param rf: RandomForest model
    :param n_threads: number of threads (for multithreading)
    :param beta: beta value
    :param error_file: path to txt file where to write slice of the cube (bb) in case of runtimeerror
    :return: segmentation
    """
    f = open_file(filename_raw, 'r')
    data_raw = f[dataset_raw][bb].astype(np.float32)
    shape = data_raw.shape

    if np.min(data_raw) == np.max(data_raw):
        return np.zeros(shape)

    f = open_file(filename_mem, 'r')
    data_mem = f['data'][bb].astype(np.float32).reshape(data_raw.shape)
    assert data_mem.shape == shape

    f = open_file(filename_sv, 'r')
    data_sv = f['data'][bb].astype('uint32')
    assert data_sv.shape == shape

    data_sv, maxlabel, mapping = vigra.analysis.relabelConsecutive(data_sv)

    if np.min(data_sv) == np.max(data_sv):
        return np.zeros(shape)
    try:
        # run blockwise segmentation
        segmentation = elf_workflow.multicut_segmentation(
            raw=data_raw,
            boundaries=data_mem,
            rf=rf,
            use_2dws=False,
            watershed=data_sv,
            multicut_solver='blockwise-multicut',
            solver_kwargs={
                'internal_solver': 'kernighan-lin',
                'block_shape': mc_blocks
            },
            n_threads=n_threads,
            beta=beta)
        return segmentation
    except RuntimeError:
        with open(error_file, 'a') as f:
            f.write(str(bb) + '/n')
        return np.zeros(shape)
    # membrane prediction -- boundaries
    filename = name + '/mem.h5'
    f = open_file(data_path_1 + filename, 'r')
    mem_test = f['data'][:, :, :].astype(np.float32)

    # supervoxels
    filename = name + '/sv.h5'
    f = open_file(data_path_1 + filename, 'r')
    sv_test = f['data'][:, :, :].astype('uint64')  # (np.float32)
    new_labels, maxlabel, mapping = vigra.analysis.relabelConsecutive(sv_test)

    # run blockwise segmentation
    segmentation = elf_workflow.multicut_segmentation(
        raw=raw_test,
        boundaries=mem_test,
        rf=rf,
        use_2dws=False,
        #watershed=new_labels,
        multicut_solver='kernighan-lin',
        n_threads=4)
    """
    segmentation = elf_workflow.multicut_segmentation(raw=raw_test, boundaries=mem_test, rf=rf, use_2dws=False,
                                                      watershed=new_labels, multicut_solver='blockwise-multicut',
                                                      solver_kwargs={'internal_solver': 'kernighan-lin',
                                                                     'block_shape': [128, 128, 128]},
                                                      n_threads=4)  # multicut_solver = 'kernighan-lin')
    """
    # save segmentation to h5 file
    f = open_file('./' + name + '_mc_512.h5', 'w')
    f.create_dataset('data', data=segmentation, compression="gzip")

    print(name, ' segmentation is done')
                                labels=dmv,
                                use_2dws=False,
                                watershed=supervoxels)

print("edge training is done")

#raw_segment = raw.astype(np.float32)
#membrane_predict_segment = (membrane_prediction).astype(np.float32)

#try blockwise segmentation on the same file (but on the entire one, not only the center cube)
segmentation = elf_workflow.multicut_segmentation(
    raw=raw,
    boundaries=membrane_prediction,
    rf=rf,
    use_2dws=False,
    multicut_solver='blockwise-multicut',
    solver_kwargs={
        'internal_solver': 'kernighan-lin',
        'block_shape': [100, 100, 100]
    },
    n_threads=1)

#save segmentation to h5 file
f = open_file('/scratch/emcf/s4a2_mc/s4a2_t012_mc.h5', 'w')

#h5py.File('/scratch/emcf/s4a2_mc/s4a2_t012_mc.h5', 'w')
f.create_dataset('data', data=segmentation)

with napari.gui_qt():
    viewer = napari.Viewer()
    viewer.add_image(raw, name='raw')
Example #7
0
    #print(len(np.unique(sv_test)))
    #print(np.max(sv_test))

    new_labels, maxlabel, mapping = vigra.analysis.relabelConsecutive(sv_test)

    #print(np.unique(new_labels))
    #print(len(np.unique(new_labels)))
    #print(np.max(new_labels), maxlabel)

    #run blockwise segmentation
    segmentation = elf_workflow.multicut_segmentation(
        raw=raw_test,
        boundaries=mem_test,
        rf=rf,
        use_2dws=False,
        watershed=new_labels,
        multicut_solver='blockwise-multicut',
        solver_kwargs={
            'internal_solver': 'kernighan-lin',
            'block_shape': [128, 128, 128]
        },
        n_threads=1)  #multicut_solver = 'kernighan-lin')

    #save segmentation to h5 file
    f = open_file(
        '/scratch/gross/src/segmentation/results/' + name + '_mc_blockwise.h5',
        'w')
    f.create_dataset('data', data=segmentation, compression="gzip")

    print(name, ' segmentation is done')

    #run segmentation