experiment_apd.py 15.4 KB
Newer Older
Jiri Borovec's avatar
Jiri Borovec committed
1
"""
Jiri Borovec's avatar
Jiri Borovec committed
2 3
tha base class for all Atomic Pattern Dictionary methods
such as the stat of the art and our newly developed
Jiri Borovec's avatar
Jiri Borovec committed
4 5 6 7

Example run:
>> nohup python experiments_sta.py > ~/Medical-temp/experiments_APD-sta/nohup.log &

Jiri Borovec's avatar
Jiri Borovec committed
8
Copyright (C) 2015-2016 Jiri Borovec <jiri.borovec@fel.cvut.cz>
Jiri Borovec's avatar
Jiri Borovec committed
9 10 11 12 13 14 15
"""

# to suppress all visu, has to be on the beginning
# import matplotlib
# matplotlib.use('Agg')

import os
Jiri Borovec's avatar
Jiri Borovec committed
16 17
import copy
import time
Jiri Borovec's avatar
Jiri Borovec committed
18
import types
Jiri Borovec's avatar
Jiri Borovec committed
19
import argparse
Jiri Borovec's avatar
Jiri Borovec committed
20 21 22
import logging
import copy_reg
import tqdm
Jiri Borovec's avatar
Jiri Borovec committed
23
import multiprocessing as mproc
Jiri Borovec's avatar
Jiri Borovec committed
24 25 26 27 28

import numpy as np
import pandas as pd
from sklearn import metrics

Jiri Borovec's avatar
Jiri Borovec committed
29
import dataset_utils as gen_data
Jiri Borovec's avatar
Jiri Borovec committed
30
import pattern_disctionary as ptn_dict
Jiri Borovec's avatar
Jiri Borovec committed
31
import src.own_utils.tool_experiments as tl_expt
Jiri Borovec's avatar
Jiri Borovec committed
32 33


Jiri Borovec's avatar
Jiri Borovec committed
34
def _reduce_method(m):
Jiri Borovec's avatar
Jiri Borovec committed
35 36 37 38 39
    """ 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
    """
Jiri Borovec's avatar
Jiri Borovec committed
40 41 42 43 44 45 46 47
    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)

NB_THREADS = int(mproc.cpu_count() * .9)
Jiri Borovec's avatar
Jiri Borovec committed
48 49 50
PATH_DATA_SYNTH = '/datagrid/Medical/microscopy/drosophila/synthetic_data'
PATH_DATA_REAL = '/datagrid/Medical/microscopy/drosophila/TEMPORARY'
PATH_RESULTS = '/datagrid/Medical/microscopy/drosophila/TEMPORARY/experiments_APD_temp'
Jiri Borovec's avatar
Jiri Borovec committed
51 52
DEFAULT_PARAMS = {
    'computer': os.uname(),
Jiri Borovec's avatar
Jiri Borovec committed
53
    'nb_samples': None,
54 55 56
    'tol': 1e-3,
    'init_tp': 'msc',  # msc. rnd
    'max_iter': 25,  # 250
Jiri Borovec's avatar
Jiri Borovec committed
57
    'gc_regul': 0.,
58
    'nb_labels': 2,
Jiri Borovec's avatar
Jiri Borovec committed
59
    'nb_runs': NB_THREADS,  # 500
Jiri Borovec's avatar
Jiri Borovec committed
60
    'gc_reinit': True,
61
    'ptn_split': False,
62 63
    'ptn_compact': True,
    'overlap_mj': True,
Jiri Borovec's avatar
Jiri Borovec committed
64 65
}

Jiri Borovec's avatar
Jiri Borovec committed
66
SYNTH_DATASET_NAME = 'atomicPatternDictionary_v0'
Jiri Borovec's avatar
Jiri Borovec committed
67 68 69 70 71 72
SYNTH_PATH_APD = os.path.join(PATH_DATA_SYNTH, SYNTH_DATASET_NAME)
SYNTH_SUB_DATASETS = ['datasetBinary_raw',
                      'datasetBinary_noise',
                      'datasetBinary_deform',
                      'datasetBinary_defNoise']
