def test_single_tile(test_apply_correction, render, tmpdir):

    # get tilespecs
    # Z = test_apply_correction.args['z_index']
    Z = 400
    inp_tilespecs = renderapi.tilespec.get_tile_specs_from_z(
        test_apply_correction.args['input_stack'],
        Z,
        render=test_apply_correction.render)
    corr_tilespecs = renderapi.tilespec.get_tile_specs_from_z(
        test_apply_correction.args['correction_stack'],
        Z,
        render=test_apply_correction.render)
    # mult intensity correct each tilespecs and return tilespecs
    N, M, C = getImage(corr_tilespecs[0])
    corr_dict = {}
    for channel in ["DAPI_1", "PSD95"]:
        _, _, corr_dict[channel] = getImage(corr_tilespecs[0], channel=channel)

#process_tile(C, corr_dict, dirout, stackname, clip, scale_factor, clip_min, clip_max, input_ts, corr_dict=None)
    process_tile(C,
                 test_apply_correction.args['output_directory'],
                 test_apply_correction.args['output_stack'],
                 True,
                 1.0,
                 0,
                 65535,
                 inp_tilespecs[0],
                 corr_dict=corr_dict)
def test_apply_correction(test_median_stack, mini_raw_stack, render,
                          tmpdir_factory):
    # output_directory = os.path.join(os.path.split(
    #    MULTIPLICATIVE_INPUT_JSON)[0], 'corrected')
    output_directory = str(tmpdir_factory.mktemp('intensity_corrected'))
    output_stack = "Flatfield_corrected_test"
    params = {
        "render": render_params,
        "input_stack": mini_raw_stack,
        "correction_stack": test_median_stack,
        "output_stack": output_stack,
        "output_directory": output_directory,
        "z_index": 0,
        "pool_size": 10,
        "overwrite_zlayer": True
    }
    mod = MultIntensityCorr(input_data=params, args=[])
    mod.run()

    expected_directory = os.path.join(multiplicative_correction_example_dir,
                                      'intensity_corrected')
    output_tilespecs = renderapi.tilespec.get_tile_specs_from_z(output_stack,
                                                                0,
                                                                render=render)

    for ts in output_tilespecs:
        N, M, out_image = getImage(ts)
        out_filepath = ts.ip[0].imageUrl
        out_file = os.path.split(out_filepath)[1]
        exp_image = tifffile.imread(os.path.join(expected_directory, out_file))
        assert (np.max(np.abs(exp_image - out_image)) < 3)
    return mod
def test_median_stack(raw_stack, render, tmpdir_factory):
    median_stack = 'median_stack'
    output_directory = str(tmpdir_factory.mktemp('Median'))
    #output_directory = os.path.join(os.path.split(MULTIPLICATIVE_INPUT_JSON)[0],'Medians')
    params = {
        "render": render_params,
        "input_stack": raw_stack,
        "file_prefix": "Median",
        "output_stack": median_stack,
        "output_directory": output_directory,
        "minZ": 0,
        "maxZ": 0,
        "pool_size": 3,
        "log_level": "DEBUG"
    }
    mod = MakeMedian(input_data=params, args=[])
    mod.run()

    median_tilespecs = renderapi.tilespec.get_tile_specs_from_stack(
        median_stack, render=render)
    N, M, median_image = getImage(median_tilespecs[0])

    expected_median_file = os.path.join(multiplicative_correction_example_dir,
                                        'median', 'Median_median_stack_0.tif')
    expect_median = tifffile.imread(expected_median_file)

    assert (np.max(np.abs(median_image - expect_median)) < 3)

    yield median_stack
