Пример #1
0
def export_visual_atlas(i, out_dir, atlas=None, prefix='debug'):
    """ export the atlas and/or weights to results directory

    :param int i: iteration to be shown in the img name
    :param str out_dir: path to the resulting folder
    :param ndarray atlas: np.array<height, width>
    :param str prefix:

    >>> import shutil
    >>> logging.getLogger().setLevel(logging.DEBUG)
    >>> dir_name = 'sample_dir'
    >>> os.mkdir(dir_name)
    >>> export_visual_atlas(0, dir_name, np.random.randint(0, 5, (10, 5)))
    >>> shutil.rmtree(dir_name, ignore_errors=True)
    """
    if logging.getLogger().getEffectiveLevel() < logging.DEBUG:
        return
    if out_dir is None or not os.path.exists(out_dir):
        logging.debug('results path "%s" does not exist', out_dir)
        return None
    if atlas is not None:
        # export_visualization_image(atlas, i, out_dir, prefix, 'atlas',
        #                            labels=['X', 'Y'])
        n_img = TEMPLATE_NAME_ATLAS.format(prefix, 'atlas', i)
        export_image(out_dir, atlas, n_img)
Пример #2
0
def extract_activation(path_img, path_out):
    name = os.path.splitext(os.path.basename(path_img))[0]
    img = io_imread(path_img)
    mask = img[:, :, 0] > 5

    im_struc = img[:, :, 0]
    im_struc_gauss = ndimage.gaussian_filter(im_struc, 1)
    im_gene = img[:, :, 1]
    im_gene_gauss = ndimage.gaussian_filter(im_gene, 1)

    mask_struc = im_struc_gauss > filters.threshold_otsu(im_struc_gauss[mask])
    ms = np.median(im_struc_gauss[mask_struc])
    mask_gene = im_gene_gauss > filters.threshold_otsu(im_gene_gauss[mask])
    mg = np.median(im_gene_gauss[mask_gene])

    ration_gene = np.sum(mask_gene) / float(np.sum(mask))
    coef = (ms / mg * 2.5) if ration_gene > 0.3 else (ms / mg * 5)
    im_mean = np.max(np.array([im_gene_gauss + (im_struc_gauss / coef)]), axis=0)

    otsu = filters.threshold_otsu(im_mean[mask])
    im_gene = im_mean.copy()
    im_gene[im_gene < otsu] = 0

    # gmm = GaussianMixture(n_components=3, n_init=10)
    # data = np.array([im_gene_gauss[mask].ravel(), im_struc_gauss[mask]]).T
    # gmm.fit(data)
    # id_max = np.argmax(gmm.means_[:, 0])
    # gm_mean = gmm.means_[id_max, 0]
    # gm_std = np.sqrt(gmm.covariances_[id_max, 0, 0])
    # im_gene[im_gene_gauss < (gm_mean - gm_std)] = 0

    # p_out = os.path.join(path_out, name)
    export_image(path_out, im_gene, name)
Пример #3
0
def add_noise_image(img_name, path_in, path_out, noise_level):
    """

    :param str img_name:
    :param str path_in:
    :param str path_out:
    :param float noise_level:
    """
    path_img = os.path.join(path_in, img_name)
    logging.debug('loading image: %s', path_img)
    name, img = load_image(path_img)
    img_noise = add_image_fuzzy_gauss_noise(img, noise_level)
    export_image(path_out, img_noise, name)
Пример #4
0
    def _export_atlas(self, atlas, suffix=''):
        """ export estimated atlas

        :param ndarray atlas: np.array<height, width>
        :param str suffix:
        """
        n_img = NAME_ATLAS.format(suffix)
        export_image(self.params.get('path_exp'),
                     atlas,
                     n_img,
                     stretch_range=False)
        path_atlas_rgb = os.path.join(self.params.get('path_exp'),
                                      n_img + '_rgb.png')
        logging.debug('exporting RGB atlas: %s', path_atlas_rgb)
        plt.imsave(path_atlas_rgb, atlas, cmap=plt.cm.jet)
Пример #5
0
def main(path_pattern_in, path_out, nb_workers=NB_WORKERS):
    assert os.path.isdir(os.path.dirname(path_pattern_in)), \
        'missing: %s' % path_pattern_in
    assert os.path.isdir(os.path.dirname(path_out)), \
        'missing: %s' % os.path.dirname(path_out)

    if not os.path.isdir(path_out):
        logging.info('create dir: %s', path_out)
        os.mkdir(path_out)

    list_img_paths = glob.glob(path_pattern_in)
    logging.info('found images: %i', len(list_img_paths))

    # create partial subset with image pathes
    list_img_paths_partial = [
        list_img_paths[i::nb_workers * LOAD_SUBSET_COEF]
        for i in range(nb_workers * LOAD_SUBSET_COEF)
    ]
    list_img_paths_partial = [ls for ls in list_img_paths_partial if ls]
    mean_imgs = list(
        WrapExecuteSequence(load_mean_image,
                            list_img_paths_partial,
                            nb_workers=nb_workers,
                            desc='loading mean images'))
    # imgs, im_names = tl_data.dataset_load_images(list_img_paths, nb_workers=1)
    img_mean = np.mean(np.asarray(mean_imgs), axis=0)
    export_image(path_out, img_mean, 'mean_image')

    logging.info('original image size: %r', img_mean.shape)
    # bbox = find_min_bbox_cumul_sum(img_mean, params['threshold'])
    if params['thr_method'] == 'line-grad':
        bbox = find_min_bbox_grad(img_mean)
    elif params['threshold'] == 0:
        bbox = [0] * 4
    elif params['thr_method'] == 'line-sum':
        bbox = find_min_bbox_line_sum(img_mean, params['threshold'])
    else:
        bbox = find_min_bbox_cumul_sum(img_mean, params['threshold'])
    d_bbox = export_bbox_json(path_out, bbox)
    logging.info('found BBox: %r', d_bbox)

    _cut_export = partial(export_cut_image, d_bbox=d_bbox, path_out=path_out)
    list(
        WrapExecuteSequence(_cut_export,
                            list_img_paths,
                            nb_workers,
                            desc='exporting cut images'))
