Coverage for src/bob/measure/load.py: 50%

24 statements  

« prev     ^ index     » next       coverage.py v7.0.5, created at 2023-06-16 14:10 +0200

1#!/usr/bin/env python 

2# vim: set fileencoding=utf-8 : 

3# Mon 23 May 2011 16:23:05 CEST 

4 

5"""A set of utilities to load score files with different formats. 

6""" 

7 

8import logging 

9 

10import numpy 

11 

12LOGGER = logging.getLogger("bob.measure") 

13 

14 

15def split(filename): 

16 """split(filename) -> negatives, positives 

17 

18 Loads the scores from the given file and splits them into positive 

19 and negative arrays. The file must be a two columns file where the first 

20 column contains -1 or 1 (for negative or positive respectively) and the 

21 second the corresponding scores. 

22 

23 Parameters 

24 ---------- 

25 filename: :py:class:`str`: 

26 The name of the file containing the scores. 

27 

28 Returns 

29 ------- 

30 negatives: 1D :py:class:`numpy.ndarray` of type float 

31 This array contains the list of negative scores 

32 

33 positives: 1D :py:class:`numpy.ndarray` of type float 

34 This array contains the list of positive scores 

35 

36 """ 

37 try: 

38 columns = numpy.loadtxt(filename) 

39 neg_pos = columns[:, 0] 

40 scores = columns[:, 1] 

41 except Exception: 

42 LOGGER.error( 

43 """Cannot read {}. This file must be a two columns file with 

44 the first column containing -1 or 1 (i.e. negative or 

45 positive) and the second the scores 

46 (float).""".format( 

47 filename 

48 ) 

49 ) 

50 raise 

51 positives = neg_pos == 1 

52 return (scores[~positives], scores[positives]) 

53 

54 

55def split_files(filenames): 

56 """split_files 

57 

58 Parse a list of files using :py:func:`split` 

59 

60 Parameters 

61 ---------- 

62 

63 filenames : 

64 :any:`list`: A list of file paths 

65 

66 Returns 

67 ------- 

68 :any:`list`: A list of tuples, where each tuple contains the 

69 ``negative`` and ``positive`` scores for one probe of the database. Both 

70 ``negatives`` and ``positives`` can be either an 1D 

71 :py:class:`numpy.ndarray` of type ``float``, or ``None``. 

72 """ 

73 if filenames is None: 

74 return None 

75 res = [] 

76 for file_path in filenames: 

77 try: 

78 res.append(split(file_path)) 

79 except Exception: 

80 raise 

81 return res