common_options.py 19.3 KB
Newer Older
1
2
'''Stores click common options for plots'''

3
import math
4
import pkg_resources  # to make sure bob gets imported properly
5
6
7
8
9
10
11
12
13
import logging
import click
from click.types import INT, FLOAT, Choice, File
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from bob.extension.scripts.click_helper import verbosity_option

logger = logging.getLogger(__name__)

14
15
16
17
18
19
20
21
22
def scores_argument(test_mandatory=False, **kwargs):
    '''Get the argument for scores, and add `dev-scores` and `test-scores` in
    the context if `--test` flag is on (default `--no-test`).'''
    def custom_scores_argument(func):
        def callback(ctx, param, value):
            length = len(value)
            if length < 1:
                raise click.BadParameter('No scores provided', ctx=ctx)
            else:
23
                div = 1
24
25
                ctx.meta['scores'] = value
                if test_mandatory or ctx.meta['test']:
26
                    div = 2
27
28
29
30
31
                    if (length % 2) != 0:
                        pref = 'T' if test_mandatory else ('When `--test` flag'
                                                           ' is on t')
                        raise click.BadParameter(
                            '%sest-score(s) must '
32
33
                            'be provided along with dev-score(s)' % pref, ctx=ctx
                        )
34
35
36
37
38
39
                    else:
                        ctx.meta['dev-scores'] = [value[i] for i in
                                                  range(length) if not i % 2]
                        ctx.meta['test-scores'] = [value[i] for i in
                                                   range(length) if i % 2]
                        ctx.meta['n_sys'] = len(ctx.meta['test-scores'])
40
41
42
43
44
                if 'titles' in ctx.meta and \
                   len(ctx.meta['titles']) != len(value) / div:
                    raise click.BadParameter(
                        '#titles not equal to #sytems', ctx=ctx
                    )
45
            return value
46
47
48
49
        return click.argument(
            'scores', type=click.Path(exists=True),
            callback=callback, **kwargs
        )(func)
50
51
    return custom_scores_argument

52
53
54
55
def bool_option(name, short_name, desc, dflt=False, **kwargs):
    '''Generic provider for boolean options
    Parameters
    ----------
56

57
58
59
60
61
62
63
64
65
66
    name: str
        name of the option
    short_name: str
        short name for the option
    desc: str
        short description for the option
    dflt: bool or None
        Default value
     '''
    def custom_bool_option(func):
67
        def callback(ctx, param, value):
68
            ctx.meta[name.replace('-', '_')] = value
69
70
            return value
        return click.option(
71
72
73
74
75
76
77
78
            '-' + short_name, '--%s/--no-%s' % (name, name), default=dflt,
            help=desc,
            show_default=True, callback=callback, is_eager=True, **kwargs)(func)
    return custom_bool_option

def test_option(**kwargs):
    '''Get option flag to say if test-scores are provided'''
    return bool_option('test', 't', 'If set, test scores must be provided')
79

80
def sep_dev_test_option(dflt=True, **kwargs):
81
82
    '''Get option flag to say if dev and test plots should be in different
    plots'''
83
84
85
86
87
88
89
    return bool_option(
        'split', 's','If set, test and dev curve in different plots', dflt
    )

def cmc_option(**kwargs):
    '''Get option flag to say if cmc scores'''
    return bool_option('cmc', 'C', 'If set, CMC score files are provided')
90

91
92
def semilogx_option(dflt= False, **kwargs):
    '''Option to use semilog X-axis'''
93
    return bool_option('semilogx', 'G', 'If set, use semilog on X axis', dflt)
94

95
96
def list_float_option(name, short_name, desc, nitems=None, dflt=None, **kwargs):
    '''Get option to get a list of float f
97
98
    Parameters
    ----------
99

100
101
102
103
104
105
106
107
    name: str
        name of the option
    short_name: str
        short name for the option
    desc: str
        short description for the option
    nitems: :obj:`int`
        if given, the parsed list must contains this number of items
108
    dflt: :any:`list`
109
        List of default  values for axes.
110
    '''
111
    def custom_list_float_option(func):
112
        def callback(ctx, param, value):
113
114
            if value is not None:
                tmp = value.split(',')
115
116
117
118
                if nitems is not None and len(tmp) != nitems:
                    raise click.BadParameter(
                        '%s Must provide %d axis limits' % (name, nitems)
                    )
119
120
121
                try:
                    value = [float(i) for i in tmp]
                except:
122
                    raise click.BadParameter('Inputs of %s be floats' % name)
123
124
                if None in value:
                    value = None
125
                elif dflt is not None and None not in dflt and len(dflt) == nitems:
