-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
data_container.py
277 lines (223 loc) · 9.28 KB
/
data_container.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
Neuraxle's DataContainer classes
====================================
Classes for containing the data that flows throught the pipeline steps.
..
Copyright 2019, Neuraxio Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
..
Thanks to Umaneo Technologies Inc. for their contributions to this Machine Learning
project, visit https://www.umaneo.com/ for more information on Umaneo Technologies Inc.
"""
import hashlib
from typing import Any, Iterable, List
from conv import convolved_1d
class DataContainer:
"""
DataContainer class to store data inputs, expected outputs, and ids together.
Each :class:`BaseStep` needs to rehash ids with hyperparameters so that the :class:`Checkpoint` step
can create checkpoints for a set of hyperparameters.
The DataContainer object is passed to all of the :class:`BaseStep` handle methods :
* :func:`~neuraxle.base.BaseStep.handle_transform`
* :func:`~neuraxle.base.BaseStep.handle_fit_transform`
* :func:`~neuraxle.base.BaseStep.handle_fit`
Most of the time, you won't need to care about the DataContainer because it is the pipeline that manages it.
.. seealso::
:class:`BaseHasher`,
:class: `BaseStep`
"""
def __init__(
self,
current_ids,
data_inputs: Any,
summary_id=None,
expected_outputs: Any = None
):
self.current_ids = current_ids
self.summary_id = summary_id
self.data_inputs = data_inputs
if expected_outputs is None and isinstance(data_inputs, Iterable):
self.expected_outputs = [None] * len(data_inputs)
else:
self.expected_outputs = expected_outputs
def set_data_inputs(self, data_inputs: Iterable):
"""
Set data inputs.
:param data_inputs: data inputs
:type data_inputs: Iterable
:return:
"""
self.data_inputs = data_inputs
def set_expected_outputs(self, expected_outputs: Iterable):
"""
Set expected outputs.
:param expected_outputs: expected outputs
:type expected_outputs: Iterable
:return:
"""
self.expected_outputs = expected_outputs
def set_current_ids(self, current_ids: List[str]):
"""
Set current ids.
:param current_ids: data inputs
:type current_ids: List[str]
:return:
"""
self.current_ids = current_ids
def set_summary_id(self, summary_id: str):
"""
Set summary id.
:param summary_id: str
:return:
"""
self.summary_id = summary_id
def hash_summary(self):
"""
Hash :class:`DataContainer`.current_ids, data inputs, and hyperparameters together into one id.
:return: single hashed current id for all of the current ids
:rtype: str
"""
m = hashlib.md5()
for current_id in self.current_ids:
m.update(str.encode(str(current_id)))
return m.hexdigest()
def convolved_1d(self, stride, kernel_size) -> Iterable['DataContainer']:
"""
Returns an iterator that iterates through batches of the DataContainer.
:param stride: step size for the convolution operation
:param kernel_size:
:return: an iterator of DataContainer
:rtype: Iterable[DataContainer]
.. seealso::
`<https://github.com/guillaume-chevalier/python-conv-lib>`_
"""
conv_current_ids = convolved_1d(stride=stride, iterable=self.current_ids, kernel_size=kernel_size,
include_incomplete_pass=True)
conv_data_inputs = convolved_1d(stride=stride, iterable=self.data_inputs, kernel_size=kernel_size,
include_incomplete_pass=True)
conv_expected_outputs = convolved_1d(stride=stride, iterable=self.expected_outputs, kernel_size=kernel_size,
include_incomplete_pass=True)
for current_ids, data_inputs, expected_outputs in zip(conv_current_ids, conv_data_inputs,
conv_expected_outputs):
for i, (ci, di, eo) in enumerate(zip(current_ids, data_inputs, expected_outputs)):
if di is None:
current_ids = current_ids[:i]
data_inputs = data_inputs[:i]
expected_outputs = expected_outputs[:i]
break
yield DataContainer(
summary_id=self.summary_id,
current_ids=current_ids,
data_inputs=data_inputs,
expected_outputs=expected_outputs
)
def copy(self):
return DataContainer(
summary_id=self.summary_id,
current_ids=self.current_ids,
data_inputs=self.data_inputs,
expected_outputs=self.expected_outputs,
)
def __iter__(self):
"""
Iter method returns a zip of all of the current_ids, data_inputs, and expected_outputs in the data container.
:return: iterator of tuples containing current_ids, data_inputs, and expected outputs
:rtype: Iterator[Tuple]
"""
current_ids = self.current_ids
if self.current_ids is None:
current_ids = [None] * len(self.data_inputs)
expected_outputs = self.expected_outputs
if self.expected_outputs is None:
expected_outputs = [None] * len(self.data_inputs)
return zip(current_ids, self.data_inputs, expected_outputs)
def __repr__(self):
return str(self)
def __str__(self):
return self.__class__.__name__ + "(current_ids=" + repr(list(self.current_ids)) + ", summary_id=" + repr(
self.summary_id)
def __len__(self):
return len(self.data_inputs)
class ExpandedDataContainer(DataContainer):
"""
Sub class of DataContainer to expand data container dimension.
.. seealso::
:class:`ExpandedDataContainer`,
"""
def __init__(self, current_ids, data_inputs, expected_outputs, summary_id, old_current_ids):
DataContainer.__init__(
self,
current_ids=current_ids,
data_inputs=data_inputs,
expected_outputs=expected_outputs,
summary_id=summary_id
)
self.old_current_ids = old_current_ids
def reduce_dim(self) -> 'DataContainer':
"""
Reduce DataContainer to its original shape with a list of multiple current_ids, data_inputs, and expected outputs.
:return: reduced data container
:rtype: DataContainer
"""
return DataContainer(
current_ids=self.old_current_ids,
data_inputs=self.data_inputs[0],
expected_outputs=self.expected_outputs[0],
summary_id=self.summary_id,
)
@staticmethod
def create_from(data_container: DataContainer) -> 'ExpandedDataContainer':
"""
Create ExpandedDataContainer with the given summary hash for the single current id.
:param data_container: data container to transform
:type data_container: DataContainer
:return: expanded data container
:rtype: ExpandedDataContainer
"""
return ExpandedDataContainer(
current_ids=[data_container.summary_id],
data_inputs=[data_container.data_inputs],
expected_outputs=[data_container.expected_outputs],
summary_id=data_container.summary_id,
old_current_ids=data_container.current_ids
)
class ListDataContainer(DataContainer):
"""
Sub class of DataContainer to perform list operations.
It allows to perform append, and concat operations on a DataContainer.
.. seealso::
:class:`DataContainer`
"""
@staticmethod
def empty() -> 'ListDataContainer':
return ListDataContainer([], [], [])
def append(self, current_id: str, data_input: Any, expected_output: Any):
"""
Append a new data input to the DataContainer.
:param current_id: current id for the data input
:type current_id: str
:param data_input: data input
:param expected_output: expected output
:return:
"""
self.current_ids.append(current_id)
self.data_inputs.append(data_input)
self.expected_outputs.append(expected_output)
def concat(self, data_container: DataContainer):
"""
Concat the given data container to the current data container.
:param data_container: data container
:type data_container: DataContainer
:return:
"""
self.current_ids.extend(data_container.current_ids)
self.data_inputs.extend(data_container.data_inputs)
self.expected_outputs.extend(data_container.expected_outputs)