SYNTH_PARAMS = DEFAULT_PARAMS.copy()
Jiri Borovec's avatar
Jiri Borovec committed
73 74
SYNTH_PARAMS.update({
    'path_in': SYNTH_PATH_APD,
Jiri Borovec's avatar
Jiri Borovec committed
75
    'dataset': SYNTH_SUB_DATASETS,
Jiri Borovec's avatar
Jiri Borovec committed
76 77 78 79
    'path_out': PATH_RESULTS,
})
SYNTH_PTN_RANGE = {
    'atomicPatternDictionary_00': range(5),
Jiri Borovec's avatar
Jiri Borovec committed
80
    'atomicPatternDictionary_v0': range(3, 15, 1),
Jiri Borovec's avatar
Jiri Borovec committed
81
    'atomicPatternDictionary_v1': range(5, 20, 1),
82
    'atomicPatternDictionary_v2': range(10, 40, 2) + [23],
Jiri Borovec's avatar
Jiri Borovec committed
83 84 85 86
    'atomicPatternDictionary_v3': range(10, 40, 2),
    'atomicPatternDictionary3D_v0': range(2, 14, 1),
    'atomicPatternDictionary3D_v1': range(6, 30, 2),
}
Jiri Borovec's avatar
Jiri Borovec committed
87 88 89
# SYNTH_RESULTS_NAME = 'experiments_APD'

REAL_DATASET_NAME = '1000_images_improved_binary'
Jiri Borovec's avatar
Jiri Borovec committed
90
# REAL_SUB_DATASETS = ['binary-fix', 'binary-otsu', 'binary-adapt']
Jiri Borovec's avatar
Jiri Borovec committed
91
NB_PATTERNS_REAL = [5, 8, 10, 12, 14, 16, 18, 20, 25, 30, 40, 60]
Jiri Borovec's avatar
Jiri Borovec committed
92 93 94 95 96
REAL_SUB_DATASETS = [
    # 'gene',
    # 'gene_small',
    'gene_ssmall',
]
Jiri Borovec's avatar
Jiri Borovec committed
97
REAL_PARAMS = DEFAULT_PARAMS.copy()
Jiri Borovec's avatar
Jiri Borovec committed
98 99
REAL_PARAMS.update({
    'path_in': os.path.join(PATH_DATA_REAL, REAL_DATASET_NAME),
Jiri Borovec's avatar
Jiri Borovec committed
100
    'dataset': REAL_SUB_DATASETS,
Jiri Borovec's avatar
Jiri Borovec committed
101 102 103
    'path_out': PATH_RESULTS,
    'max_iter': 50,
    'nb_runs': 10})
Jiri Borovec's avatar
Jiri Borovec committed
104 105 106
# PATH_OUTPUT = os.path.join('..','..','results')


Jiri Borovec's avatar
Jiri Borovec committed
107 108 109
def create_args_parser(dict_params):
    """ create simple arg parser with default values (input, output, dataset)

Jiri Borovec's avatar
Jiri Borovec committed
110
    :param dict_params: {str: ...}
Jiri Borovec's avatar
Jiri Borovec committed
111 112 113
    :return: object argparse<in, out, ant, name>
    """
    parser = argparse.ArgumentParser()
Jiri Borovec's avatar
Jiri Borovec committed
114
    parser.add_argument('-in', '--path_in', type=str, required=True,
Jiri Borovec's avatar
Jiri Borovec committed
115 116
                        help='path to the input image dataset',
                        default=dict_params['path_in'])
Jiri Borovec's avatar
Jiri Borovec committed
117
    parser.add_argument('-out', '--path_out', type=str, required=True,
Jiri Borovec's avatar
Jiri Borovec committed
118 119
                        help='path to the output with experiment results',
                        default=dict_params['path_out'])
Jiri Borovec's avatar
Jiri Borovec committed
120 121 122
    parser.add_argument('-tp', '--type', type=str, required=False,
                        help='switch between real and synth. images',
                        default='synth', choices=['real', 'synth'])