Пример #6
0
def perform_reconstruction(set_variables, atlas, path_out, path_visu=None):
    name, w_bins, segm, image, deform = set_variables
    if deform is not None:
        atlas = warp2d_apply_deform_field(atlas, deform, method='nearest')

    w_bin_ext = np.array([0] + w_bins.tolist())
    seg_reconst = np.asarray(w_bin_ext)[atlas].astype(atlas.dtype)
    export_image(path_out, seg_reconst, name)

    if path_visu is not None and os.path.isdir(path_visu):
        fig = draw_reconstruction(atlas, seg_reconst, segm, img_rgb=image)
        p_fig = os.path.join(path_visu, name + '.png')
        fig.savefig(p_fig, bbox_inches='tight')
        plt.close(fig)

    diff = np.sum(np.abs(image - seg_reconst)) if image is not None else None
    return name, diff
Пример #7
0
def export_cut_image(path_img, d_bbox, path_out):
    name, im = load_image(path_img)
    im_cut = im[d_bbox['top']:-d_bbox['bottom'],
                d_bbox['left']:-d_bbox['right']]
    export_image(path_out, im_cut, name)
def parse_experiment_folder(path_expt, params):
    """ parse experiment folder, get configuration and results

    :param str path_expt: path to experiment folder
    :param dict params:
    """
    assert os.path.isdir(path_expt), 'missing EXPERIMENT: %s' % path_expt

    path_config = os.path.join(path_expt, params['name_config'])
    assert any(path_config.endswith(ext)
               for ext in ['.yaml', '.yml']), '%s' % path_config
    assert os.path.exists(path_config), 'missing config: %s' % path_config
    dict_info = load_config_yaml(path_config)
    logging.debug(' -> loaded params: %r', dict_info.keys())

    path_results = os.path.join(path_expt, params['name_results'])
    assert path_results.endswith('.csv'), '%s' % path_results
    assert os.path.exists(path_results), 'missing result: %s' % path_results
    df_res = pd.read_csv(path_results, index_col=0)
    index_name = df_res.index.name
    df_res[index_name] = df_res.index

    if dict_info.get('type') != 'synth':
        logging.debug('non "synthetic" datasets does not have GT atlas')
        return

    # TODO: add recompute reconstruction error

    # load the GT atlas
    path_atlas = os.path.join(dict_info['path_in'], DIR_NAME_DICTIONARY)
    atlas_gt = dataset_compose_atlas(path_atlas)
    path_atlas_gt = os.path.join(dict_info['path_in'], SUB_PATH_GT_ATLAS)
    atlas_name = str(os.path.splitext(os.path.basename(path_atlas_gt))[0])
    export_image(os.path.dirname(path_atlas_gt), atlas_gt, atlas_name)
    plt.imsave(os.path.splitext(path_atlas_gt)[0] + '_visual.png', atlas_gt)

    results_new = []
    for _, row in df_res.iterrows():
        dict_row = dict(row)
        # if not isinstance(idx, str) and idx - int(idx) == 0:
        #     idx = int(idx)
        atlas_name = NAME_PATTERN_ATLAS % dict_row['name_suffix']
        atlas = load_atlas(os.path.join(path_expt, atlas_name))
        # try to find the mest match among patterns / labels
        atlas = relabel_max_overlap_unique(atlas_gt, atlas)
        # recompute the similarity measure
        dict_measure = compute_classif_metrics(atlas_gt.ravel(), atlas.ravel())
        dict_measure = {'atlas %s' % k: dict_measure[k] for k in dict_measure}
        dict_row.update(dict_measure)
        results_new.append(dict_row)
        # visualise atlas
        atlas_name_visu = os.path.splitext(atlas_name)[0] + '_visual.png'
        path_visu = os.path.join(path_expt, atlas_name_visu)
        export_atlas_overlap(atlas_gt, atlas, path_visu)
        export_atlas_both(atlas_gt, atlas, path_visu)

    df_results_new = pd.DataFrame(results_new).set_index([index_name])
    path_results = os.path.join(path_expt, NAME_OUTPUT_RESULT)
    df_results_new.to_csv(path_results)
    # just to let it releases memory
    gc.collect()
    time.sleep(1)