1
"""
2
All other benchmarks should be deviated this way so the core functionality
3
of the benchmarks such as loading or evaluation is not overwritten
4

5
INSTALLATION:
6

7
1. ...
8

9
Sample run::
10

11
    mkdir ./results
12
    touch sample-config.yaml
13
    python birl/bm_template.py \
14
        -t ./data-images/pairs-imgs-lnds_histol.csv \
15
        -d ./data-images \
16
        -o ./results \
17
        --visual --unique \
18
        -cfg ./sample-config.yaml
19

20
Copyright (C) 2017-2019 Jiri Borovec <jiri.borovec@fel.cvut.cz>
21
"""
22

23 30
import logging
24 30
import os
25 30
import sys
26

27
# this is used while calling this file as a script
28 30
sys.path += [os.path.abspath('.'), os.path.abspath('..')]  # Add path to root
29 30
from birl.utilities.experiments import create_basic_parser
30 30
from birl.benchmark import ImRegBenchmark
31

32

33 30
class BmTemplate(ImRegBenchmark):
34
    """ Basic template showing utilization by inheriting general workflow.
35
    This case serves as an example of using the general image regist. benchmark.
36

37
    :param dict params: dictionary with experiment configuration,
38
        the required options are names in `REQUIRED_PARAMS`,
39
        note that the basic parameters are inherited
40

41
    General methods that should be overwritten:
42

43
     * `_check_required_params`
44
     * `_prepare_img_registration`
45
     * `_execute_img_registration`/`_generate_regist_command`
46
     * `_extract_warped_image_landmarks`
47
     * `_extract_execution_time`
48
     * `_clear_after_registration`
49

50
    .. note:: The actual implementation simulates the "WORSE" registration while
51
     it blindly copies the moving landmarks as results of the registration.
52
     It also copies the moving images so there is correct "warping" between
53
     image and landmarks. It means that there was no registration performed.
54

55
    Examples
56
    --------
57
    >>> # Running in single thread:
58
    >>> from birl.utilities.data_io import create_folder, update_path
59
    >>> path_out = create_folder('temp_results')
60
    >>> path_csv = os.path.join(update_path('data-images'), 'pairs-imgs-lnds_mix.csv')
61
    >>> open('sample_config.yaml', 'w').close()
62
    >>> BmTemplate.main({
63
    ...       'path_table': path_csv,
64
    ...       'path_out': path_out,
65
    ...       'nb_workers': 1,
66
    ...       'unique': False,
67
    ...       'visual': True,
68
    ...       'path_config': 'sample_config.yaml'
69
    ... })  # doctest: +ELLIPSIS
70
    ({...}, '...BmTemplate')
71
    >>> import shutil
72
    >>> shutil.rmtree(path_out, ignore_errors=True)
73
    >>> os.remove('sample_config.yaml')
74

75
    >>> # Running in multiple parallel threads:
76
    >>> from birl.utilities.data_io import create_folder, update_path
77
    >>> path_out = create_folder('temp_results')
78
    >>> path_csv = os.path.join(update_path('data-images'), 'pairs-imgs-lnds_mix.csv')
79
    >>> params = {'path_table': path_csv,
80
    ...           'path_out': path_out,
81
    ...           'nb_workers': 2,
82
    ...           'unique': False,
83
    ...           'visual': True,
84
    ...           'path_config': ''}
85
    >>> benchmark = BmTemplate(params)
86
    >>> benchmark.run()
87
    True
88
    >>> del benchmark
89
    >>> import shutil
90
    >>> shutil.rmtree(path_out, ignore_errors=True)
91
    """
92 30
    REQUIRED_PARAMS = ImRegBenchmark.REQUIRED_PARAMS + ['path_config']
93

94 30
    def _prepare(self):
95 30
        logging.info('-> copy configuration...')
96

97 30
        self._copy_config_to_expt('path_config')
98

99 30
    def _prepare_img_registration(self, item):
100
        """ prepare the experiment folder if it is required,
101
        eq. copy some extra files
102

103
        :param dict item: dictionary with regist. params
104
        :return dict: the same or updated registration info
105
        """
106 30
        logging.debug('.. no preparing before registration experiment')
107 30
        return item
108

109 30
    def _generate_regist_command(self, item):
110
        """ generate the registration command(s)
111

112
        :param dict item: dictionary with registration params
113
        :return str|list(str): the execution commands
114
        """
115 30
        logging.debug('.. simulate registration: copy the source image and landmarks, like regist. failed')
116 30
        _, path_im_move, _, path_lnds_move = self._get_paths(item)
117 30
        path_reg_dir = self._get_path_reg_dir(item)
118 30
        name_img = os.path.basename(item[self.COL_IMAGE_MOVE])
119 30
        cmd_img = 'cp %s %s' % (path_im_move, os.path.join(path_reg_dir, name_img))
120 30
        name_lnds = os.path.basename(item[self.COL_POINTS_MOVE])
121 30
        cmd_lnds = 'cp %s %s' % (path_lnds_move, os.path.join(path_reg_dir, name_lnds))
122 30
        commands = [cmd_img, cmd_lnds]
123 30
        return commands
124

125 30
    def _extract_warped_image_landmarks(self, item):
126
        """ get registration results - warped registered images and landmarks
127

128
        :param dict item: dictionary with registration params
129
        :return dict: paths to warped images/landmarks
130
        """
131 30
        path_reg_dir = self._get_path_reg_dir(item)
132
        # detect image
133 30
        path_img = os.path.join(path_reg_dir, os.path.basename(item[self.COL_IMAGE_MOVE]))
134
        # detect landmarks
135 30
        path_lnd = os.path.join(path_reg_dir, os.path.basename(item[self.COL_POINTS_MOVE]))
136
        # return formatted results
137 30
        return {self.COL_IMAGE_MOVE_WARP: path_img, self.COL_POINTS_MOVE_WARP: path_lnd}
138

139 30
    def _extract_execution_time(self, item):
140
        """ if needed update the execution time
141

142
        :param dict item: dictionary with registration params
143
        :return float|None: time in minutes
144
        """
145 30
        return 1. / 60  # running constant time 1 sec.
146

147 30
    def _clear_after_registration(self, item):
148
        """ clean unnecessarily files after the registration
149

150
        :param dict item: dictionary with regist. information
151
        :return dict: the same or updated regist. info
152
        """
153 30
        logging.debug('.. no cleaning after registration experiment')
154 30
        return item
155

156 30
    @staticmethod
157 6
    def extend_parse(arg_parser):
158
        """ extent the basic arg parses by some extra required parameters
159

160
        :return object:
161

162
        >>> parser = BmTemplate.extend_parse(create_basic_parser())
163
        >>> type(parser)
164
        <class 'argparse.ArgumentParser'>
165
        """
166
        # SEE: https://docs.python.org/3/library/argparse.html
167 30
        arg_parser.add_argument('-cfg', '--path_config', type=str, required=True, help='some extra parameters')
168 30
        return arg_parser
169

170

171
# RUN by given parameters
172 30
if __name__ == '__main__':
173 0
    logging.basicConfig(level=logging.INFO)
174 0
    logging.info(__doc__)
175 0
    arg_params, path_expt = BmTemplate.main()
176

177 0
    if arg_params.get('run_comp_benchmark', False):
178
        # from bm_experiments import bm_comp_perform
179
        # bm_comp_perform.main(path_expt)
180 0
        logging.info('Here you can call the separate benchmark to measure your computer performances.')

Read our documentation on viewing source code .

Loading