Jiri Borovec's avatar
Jiri Borovec committed
123 124
    parser.add_argument('-n', '--name', type=str, required=False,
                        help='specific name', default=None)
Jiri Borovec's avatar
Jiri Borovec committed
125 126
    parser.add_argument('--dataset', type=str, required=False, nargs='+', default=None,
                        help='name of dataset to be used')
Jiri Borovec's avatar
Jiri Borovec committed
127 128
    parser.add_argument('--nb_jobs', type=int, required=False, default=NB_THREADS,
                        help='number of processes running in parallel')
Jiri Borovec's avatar
Jiri Borovec committed
129
    parser.add_argument('--method', type=str, required=False, nargs='+', default=None,
Jiri Borovec's avatar
Jiri Borovec committed
130 131
                        help='possible APD methods',
                        choices=['PCA', 'ICA', 'DL', 'NMF', 'APDL'])
Jiri Borovec's avatar
Jiri Borovec committed
132 133 134 135 136 137 138 139 140 141 142 143
    return parser


def parse_arg_params(parser):
    """ parse basic args and return as dictionary

    :param parser: argparse
    :return: {str: ...}
    """
    args = vars(parser.parse_args())
    # remove not filled parameters
    args = {k: args[k] for k in args if args[k] is not None}
Jiri Borovec's avatar
Jiri Borovec committed
144
    for n in (k for k in args if 'path' in k and args[k] is not None):
Jiri Borovec's avatar
Jiri Borovec committed
145
        args[n] = os.path.abspath(os.path.expanduser(args[n]))
Jiri Borovec's avatar
Jiri Borovec committed
146
        assert os.path.exists(args[n]), '%s' % args[n]
Jiri Borovec's avatar
Jiri Borovec committed
147 148 149
    return args


Jiri Borovec's avatar
Jiri Borovec committed
150 151 152 153 154 155
def parse_params(default_params):
    parser = create_args_parser(default_params)
    params = copy.deepcopy(default_params)
    arg_params = parse_arg_params(parser)
    params.update(arg_params)
    return params
Jiri Borovec's avatar
Jiri Borovec committed
156 157


Jiri Borovec's avatar
Jiri Borovec committed
158 159
class ExperimentAPD(tl_expt.Experiment):
    """
Jiri Borovec's avatar
Jiri Borovec committed
160
    main class for APD experiments State-of-the-Art and ALPE
Jiri Borovec's avatar
Jiri Borovec committed
161 162 163 164 165 166 167
    """

    def __init__(self, dict_params):
        """ initialise class and set the experiment parameters

        :param dict_params: {str: ...}
        """
Jiri Borovec's avatar
Jiri Borovec committed
168
        if not 'name' in dict_params:
Jiri Borovec's avatar
Jiri Borovec committed
169
            dict_params['name'] = '{}_{}_{}'.format(dict_params['type'],
Jiri Borovec's avatar
Jiri Borovec committed
170 171
                                                    os.path.basename(dict_params['path_in']),
                                                    dict_params['dataset'])
Jiri Borovec's avatar
Jiri Borovec committed
172 173
        if not os.path.exists(dict_params['path_out']):
            os.mkdir(dict_params['path_out'])
Jiri Borovec's avatar
Jiri Borovec committed
174 175
        super(ExperimentAPD, self).__init__(dict_params)
        self.df_stat = pd.DataFrame()
Jiri Borovec's avatar
Jiri Borovec committed
176
        self.path_stat = os.path.join(self.params.get('path_exp'), self.RESULTS_TXT)
Jiri Borovec's avatar
Jiri Borovec committed
177
        # self.params.export_as(self.path_stat)
Jiri Borovec's avatar
Jiri Borovec committed
178 179
        str_params = 'PARAMETERS: \n' + '\n'.join(['"{}": \t {}'.format(k, v)
                                                   for k, v in self.params.iteritems()])
Jiri Borovec's avatar
Jiri Borovec committed
180
        logging.info(str_params)
