Coverage for src/bob/measure/script/gen.py: 0%
32 statements
« prev ^ index » next coverage.py v7.0.5, created at 2023-06-16 14:10 +0200
« prev ^ index » next coverage.py v7.0.5, created at 2023-06-16 14:10 +0200
1#!/usr/bin/env python
2# coding=utf-8
5"""Generate random scores.
6"""
8import logging
9import os
11import click
12import numpy
13import numpy.random
15from clapper.click import verbosity_option
16from click.types import FLOAT
18logger = logging.getLogger(__name__)
20NUM_NEG = 5000
21NUM_POS = 5000
24def gen_score_distr(mean_neg, mean_pos, sigma_neg=1, sigma_pos=1):
25 """Generate scores from normal distributions
27 Parameters
28 ----------
30 mean_neg : float
31 Mean for negative scores
33 mean_pos : float
34 Mean for positive scores
36 sigma_neg : float
37 STDev for negative scores
39 sigma_pos : float
40 STDev for positive scores
43 Returns
44 -------
46 neg_scores : numpy.ndarray (1D, float)
47 Negatives scores
49 pos_scores : numpy.ndarray (1D, float)
50 Positive scores
52 """
54 return numpy.random.normal(
55 mean_neg, sigma_neg, (NUM_NEG,)
56 ), numpy.random.normal(mean_pos, sigma_pos, (NUM_POS,))
59def write_scores_to_file(neg, pos, filename):
60 """Writes score distributions into 2-column score files.
62 For the format of the 2-column score files, please refer to Bob's
63 documentation. See :py:func:`bob.measure.load.split`.
65 Parameters
66 ----------
68 neg : :py:class:`numpy.ndarray`
69 Scores for negative samples.
71 pos : :py:class:`numpy.ndarray`
72 Scores for positive samples.
74 filename : str
75 The path to write the score to.
77 """
79 os.makedirs(os.path.dirname(filename), exist_ok=True)
81 with open(filename, "wt") as f:
82 for i in pos:
83 text = (
84 "1 %f\n" % i if numpy.random.normal(0, 1) > 0.01 else "1 nan\n"
85 )
86 f.write(text)
88 for i in neg:
89 text = (
90 "-1 %f\n" % i if numpy.random.normal(0, 1) > 0.01 else "1 nan\n"
91 )
92 f.write(text)
95@click.command()
96@click.argument("outdir")
97@click.option("--mean-neg", default=-1, type=FLOAT, show_default=True)
98@click.option("--mean-pos", default=1, type=FLOAT, show_default=True)
99@verbosity_option(logger, expose_value=False)
100def gen(outdir, mean_neg, mean_pos):
101 """Generate random scores.
103 Generates random scores for negative and positive scores, whatever they
104 could be. The scores are generated using Gaussian distribution whose mean
105 is an input parameter. The generated scores can be used as hypothetical
106 datasets.
107 """
108 # Generate the data
109 neg_dev, pos_dev = gen_score_distr(mean_neg, mean_pos)
110 neg_eval, pos_eval = gen_score_distr(mean_neg, mean_pos)
112 # Write the data into files
113 write_scores_to_file(neg_dev, pos_dev, os.path.join(outdir, "scores-dev"))
114 write_scores_to_file(
115 neg_eval, pos_eval, os.path.join(outdir, "scores-eval")
116 )