Commit 3798ddc8 authored by Jiri Borovec's avatar Jiri Borovec

fix & update

parent c7c2cb9f
import os
import numpy as np
import ptn_disctionary as ptn_dict
import ptn_weights as ptn_weight
import similarity_metric as sim_metric
import pattern_disctionary as ptn_dict
import pattern_weights as ptn_weight
import metric_similarity as sim_metric
import generate_dataset as gen_data
import matplotlib.pyplot as plt
import logging
......@@ -11,11 +11,10 @@ logger = logging.getLogger(__name__)
DEFAULT_UNARY_BACKGROUND = 1
# TODO: spatial clustering
# TODO: init with ICA
# TODO: # Spis by to mozna slo "greedy" metodou
# TODO: init: spatial clustering
# TODO: init: use ICA
# TODO: init: greedy
# Spis by to mozna slo "greedy" metodou
# prvni obrazek vezmu nahodne, nebo naopak co "nejaktivnejsi" = co nejvetsi
# energie. Pak od vsech ostatnich odectu vhodne zvoleny nasobek a spocitam
# "zbytky". Ze "zbytku" opet vezmu ten s nejvetsi energii atp. Je mozne
......@@ -41,7 +40,7 @@ DEFAULT_UNARY_BACKGROUND = 1
# # logger.debug(weights_ext)
# imgs = np.array(l_imgs)
# logger.debug('DIMS pott: {}, l_imgs {}, w_bin: {}'.format(pott_sum.shape,
# imgs.shape, weights_ext.shape))
# imgs.shape, weights_ext.shape))
# logger.debug('... walk over all pixels in each image')
# for i in range(pott_sum.shape[0]):
# for j in range(pott_sum.shape[1]):
......@@ -218,7 +217,6 @@ def export_visualization_image(img, i, out_dir, prefix='debug', name='',
:param name: str, name of this particular visual
:param ration: str, mainly for weights to ne stretched
:param labels: [str<x>, str<y>] labels for axis
:return:
"""
fig = plt.figure()
plt.imshow(img, interpolation='none', aspect=ration)
......@@ -239,7 +237,6 @@ def export_visual_atlas(i, out_dir, atlas=None, weights=None, prefix='debug'):
:param atlas: np.array<w, h>
:param weights: np.array<nb_imgs, nb_lbs>
:param prefix: str
:return:
"""
if logger.getEffectiveLevel()==logging.DEBUG:
if not os.path.exists(out_dir):
......@@ -253,7 +250,6 @@ def export_visual_atlas(i, out_dir, atlas=None, weights=None, prefix='debug'):
# if weights is not None:
# export_visualization_image(weights, i, out_dir, prefix, 'weights',
# 'auto', ['patterns', 'images'])
return None
def alpe_initialisation(imgs, init_atlas, init_weights, out_dir, out_prefix):
......@@ -396,7 +392,6 @@ def alpe_pipe_atlas_learning_ptn_weights(imgs, init_atlas=None, init_weights=Non
if step_diff <= thr_step_diff:
logger.info('>> exiting while the atlas diff %f is smaller then %f',
step_diff, thr_step_diff)
w_bins = [ptn_weight.weights_image_atlas_overlap_major(img, atlas)
for img in imgs]
break
w_bins = [ptn_weight.weights_image_atlas_overlap_major(img, atlas) for img in imgs]
return atlas, np.array(w_bins)
This diff is collapsed.
......@@ -8,6 +8,7 @@ import random
import glob
import logging
import traceback
import itertools
import multiprocessing as mproc
import numpy as np
......@@ -15,10 +16,11 @@ import pandas as pd
import matplotlib.pyplot as plt
from scipy import ndimage
from skimage import io, draw, transform, filters
# from PIL import Image
from PIL import Image
sys.path.append(os.path.abspath(os.path.join('..','..'))) # Add path to root
import src.own_utils.tool_data_io as tl_data
logger = logging.getLogger(__name__)
jirka = False
......@@ -43,6 +45,7 @@ DEFAULT_IM_PATTERN = 'pattern_{:03d}'
DEFAULT_IM_SEGM = 'sample_{:05d}'
DEFAULT_DATASET = 'datasetBinary_raw'
DEFAULT_NAME_WEIGHTS = 'combination.csv'
BLOCK_NB_LOAD_IMAGES = 50
def image_deform_elastic(im, coef=0.5, grid_size=(20, 20)):
......@@ -269,13 +272,13 @@ def export_image(outDir, im, im_name, n_template=DEFAULT_IM_SEGM):
"""
if not type(im_name)==str:
im_name = n_template.format(im_name)
p_img = os.path.join(outDir, im_name + DEFAULT_IM_POSIX)
path_img = os.path.join(outDir, im_name + DEFAULT_IM_POSIX)
im_norm = im / (np.max(im) * 1.)
logger.debug(' .. saving image %s to "%s...%s"',
repr(im_norm.shape), p_img[:25], p_img[-25:])
io.imsave(p_img, im_norm)
# Image.fromarray(im).save(pImg)
return p_img
logger.debug(' .. saving image %s with %s to "%s...%s"', repr(im_norm.shape),
repr(np.unique(im_norm)), path_img[:25], path_img[-25:])
io.imsave(path_img, im_norm)
# Image.fromarray(im_norm).save(path_img)
return path_img
def dataset_binary_deform_images(imgs, out_dir, coef=0.5):
......@@ -372,6 +375,12 @@ def dataset_prob_construct(imgs, out_dir, coef=0.5):
return imgs_prob
def mproc_load_images(list_path_img):
logger.debug('parallel loading %i images', len(list_path_img))
list_names_imgs = map(load_image, list_path_img)
return list_names_imgs
def dataset_load_images(name=DEFAULT_DATASET, path_base=DEFAULT_PATH_APD,
im_ptn='*', im_posix=DEFAULT_IM_POSIX, nb_spls=None,
nb_jobs=1):
......@@ -382,34 +391,43 @@ def dataset_load_images(name=DEFAULT_DATASET, path_base=DEFAULT_PATH_APD,
:param im_ptn: str, specific pattern of loaded images
:param im_posix: str image pattern line '.png'
:param nb_spls: int, number of samples to be loaded, None means all
:return:
:param nb_jobs: int
:return: [np.array], [str]
"""
path_dir = os.path.join(path_base, name)
logger.info('loading folder (%i) <- "%s"', os.path.exists(path_dir), path_dir)
p_search = os.path.join(path_dir, im_ptn + im_posix)
logger.debug('image search "%s"', p_search)
p_imgs = glob.glob(p_search)
logger.debug('number spls %i in dataset "%s"', len(p_imgs), name)
p_imgs = sorted(p_imgs)[:nb_spls]
path_search = os.path.join(path_dir, im_ptn + im_posix)
logger.debug('image search "%s"', path_search)
path_imgs = glob.glob(path_search)
logger.debug('number spls %i in dataset "%s"', len(path_imgs), name)
path_imgs = sorted(path_imgs)[:nb_spls]
if nb_jobs > 1:
logger.debug('running in %i threads...', nb_jobs)
logger.info('running in %i threads...', nb_jobs)
nb_load_blocks = len(path_imgs) / BLOCK_NB_LOAD_IMAGES
logger.debug('estimated %i loading blocks', nb_load_blocks)
list_path_im = [path_imgs[i::nb_load_blocks] for i in range(nb_load_blocks)]
mproc_pool = mproc.Pool(nb_jobs)
imgs = mproc_pool.map(load_image, p_imgs)
list_names_imgs = mproc_pool.map(mproc_load_images, list_path_im)
mproc_pool.close()
mproc_pool.join()
logger.info('transforming the parallel results')
im_names, imgs = zip(*itertools.chain(*list_names_imgs))
else:
logger.debug('running in single thread...')
imgs = map(load_image, p_imgs)
im_names = [os.path.splitext(os.path.basename(p))[0] for p in p_imgs]
logger.info('running in single thread...')
names_imgs = map(load_image, path_imgs)
logger.info('split the resulting tuples')
im_names, imgs = zip(*names_imgs)
assert len(path_imgs) == len(imgs)
return imgs, im_names
def load_image(p_img):
im = io.imread(p_img)
im = im / float(np.max(im))
return im
def load_image(path_img):
n_img = os.path.splitext(os.path.basename(path_img))[0]
# img = io.imread(path_img)
img = np.array(Image.open(path_img))
img = img / float(img.max())
return n_img, img
def dataset_load_weights(path_base=DEFAULT_PATH_APD, n_file=DEFAULT_NAME_WEIGHTS):
......@@ -465,12 +483,12 @@ def dataset_export_images(p_out, imgs, names=None, nb_jobs=1):
if nb_jobs > 1:
logger.debug('running in %i threads...', nb_jobs)
mproc_pool = mproc.Pool(nb_jobs)
mproc_pool.map(perfom_mproc, mp_set)
mproc_pool.map(mproc_wrapper, mp_set)
mproc_pool.close()
mproc_pool.join()
else:
logger.debug('running in single thread...')
map(perfom_mproc, mp_set)
map(mproc_wrapper, mp_set)
try:
path_npz = os.path.join(p_out, 'input_images.npz')
np.savez(open(path_npz, 'w'), imgs)
......@@ -479,7 +497,7 @@ def dataset_export_images(p_out, imgs, names=None, nb_jobs=1):
os.remove(path_npz)
def perfom_mproc(mp_set):
def mproc_wrapper(mp_set):
export_image(*mp_set)
......
import numpy as np
import logging
from sklearn import metrics
logger = logging.getLogger(__name__)
def compare_atlas_rnd_pairs(a1, a2):
""" compare two atlases as taking random pixels pairs from both
and evaluate that the are labeled equally of differently
:param a1: np.array<w, h>
:param a2: np.array<w, h>
:return: float with 0 means no difference
"""
logger.debug('comparing two atlases '
'of shapes {} <-> {}'.format(a1.shape, a2.shape))
assert np.array_equal(a1.shape, a2.shape)
# assert A1.shape[0]==A2.shape[0] and A1.shape[1]==A2.shape[1]
logger.debug('unique labels are {} and {}'.format(np.unique(a1).tolist(), np.unique(a2).tolist()))
X, Y = np.meshgrid(range(a1.shape[0]), range(a1.shape[1]))
vec_x, vec_y = X.flatten(), Y.flatten()
vec_x_perm = np.random.permutation(vec_x)
vec_y_perm = np.random.permutation(vec_y)
diffs = 0
for x1, y1, x2, y2 in zip(vec_x, vec_y, vec_x_perm, vec_y_perm):
b1 = a1[x1, y1] == a1[x2, y2]
b2 = a2[x1, y1] == a2[x2, y2]
if not b1 == b2: # T&F or F&T
# logger.debug('coords [{},{}], [{},{}] gives vals: {}, {}'.format(x1, y1, x2, y2, b1, b2))
# logger.debug('diff: {}?={} and {}?={}'.format(A1[x1,y1], A1[x2,y2], A2[x1,y1], A2[x2,y2]))
diffs += 1
res = diffs / float(len(vec_x))
return res
# Rand index, o tom uz jsme mluvili.
# Ted jde jen o to, jak tento index vypocitat efektivne. Coz, kdyz si
# rozmyslite, neni prilis tezke: udelate kontingencni tabulku=histogram, tedy oznacime n_ij kolik pixelu na stejnych
# souradnicich patri do tridy 'i'v prvnim obrazku a do tridy 'j' v druhem. Pak pocet shod(dva prvky jsou ve strejne
# tride v prvnim obrazku a ve stejne tride v druhem, nebo jsou v ruznych tridach v prvnim i v druhem) lze vyjadrit
# z histogramu - viz vzorec(1), clanek http://link.springer.com/article/10.1007%2FBF01908075
#
# Existuje i Adjusted Random Index, viz stejny clanek, nebo taky https://en.wikipedia.org/wiki/Rand_index
# Pro nase ucely je to asi jedno, proto budeme chtit jen porovnavat metody mezi sebou.
def compare_atlas_adjusted_rand(a1, a2):
""" using adjusted rand and transform original values from (-1, 1) to (0, 1)
http://scikit-learn.org/stable/modules/generated/sklearn.metrics.adjusted_rand_score.html
:param a1: np.array<w, h>
:param a2: np.array<w, h>
:return: float with 0 means no difference
"""
assert np.array_equal(a1.shape, a2.shape)
ars = metrics.adjusted_rand_score(a1.ravel(), a2.ravel())
res = 0.5 - (ars / 2.)
return res
def overlap_matrix_mlabel_segm(seg1, seg2):
"""
:param seg1: np.array<w, h>
:param seg2: np.array<w, h>
:return: np.array<w, h>
"""
logger.debug('computing overlap of two segm '
'of shapes {} <-> {}'.format(seg1.shape, seg2.shape))
assert np.array_equal(seg1.shape, seg2.shape)
u_lb1 = np.unique(seg1)
u_lb2 = np.unique(seg2)
u_lb1 = dict(zip(u_lb1,range(len(u_lb1))))
u_lb2 = dict(zip(u_lb2,range(len(u_lb2))))
logger.debug('unique labels:\n {}\n {}'.format(u_lb1, u_lb2))
res = np.zeros([len(u_lb1), len(u_lb2)])
for i in range(seg1.shape[0]):
for j in range(seg1.shape[1]):
u1, u2 = u_lb1[seg1[i, j]], u_lb2[seg2[i, j]]
res[u1, u2] += 1
res[u2, u1] += 1
# logger.debug(res)
return res
def compare_matrices(m1, m2):
"""
:param m1: np.array<w, h>
:param m2: np.array<w, h>
:return: float
"""
assert np.array_equal(m1.shape, m2.shape)
diff = np.sum(abs(m1 - m2))
return diff / float(np.product(m1.shape))
def compare_weights(c1, c2):
"""
:param c1: np.array<w, h>
:param c2: np.array<w, h>
:return: float
"""
return compare_matrices(c1, c2)
def test_atlases():
"""
:return:
"""
logger.info('testing METRIC')
a = np.random.randint(0,4,(5,5))
a2 = a.copy()
a2[a2==0] = -1
b = np.random.randint(0,4,(5,5))
logger.debug('compare_atlas_rnd_pairs, a <-> a: {}'.format(compare_atlas_rnd_pairs(a, a2)))
logger.debug('compare_atlas_rnd_pairs, a <-> b: {}'.format(compare_atlas_rnd_pairs(a, b)))
logger.debug('compare_atlas_adjusted_rand, a <-> a: {}'.format(compare_atlas_adjusted_rand(a, a2)))
logger.debug('compare_atlas_adjusted_rand, a <-> b: {}'.format(compare_atlas_adjusted_rand(a, b)))
return None
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
test_atlases()
import numpy as np
import logging
from sklearn import metrics
logger = logging.getLogger(__name__)
def compare_atlas_rnd_pairs(a1, a2):
""" compare two atlases as taking random pixels pairs from both
and evaluate that the are labeled equally of differently
:param a1: np.array<w, h>
:param a2: np.array<w, h>
:return: float with 0 means no difference
"""
logger.debug('comparing two atlases '
'of shapes {} <-> {}'.format(a1.shape, a2.shape))
assert np.array_equal(a1.shape, a2.shape)
# assert A1.shape[0]==A2.shape[0] and A1.shape[1]==A2.shape[1]
logger.debug('unique labels are {} and {}'.format(np.unique(a1).tolist(), np.unique(a2).tolist()))
X, Y = np.meshgrid(range(a1.shape[0]), range(a1.shape[1]))
vec_x, vec_y = X.flatten(), Y.flatten()
vec_x_perm = np.random.permutation(vec_x)
vec_y_perm = np.random.permutation(vec_y)
diffs = 0
for x1, y1, x2, y2 in zip(vec_x, vec_y, vec_x_perm, vec_y_perm):
b1 = a1[x1, y1] == a1[x2, y2]
b2 = a2[x1, y1] == a2[x2, y2]
if not b1 == b2: # T&F or F&T
# logger.debug('coords [{},{}], [{},{}] gives vals: {}, {}'.format(x1, y1, x2, y2, b1, b2))
# logger.debug('diff: {}?={} and {}?={}'.format(A1[x1,y1], A1[x2,y2], A2[x1,y1], A2[x2,y2]))
diffs += 1
res = diffs / float(len(vec_x))
return res
# Rand index, o tom uz jsme mluvili.
# Ted jde jen o to, jak tento index vypocitat efektivne. Coz, kdyz si
# rozmyslite, neni prilis tezke: udelate kontingencni tabulku=histogram, tedy oznacime n_ij kolik pixelu na stejnych
# souradnicich patri do tridy 'i'v prvnim obrazku a do tridy 'j' v druhem. Pak pocet shod(dva prvky jsou ve strejne
# tride v prvnim obrazku a ve stejne tride v druhem, nebo jsou v ruznych tridach v prvnim i v druhem) lze vyjadrit
# z histogramu - viz vzorec(1), clanek http://link.springer.com/article/10.1007%2FBF01908075
#
# Existuje i Adjusted Random Index, viz stejny clanek, nebo taky https://en.wikipedia.org/wiki/Rand_index
# Pro nase ucely je to asi jedno, proto budeme chtit jen porovnavat metody mezi sebou.
def compare_atlas_adjusted_rand(a1, a2):
""" using adjusted rand and transform original values from (-1, 1) to (0, 1)
http://scikit-learn.org/stable/modules/generated/sklearn.metrics.adjusted_rand_score.html
:param a1: np.array<w, h>
:param a2: np.array<w, h>
:return: float with 0 means no difference
"""
assert np.array_equal(a1.shape, a2.shape)
ars = metrics.adjusted_rand_score(a1.ravel(), a2.ravel())
res = 0.5 - (ars / 2.)
return res
def overlap_matrix_mlabel_segm(seg1, seg2):
"""
:param seg1: np.array<w, h>
:param seg2: np.array<w, h>
:return: np.array<w, h>
"""
logger.debug('computing overlap of two segm '
'of shapes {} <-> {}'.format(seg1.shape, seg2.shape))
assert np.array_equal(seg1.shape, seg2.shape)
u_lb1 = np.unique(seg1)
u_lb2 = np.unique(seg2)
u_lb1 = dict(zip(u_lb1,range(len(u_lb1))))
u_lb2 = dict(zip(u_lb2,range(len(u_lb2))))
logger.debug('unique labels:\n {}\n {}'.format(u_lb1, u_lb2))
res = np.zeros([len(u_lb1), len(u_lb2)])
for i in range(seg1.shape[0]):
for j in range(seg1.shape[1]):
u1, u2 = u_lb1[seg1[i, j]], u_lb2[seg2[i, j]]
res[u1, u2] += 1
res[u2, u1] += 1
# logger.debug(res)
return res
def compare_matrices(m1, m2):
"""
:param m1: np.array<w, h>
:param m2: np.array<w, h>
:return: float
"""
assert np.array_equal(m1.shape, m2.shape)
diff = np.sum(abs(m1 - m2))
return diff / float(np.product(m1.shape))
def compare_weights(c1, c2):
"""
:param c1: np.array<w, h>
:param c2: np.array<w, h>
:return: float
"""
return compare_matrices(c1, c2)
def test_atlases():
"""
:return:
"""
logger.info('testing METRIC')
a = np.random.randint(0,4,(5,5))
a2 = a.copy()
a2[a2==0] = -1
b = np.random.randint(0,4,(5,5))
logger.debug('compare_atlas_rnd_pairs, a <-> a: {}'.format(compare_atlas_rnd_pairs(a, a2)))
logger.debug('compare_atlas_rnd_pairs, a <-> b: {}'.format(compare_atlas_rnd_pairs(a, b)))
logger.debug('compare_atlas_adjusted_rand, a <-> a: {}'.format(compare_atlas_adjusted_rand(a, a2)))
logger.debug('compare_atlas_adjusted_rand, a <-> b: {}'.format(compare_atlas_adjusted_rand(a, b)))
return None
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
test_atlases()
logger.info('DONE')
\ No newline at end of file
import numpy as np
import matplotlib.pyplot as plt
import generate_dataset as data
import ptn_weights as ptn_weight
import pattern_weights as ptn_weight
from skimage import morphology
from scipy import ndimage
import logging
......
......@@ -43,14 +43,14 @@ THT_BACKGROUND = 0.95
THT_CONVEX = 0.85
def labels_ration(p_seg):
def labels_ration(path_seg):
""" compute radion among labels in segmentation as histogram
:param p_seg: str
:param path_seg: str
:return: {int: float}
"""
seg = io.imread(p_seg)
n_seg = os.path.basename(p_seg)
seg = io.imread(path_seg)
n_seg = os.path.basename(path_seg)
d_lb_hist = {lb: np.sum(seg == lb) / float(np.product(seg.shape) )
for lb in np.unique(seg)}
# the image borders
......@@ -68,7 +68,7 @@ def labels_ration(p_seg):
'r_cx': obj_bg}
def plot_histo_labels(d_hist, p_dir=''):
def plot_histo_labels(d_hist, path_dir=''):
""" plot some simple histogram
:param d_hist: {int: float}
......@@ -82,44 +82,44 @@ def plot_histo_labels(d_hist, p_dir=''):
plt.ylabel('label cover')
plt.legend(loc=0)
plt.grid()
if os.path.exists(p_dir):
fig.savefig(os.path.join(p_dir, FIG_STAT))
if os.path.exists(path_dir):
fig.savefig(os.path.join(path_dir, FIG_STAT))
def read_make_hist(p_segs):
def read_make_hist(paths_seg):
""" in parallel read all segmentation and compute individual histogram
:param p_segs: [str], paths to all segmentation
:param paths_seg: [str], paths to all segmentation
:return:[str, {int: float}] list or pairs with image name
and relative label histogram
"""
logger.debug('run in %i threads...', NB_JOBS)
mproc_pool = mproc.Pool(NB_JOBS)
l_desc = mproc_pool.map(labels_ration, p_segs)
l_desc = mproc_pool.map(labels_ration, paths_seg)
mproc_pool.close()
mproc_pool.join()
return l_desc
def merge_hist_stat(l_n_hist):
def merge_hist_stat(list_name_hist):
""" merge particular histograms per segmentation into one global per label
:param l_n_hist: [str, {int: float}] list or pairs with image name
:param list_name_hist: [str, {int: float}] list or pairs with image name
and relative label histogram
:return: {int: [float]}, histogram per label over all images
"""
logger.debug('merge partial results...')
l_hist = [l['lb_hist'] for l in l_n_hist]
l_hist = [l['lb_hist'] for l in list_name_hist]
lbs = itertools.chain(*[h.keys() for h in l_hist])
u_lbs = np.unique(list(lbs)).tolist()
d_hist = {lb: [h[lb] for h in l_hist if lb in h]
for lb in u_lbs}
uq_lbs = np.unique(list(lbs)).tolist()
dict_hist = {lb: [h[lb] for h in l_hist if lb in h]
for lb in uq_lbs}
# join the foregrounds
d_hist['fg'] = []
dict_hist['fg'] = []
for hist in l_hist:
d_hist['fg'].append(hist.get(1, 0) + hist.get(2, 0))
dict_hist['fg'].append(hist.get(1, 0) + hist.get(2, 0))
logger.debug('compute statistic...')
for lb, vals in d_hist.iteritems():
for lb, vals in dict_hist.iteritems():
if len(vals) == 0:
logger.warning('label %s has no values to compute', str(lb))
continue
......@@ -127,19 +127,19 @@ def merge_hist_stat(l_n_hist):
str(lb), np.mean(vals), np.median(vals), np.std(vals))
logger.debug(' -> count outliers: %i',
np.sum(abs(vals - np.median(vals)) > 3 * np.std(vals)))
return d_hist
return dict_hist
def segm_decision(l_desc, d_hist):
def segm_decision(l_desc, dict_hist):
""" according given rules decide weather the segmentation is good or not
:param l_desc: [{str, {int: float}}] list or pairs with image name
and relative label histogram
:param d_hist: {int: [float]}, histogram per label over all images
:param dict_hist: {int: [float]}, histogram per label over all images
:return: [str], [str]
"""
l_good, l_fail = [], []
fg_median, fg_std = np.median(d_hist['fg']), np.std(d_hist['fg'])
fg_median, fg_std = np.median(dict_hist['fg']), np.std(dict_hist['fg'])
for i, desc in enumerate(l_desc):
fg = desc['lb_hist'].get(1, 0) + desc['lb_hist'].get(2, 0)
b_range = abs(fg - fg_median) <= 3 * fg_std
......@@ -153,31 +153,31 @@ def segm_decision(l_desc, d_hist):
return l_good, l_fail
def export_results(p_dir, l_good, l_fail):
def export_results(path_dir, l_good, l_fail):
""" export the results into csv file
:param p_dir: str
:param path_dir: str
:param l_good: [str], names of images
:param l_fail: [str], names of images
"""
logger.info('export results as CSV files')
pd.DataFrame(['images'] + l_good).to_csv(
os.path.join(p_dir, CSV_SEGM_GOOD), index=False, header=False)
os.path.join(path_dir, CSV_SEGM_GOOD), index=False, header=False)
pd.DataFrame(['images'] + l_fail).to_csv(
os.path.join(p_dir, CSV_SEGM_FAIL), index=False, header=False)
os.path.join(path_dir, CSV_SEGM_FAIL), index=False, header=False)
def segm_detect_fails(p_dir=PATH_SEGM, im_ptn='*.png'):
def segm_detect_fails(path_dir=PATH_SEGM, im_pattern='*.png'):
""" make the statistic over all segmentation in given folder
and decide weather that are correct or fails
:param p_dir: str
:param im_ptn: str, pattern fro images
:param path_dir: str
:param im_pattern: str, pattern fro images
"""
logger.info('FOLDER: "%s"', p_dir)
if not os.path.exists(p_dir):
raise Exception('folder "{}" dnot exist'.format(p_dir))
p_segs = glob.glob(os.path.join(p_dir, im_ptn))
logger.info('FOLDER: "%s"', path_dir)
if not os.path.exists(path_dir):
raise Exception('folder "{}" dnot exist'.format(path_dir))
p_segs = glob.glob(os.path.join(path_dir, im_pattern))
logger.debug('found %i segmentation', len(p_segs))
l_desc = read_make_hist(p_segs)
......@@ -188,24 +188,24 @@ def segm_detect_fails(p_dir=PATH_SEGM, im_ptn='*.png'):
l_good, l_fail = segm_decision(l_desc, d_hist)
logger.info('number good %i and fails %i', len(l_good), len(l_fail))
export_results(p_dir, l_good, l_fail)
export_results(path_dir, l_good, l_fail)
# show all samples
plot_histo_labels(d_hist, p_dir)
plot_histo_labels(d_hist, path_dir)
def mproc_copy_file(mp_set):
shutil.copyfile(*mp_set)
def copy_files(l_imgs, p_dir_visu, p_out):
def copy_files(l_imgs, path_dir_visu, path_out):
""" copy list of images in multi thread
:param l_imgs: [str]
:param p_dir_visu: str
:param p_out: str
:param path_dir_visu: str
:param path_out: str
"""
pp_dir_visu = os.path.join(p_dir_visu, PREFIX_VISU_SEGM)
pp_out = os.path.join(p_out, PREFIX_VISU_SEGM)
pp_dir_visu = os.path.join(path_dir_visu, PREFIX_VISU_SEGM)
pp_out = os.path.join(path_out, PREFIX_VISU_SEGM)
mp_set = [(pp_dir_visu + n_img, pp_out + n_img) for n_img in l_imgs]
mproc_pool = mproc.Pool(NB_JOBS)
......@@ -214,30 +214,30 @@ def copy_files(l_imgs, p_dir_visu, p_out):
mproc_pool.join()
def filter_copy_visu(p_dir_seg=PATH_SEGM, p_dir_visu=PATH_VISU):
def filter_copy_visu(path_dir_seg=PATH_SEGM, path_dir_visu=PATH_VISU):
""" load csv file vith good and bad segmentation and in the visual folder
create subfolder for good and bad segm and copy relevant iimages there
:param p_dir_seg: str
:param p_dir_visu: str
:param path_dir_seg: str
:param path_dir_visu: str
"""
logger.info('filter and copy cases')
logger.debug('segmentation: %s,\n visual: %s', p_dir_seg, p_dir_visu)
logger.debug('segmentation: %s,\n visual: %s', path_dir_seg, path_dir_visu)
for n_csv in [CSV_SEGM_GOOD, CSV_SEGM_FAIL]:
logger.info('reading "%s"', n_csv)
p_out = os.path.join(p_dir_visu, os.path.splitext(n_csv)[0])
p_out = os.path.join(path_dir_visu, os.path.splitext(n_csv)[0])
if os.path.exists(p_out):
logger.debug('remove old dir %s', p_out)
shutil.rmtree(p_out)
os.mkdir(p_out)
df = pd.DataFrame.from_csv(os.path.join(p_dir_seg, n_csv),
df = pd.DataFrame.from_csv(os.path.join(path_dir_seg, n_csv),
index_col=False)
logger.info('copy %i images to "%s"', len(df), n_csv)
copy_files(df['images'].values.tolist(), p_dir_visu, p_out)
copy_files(df['images'].values.tolist(), path_dir_visu, p_out)
def main():
""" the main entry point """
""" the main_real entry point """
# defaults run
segm_detect_fails()
filter_copy_visu()
......
......@@ -16,12 +16,12 @@ import logging
import numpy as np
from sklearn import decomposition
import expt_apd
import experiment_apd
logger = logging.getLogger(__name__)