Jiri Borovec's avatar
Jiri Borovec committed
181 182
        with open(self.path_stat, 'w') as fp:
            fp.write(str_params)
Jiri Borovec's avatar
Jiri Borovec committed
183

Jiri Borovec's avatar
Jiri Borovec committed
184
    def _load_data_ground_truth(self):
Jiri Borovec's avatar
Jiri Borovec committed
185 186 187 188
        """ loading all GT suh as atlas and reconstructed images from GT encoding

        :param params: {str: ...}, parameter settings
        """
Jiri Borovec's avatar
Jiri Borovec committed
189
        self.gt_atlas = gen_data.dataset_compose_atlas(self.params.get('path_in'))
Jiri Borovec's avatar
Jiri Borovec committed
190
        gt_encoding = gen_data.dataset_load_weights(self.params.get('path_in'))
Jiri Borovec's avatar
Jiri Borovec committed
191 192 193 194 195 196 197
        self.gt_img_rct = ptn_dict.reconstruct_samples(self.gt_atlas, gt_encoding)

    def _load_data(self, gt=True):
        """ load all required data for APD and also ground-truth if required

        :param gt: bool
        """
Jiri Borovec's avatar
Jiri Borovec committed
198
        logging.info('loading required data')
199
        self._load_images()
Jiri Borovec's avatar
Jiri Borovec committed
200
        if gt:
Jiri Borovec's avatar
Jiri Borovec committed
201
            self._load_data_ground_truth()
202 203 204
            assert len(self.imgs) == len(self.gt_img_rct)
        logging.debug('loaded %i images', len(self.imgs))
        self.imgs = [im.astype(np.uint8, copy=False) for im in self.imgs]
Jiri Borovec's avatar
Jiri Borovec committed
205 206 207 208 209

    def _load_images(self):
        """ load image data """
        path_data = os.path.join(self.params.get('path_in'),
                                 self.params.get('dataset'))
Jiri Borovec's avatar
Jiri Borovec committed
210
        self.imgs, self._im_names = gen_data.dataset_load_images(path_data)
Jiri Borovec's avatar
Jiri Borovec committed
211 212

    def run(self, gt=True, iter_var='case', iter_vals=range(1)):
Jiri Borovec's avatar
Jiri Borovec committed
213
        """ the main_real procedure that load, perform and evaluete experiment
Jiri Borovec's avatar
Jiri Borovec committed
214 215 216 217 218

        :param gt: bool
        :param iter_var: str name of variable to be iterated in the experiment
        :param iter_vals: [] list of possible values for :param iter_var:
        """
Jiri Borovec's avatar
Jiri Borovec committed
219
        logging.info('perform the complete experiment')
Jiri Borovec's avatar
Jiri Borovec committed
220 221
        self.iter_var_name = iter_var
        self.iter_values = iter_vals
Jiri Borovec's avatar
Jiri Borovec committed
222 223 224 225 226 227 228 229 230
        super(ExperimentAPD, self).run(gt)

    def _perform(self):
        """ perform experiment as sequence of iterated configurations """
        self._perform_sequence()

    def _perform_sequence(self):
        """ iteratively change a single experiment parameter with the same data
        """
Jiri Borovec's avatar
Jiri Borovec committed
231
        logging.info('perform_sequence in single thread')
Jiri Borovec's avatar
Jiri Borovec committed
232
        self.l_stat = []
Jiri Borovec's avatar
Jiri Borovec committed
233
        tqdm_bar = tqdm.tqdm(total=len(self.iter_values))
Jiri Borovec's avatar
Jiri Borovec committed
234 235
        for v in self.iter_values:
            self.params[self.iter_var_name] = v
Jiri Borovec's avatar
Jiri Borovec committed
236
            logging.debug(' -> set iterable "%s" on %s', self.iter_var_name,
Jiri Borovec's avatar
Jiri Borovec committed
237
                         repr(self.params[self.iter_var_name]))
Jiri Borovec's avatar
Jiri Borovec committed
238
            t = time.time()
Jiri Borovec's avatar
Jiri Borovec committed
239
            stat = self._perform_once(v)
