1818import numpy
1919from datetime import datetime
2020import tensorflow as tf
21- import timeseries .encoder_decoder .encoder_decoder_preprocessing as encoder_decoder_preprocessing
21+ import ml_pipeline . timeseries .encoder_decoder .encoder_decoder_preprocessing as encoder_decoder_preprocessing
2222from scipy .stats import stats
2323from tensorflow_transform .beam import tft_unit
2424from tensorflow_transform .beam import impl as beam_impl
25- from timeseries .utils import timeseries_transform_utils as ts_utils
26-
25+ from ml_pipeline .timeseries .utils import timeseries_transform_utils as ts_utils
2726
2827
2928class BeamImplTest (tft_unit .TransformTestCase ):
3029 def setUp (self ):
3130 tf .compat .v1 .logging .info (
32- 'Starting test case: %s' , self ._testMethodName )
31+ 'Starting test case: %s' , self ._testMethodName )
3332
3433 self ._context = beam_impl .Context (use_deep_copy_optimization = True )
3534 self ._context .__enter__ ()
@@ -43,15 +42,15 @@ def _SkipIfExternalEnvironmentAnd(self, predicate, reason):
4342
4443 def testBasicType (self ):
4544 config = {
46- 'timesteps' : 3 ,
47- 'time_features' : [],
48- 'features' : ['a' ],
49- 'enable_timestamp_features' : False
45+ 'timesteps' : 3 ,
46+ 'time_features' : [],
47+ 'features' : ['a' ],
48+ 'enable_timestamp_features' : False
5049 }
5150
5251 input_data = [{'a' : [1000.0 , 2000.0 , 3000.0 ]}]
5352 input_metadata = tft_unit .metadata_from_feature_spec (
54- {'a' : tf .io .VarLenFeature (tf .float32 )})
53+ {'a' : tf .io .VarLenFeature (tf .float32 )})
5554
5655 output = [[1000 ], [2000 ], [3000 ]]
5756
@@ -60,37 +59,37 @@ def testBasicType(self):
6059 expected_data = [{'Float32' : output , 'LABEL' : output }]
6160
6261 expected_metadata = tft_unit .metadata_from_feature_spec ({
63- 'Float32' : tf .io .FixedLenFeature ([config ['timesteps' ], 1 ],
64- tf .float32 ),
65- 'LABEL' : tf .io .FixedLenFeature ([config ['timesteps' ], 1 ],
66- tf .float32 )
62+ 'Float32' : tf .io .FixedLenFeature ([config ['timesteps' ], 1 ],
63+ tf .float32 ),
64+ 'LABEL' : tf .io .FixedLenFeature ([config ['timesteps' ], 1 ],
65+ tf .float32 )
6766 })
6867
6968 preprocessing_fn = functools .partial (
70- encoder_decoder_preprocessing .preprocessing_fn ,
71- custom_config = config )
69+ encoder_decoder_preprocessing .preprocessing_fn ,
70+ custom_config = config )
7271
7372 self .assertAnalyzeAndTransformResults (
74- input_data ,
75- input_metadata ,
76- preprocessing_fn ,
77- expected_data ,
78- expected_metadata )
73+ input_data ,
74+ input_metadata ,
75+ preprocessing_fn ,
76+ expected_data ,
77+ expected_metadata )
7978
8079 def testMixedType (self ):
8180 config = {
82- 'timesteps' : 3 ,
83- 'time_features' : ['MINUTE' , 'MONTH' , 'HOUR' , 'DAY' , 'YEAR' ],
84- 'features' : ['a' , 'b' ],
85- 'enable_timestamp_features' : False
81+ 'timesteps' : 3 ,
82+ 'time_features' : ['MINUTE' , 'MONTH' , 'HOUR' , 'DAY' , 'YEAR' ],
83+ 'features' : ['a' , 'b' ],
84+ 'enable_timestamp_features' : False
8685 }
8786
8887 input_data = [{
89- 'a' : [1000.0 , 2000.0 , 3000.0 ], 'b' : [3000 , 2000 , 1000 ]
88+ 'a' : [1000.0 , 2000.0 , 3000.0 ], 'b' : [3000 , 2000 , 1000 ]
9089 }]
9190 input_metadata = tft_unit .metadata_from_feature_spec ({
92- 'a' : tf .io .VarLenFeature (tf .float32 ),
93- 'b' : tf .io .VarLenFeature (tf .int64 )
91+ 'a' : tf .io .VarLenFeature (tf .float32 ),
92+ 'b' : tf .io .VarLenFeature (tf .int64 )
9493 })
9594
9695 output = [[1000.0 , 3000.0 ], [2000.0 , 2000.0 ], [3000.0 , 1000.0 ]]
@@ -100,43 +99,43 @@ def testMixedType(self):
10099 expected_data = [{'Float32' : output , 'LABEL' : output }]
101100
102101 expected_metadata = tft_unit .metadata_from_feature_spec ({
103- 'Float32' : tf .io .FixedLenFeature ([config ['timesteps' ], 2 ],
104- tf .float32 ),
105- 'LABEL' : tf .io .FixedLenFeature ([config ['timesteps' ], 2 ],
106- tf .float32 )
102+ 'Float32' : tf .io .FixedLenFeature ([config ['timesteps' ], 2 ],
103+ tf .float32 ),
104+ 'LABEL' : tf .io .FixedLenFeature ([config ['timesteps' ], 2 ],
105+ tf .float32 )
107106 })
108107
109108 preprocessing_fn = functools .partial (
110- encoder_decoder_preprocessing .preprocessing_fn ,
111- custom_config = config )
109+ encoder_decoder_preprocessing .preprocessing_fn ,
110+ custom_config = config )
112111
113112 self .assertAnalyzeAndTransformResults (
114- input_data ,
115- input_metadata ,
116- preprocessing_fn ,
117- expected_data ,
118- expected_metadata )
113+ input_data ,
114+ input_metadata ,
115+ preprocessing_fn ,
116+ expected_data ,
117+ expected_metadata )
119118
120119 def testWithTimeStamps (self ):
121120
122121 config = {
123- 'timesteps' : 2 ,
124- 'time_features' : ['MINUTE' , 'MONTH' , 'HOUR' , 'DAY' , 'YEAR' ],
125- 'features' : ['float32' , 'foo_TIMESTAMP' ],
126- 'enable_timestamp_features' : True
122+ 'timesteps' : 2 ,
123+ 'time_features' : ['MINUTE' , 'MONTH' , 'HOUR' , 'DAY' , 'YEAR' ],
124+ 'features' : ['float32' , 'foo_TIMESTAMP' ],
125+ 'enable_timestamp_features' : True
127126 }
128127
129128 # The values will need to be different enough for the zscore not to nan
130129 timestamp_1 = int (datetime (2000 , 1 , 1 , 0 , 0 , 0 ).timestamp ())
131130 timestamp_2 = int (datetime (2001 , 6 , 15 , 12 , 30 , 30 ).timestamp ())
132131
133132 input_data = [{
134- 'float32' : [1000.0 , 2000.0 ],
135- 'foo_TIMESTAMP' : [timestamp_1 * 1000 , timestamp_2 * 1000 ]
133+ 'float32' : [1000.0 , 2000.0 ],
134+ 'foo_TIMESTAMP' : [timestamp_1 * 1000 , timestamp_2 * 1000 ]
136135 }]
137136 input_metadata = tft_unit .metadata_from_feature_spec ({
138- 'float32' : tf .io .VarLenFeature (tf .float32 ),
139- 'foo_TIMESTAMP' : tf .io .VarLenFeature (tf .int64 )
137+ 'float32' : tf .io .VarLenFeature (tf .float32 ),
138+ 'foo_TIMESTAMP' : tf .io .VarLenFeature (tf .int64 )
140139 })
141140
142141 output_timestep_1 = self .create_transform_output (timestamp_1 )
@@ -160,22 +159,22 @@ def testWithTimeStamps(self):
160159 expected_data = [{'Float32' : output , 'LABEL' : output }]
161160
162161 expected_metadata = tft_unit .metadata_from_feature_spec ({
163- 'Float32' : tf .io .FixedLenFeature ([config ['timesteps' ], 11 ],
164- tf .float32 ),
165- 'LABEL' : tf .io .FixedLenFeature ([config ['timesteps' ], 11 ],
166- tf .float32 )
162+ 'Float32' : tf .io .FixedLenFeature ([config ['timesteps' ], 11 ],
163+ tf .float32 ),
164+ 'LABEL' : tf .io .FixedLenFeature ([config ['timesteps' ], 11 ],
165+ tf .float32 )
167166 })
168167
169168 preprocessing_fn = functools .partial (
170- encoder_decoder_preprocessing .preprocessing_fn ,
171- custom_config = config )
169+ encoder_decoder_preprocessing .preprocessing_fn ,
170+ custom_config = config )
172171
173172 self .assertAnalyzeAndTransformResults (
174- input_data ,
175- input_metadata ,
176- preprocessing_fn ,
177- expected_data ,
178- expected_metadata )
173+ input_data ,
174+ input_metadata ,
175+ preprocessing_fn ,
176+ expected_data ,
177+ expected_metadata )
179178
180179 def create_transform_output (self , timestamp : int ) -> [float ]:
181180 # Needs to be in lexical order
@@ -195,16 +194,16 @@ def create_transform_output(self, timestamp: int) -> [float]:
195194 cos_year = math .cos (timestamp * (2.0 * math .pi / ts_utils .YEAR ))
196195
197196 return [
198- cos_day ,
199- cos_hour ,
200- cos_min ,
201- cos_month ,
202- cos_year ,
203- sin_day ,
204- sin_hour ,
205- sin_min ,
206- sin_month ,
207- sin_year
197+ cos_day ,
198+ cos_hour ,
199+ cos_min ,
200+ cos_month ,
201+ cos_year ,
202+ sin_day ,
203+ sin_hour ,
204+ sin_min ,
205+ sin_month ,
206+ sin_year
208207 ]
209208
210209
0 commit comments