Commit 897e2d05 authored by Jirka's avatar Jirka

experiments in parallel

parent 4cc5fc9a
......@@ -4,6 +4,7 @@ matplotlib.use('Agg')
import os
import time
import gc
import traceback
import numpy as np
import pandas as pd
......@@ -35,7 +36,7 @@ DEFAULT_PATH_APD = os.path.join(DEFAULT_PATH_DATA, DEFAULT_DATASET_NAME)
DEFAULT_SUB_DATASETS = ['datasetBinary_raw',
'datasetBinary_noise',
'datasetBinary_deform',
'datasetBinary_defNoise',]
'datasetBinary_defNoise']
DEFAULT_PARAMS = {
'name': 'NAME',
'in_path': DEFAULT_PATH_DATA,
......@@ -46,12 +47,17 @@ DEFAULT_PARAMS = {
'max_iter': 29,
'gc_regul': 0.,
'nb_lbs': 7,
'nb_runs': 500, # 500
'nb_iters': 500, # 500
'gc_reinit': True,
'ptn_split': True,
}
def test_simple_show_case():
"""
:return:
"""
# implement simple case just with 2 images and 2/3 classes in atlas
atlas = gen_data.get_simple_atlas()
# atlas2 = atlas.copy()
......@@ -86,6 +92,11 @@ def test_simple_show_case():
def experiment_pipeline_alpe_showcase(p_out=DEFAULT_PATH_OUTPUT):
""" an simple show case to prove that the particular steps are computed
:param p_out: str
:return:
"""
atlas = gen_data.dataset_create_atlas(path_base=DEFAULT_PATH_APD)
# plt.imshow(atlas)
......@@ -104,12 +115,12 @@ def experiment_pipeline_alpe_showcase(p_out=DEFAULT_PATH_OUTPUT):
def alpe_init_atlas(im_size, nb_lbs, init_tp):
"""
""" init atlas according an param
:param im_size:
:param nb_lbs:
:param init_tp:
:return:
:param im_size: (int, int)
:param nb_lbs: int, number of labels
:param init_tp: str
:return: np.array<w, h>
"""
if init_tp == 'msc':
init_atlas = ptn_dict.initialise_atlas_mosaic(im_size, nb_lbs)
......@@ -119,13 +130,13 @@ def alpe_init_atlas(im_size, nb_lbs, init_tp):
def alpe_compute_stat(GT_atlas, GT_img_rct, atlas, w_bins):
"""
""" copuet the statistic gor GT and estimated atlas and reconstructed images
:param GT_atlas:
:param GT_img_rct:
:param atlas:
:param w_bins:
:return:
:param GT_atlas: np.array<w, h>
:param GT_img_rct: [np.array<w, h>]
:param atlas: np.array<w, h>
:param w_bins: np.array<nb_spls, nb_lbs>
:return: {str: float, }
"""
ars = metrics.adjusted_rand_score(GT_atlas.ravel(), atlas.ravel())
img_rct = ptn_dict.reconstruct_samples(atlas, w_bins)
......@@ -136,6 +147,14 @@ def alpe_compute_stat(GT_atlas, GT_img_rct, atlas, w_bins):
def alpe_pipe_atlas(i, imgs, init_atlas, params):
""" set all params and run the atlas estimation in try mode
:param i: int, index of try
:param imgs: [np.array<w, h>]
:param init_atlas: np.array<w, h>
:param params: {str: ...}, parameter settings
:return:
"""
prefix = 'expt_{}'.format(params['init_tp'])
p_out = os.path.join(params['res_path'], 'case_{:05d}'.format(i))
if not os.path.exists(p_out):
......@@ -155,10 +174,10 @@ def alpe_pipe_atlas(i, imgs, init_atlas, params):
def load_data_ground_truth(params):
"""
""" loading all GT suh as atlas and reconstructed images from GT encoding
:param params:
:return:
:param params: {str: ...}, parameter settings
:return: np.array<w, h>, [np.array<w, h>]
"""
p_data = os.path.join(params['in_path'], params['dataset'])
GT_atlas = gen_data.dataset_create_atlas(path_base=p_data)
......@@ -168,6 +187,11 @@ def load_data_ground_truth(params):
def perform_experiment(expt_batch):
""" extracted code for running experiemnt in parallel mode
:param expt_batch: (gt_atlas, gt_img_rct, i, imgs, params)
:return: {str: float, }
"""
gt_atlas, gt_img_rct, i, imgs, params = expt_batch
init_atlas = alpe_init_atlas(imgs[0].shape, params['nb_lbs'],
params['init_tp'])
......@@ -177,12 +201,12 @@ def perform_experiment(expt_batch):
return stat
def expt_bacth_pipeline_alpe(gt_atlas, gt_img_rct, params):
"""
def expt_bacth_pipeline_alpe_parallel(gt_atlas, gt_img_rct, params):
""" run batch experiment in multi thread mode, as nb of cores
:param gt_atlas:
:param gt_img_rct:
:param params:
:param gt_atlas: np.array<w, h>, ground truth atlas
:param gt_img_rct: [np.array<w, h>], list of GT reconstructed images
:param params: {str: ...}, parameter settings
:return:
"""
tl_expt.check_exist_dirs_files(params)
......@@ -198,20 +222,53 @@ def expt_bacth_pipeline_alpe(gt_atlas, gt_img_rct, params):
mproc_pool = mproc.Pool(mproc.cpu_count())
expt_batch = [(gt_atlas, gt_img_rct, i, imgs, params)
for i in range(params['nb_runs'])]
for i in range(params['nb_iters'])]
l_stat = mproc_pool.map(perform_experiment, expt_batch)
mproc_pool.close()
mproc_pool.join()
for stat in l_stat:
df_stat = df_stat.append(stat, ignore_index=True)
df_stat = df_stat.set_index('idx')
df_stat.to_csv(os.path.join(params['res_path'], 'results.csv'))
# for i in range(params['nb_runs']):
# init_atlas = alpe_init_atlas(im_size, params['nb_lbs'], params['init_tp'])
# atlas, w_bins = alpe_pipe_atlas(i, imgs, init_atlas, params)
# stat = alpe_compute_stat(gt_atlas, gt_img_rct, atlas, w_bins)
# df_stat = df_stat.append(stat, ignore_index=True)
# df_stat.to_csv(os.path.join(params['res_path'], 'results.csv'))
logger.debug('statistic: \n{}'.format(df_stat.describe()))
f = open(p_res_stat, 'a')
f.write('\n' * 3 + 'RESULTS: \n' + '=' * 9)
f.write('\n{}'.format(df_stat.describe()))
f.close()
del imgs, expt_batch, mproc_pool
gc.collect(), time.sleep(9)
return df_stat
def expt_bacth_pipeline_alpe_single(gt_atlas, gt_img_rct, params):
""" run batch experiment in single thread mode
:param gt_atlas: np.array<w, h>, ground truth atlas
:param gt_img_rct: [np.array<w, h>], list of GT reconstructed images
:param params: {str: ...}, parameter settings
:return:
"""
tl_expt.check_exist_dirs_files(params)
params['res_path'] = tl_data.create_experiment_folder(params['out_path'],
params['name'])
p_res_stat = os.path.join(params['res_path'], tl_expt.DEFAULT_FILE_RESULTS)
tl_expt.export_params(params, p_res_stat)
p_data = os.path.join(params['in_path'], params['dataset'])
imgs = gen_data.dataset_load_images(params['sub_dataset'], p_data)
df_stat = pd.DataFrame()
for i in range(params['nb_iters']):
init_atlas = alpe_init_atlas(imgs[0].shape, params['nb_lbs'],
params['init_tp'])
atlas, w_bins = alpe_pipe_atlas(i, imgs, init_atlas, params)
stat = alpe_compute_stat(gt_atlas, gt_img_rct, atlas, w_bins)
df_stat = df_stat.append(stat, ignore_index=True)
df_stat.to_csv(os.path.join(params['res_path'], 'results.csv'))
logger.debug('statistic: \n{}'.format(df_stat.describe()))
f = open(p_res_stat, 'a')
......@@ -221,15 +278,14 @@ def expt_bacth_pipeline_alpe(gt_atlas, gt_img_rct, params):
return df_stat
# TODO: compare error to nb classes
# TODO: other datasets
def params_extend_all_sub_datasets(l_params):
"""
""" extend the parameter list by all sub-datasets
:param l_params:
:return:
:param l_params: [{str: ...}]
:return: [{str: ...}]
"""
l_params_ds = []
for p in l_params:
......@@ -241,10 +297,10 @@ def params_extend_all_sub_datasets(l_params):
def experiment_nb_classes(name='nb-Labels', params=None):
"""
""" compare error to nb classes
:param name:
:param params:
:param name: str
:param params: {str: ...}
:return:
"""
if params is None:
......@@ -259,21 +315,24 @@ def experiment_nb_classes(name='nb-Labels', params=None):
logger.debug('list params: {}'.format(len(l_params)))
l_params = params_extend_all_sub_datasets(l_params)
for p in l_params:
expt_bacth_pipeline_alpe(gt_atlas, gt_img_rct, p)
expt_bacth_pipeline_alpe_parallel(gt_atlas, gt_img_rct, p)
gc.collect(), time.sleep(9)
return
def experiments_all():
""" run all experiments
:return:
"""
config = DEFAULT_PARAMS.copy()
experiment_nb_classes()
experiment_nb_classes(params=config)
config.update({'ptn_split': False})
experiment_nb_classes(params=config)
config = DEFAULT_PARAMS.copy()
config.update({'init_tp': 'rnd'})
experiment_nb_classes()
experiment_nb_classes(params=config)
config.update({'ptn_split': False})
experiment_nb_classes(params=config)
......@@ -282,11 +341,17 @@ def experiments_all():
def experiments_test():
""" simple test of the experiments
:return:
"""
experiment_pipeline_alpe_showcase()
params = DEFAULT_PARAMS.copy()
params['nb_iters'] = 3
GT_atlas, GT_img_rct = load_data_ground_truth(params)
expt_bacth_pipeline_alpe(GT_atlas, GT_img_rct, params)
expt_bacth_pipeline_alpe_single(GT_atlas, GT_img_rct, params)
expt_bacth_pipeline_alpe_parallel(GT_atlas, GT_img_rct, params)
return
......
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