Jiri Borovec's avatar
Jiri Borovec committed
240
            stat['time'] = time.time() - t
Jiri Borovec's avatar
Jiri Borovec committed
241
            self.l_stat.append(stat)
Jiri Borovec's avatar
Jiri Borovec committed
242 243
            logging.info('partial results: %s', repr(stat))
            tqdm_bar.update(1)
Jiri Borovec's avatar
Jiri Borovec committed
244 245
            # just partial export
            self._evaluate()
Jiri Borovec's avatar
Jiri Borovec committed
246 247 248 249 250 251 252

    def _perform_once(self, v):
        """ perform single experiment

        :param v: value
        :return: {str: val}
        """
Jiri Borovec's avatar
Jiri Borovec committed
253
        stat = {self.iter_var_name: v}
Jiri Borovec's avatar
Jiri Borovec committed
254 255
        return stat

Jiri Borovec's avatar
Jiri Borovec committed
256
    def _export_atlas(self, posix=''):
Jiri Borovec's avatar
Jiri Borovec committed
257 258 259 260 261
        """ export estimated atlas

        :param atlas: np.arra<h, w>
        :param posix: str
        """
Jiri Borovec's avatar
Jiri Borovec committed
262
        assert hasattr(self, 'atlas')
Jiri Borovec's avatar
Jiri Borovec committed
263
        n_img = 'atlas{}'.format(posix)
Jiri Borovec's avatar
Jiri Borovec committed
264
        gen_data.export_image(self.params.get('path_exp'), self.atlas, n_img)
Jiri Borovec's avatar
Jiri Borovec committed
265

Jiri Borovec's avatar
Jiri Borovec committed
266
    def _export_coding(self, posix=''):
Jiri Borovec's avatar
Jiri Borovec committed
267 268 269 270 271
        """ export estimated atlas

        :param atlas: np.array<height, width>
        :param posix: str
        """
Jiri Borovec's avatar
Jiri Borovec committed
272
        assert hasattr(self, 'w_bins')
Jiri Borovec's avatar
Jiri Borovec committed
273
        n_csv = 'encoding{}.csv'.format(posix)
Jiri Borovec's avatar
Jiri Borovec committed
274
        path_csv = os.path.join(self.params.get('path_exp'), n_csv)
Jiri Borovec's avatar
Jiri Borovec committed
275
        if not hasattr(self, '_im_names'):
276
            self._im_names = [str(i) for i in range(self.w_bins.shape[0])]
Jiri Borovec's avatar
Jiri Borovec committed
277
        df = pd.DataFrame(data=self.w_bins, index=self._im_names[:len(self.w_bins)])
Jiri Borovec's avatar
Jiri Borovec committed
278
        df.columns = ['ptn {:02d}'.format(lb + 1) for lb in df.columns]
Jiri Borovec's avatar
Jiri Borovec committed
279 280 281
        df.index.name = 'image'
        df.to_csv(path_csv)

282
    def _compute_statistic_gt(self, imgs_rct=None):
Jiri Borovec's avatar
Jiri Borovec committed
283 284 285
        """ compute the statistic gor GT and estimated atlas and reconstructed images

        :param atlas: np.array<height, width>
286
        :param imgs_rct: [np.array<height, width>]
Jiri Borovec's avatar
Jiri Borovec committed
287 288 289
        :return: {str: float, }
        """
        stat = {}
Jiri Borovec's avatar
Jiri Borovec committed
290
        logging.debug('compute static - %s', hasattr(self, 'gt_atlas'))
Jiri Borovec's avatar
Jiri Borovec committed
291 292 293 294
        if hasattr(self, 'gt_atlas') and hasattr(self, 'atlas'):
            if self.gt_atlas.shape == self.atlas.shape:
                stat['atlas_ARS'] = metrics.adjusted_rand_score(self.gt_atlas.ravel(),
                                                                self.atlas.ravel())
Jiri Borovec's avatar
Jiri Borovec committed
295
        logging.debug('compute reconstruction - %s', hasattr(self, 'gt_img_rct'))
