Coverage for /scratch/builds/bob/bob.ip.binseg/miniconda/conda-bld/bob.ip.binseg_1673966692152/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_p/lib/python3.10/site-packages/bob/ip/common/data/sample.py: 69%

35 statements  

« prev     ^ index     » next       coverage.py v7.0.5, created at 2023-01-17 15:03 +0000

1from collections.abc import MutableSequence 

2 

3"""Base definition of sample 

4 

5.. todo:: 

6 

7 Copied from bob/bob.pipelines **TEMPORARILY**! Remove this and use the 

8 package directly! 

9 

10""" 

11 

12 

13def _copy_attributes(s, d): 

14 """Copies attributes from a dictionary to self""" 

15 s.__dict__.update( 

16 dict( 

17 [k, v] for k, v in d.items() if k not in ("data", "load", "samples") 

18 ) 

19 ) 

20 

21 

22class DelayedSample: 

23 """Representation of sample that can be loaded via a callable 

24 

25 The optional ``**kwargs`` argument allows you to attach more attributes to 

26 this sample instance. 

27 

28 

29 Parameters 

30 ---------- 

31 

32 load : object 

33 A python function that can be called parameterlessly, to load the 

34 sample in question from whatever medium 

35 

36 parent : :py:class:`DelayedSample`, :py:class:`Sample`, None 

37 If passed, consider this as a parent of this sample, to copy 

38 information 

39 

40 kwargs : dict 

41 Further attributes of this sample, to be stored and eventually 

42 transmitted to transformed versions of the sample 

43 

44 """ 

45 

46 def __init__(self, load, parent=None, **kwargs): 

47 self.load = load 

48 if parent is not None: 

49 _copy_attributes(self, parent.__dict__) 

50 _copy_attributes(self, kwargs) 

51 

52 @property 

53 def data(self): 

54 """Loads the data from the disk file""" 

55 return self.load() 

56 

57 

58class Sample: 

59 """Representation of sample that is sufficient for the blocks in this module 

60 

61 Each sample must have the following attributes: 

62 

63 * attribute ``data``: Contains the data for this sample 

64 

65 

66 Parameters 

67 ---------- 

68 

69 data : object 

70 Object representing the data to initialize this sample with. 

71 

72 parent : object 

73 A parent object from which to inherit all other attributes (except 

74 ``data``) 

75 

76 """ 

77 

78 def __init__(self, data, parent=None, **kwargs): 

79 self.data = data 

80 if parent is not None: 

81 _copy_attributes(self, parent.__dict__) 

82 _copy_attributes(self, kwargs) 

83 

84 

85class SampleSet(MutableSequence): 

86 """A set of samples with extra attributes 

87 https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes 

88 """ 

89 

90 def __init__(self, samples, parent=None, **kwargs): 

91 self.samples = samples 

92 if parent is not None: 

93 _copy_attributes(self, parent.__dict__) 

94 _copy_attributes(self, kwargs) 

95 

96 def __len__(self): 

97 return len(self.samples) 

98 

99 def __getitem__(self, item): 

100 return self.samples.__getitem__(item) 

101 

102 def __setitem__(self, key, item): 

103 return self.samples.__setitem__(key, item) 

104 

105 def __delitem__(self, item): 

106 return self.samples.__delitem__(item) 

107 

108 def insert(self, index, item): 

109 # if not item in self.samples: 

110 self.samples.insert(index, item)