Coverage for src/deepdraw/data/sample.py: 69%

35 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-11-30 15:00 +0100

1# SPDX-FileCopyrightText: Copyright © 2023 Idiap Research Institute <contact@idiap.ch> 

2# 

3# SPDX-License-Identifier: GPL-3.0-or-later 

4 

5from collections.abc import MutableSequence 

6 

7"""Base definition of sample 

8 

9.. todo:: 

10 

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

12 package directly! 

13 

14""" 

15 

16 

17def _copy_attributes(s, d): 

18 """Copies attributes from a dictionary to self.""" 

19 s.__dict__.update( 

20 {k: v for k, v in d.items() if k not in ("data", "load", "samples")} 

21 ) 

22 

23 

24class DelayedSample: 

25 """Representation of sample that can be loaded via a callable. 

26 

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

28 this sample instance. 

29 

30 

31 Parameters 

32 ---------- 

33 

34 load : object 

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

36 sample in question from whatever medium 

37 

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

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

40 information 

41 

42 kwargs : dict 

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

44 transmitted to transformed versions of the sample 

45 """ 

46 

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

48 self.load = load 

49 if parent is not None: 

50 _copy_attributes(self, parent.__dict__) 

51 _copy_attributes(self, kwargs) 

52 

53 @property 

54 def data(self): 

55 """Loads the data from the disk file.""" 

56 return self.load() 

57 

58 

59class Sample: 

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

61 module. 

62 

63 Each sample must have the following attributes: 

64 

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

66 

67 

68 Parameters 

69 ---------- 

70 

71 data : object 

72 Object representing the data to initialize this sample with. 

73 

74 parent : object 

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

76 ``data``) 

77 """ 

78 

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

80 self.data = data 

81 if parent is not None: 

82 _copy_attributes(self, parent.__dict__) 

83 _copy_attributes(self, kwargs) 

84 

85 

86# NEVER USED ANYWHERE IN THE PACKAGE 

87# Should it be kept? 

88class SampleSet(MutableSequence): 

89 """A set of samples with extra attributes 

90 https://docs.python.org/3/library/collections.abc.html#collections- 

91 abstract-base-classes.""" 

92 

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

94 self.samples = samples 

95 if parent is not None: 

96 _copy_attributes(self, parent.__dict__) 

97 _copy_attributes(self, kwargs) 

98 

99 def __len__(self): 

100 return len(self.samples) 

101 

102 def __getitem__(self, item): 

103 return self.samples.__getitem__(item) 

104 

105 def __setitem__(self, key, item): 

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

107 

108 def __delitem__(self, item): 

109 return self.samples.__delitem__(item) 

110 

111 def insert(self, index, item): 

112 # if not item in self.samples: 

113 self.samples.insert(index, item)