Jiri Borovec's avatar
Jiri Borovec committed
296
        # error estimation from original reconstruction
297 298 299 300 301 302
        if hasattr(self, 'gt_img_rct') and imgs_rct is not None:
            # imgs_rct = ptn_dict.reconstruct_samples(self.atlas, self.w_bins)
            # imgs_rct = self._binarize_img_reconstruction(imgs_rct)
            imgs_gt = self.gt_img_rct[:len(imgs_rct)]
            diff = np.asarray(imgs_gt) - np.asarray(imgs_rct)
            stat['reconstruct_diff'] = np.sum(abs(diff)) / float(np.prod(diff.shape))
Jiri Borovec's avatar
Jiri Borovec committed
303 304 305 306
        elif hasattr(self, 'imgs') and imgs_rct is not None:
            imgs = self.imgs[:len(imgs_rct)]
            diff = np.asarray(imgs) - np.asarray(imgs_rct)
            stat['reconstruct_diff'] = np.sum(abs(diff)) / float(np.prod(diff.shape))
Jiri Borovec's avatar
Jiri Borovec committed
307 308 309 310 311 312 313
        return stat

    def _evaluate(self):
        """ evaluate experiment with given GT """
        self.df_stat = pd.DataFrame()
        for stat in self.l_stat:
            self.df_stat = self.df_stat.append(stat, ignore_index=True)
Jiri Borovec's avatar
Jiri Borovec committed
314
        if self.iter_var_name in stat:
Jiri Borovec's avatar
Jiri Borovec committed
315
            self.df_stat.set_index(self.iter_var_name, inplace=True)
Jiri Borovec's avatar
Jiri Borovec committed
316
        path_csv = os.path.join(self.params.get('path_exp'), self.RESULTS_CSV)
Jiri Borovec's avatar
Jiri Borovec committed
317
        logging.debug('save results: "%s"', path_csv)
Jiri Borovec's avatar
Jiri Borovec committed
318 319 320 321
        self.df_stat.to_csv(path_csv)

    def _summarise(self):
        """ summarise and export experiment results """
Jiri Borovec's avatar
Jiri Borovec committed
322
        logging.info('summarise the experiment')
Jiri Borovec's avatar
Jiri Borovec committed
323
        if hasattr(self, 'df_stat') and not self.df_stat.empty:
Jiri Borovec's avatar
Jiri Borovec committed
324 325 326
            with open(self.path_stat, 'a') as fp:
                fp.write('\n' * 3 + 'RESULTS: \n' + '=' * 9)
                fp.write('\n{}'.format(self.df_stat.describe()))
Jiri Borovec's avatar
Jiri Borovec committed
327
            logging.debug('statistic: \n%s', repr(self.df_stat.describe()))
Jiri Borovec's avatar
Jiri Borovec committed
328 329


Jiri Borovec's avatar
Jiri Borovec committed
330
class ExperimentAPD_parallel(ExperimentAPD):
Jiri Borovec's avatar
Jiri Borovec committed
331 332 333 334 335 336 337 338 339 340
    """
    run the experiment in multiple threads
    """

    def __init__(self, dict_params, nb_jobs=NB_THREADS):
        """ initialise parameters and nb jobs in parallel

        :param dict_params: {str: ...}
        :param nb_jobs: int
        """
Jiri Borovec's avatar
Jiri Borovec committed
341
        super(ExperimentAPD_parallel, self).__init__(dict_params)
Jiri Borovec's avatar
Jiri Borovec committed
342 343
        self.nb_jobs = nb_jobs

Jiri Borovec's avatar
Jiri Borovec committed
344 345 346 347
    def _load_images(self):
        """ load image data """
        path_data = os.path.join(self.params.get('path_in'),
                                 self.params.get('dataset'))
Jiri Borovec's avatar
Jiri Borovec committed
348
        self.imgs, self._im_names = gen_data.dataset_load_images(path_data,
Jiri Borovec's avatar
Jiri Borovec committed
349
                                                         nb_jobs=self.nb_jobs)
