-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathhandlers_test.py
More file actions
634 lines (571 loc) · 21.8 KB
/
handlers_test.py
File metadata and controls
634 lines (571 loc) · 21.8 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
# pytype: skip-file
import os
import shutil
import sys
import tempfile
import unittest
import uuid
from collections.abc import Sequence
from typing import NamedTuple
from typing import Union
import numpy as np
from parameterized import parameterized
import apache_beam as beam
from apache_beam.io.filesystems import FileSystems
# pylint: disable=wrong-import-position, ungrouped-imports
try:
import tensorflow as tf
from tensorflow_transform.tf_metadata import dataset_metadata
from tensorflow_transform.tf_metadata import schema_utils
from apache_beam.ml.transforms import handlers
from apache_beam.ml.transforms import tft
from apache_beam.ml.transforms.tft import TFTOperation
from apache_beam.testing.util import assert_that
from apache_beam.testing.util import equal_to
except ImportError:
tft = None # type: ignore[assignment]
if not tft:
raise unittest.SkipTest('tensorflow_transform is not installed.')
class _AddOperation(TFTOperation):
def apply_transform(self, inputs, output_column_name, **kwargs):
return {output_column_name: inputs + 1}
class _MultiplyOperation(TFTOperation):
def apply_transform(self, inputs, output_column_name, **kwargs):
return {output_column_name: inputs * 10}
class IntType(NamedTuple):
x: int
class ListIntType(NamedTuple):
x: list[int]
class NumpyType(NamedTuple):
x: np.int64
class TFTProcessHandlerTest(unittest.TestCase):
def setUp(self) -> None:
self.artifact_location = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.artifact_location)
@parameterized.expand([
({
'x': 1, 'y': 2
}, ['x'], {
'x': 20, 'y': 2
}),
({
'x': 1, 'y': 2
}, ['x', 'y'], {
'x': 20, 'y': 30
}),
])
def test_tft_operation_preprocessing_fn(
self, inputs, columns, expected_result):
add_fn = _AddOperation(columns=columns)
mul_fn = _MultiplyOperation(columns=columns)
process_handler = handlers.TFTProcessHandler(
transforms=[add_fn, mul_fn], artifact_location=self.artifact_location)
actual_result = process_handler.process_data_fn(inputs)
self.assertDictEqual(actual_result, expected_result)
def test_input_type_from_schema_named_tuple_pcoll(self):
data = [{'x': 1}]
with beam.Pipeline() as p:
data = (
p | beam.Create(data)
| beam.Map(lambda x: IntType(**x)).with_output_types(IntType))
element_type = data.element_type
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
inferred_input_type = process_handler._map_column_names_to_types(
element_type)
expected_input_type = dict(x=list[int])
self.assertEqual(inferred_input_type, expected_input_type)
def test_input_type_from_schema_named_tuple_pcoll_list(self):
data = [{'x': [1, 2, 3]}, {'x': [4, 5, 6]}]
with beam.Pipeline() as p:
data = (
p | beam.Create(data)
| beam.Map(lambda x: ListIntType(**x)).with_output_types(ListIntType))
element_type = data.element_type
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
inferred_input_type = process_handler._map_column_names_to_types(
element_type)
expected_input_type = dict(x=list[int])
self.assertEqual(inferred_input_type, expected_input_type)
def test_input_type_from_row_type_pcoll(self):
data = [{'x': 1}]
with beam.Pipeline() as p:
data = (
p | beam.Create(data)
| beam.Map(lambda ele: beam.Row(x=int(ele['x']))))
element_type = data.element_type
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
inferred_input_type = process_handler._map_column_names_to_types(
element_type)
expected_input_type = dict(x=list[int])
self.assertEqual(inferred_input_type, expected_input_type)
def test_input_type_from_row_type_pcoll_list(self):
data = [{'x': [1, 2, 3]}, {'x': [4, 5, 6]}]
with beam.Pipeline() as p:
data = (
p | beam.Create(data)
| beam.Map(lambda ele: beam.Row(x=list(ele['x']))).with_output_types(
beam.row_type.RowTypeConstraint.from_fields([('x', list[int])])))
element_type = data.element_type
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
inferred_input_type = process_handler._map_column_names_to_types(
element_type)
expected_input_type = dict(x=list[int])
self.assertEqual(inferred_input_type, expected_input_type)
def test_input_type_from_named_tuple_pcoll_numpy(self):
np_data = [{
'x': np.array([1, 2, 3], dtype=np.int64)
}, {
'x': np.array([4, 5, 6], dtype=np.int64)
}]
with beam.Pipeline() as p:
data = (
p | beam.Create(np_data)
| beam.Map(lambda x: NumpyType(**x)).with_output_types(NumpyType))
element_type = data.element_type
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
inferred_input_type = process_handler._map_column_names_to_types(
element_type)
expected_type = dict(x=np.int64)
self.assertEqual(inferred_input_type, expected_type)
def test_tensorflow_raw_data_metadata_primitive_types(self):
input_types = dict(x=int, y=float, k=bytes, l=str)
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
for col_name, typ in input_types.items():
feature_spec = process_handler._get_raw_data_feature_spec_per_column(
typ=typ, col_name=col_name)
self.assertEqual(
handlers._default_type_to_tensor_type_map[typ], feature_spec.dtype)
self.assertIsInstance(feature_spec, tf.io.VarLenFeature)
def test_tensorflow_raw_data_metadata_primitive_types_in_containers(self):
input_types = dict([("x", list[int]), ("y", list[float]),
("k", list[bytes]), ("l", list[str])])
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
for col_name, typ in input_types.items():
feature_spec = process_handler._get_raw_data_feature_spec_per_column(
typ=typ, col_name=col_name)
self.assertIsInstance(feature_spec, tf.io.VarLenFeature)
@unittest.skipIf(sys.version_info < (3, 9), "not supported in python<3.9")
def test_tensorflow_raw_data_metadata_primitive_native_container_types(self):
input_types = dict([("x", list[int]), ("y", list[float]),
("k", list[bytes]), ("l", list[str])])
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
for col_name, typ in input_types.items():
feature_spec = process_handler._get_raw_data_feature_spec_per_column(
typ=typ, col_name=col_name)
self.assertIsInstance(feature_spec, tf.io.VarLenFeature)
def test_tensorflow_raw_data_metadata_numpy_types(self):
input_types = dict(x=np.int64, y=np.float32, z=list[np.int64])
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
for col_name, typ in input_types.items():
feature_spec = process_handler._get_raw_data_feature_spec_per_column(
typ=typ, col_name=col_name)
self.assertIsInstance(feature_spec, tf.io.VarLenFeature)
def test_tensorflow_raw_data_metadata_union_type_in_single_column(self):
input_types = dict(x=Union[int, float])
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
with self.assertRaises(TypeError):
for col_name, typ in input_types.items():
_ = process_handler._get_raw_data_feature_spec_per_column(
typ=typ, col_name=col_name)
def test_tensorflow_raw_data_metadata_dtypes(self):
input_types = dict(x=np.int32, y=np.float64)
expected_dtype = dict(x=np.int64, y=np.float32)
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
for col_name, typ in input_types.items():
feature_spec = process_handler._get_raw_data_feature_spec_per_column(
typ=typ, col_name=col_name)
self.assertEqual(expected_dtype[col_name], feature_spec.dtype)
def test_tft_process_handler_default_transform_types(self):
transforms = [
tft.ScaleTo01(columns=['x']),
tft.ScaleToZScore(columns=['y']),
tft.Bucketize(columns=['z'], num_buckets=2),
tft.ComputeAndApplyVocabulary(columns=['w'])
]
process_handler = handlers.TFTProcessHandler(
transforms=transforms, artifact_location=self.artifact_location)
column_type_mapping = (
process_handler._map_column_names_to_types_from_transforms())
expected_column_type_mapping = {
'x': float, 'y': float, 'z': float, 'w': str
}
self.assertDictEqual(column_type_mapping, expected_column_type_mapping)
expected_tft_raw_data_feature_spec = {
'x': tf.io.VarLenFeature(tf.float32),
'y': tf.io.VarLenFeature(tf.float32),
'z': tf.io.VarLenFeature(tf.float32),
'w': tf.io.VarLenFeature(tf.string)
}
actual_tft_raw_data_feature_spec = (
process_handler.get_raw_data_feature_spec(column_type_mapping))
self.assertDictEqual(
actual_tft_raw_data_feature_spec, expected_tft_raw_data_feature_spec)
def test_tft_process_handler_transformed_data_schema(self):
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location)
raw_data_feature_spec = {
'x': tf.io.VarLenFeature(tf.float32),
'y': tf.io.VarLenFeature(tf.float32),
'z': tf.io.VarLenFeature(tf.string),
}
raw_data_metadata = dataset_metadata.DatasetMetadata(
schema_utils.schema_from_feature_spec(raw_data_feature_spec))
expected_transformed_data_schema = {
'x': Sequence[np.float32],
'y': Sequence[np.float32],
'z': Sequence[bytes]
}
actual_transformed_data_schema = (
process_handler._get_transformed_data_schema(raw_data_metadata))
self.assertDictEqual(
actual_transformed_data_schema, expected_transformed_data_schema)
def test_tft_process_handler_verify_artifacts(self):
with beam.Pipeline() as p:
raw_data = (
p
| beam.Create([{
'x': np.array([1, 3])
}, {
'x': np.array([4, 6])
}]))
process_handler = handlers.TFTProcessHandler(
transforms=[tft.ScaleTo01(columns=['x'])],
artifact_location=self.artifact_location,
)
_ = raw_data | process_handler
self.assertTrue(
FileSystems.exists(
# To check the gcs directory with FileSystems, the dir path must
# end with /
os.path.join(
self.artifact_location,
handlers.RAW_DATA_METADATA_DIR + '/')))
self.assertTrue(
FileSystems.exists(
os.path.join(
self.artifact_location,
handlers.RAW_DATA_METADATA_DIR,
handlers.SCHEMA_FILE)))
with beam.Pipeline() as p:
raw_data = (p | beam.Create([{'x': np.array([2, 5])}]))
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location, artifact_mode='consume')
transformed_data = raw_data | process_handler
transformed_data |= beam.Map(lambda x: x.x)
# the previous min is 1 and max is 6. So this should scale by (1, 6)
assert_that(
transformed_data,
equal_to([np.array([0.2, 0.8], dtype=np.float32)],
equals_fn=np.array_equal))
def test_tft_process_handler_unused_column(self):
data = [
{
'x': [5, 1], 'y': 2
},
{
'x': [8, 4], 'y': 5
},
{
'x': [9, 5], 'y': 6
},
{
'x': [10, 6], 'y': 7
},
{
'x': [11, 7], 'y': 8
},
{
'x': [12, 8], 'y': 9
},
{
'x': [13, 9], 'y': 10
},
{
'x': [14, 10], 'y': 11
},
{
'x': [15, 11], 'y': 12
},
{
'x': [16, 12], 'y': 13
},
{
'x': [17, 13], 'y': 14
},
{
'x': [18, 14], 'y': 15
},
{
'x': [19, 15], 'y': 16
},
{
'x': [20, 16], 'y': 17
},
{
'x': [21, 17], 'y': 18
},
{
'x': [22, 18], 'y': 19
},
{
'x': [23, 19], 'y': 20
},
{
'x': [24, 20], 'y': 21
},
{
'x': [25, 21], 'y': 22
},
]
# pylint: disable=line-too-long
expected_data = [
beam.Row(
x=np.array([0.16666667, 0.], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=2),
beam.Row(
x=np.array([0.29166666, 0.125], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=5),
beam.Row(
x=np.array([0.33333334, 0.16666667], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=6),
beam.Row(
x=np.array([0.375, 0.20833333], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=7),
beam.Row(
x=np.array([0.41666666, 0.25], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=8),
beam.Row(
x=np.array([0.45833334, 0.29166666], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=9),
beam.Row(
x=np.array([0.5, 0.33333334], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=10),
beam.Row(
x=np.array([0.5416667, 0.375], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=11),
beam.Row(
x=np.array([0.5833333, 0.41666666], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=12),
beam.Row(
x=np.array([0.625, 0.45833334], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=13),
beam.Row(
x=np.array([0.6666667, 0.5], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=14),
beam.Row(
x=np.array([0.7083333, 0.5416667], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=15),
beam.Row(
x=np.array([0.75, 0.5833333], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=16),
beam.Row(
x=np.array([0.7916667, 0.625], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=17),
beam.Row(
x=np.array([0.8333333, 0.6666667], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=18),
beam.Row(
x=np.array([0.875, 0.7083333], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=19),
beam.Row(
x=np.array([0.9166667, 0.75], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=20),
beam.Row(
x=np.array([0.9583333, 0.7916667], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=21),
beam.Row(
x=np.array([1., 0.8333333], dtype=np.float32),
x_max=np.array([25.], dtype=np.float32),
x_min=np.array([1.], dtype=np.float32),
y=22),
]
# pylint: enable=line-too-long
expected_data_x = [row.x for row in expected_data]
expected_data_y = [row.y for row in expected_data]
scale_to_0_1_fn = tft.ScaleTo01(columns=['x'])
with beam.Pipeline() as p:
raw_data = p | beam.Create(data)
process_handler = handlers.TFTProcessHandler(
transforms=[scale_to_0_1_fn],
artifact_location=self.artifact_location,
)
transformed_pcoll = raw_data | process_handler
transformed_pcoll_x = transformed_pcoll | beam.Map(lambda x: x.x)
transformed_pcoll_y = transformed_pcoll | beam.Map(lambda x: x.y)
assert_that(
transformed_pcoll_x,
equal_to(expected_data_x, equals_fn=np.array_equal),
label='transformed data')
assert_that(
transformed_pcoll_y,
equal_to(expected_data_y, equals_fn=np.array_equal),
label='unused column')
def test_consume_mode_with_extra_columns_in_the_input(self):
with beam.Pipeline() as p:
raw_data = (
p
| beam.Create([{
'x': np.array([1, 3])
}, {
'x': np.array([4, 6])
}]))
process_handler = handlers.TFTProcessHandler(
transforms=[tft.ScaleTo01(columns=['x'])],
artifact_location=self.artifact_location,
)
_ = raw_data | process_handler
test_data = [{
'x': np.array([2, 5]), 'y': np.array([1, 2]), 'z': 'fake_string'
},
{
'x': np.array([1, 10]),
'y': np.array([2, 3]),
'z': 'fake_string2'
}]
expected_test_data = [{
'x': np.array([0.2, 0.8], dtype=np.float32),
'y': np.array([1, 2]),
'z': 'fake_string'
},
{
'x': np.array([0., 1.8], dtype=np.float32),
'y': np.array([2, 3]),
'z': 'fake_string2'
}]
expected_test_data_x = [row['x'] for row in expected_test_data]
expected_test_data_y = [row['y'] for row in expected_test_data]
expected_test_data_z = [row['z'] for row in expected_test_data]
with beam.Pipeline() as p:
raw_data = (p | beam.Create(test_data))
process_handler = handlers.TFTProcessHandler(
artifact_location=self.artifact_location, artifact_mode='consume')
transformed_data = raw_data | process_handler
transformed_data_x = transformed_data | beam.Map(lambda x: x.x)
transformed_data_y = transformed_data | beam.Map(lambda x: x.y)
transformed_data_z = transformed_data | beam.Map(lambda x: x.z)
assert_that(
transformed_data_x,
equal_to(expected_test_data_x, equals_fn=np.array_equal),
label='transformed data')
assert_that(
transformed_data_y,
equal_to(expected_test_data_y, equals_fn=np.array_equal),
label='unused column: y')
assert_that(
transformed_data_z,
equal_to(expected_test_data_z, equals_fn=np.array_equal),
label='unused column: z')
def test_handler_with_same_input_elements(self):
with beam.Pipeline() as p:
data = [
{
'x': 'I'
},
{
'x': 'love'
},
{
'x': 'Beam'
},
{
'x': 'Beam'
},
{
'x': 'is'
},
{
'x': 'awesome'
},
]
raw_data = (p | beam.Create(data))
process_handler = handlers.TFTProcessHandler(
transforms=[tft.ComputeAndApplyVocabulary(columns=['x'])],
artifact_location=self.artifact_location,
)
transformed_data = raw_data | process_handler
expected_data = [
beam.Row(x=np.array([4])),
beam.Row(x=np.array([1])),
beam.Row(x=np.array([0])),
beam.Row(x=np.array([0])),
beam.Row(x=np.array([2])),
beam.Row(x=np.array([3])),
]
expected_data_x = [row.x for row in expected_data]
actual_data_x = transformed_data | beam.Map(lambda x: x.x)
assert_that(
actual_data_x,
equal_to(expected_data_x, equals_fn=np.array_equal),
label='transformed data')
class TFTProcessHandlerTestWithGCSLocation(TFTProcessHandlerTest):
def setUp(self) -> None:
self.artifact_location = self.gcs_artifact_location = os.path.join(
'gs://temp-storage-for-perf-tests/tft_handler', uuid.uuid4().hex)
def tearDown(self):
pass
if __name__ == '__main__':
unittest.main()