126
127
128
                    value = dflt if not all(
                        isinstance(x, float) for x in dflt
                    ) else None
129
            ctx.meta[name.replace('-', '_')] = value
130
131
            return value
        return click.option(
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
            '-'+short_name, '--'+name, default=None, show_default=True,
            help=desc, callback=callback, **kwargs)(func)
    return custom_list_float_option

def axes_val_option(dflt=None, **kwargs):
    return list_float_option(
        name='axlim', short_name='L',
        desc='min/max axes values separated by commas (min_x, max_x, min_y, max_y)',
        nitems=4, dflt=dflt, **kwargs
    )

def thresholds_option(**kwargs):
    return list_float_option(
        name='thres', short_name='T',
        desc='Given threshold for metrics computations',
        nitems=None, dflt=None, **kwargs
    )
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

def axis_fontsize_option(dflt=8, **kwargs):
    '''Get option for axis font size'''
    def custom_axis_fontsize_option(func):
        def callback(ctx, param, value):
            value = abs(value)
            ctx.meta['fontsize'] = value
            return value
        return click.option(
            '-F', '--fontsize', type=click.INT, default=dflt, show_default=True,
            help='Axis fontsize',
            callback=callback, **kwargs)(func)
    return custom_axis_fontsize_option

def x_rotation_option(dflt=0, **kwargs):
    '''Get option for rotartion of the x axis lables'''
    def custom_x_rotation_option(func):
        def callback(ctx, param, value):
            value = abs(value)
            ctx.meta['x_rotation'] = value
            return value
        return click.option(
            '-r', '--x-rotation', type=click.INT, default=dflt, show_default=True,
            help='X axis labels ration',
            callback=callback, **kwargs)(func)
    return custom_x_rotation_option

def fmr_line_at_option(**kwargs):
    '''Get option to draw const fmr line'''
    def custom_fmr_line_at_option(func):
        def callback(ctx, param, value):
            if value is not None:
                value = min(max(0.0, value), 1.0)
            ctx.meta['fmr_at'] = value
            return value
        return click.option(
            '-L', '--fmr-at', type=float, default=None, show_default=True,
            help='If given, draw a veritcal line for constant FMR on ROC plots',
            callback=callback, **kwargs)(func)
    return custom_fmr_line_at_option
189

190
191
192
193
194
195
196
197
198
199
200
201
202
203
def cost_option(**kwargs):
    '''Get option to get cost for FAR'''
    def custom_cost_option(func):
        def callback(ctx, param, value):
            if value < 0 or value > 1:
                raise click.BadParameter("Cost for FAR must be betwen 0 and 1")
            ctx.meta['cost'] = value
            return value
        return click.option(
            '-C', '--cost', type=float, default=0.99, show_default=True,
            help='Cost for FAR in minDCF',
            callback=callback, **kwargs)(func)
    return custom_cost_option

204
205
206
207
208
209
210
211
212
def n_sys_option(**kwargs):
    '''Get the number of systems to be processed'''
    def custom_n_sys_option(func):
        def callback(ctx, param, value):
            ctx.meta['n_sys'] = value
            return value
        return click.option(
            '--n-sys', type=INT, default=1, show_default=True,
            help='The number of systems to be processed',
213
            callback=callback, is_eager=True , **kwargs)(func)
214
215
216
217
218
219
220
221
222
223
224
    return custom_n_sys_option

def points_curve_option(**kwargs):
    '''Get the number of points use to draw curves'''
    def custom_points_curve_option(func):
        def callback(ctx, param, value):
            if value < 2:
                raise click.BadParameter(
                    'Number of points to draw curves must be greater than 1'
                    , ctx=ctx
                )
225
            ctx.meta['points'] = value
226
227
228
229
230
231
232
233
234
235
236
            return value
        return click.option(
            '-n', '--points', type=INT, default=100, show_default=True,
            help='The number of points use to draw curves in plots',
            callback=callback, **kwargs)(func)
    return custom_points_curve_option

def n_bins_option(**kwargs):
    '''Get the number of bins in the histograms'''
    def custom_n_bins_option(func):
        def callback(ctx, param, value):
237
238
239
            if value is None:
                value = 'auto'
            elif value < 2:
240
241
242
243
244
245
246
                raise click.BadParameter(
                    'Number of bins must be greater than 1'
                    , ctx=ctx
                )
            ctx.meta['n_bins'] = value
            return value
        return click.option(
247
248
            '-b', '--nbins', type=INT, default=None,
            help='The number of bins in the histogram(s). Default: `auto`',
249
250
251
252
253
254
255
256
257
258
259
260
261
262
            callback=callback, **kwargs)(func)
    return custom_n_bins_option

