Coverage for src/bob/measure/script/gen.py: 0%

32 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-06 21:23 +0100

1#!/usr/bin/env python 

2# coding=utf-8 

3 

4 

5"""Generate random scores. 

6""" 

7 

8import logging 

9import os 

10 

11import click 

12import numpy 

13import numpy.random 

14 

15from clapper.click import verbosity_option 

16from click.types import FLOAT 

17 

18logger = logging.getLogger(__name__) 

19 

20NUM_NEG = 5000 

21NUM_POS = 5000 

22 

23 

24def gen_score_distr(mean_neg, mean_pos, sigma_neg=1, sigma_pos=1): 

25 """Generate scores from normal distributions 

26 

27 Parameters 

28 ---------- 

29 

30 mean_neg : float 

31 Mean for negative scores 

32 

33 mean_pos : float 

34 Mean for positive scores 

35 

36 sigma_neg : float 

37 STDev for negative scores 

38 

39 sigma_pos : float 

40 STDev for positive scores 

41 

42 

43 Returns 

44 ------- 

45 

46 neg_scores : numpy.ndarray (1D, float) 

47 Negatives scores 

48 

49 pos_scores : numpy.ndarray (1D, float) 

50 Positive scores 

51 

52 """ 

53 

54 return numpy.random.normal( 

55 mean_neg, sigma_neg, (NUM_NEG,) 

56 ), numpy.random.normal(mean_pos, sigma_pos, (NUM_POS,)) 

57 

58 

59def write_scores_to_file(neg, pos, filename): 

60 """Writes score distributions into 2-column score files. 

61 

62 For the format of the 2-column score files, please refer to Bob's 

63 documentation. See :py:func:`bob.measure.load.split`. 

64 

65 Parameters 

66 ---------- 

67 

68 neg : :py:class:`numpy.ndarray` 

69 Scores for negative samples. 

70 

71 pos : :py:class:`numpy.ndarray` 

72 Scores for positive samples. 

73 

74 filename : str 

75 The path to write the score to. 

76 

77 """ 

78 

79 os.makedirs(os.path.dirname(filename), exist_ok=True) 

80 

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) 

87 

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) 

93 

94 

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. 

102 

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) 

111 

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 )