Jiri Borovec's avatar
Jiri Borovec committed
350

Jiri Borovec's avatar
Jiri Borovec committed
351
    def _warp_perform_once(self, v):
Jiri Borovec's avatar
Jiri Borovec committed
352
        self.params[self.iter_var_name] = v
Jiri Borovec's avatar
Jiri Borovec committed
353
        logging.debug(' -> set iterable "%s" on %s', self.iter_var_name,
Jiri Borovec's avatar
Jiri Borovec committed
354 355 356 357 358 359
                     repr(self.params[self.iter_var_name]))
        t = time.time()
        # stat = super(ExperimentAPD_mp, self)._perform_once(v)
        stat = self._perform_once(v)
        stat['time'] = time.time() - t
        logging.info('partial results: %s', repr(stat))
Jiri Borovec's avatar
Jiri Borovec committed
360 361
        return stat

Jiri Borovec's avatar
Jiri Borovec committed
362 363 364 365 366 367 368 369 370 371 372 373
    # def _perform_once(self, v):
    #     """ perform single experiment
    #
    #     :param v: value
    #     :return: {str: val}
    #     """
    #     t = time.time()
    #     self.params[self.iter_var_name] = v
    #     stat = super(ExperimentAPD_mp, self)._perform_once(v)
    #     stat['time'] = time.time() - t
    #     return stat

Jiri Borovec's avatar
Jiri Borovec committed
374 375
    def _perform_sequence(self):
        """ perform sequence in multiprocessing pool """
Jiri Borovec's avatar
Jiri Borovec committed
376 377
        logging.debug('perform_sequence in %i threads for %i values',
                      self.nb_jobs, len(self.iter_values))
Jiri Borovec's avatar
Jiri Borovec committed
378 379
        # ISSUE with passing large date to processes so the images are saved
        # and loaded in particular process again
Jiri Borovec's avatar
Jiri Borovec committed
380
        # p_imgs = os.path.join(self.params.get('path_exp'), 'input_images.npz')
Jiri Borovec's avatar
Jiri Borovec committed
381 382
        # np.savez(open(p_imgs, 'w'), imgs=self.imgs)

Jiri Borovec's avatar
Jiri Borovec committed
383
        self.l_stat = []
Jiri Borovec's avatar
Jiri Borovec committed
384
        # tqdm_bar = tqdm.tqdm(total=len(self.iter_values))
Jiri Borovec's avatar
Jiri Borovec committed
385
        mproc_pool = mproc.Pool(self.nb_jobs)
Jiri Borovec's avatar
Jiri Borovec committed
386 387
        for stat in mproc_pool.map(self._warp_perform_once, self.iter_values):
            self.l_stat.append(stat)
388
            self._evaluate()
Jiri Borovec's avatar
Jiri Borovec committed
389
            # tqdm_bar.update(1)
Jiri Borovec's avatar
Jiri Borovec committed
390 391 392 393 394 395 396
        mproc_pool.close()
        mproc_pool.join()

        # remove temporary image file
        # os.remove(p_imgs)


Jiri Borovec's avatar
Jiri Borovec committed
397
def extend_list_params(list_params, name_param, list_options):
Jiri Borovec's avatar
Jiri Borovec committed
398 399
    """ extend the parameter list by all sub-datasets

Jiri Borovec's avatar
Jiri Borovec committed
400 401 402
    :param list_params: [{str: ...}]
    :param name_param: str
    :param list_options: list
Jiri Borovec's avatar
Jiri Borovec committed
403 404
    :return: [{str: ...}]
    """
Jiri Borovec's avatar
Jiri Borovec committed
405 406 407 408 409
    if not isinstance(list_options, list):
        list_options = [list_options]
    list_params_new = []
    for param in list_params:
        for v in list_options:
Jiri Borovec's avatar
Jiri Borovec committed
410
            param_new = param.copy()
Jiri Borovec's avatar
Jiri Borovec committed
411 412 413
            param_new.update({name_param: v})
            list_params_new.append(param_new)
    return list_params_new