def test_apply_correction(test_median_stack, mini_raw_stack, render,
                          tmpdir_factory):
    output_directory = str(tmpdir_factory.mktemp('intensity_corrected'))
    #output_directory = os.path.join(multiplicative_multichan_correction_example_dir, 'intensity_corrected')
    output_stack = "Flatfield_corrected_test"
    params = {
        "render": render_params,
        "input_stack": mini_raw_stack,
        "correction_stack": test_median_stack,
        "output_stack": output_stack,
        "output_directory": output_directory,
        "z_index": 400,
        "pool_size": 1,
        "overwrite_zlayer": True
    }
    mod = MultIntensityCorr(input_data=params, args=[])
    mod.run()

    expected_directory = os.path.join(
        multiplicative_multichan_correction_example_dir, 'intensity_corrected')
    output_tilespecs = renderapi.tilespec.get_tile_specs_from_z(output_stack,
                                                                400,
                                                                render=render)
    for channel in ["DAPI_1", "PSD95"]:
        for ts in output_tilespecs:
            N, M, out_image = getImage(ts, channel=channel)
            chan = next(ch for ch in ts.channels if ch.name == channel)
            out_filepath = chan.ip[0].imageUrl
            out_file = os.path.split(out_filepath)[1]
            exp_image = tifffile.imread(
                os.path.join(expected_directory, out_file))
            assert (np.max(np.abs(exp_image - out_image)) < 3)
    return mod
def test_median_stack(raw_stack, render, tmpdir_factory):
    median_stack = 'median_stack'
    output_directory = str(tmpdir_factory.mktemp('median'))
    #output_directory = os.path.join(multiplicative_multichan_correction_example_dir,'median')
    params = {
        "render": render_params,
        "input_stack": raw_stack,
        "file_prefix": "Median",
        "output_stack": median_stack,
        "output_directory": output_directory,
        "minZ": 400,
        "maxZ": 400,
        "pool_size": 3,
        "log_level": "DEBUG"
    }
    mod = MakeMedian(input_data=params, args=[])
    mod.run()

    median_tilespecs = renderapi.tilespec.get_tile_specs_from_stack(
        median_stack, render=render)
    for channel in ["DAPI_1", "PSD95"]:
        N, M, median_image = getImage(median_tilespecs[0], channel=channel)
        expected_median_file = os.path.join(
            multiplicative_multichan_correction_example_dir, 'median',
            'Median_median_stack_%s_400-400.tif' % channel)
        expect_median = tifffile.imread(expected_median_file)

    assert (np.max(np.abs(median_image - expect_median)) < 3)

    yield median_stack
def test_single_tile(test_apply_correction, render, tmpdir):

    # get tilespecs
    # Z = test_apply_correction.args['z_index']
    Z = test_apply_correction.zValues[0]
    inp_tilespecs = renderapi.tilespec.get_tile_specs_from_z(
        test_apply_correction.args['input_stack'],
        Z,
        render=test_apply_correction.render)
    corr_tilespecs = renderapi.tilespec.get_tile_specs_from_z(
        test_apply_correction.args['correction_stack'],
        Z,
        render=test_apply_correction.render)
    # mult intensity correct each tilespecs and return tilespecs
    N, M, C = getImage(corr_tilespecs[0])

    #process_tile(C, dirout, stackname, clip, scale_factor, clip_min, clip_max, input_ts, corr_dict=None)
    process_tile(C, test_apply_correction.args['output_directory'],
                 test_apply_correction.args['output_stack'], True, 1.0, 0,
                 65535, inp_tilespecs[0])
Beispiel #7
0
def make_median_image(alltilespecs, numtiles, outImage, pool_size, chan=None, gauss_size=10):
    # read images and create stack
    N, M, img0 = getImage(alltilespecs[0], channel=chan)
    stack = np.zeros((N, M, numtiles), dtype=img0.dtype)
    mypartial = partial(getImageFromTilespecs, alltilespecs, channel = chan)
    indexes = range(0, numtiles)
    with renderapi.client.WithPool(pool_size) as pool:
        images = pool.map(mypartial, indexes)

    # calculate median
    for i in range(0, len(images)):
        stack[:, :, i] = images[i]
    np.median(stack, axis=2, overwrite_input=True)
    (A, B, C) = stack.shape
    if (numtiles % 2 == 0):
        med1 = stack[:, :, numtiles // 2 - 1]
        med2 = stack[:, :, numtiles // 2 + 1]
        med = (med1 + med2) // 2
    else:
        med = stack[:, :, (numtiles - 1) // 2]
    med = gaussian_filter(med, gauss_size)

    tifffile.imsave(outImage, med)
Beispiel #8
0
def getImageFromTilespecs(alltilespecs, index, channel=None):
    N, M, img = getImage(alltilespecs[index], channel)
    return img