Esempio n. 1
0
def demo1():
    # params
    tile_shape = (30, 30)

    # centered cell
    centroid = ((tile_shape[0] - 1) / 2, (tile_shape[1] - 1) / 2)
    cell_width = 10
    distr_fn = stats.multivariate_normal(mean=centroid,
                                         cov=(cell_width, cell_width))
    center_cell = eval_grid(distr_fn, tile_shape)

    # neighbor cells
    center = [(14.5, 0), (0, 14.5), (tile_shape[0] - 2, tile_shape[1] - 2)]
    sat_cell = []
    for c in center:
        distr_fn = stats.multivariate_normal(mean=c,
                                             cov=(cell_width, cell_width))
        cell = eval_grid(distr_fn, tile_shape)
        sat_cell.append(cell)

    # make RGB image
    rgb = np.zeros(tile_shape + (3, ))
    for ch in range(rgb.shape[2]):
        rgb[..., ch] += center_cell
        rgb[..., ch] += sat_cell[ch]

    rgb /= rgb.max(axis=(0, 1), keepdims=True)  # normalize per channel

    # convert to polar coord
    rgb_polar = np.zeros_like(rgb)
    for ch in range(rgb.shape[2]):
        _, _, image_rt = polartk.xy2rt(rgb[..., ch])
        rgb_polar[..., ch] = image_rt

    return rgb, rgb_polar
Esempio n. 2
0
def demo2():
    # params
    tile_shape = (30, 30)

    # centered cell
    center_cell = eval_grid_polarized(tile_shape,
                                      centroid=(14.5, 14.5),
                                      cell_width=10,
                                      angle=np.radians(135))

    # neighbor cells
    sat_cell_1 = eval_grid_polarized(tile_shape,
                                     centroid=(10, 1),
                                     cell_width=10,
                                     angle=np.radians(-60))
    sat_cell_2 = eval_grid_polarized(tile_shape,
                                     centroid=(1, 10),
                                     cell_width=10,
                                     angle=np.radians(-30))
    sat_cell_3 = eval_grid_polarized(tile_shape,
                                     centroid=(28, 28),
                                     cell_width=10,
                                     angle=np.radians(135))

    # make RGB image
    sat_cell = [sat_cell_1, sat_cell_2, sat_cell_3]
    rgb = np.zeros(tile_shape + (3, ))
    for ch in range(rgb.shape[2]):
        rgb[..., ch] += center_cell
        rgb[..., ch] += sat_cell[ch]

    rgb /= rgb.max(axis=(0, 1), keepdims=True)  # normalize per channel

    # convert to polar coord
    rgb_polar = np.zeros_like(rgb)
    for ch in range(rgb.shape[2]):
        _, _, image_rt = polartk.xy2rt(rgb[..., ch])
        rgb_polar[..., ch] = image_rt

    return rgb, rgb_polar
Esempio n. 3
0
def run_job(job: np.ndarray) -> np.ndarray:
    # params
    params = {
        'pw': 1,  # pad width, unit: pixel
        'pv': 0,  # pad value for image
        'n_neighbors': 5,  # params for KNN intensity model
    }

    # unpack job
    label_xy = job[..., 0]
    images = [job[..., i] for i in range(1, job.shape[2])]

    # run transformation
    out_dict = polartk.xy2rt(images=images, label=label_xy)

    # pack output
    output_array = np.zeros_like(job)
    output_array[..., 0] = out_dict['label_rt']
    for i, image_rt in enumerate(out_dict['image_rt_list']):
        output_array[..., i + 1] = image_rt

    return output_array