def table_option(**kwargs):
    '''Get table option for tabulate package
    More informnations: https://pypi.python.org/pypi/tabulate
    '''
    def custom_table_option(func):
        def callback(ctx, param, value):
            if value is not None:
                ctx.meta['tablefmt'] = value
            elif 'log' in ctx.meta and ctx.meta['log'] is not None:
                value = 'latex'
            else:
263
                value = 'rst'
264
265
266
267
268
            ctx.meta['tablefmt'] = value
            return value
        return click.option(
            '--tablefmt', type=click.STRING, default=None,
            show_default=True, help='Format for table display: `plain`, '
269
270
            '`simple`, `grid`, `fancy_grid`, `pipe`, `orgtbl`, '
            '`jira`, `presto`, `psql`, (default) `rst`, `mediawiki`, `moinmoin`, '
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
            '`youtrack`, `html`, (default with `--log`)`latex`, '
            '`latex_raw`, `latex_booktabs`, `textile`',
            callback=callback,**kwargs)(func)
    return custom_table_option

def output_plot_file_option(default_out='plots.pdf', **kwargs):
    '''Get options for output file for plots'''
    def custom_output_plot_file_option(func):
        def callback(ctx, param, value):
            ''' Save ouput file  and associated pdf in context list,
            print the path of the file in the log'''
            ctx.meta['output'] = value
            ctx.meta['PdfPages'] = PdfPages(value)
            logger.debug("Plots will be output in %s", value)
            return value
        return click.option(
            '-o', '--output',
            default=default_out, show_default=True,
            help='The file to save the plots in.',
            callback=callback, **kwargs)(func)
    return custom_output_plot_file_option

def output_plot_metric_option(**kwargs):
    '''Get options for output file for metrics'''
    def custom_output_plot_file_option(func):
        def callback(ctx, param, value):
            ''' Save ouput file  and associated pdf in context list,
            print the path of the file in the log'''
            if value is not None:
                logger.debug("Metrics will be output in %s", value)
            ctx.meta['log'] = value
            return value
        return click.option(
            '-l', '--log', default=None, type=click.STRING,
            help='If provided, computed numbers are written to '
              'this file instead of the standard output.',
            callback=callback, **kwargs)(func)
    return custom_output_plot_file_option

def open_file_mode_option(**kwargs):
    '''Get the top option for matplotlib'''
    def custom_open_file_mode_option(func):
        def callback(ctx, param, value):
            if value not in ['w', 'a', 'w+', 'a+']:
                raise click.BadParameter('Incorrect open file mode')
316
            ctx.meta['open_mode'] = value
317
318
319
320
321
322
323
            return value
        return click.option(
            '-om', '--open-mode', default='w',
            help='File open mode',
            callback=callback, **kwargs)(func)
    return custom_open_file_mode_option

324
325
326
327
328
329
330
331
332
def criterion_option(lcriteria=['eer', 'hter', 'far'], **kwargs):
    """Get option flag to tell which criteriom is used (default:eer)

    Parameters
    ----------

    lcriteria : :any:`list`
        List of possible criteria
    """
333
334
    def custom_criterion_option(func):
        def callback(ctx, param, value):
335
336
            list_accepted_crit = lcriteria if lcriteria is not None else \
                    ['eer', 'hter', 'far']
337
338
339
340
341
342
343
344
345
346
347
348
            if value not in list_accepted_crit:
                raise click.BadParameter('Incorrect value for `--criter`. '
                                         'Must be one of [`%s`]' %
                                         '`, `'.join(list_accepted_crit))
            ctx.meta['criter'] = value
            return value
        return click.option(
            '--criter', default='eer', help='Criterion to compute plots and '
            'metrics: `eer` (default), `hter`',
            callback=callback, is_eager=True ,**kwargs)(func)
    return custom_criterion_option

349
350
351
352

def far_option(**kwargs):
    '''Get option to get far value'''
    def custom_far_option(func):
353
        def callback(ctx, param, value):
354
            if value is not None and (value > 1 or value < 0):
355
356
                raise click.BadParameter("FAR value should be between 0 and 1")
            ctx.meta['far_value'] = value
357
358
            return value
        return click.option(
359
            '-f', '--far-value', type=click.FLOAT, default=None,
360
            help='The FAR value for which to compute metrics',
361
            callback=callback, show_default=True,**kwargs)(func)
362
    return custom_far_option
363

