Commit 4f127a11 authored by Jirka's avatar Jirka

update

parent 2c6c1090
......@@ -115,7 +115,7 @@ def compute_positive_cost_images_weights(imgs, weights):
return pott_sum
def get_edges_image_plane(im_size):
def edges_in_image_plane(im_size):
""" create list of edges for uniform image plane
:param im_size: (w, h) size of image
......@@ -184,7 +184,7 @@ def estimate_atlas_graphcut_general(imgs, encoding, coef=1., init_lbs=None):
logger.debug('graph unaries potentials {}: \n{}'.format(unary_cost.shape,
np.histogram(unary_cost, bins=10)))
edges = get_edges_image_plane(u_cost.shape[:2])
edges = edges_in_image_plane(u_cost.shape[:2])
logger.debug('edges for image plane are shape {}'.format(edges.shape))
edge_weights = np.ones(edges.shape[0])
logger.debug('edges weights are shape {}'.format(edge_weights.shape))
......
......@@ -13,31 +13,32 @@ matplotlib.use('Agg')
import os
import time
import traceback
import logging
import copy_reg
import types
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
import matplotlib.gridspec as gridspec
import generate_dataset as gen_data
import dictionary_learning as dl
import ptn_disctionary as ptn_dict
import ptn_weights as ptn_weigth
import experiments_sta as exp_sta
import logging
import multiprocessing as mproc
import copy_reg
import types
import expt_sta as exp_sta
logger = logging.getLogger(__name__)
# REQURED FOR MPROC POOL
# ISSUE: cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed
# http://stackoverflow.com/questions/25156768/cant-pickle-type-instancemethod-using-pythons-multiprocessing-pool-apply-a
def _reduce_method(m):
if m.im_self is None:
return getattr, (m.im_class, m.im_func.func_name)
else:
return getattr, (m.im_self, m.im_func.func_name)
copy_reg.pickle(types.MethodType, _reduce_method)
# def _reduce_method(m):
# if m.im_self is None:
# return getattr, (m.im_class, m.im_func.func_name)
# else:
# return getattr, (m.im_self, m.im_func.func_name)
#
#
# copy_reg.pickle(types.MethodType, _reduce_method)
def test_simple_show_case():
......@@ -173,7 +174,7 @@ class ExperimentALPE(exp_sta.ExperimentAPD):
p = self.params.copy()
init_atlas = self._init_atlas()
# prefix = 'expt_{}'.format(p['init_tp'])
name = '{}_{:05d}'.format(self.iter_n_var, v)
name = 'log_{}_{}'.format(self.iter_n_var, v)
p_out = os.path.join(p['exp_path'], name)
if not os.path.exists(p_out):
os.mkdir(p_out)
......@@ -188,8 +189,7 @@ class ExperimentALPE(exp_sta.ExperimentAPD):
logger.error(traceback.format_exc())
atlas = np.zeros_like(self.imgs[0])
w_bins = np.zeros((len(self.imgs), 1))
img_rct = ptn_dict.reconstruct_samples(atlas, w_bins)
return atlas, img_rct
return atlas, w_bins
def _perform_once(self, v):
""" perform single experiment
......@@ -197,9 +197,12 @@ class ExperimentALPE(exp_sta.ExperimentAPD):
:param v: value
:return:
"""
atlas, img_rct = self._estimate_atlas(v)
self._export_atlas(atlas, '_{}_{}'.format(self.iter_n_var, v))
stat = self._compute_statistic_gt(atlas, img_rct)
self.atlas, self.w_bins = self._estimate_atlas(v)
self._export_atlas(self.atlas, posix='_{}_{}'.format(self.iter_n_var, v))
self._export_coding(self.w_bins, self._im_names,
posix='_{}_{}'.format(self.iter_n_var, v))
self.img_rct = ptn_dict.reconstruct_samples(self.atlas, self.w_bins)
stat = self._compute_statistic_gt(self.atlas, self.img_rct)
stat.update({self.iter_n_var: v})
return stat
......@@ -221,15 +224,15 @@ def experiments_test():
params['nb_runs'] = 3
expt = ExperimentALPE(params)
expt.run(n_var='case', v_range=range(params['nb_runs']))
expt.run(it_var='case', it_vals=range(params['nb_runs']))
expt_p = ExperimentALPE_mp(params)
expt_p.run(n_var='case', v_range=range(params['nb_runs']))
expt_p.run(it_var='case', it_vals=range(params['nb_runs']))
return
def experiments_synthetic(dataset=None):
def experiments_synthetic(dataset=None, nb_jobs=exp_sta.DEFAULT_NB_THREADS):
""" run all experiments
:return:
......@@ -249,15 +252,15 @@ def experiments_synthetic(dataset=None):
logger.debug('list params: {}'.format(len(l_params)))
for params in l_params:
if exp_sta.DEFAULT_NB_THREADS > 1:
if nb_jobs > 1:
exp = ExperimentALPE_mp(params, exp_sta.DEFAULT_NB_THREADS)
else:
exp = ExperimentALPE(params)
exp.run()
exp.run(it_var='case', it_vals=range(params['nb_runs']))
return
def experiments_real(dataset=None):
def experiments_real(dataset=None, nb_jobs=exp_sta.DEFAULT_NB_THREADS):
""" run all experiments
:return:
......@@ -276,11 +279,11 @@ def experiments_real(dataset=None):
logger.debug('list params: {}'.format(len(l_params)))
for params in l_params:
if exp_sta.DEFAULT_NB_THREADS > 1:
if nb_jobs > 1:
exp = ExperimentALPE_mp(params, exp_sta.DEFAULT_NB_THREADS)
else:
exp = ExperimentALPE(params)
exp.run(gt=False)
exp.run(gt=False, it_var='case', it_vals=range(params['nb_runs']))
return
......@@ -294,7 +297,7 @@ if __name__ == "__main__":
experiments_synthetic()
# experiments_real()
# experiments_real(nb_jobs=1)
# experiments_real('1000_imgs_binary')
logger.info('DONE')
......
This diff is collapsed.
......@@ -76,7 +76,7 @@ def reconstruct_samples(atlas, w_bins):
return imgs
def get_prototype_new_pattern(imgs, imgs_rc, diffs, atlas):
def prototype_new_pattern(imgs, imgs_rc, diffs, atlas):
""" estimate new pattern that occurs in input images and is not cover
by any label in actual atlas, remove collision with actual atlas
......@@ -126,7 +126,7 @@ def insert_new_pattern(imgs, imgs_rc, atlas, lb):
for im, im_rc in zip(imgs, imgs_rc):
diff = np.sum((im - im_rc) > 0)
diffs.append(diff)
im_ptn = get_prototype_new_pattern(imgs, imgs_rc, diffs, atlas)
im_ptn = prototype_new_pattern(imgs, imgs_rc, diffs, atlas)
# logger.debug('new im_ptn: {}'.format(np.sum(im_ptn) / np.prod(im_ptn.shape)))
# plt.imshow(im_ptn), plt.title('im_ptn'), plt.show()
atlas[im_ptn == True] = lb
......
import os
# import sys
import glob
import re
import gc
import time
import logging
from skimage import io
import matplotlib.pylab as plt
logging.basicConfig(level=logging.INFO)
IM_PATTERN = '_seg_de'
PATH_BASE = '/datagrid/Medical/microscopy/drosophila'
PATHS = {'output': os.path.join(PATH_BASE, 'real_segmentations/compare'),
# 'original': os.path.join(PATH_BASE, 'real_segmentations/orig'),
# 'in_segm': os.path.join(PATH_BASE, 'real_segmentations/108_genes_expression'),
'original': os.path.join(PATH_BASE, 'disc_images'),
'in_segm': os.path.join(PATH_BASE, 'real_segmentations/1000_images_improved'),
}
# basically insitu115280_exp.png refers to the gene expression
# /datagrid/Medical/microscopy/drosophila/disc_images/insitu115280.tiff
for n in PATHS.keys():
logging.info('path: {} ({}) <- "...{}"'.format(n, os.path.exists(PATHS[n]),
PATHS[n][-50:]))
p_im_org = glob.glob(os.path.join(PATHS['original'], '*.tiff'))
p_im_seg = glob.glob(os.path.join(PATHS['in_segm'], '*' + IM_PATTERN + '.png'))
logging.info('found p_im_org: {} samples: {}'.format(len(p_im_org), p_im_org[:1]))
logging.info('found p_im_seg: {} samples: {}'.format(len(p_im_seg), p_im_seg[:1]))
p_im_compare = []
for p_s in sorted(p_im_seg):
# match = re.search('(\d+).', p_s)
match = re.search('(\d+)' + IM_PATTERN, p_s)
if not match: continue
idx = match.groups()[0]
for p_o in p_im_org:
if '{}.'.format(idx) in p_o:
p_im_compare.append((p_o, p_s))
continue
logging.info('pairs: {}'.format(len(p_im_compare)))
logging.info('samples: {}'.format(p_im_compare[:3]))
p_imgs_sel = p_im_compare[0::1]
PC_STEP = 0.02
report = [int((i+1) * len(p_imgs_sel) * PC_STEP)
for i in range(int(1 / PC_STEP) + 1)]
def show_pair_img_seg(i, p_ims):
fig, axarr = plt.subplots(1, 2, figsize=(10, 6))
seg = io.imread(p_ims[1])
axarr[0].imshow(io.imread(p_ims[0])), axarr[0].axis('off')
# th = filters.threshold_otsu(seg[seg > 0])
# axarr[0].contour(seg > th, linewidth=2, cmap=plt.cm.Reds)
axarr[0].set_title('original: {}'.format(os.path.basename(p_ims[0])))
axarr[1].imshow(seg, vmin=0, vmax=255), axarr[1].axis('off')
axarr[1].set_title('segmentation: {}'.format(os.path.basename(p_ims[1])))
p_fig = os.path.join(PATHS['output'], os.path.basename(p_ims[1]))
logging.debug(p_fig)
fig.savefig(p_fig, bbox_inches='tight', pad_inches=0 , dpi=300) #
plt.close()
logging.info('starting...')
logging.debug(report)
for i, p_ims in enumerate(p_imgs_sel):
try:
show_pair_img_seg(i, p_ims)
except:
pass
if i in report:
logging.info(' -> processing... \t{:3.0%}'.format(
i/float(len(p_imgs_sel))))
gc.collect(), time.sleep(1)
logging.info('Done')
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment