Coverage for src/bob/bio/spear/database/utils.py: 90%

21 statements  

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

1import logging 

2 

3from typing import Union 

4 

5from clapper.rc import UserDefaults 

6from sklearn.pipeline import Pipeline 

7 

8from bob.bio.base.database import AnnotationsLoader, FileSampleLoader 

9from bob.bio.spear.transformer import PathToAudio 

10 

11logger = logging.getLogger(__name__) 

12 

13_common_rc = UserDefaults("~/.bobrc") 

14 

15 

16def get_rc(): 

17 return _common_rc 

18 

19 

20def path_loader(path: str): 

21 logger.debug("Reading CSV row for %s", path) 

22 return path 

23 

24 

25def create_sample_loader( 

26 data_path: Union[str, None] = None, 

27 data_ext: str = ".wav", 

28 annotations_path: Union[str, None] = None, 

29 annotations_ext: str = ".json", 

30 force_sample_rate: Union[int, None] = None, 

31 force_channel: Union[int, None] = None, 

32): 

33 """Defines the data loading transformers""" 

34 

35 # Load a path into the data of the sample 

36 sample_loader = FileSampleLoader( 

37 data_loader=path_loader, 

38 dataset_original_directory=data_path, 

39 extension=data_ext, 

40 ) 

41 

42 # Read the file at path and set the data and metadata of a sample 

43 path_to_sample = PathToAudio( 

44 forced_channel=force_channel, forced_sr=force_sample_rate 

45 ) 

46 

47 # Build the data loading pipeline 

48 if annotations_path is None: 

49 sample_loader = Pipeline( 

50 [ 

51 ("db:reader_loader", sample_loader), 

52 ("db:path_to_sample", path_to_sample), 

53 ] 

54 ) 

55 else: 

56 annotations_transformer = AnnotationsLoader( 

57 annotation_directory=annotations_path, 

58 annotation_extension=annotations_ext, 

59 ) 

60 sample_loader = Pipeline( 

61 [ 

62 ("db:reader_loader", sample_loader), 

63 ("db:path_to_sample", path_to_sample), 

64 ("db:annotations_loader", annotations_transformer), 

65 ] 

66 ) 

67 

68 return sample_loader