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 """
16 s.__dict__.update(
17 dict([k, v] for k, v in d.items() if k not in ("data", "load", "samples"))
18 )
19
20
21class DelayedSample:
22 """Representation of sample that can be loaded via a callable
23
24 The optional ``**kwargs`` argument allows you to attach more attributes to
25 this sample instance.
26
27
28 Parameters
29 ----------
30
31 load : object
32 A python function that can be called parameterlessly, to load the
33 sample in question from whatever medium
34
35 parent : :py:class:`DelayedSample`, :py:class:`Sample`, None
36 If passed, consider this as a parent of this sample, to copy
37 information
38
39 kwargs : dict
40 Further attributes of this sample, to be stored and eventually
41 transmitted to transformed versions of the sample
42
43 """
44
45 def __init__(self, load, parent=None, **kwargs):
46 self.load = load
47 if parent is not None:
48 _copy_attributes(self, parent.__dict__)
49 _copy_attributes(self, kwargs)
50
51 @property
52 def data(self):
53 """Loads the data from the disk file"""
54 return self.load()
55
56
57class Sample:
58 """Representation of sample that is sufficient for the blocks in this module
59
60 Each sample must have the following attributes:
61
62 * attribute ``data``: Contains the data for this sample
63
64
65 Parameters
66 ----------
67
68 data : object
69 Object representing the data to initialize this sample with.
70
71 parent : object
72 A parent object from which to inherit all other attributes (except
73 ``data``)
74
75 """
76
77 def __init__(self, data, parent=None, **kwargs):
78 self.data = data
79 if parent is not None:
80 _copy_attributes(self, parent.__dict__)
81 _copy_attributes(self, kwargs)