Theophile GENTILHOMME's avatar
Theophile GENTILHOMME committed
364
365
366
367
368
369
370
371
372
373
374
375
def rank_option(**kwargs):
    '''Get option for rank parameter'''
    def custom_rank_option(func):
        def callback(ctx, param, value):
            value = 1 if value < 0 else value
            ctx.meta['rank'] = value
            return value
        return click.option(
            '--rank', type=click.INT, default=1,
            help='Given threshold for metrics computations',
            callback=callback, show_default=True,**kwargs)(func)
    return custom_rank_option
376

377
def label_option(name_option='x_label', **kwargs):
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
    '''Get labels options based on the given name.

    Parameters:
    ----------
    name_option: str, optional
        Name of the label option (e.g. x-lable, y1-label)
    '''
    def custom_label_option(func):
        def callback(ctx, param, value):
            ''' Get and save labels list in the context list '''
            ctx.meta[name_option] = value if value is None else \
                    [int(i) for i in value.split(',')]
            return value
        return click.option(
            '--' + name_option,
            help='The id of figures which should have x_label separated by '
            'comma. For example ``--%s 1,2,4``.' % name_option,
            callback=callback, **kwargs)(func)
    return custom_label_option

def figsize_option(**kwargs):
    '''Get option for matplotlib figsize'''
    def custom_figsize_option(func):
        def callback(ctx, param, value):
            ctx.meta['figsize'] = value if value is None else \
                    [float(x) for x in value.split(',')]
            plt.figure(figsize=ctx.meta['figsize'])
            return value
        return click.option(
            '--figsize', help='If given, will run \
            ``plt.figure(figsize=figsize)(f)``. Example: --fig-size 4,6',
            callback=callback, **kwargs)(func)
    return custom_figsize_option

def legend_ncols_option(**kwargs):
    '''Get the number of columns to set in the legend of the plot'''
    def custom_legend_ncols_option(func):
        def callback(ctx, param, value):
            ctx.meta['legend_ncol'] = value
            return value
        return click.option(
            '--legend-ncol', default=3, show_default=True,
            type=INT, help='The number of columns of the legend layout.',
            callback=callback, **kwargs)(func)
    return custom_legend_ncols_option

def legend_loc_option(**kwargs):
    '''Get tthe legend location of the plot'''
    def custom_legend_loc_option(func):
        def callback(ctx, param, value):
            ctx.meta['legend_loc'] = value
            return value
        return click.option(
            '--legend-location', default=0, show_default=True,
            type=INT, help='The lengend location code',
            callback=callback, **kwargs)(func)
    return custom_legend_loc_option

def line_width_option(**kwargs):
    '''Get line width option for the plots'''
    def custom_line_width_option(func):
        def callback(ctx, param, value):
            ctx.meta['line_width'] = value
            return value
        return click.option(
            '--line-width',
            type=FLOAT, help='The line width of plots',
            callback=callback, **kwargs)(func)
    return custom_line_width_option

def marker_style_option(**kwargs):
    '''Get marker style otpion for the plots'''
    def custom_marker_style_option(func):
        def callback(ctx, param, value):
            ctx.meta['marker_style'] = value
            return value
        return click.option(
            '--marker-style',
            type=FLOAT, help='The marker style of the plots',
            callback=callback, **kwargs)(func)
    return custom_marker_style_option

def top_option(**kwargs):
    '''Get the top option for matplotlib'''
    def custom_top_option(func):
        def callback(ctx, param, value):
            ctx.meta['top'] = value
            return value
        return click.option(
            '--top', type=FLOAT,
            help='To give to ``plt.subplots_adjust(top=top)``. If given, first'
            ' plt.tight_layout is called. If you want to tight_layout to be '
            'called, then you need to provide this option.',
            callback=callback, **kwargs)(func)
    return custom_top_option

def titles_option(**kwargs):
    '''Get the titles otpion for the different systems'''
    def custom_titles_option(func):
        def callback(ctx, param, value):
478
479
480
            if value is not None:
                value = value.split(',')
            ctx.meta['titles'] = value
481
482
            return value
        return click.option(
483
484
            '--titles', type=click.STRING, default=None,
            help='The title for each system comma separated. '
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
            'Example: --titles ISV,CNN',
            callback=callback, **kwargs)(func)
    return custom_titles_option

def style_option(**kwargs):
    '''Get option for matplotlib style'''
    def custom_style_option(func):
        def callback(ctx, param, value):
            ctx.meta['style'] = value
            plt.style.use(value)
            return value
        return click.option(
            '--style', multiple=True, type=click.types.Choice(plt.style.available),
            help='The matplotlib style to use for plotting. You can provide '
            'multiple styles by repeating this option',
            callback=callback, **kwargs)(func)
    return custom_style_option