Esempio n. 4
0
def xy2rt_maskfree(dna_xy, image_xy_list, r_threshold=None):
    # infer centroid
    x, y = np.meshgrid(range(dna_xy.shape[0]),
                       range(dna_xy.shape[1]),
                       indexing='ij')
    xc_est, yc_est = infer_centroid(dna_xy)

    # do standard polar coordinate transformation
    out_dict = polartk.xy2rt(images=[dna_xy], centroid=(xc_est, yc_est))
    z_rt = out_dict['image_rt_list'][0]

    # infer radius scaling factor
    k_rt = infer_stretch(z_rt)
    if r_threshold is not None:
        r_ladder = out_dict['r_out'][:, 0]
        k_rt[r_ladder >
             r_threshold, :] = 1  # mask out neighbors for radius estimation

    # prepare grids for modeling
    rt_out_grid = np.stack(
        [out_dict['r_out'].flatten(), out_dict['t_out'].flatten()], axis=-1)
    r_in = out_dict['r_in']  #[1:-1, 1:-1] # remove padding
    t_in = out_dict['t_in']  #[1:-1, 1:-1] # remove padding
    rt_in_grid = np.stack([r_in.flatten(), t_in.flatten()], axis=-1)

    # transform radius scaling factor from polar coordinate to Euclidean coordinate
    model = neighbors.KNeighborsRegressor(metric=polartk.polar_dist)
    model.fit(rt_out_grid, k_rt.flatten())
    k_xy = model.predict(rt_in_grid).reshape(r_in.shape)

    # crop factors for numerical stability and calculate effective radius
    k_xy[k_xy == 0] = 1
    r_in_eff = np.divide(r_in, k_xy)

    # get new grid
    rt_in_grid_eff = np.stack([r_in_eff.flatten(), t_in.flatten()], axis=-1)

    # transform DNA
    dna_xy_pad = np.pad(dna_xy, pad_width=1, constant_values=0)
    model = neighbors.KNeighborsRegressor(metric=polartk.polar_dist)
    model.fit(rt_in_grid_eff, dna_xy_pad.flatten())
    dna_rt = model.predict(rt_out_grid).reshape(dna_xy.shape)

    # transform other images
    image_rt_list = []
    for image_xy in image_xy_list:
        image_xy_pad = np.pad(image_xy, pad_width=1, constant_values=0)
        model = neighbors.KNeighborsRegressor(metric=polartk.polar_dist)
        model.fit(rt_in_grid_eff, image_xy_pad.flatten())
        image_rt = model.predict(rt_out_grid).reshape(image_xy.shape)
        image_rt_list.append(image_rt)

    new_out_dict = {
        'x_in': x,
        'y_in': y,
        'r_in': r_in_eff,
        't_in': t_in,
        'r_out': out_dict['r_out'],
        't_out': out_dict['t_out'],
        'k_in': k_xy,
        'DNA_rt': dna_rt,
        'image_rt_list': image_rt_list
    }
    return new_out_dict
Esempio n. 5
0
        't_out': out_dict['t_out'],
        'k_in': k_xy,
        'DNA_rt': dna_rt,
        'image_rt_list': image_rt_list
    }
    return new_out_dict


if __name__ == '__main__':
    # load data
    job = np.load('job_1497.npy')
    label_xy = job[..., 0]
    image_list = [job[..., i] for i in range(1, job.shape[2])]

    # standard polar coordinate transform
    out_dict_std = polartk.xy2rt(images=image_list)
    out_std = np.stack(out_dict_std['image_rt_list'], axis=-1)

    # mask-based transformation
    out_dict_mask = polartk.xy2rt(images=image_list, label=label_xy)
    out_mask = np.stack(out_dict_mask['image_rt_list'], axis=-1)

    # mask-free transformation
    dna_xy = job[..., 1]
    out_dict_maskfree = xy2rt_maskfree(dna_xy=dna_xy,
                                       image_xy_list=image_list,
                                       r_threshold=8)
    out_maskfree = np.stack(out_dict_maskfree['image_rt_list'], axis=-1)

    np.save('im_xy.npy', job[..., 1:])
    np.save('im_rt_std.npy', out_std)
Esempio n. 6
0
import os
import sys
code_folderpath = os.path.expanduser('~/polartk/code/')
sys.path.append(code_folderpath)

import numpy as np
import tqdm

import polartk

if __name__ == '__main__':
    data_folderpath = './scenario_data'
    scenario_list = []
    for name in os.listdir(data_folderpath):
        if os.path.splitext(
                name)[1] == '.npy' and not name.startswith('output_'):
            scenario_list.append(name)

    for name in tqdm.tqdm(scenario_list):
        input_filepath = os.path.join(data_folderpath, name)
        output_filepath = os.path.join(data_folderpath, 'output_' + name)
        job = np.load(input_filepath)
        _, _, image_rt, label_rt = polartk.xy2rt(image=job[..., 1],
                                                 label=job[..., 0])
        output = np.stack([label_rt, image_rt], axis=-1)
        np.save(output_filepath, output)