-
Notifications
You must be signed in to change notification settings - Fork 0
/
scalable_time_series.py
348 lines (338 loc) · 10.9 KB
/
scalable_time_series.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
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
"""Queries for scalable time-series modeling."""
def create_date_range(project, dataset_name, table_name):
sql_date_range = """
WITH
# Get min and max dates so we can enumerate the range next
CTE_date_limits AS (
SELECT
CAST(MIN(sale_date) AS DATE) AS min_sale_date,
CAST(MAX(sale_date) AS DATE) AS max_sale_date
FROM
`{0}.{1}.{2}` ),
# Expand date range using date bounds
CTE_date_range AS (
SELECT
UNIX_DATE(calendar_date) AS unix_date
FROM
UNNEST(GENERATE_DATE_ARRAY((
SELECT
min_sale_date
FROM
CTE_date_limits), (
SELECT
max_sale_date
FROM
CTE_date_limits), INTERVAL 1 DAY) ) AS calendar_date ),
# Create start and end date ranges for timescale
CTE_start_end_timescale_date_range AS (
SELECT
ROW_NUMBER() OVER (ORDER BY unix_date) - 1 AS timescale_idx,
unix_date AS unix_timescale_start_date,
LEAD(unix_date, 6) OVER (ORDER BY unix_date) AS unix_timescale_end_date
FROM
CTE_date_range )
SELECT
timescale_idx,
unix_timescale_start_date,
unix_timescale_end_date,
DATE_FROM_UNIX_DATE(unix_timescale_start_date) AS timescale_start_date,
DATE_FROM_UNIX_DATE(unix_timescale_end_date) AS timescale_end_date
FROM
CTE_start_end_timescale_date_range
""".format(project, dataset_name, table_name)
return sql_date_range
def bq_create_rolling_features_label(project, dataset, date_range_table, sales_dataset_table, value_name, downsample_size, window_size, horizon, labels_size=1):
feature_pivot_list = ["""SUM(CASE WHEN timestep_idx = {time} - 1 THEN {value_name} ELSE 0.0 END) AS price_ago_{time}""".format(time=time,value_name=value_name) for time in range(window_size, 0, -1)]
label_pivot_list = ["""SUM(CASE WHEN timestep_idx = {time} - 1 THEN {value_name} ELSE 0.0 END) AS price_ahead_{time}""".format(time=time,value_name=value_name) for time in range(1, labels_size + 1)]
feature_list = ["price_ago_{time}".format(time=time) for time in range(window_size, 0, -1)]
label_list = ["price_ahead_{time}".format(time=time) for time in range(1, labels_size + 1)]
new_line = ",\n "
sql_bqml_sub_sequences = """
WITH
# Create sequence date ranges
CTE_seq_date_ranges AS (
SELECT
A.timescale_idx AS seq_idx,
ROW_NUMBER() OVER (PARTITION BY A.timescale_idx ORDER BY B.unix_timescale_start_date) - 1 AS timestep_idx,
B.timescale_idx AS timescale_idx,
A.unix_timescale_start_date AS seq_unix_start_date,
B.unix_timescale_start_date AS timestep_unix_start_date,
B.unix_timescale_end_date AS timestep_unix_end_date
FROM
`{project}.{dataset}.{date_range_table}` AS A
INNER JOIN
`{project}.{dataset}.{date_range_table}` AS B
ON
MOD(B.unix_timescale_start_date - A.unix_timescale_start_date, {downsample_size}) = 0
AND B.unix_timescale_start_date >= A.unix_timescale_start_date
WHERE
B.unix_timescale_end_date IS NOT NULL),
# Create sequence date ranges for features data
CTE_seq_features_date_range AS (
SELECT
*
FROM
CTE_seq_date_ranges
WHERE
timestep_idx < {window_size}),
# Create sequence date ranges for labels data
CTE_seq_labels_date_range AS (
SELECT
*
FROM
CTE_seq_date_ranges
WHERE
timestep_idx < {labels_size}),
# Join timescale information with data to be aggregated over timescale
CTE_timescale_joined_data AS (
SELECT
timescale_idx,
unix_timescale_start_date,
unix_timescale_end_date,
all_sales.sale_price
FROM
`{project}.{dataset}.{date_range_table}` AS start_end_timescale_date_range
INNER JOIN
`{project}.{sales_dataset_table}` AS all_sales
ON
start_end_timescale_date_range.unix_timescale_start_date <= UNIX_DATE(CAST(all_sales.sale_date AS DATE))
AND UNIX_DATE(CAST(all_sales.sale_date AS DATE)) <= start_end_timescale_date_range.unix_timescale_end_date),
# Group data we want aggregated over timescale
CTE_grouped_data AS (
SELECT
timescale_idx,
unix_timescale_start_date,
unix_timescale_end_date,
APPROX_QUANTILES(sale_price, 100)[
OFFSET
(50)] AS {value_name}
FROM
CTE_timescale_joined_data
GROUP BY
timescale_idx,
unix_timescale_start_date,
unix_timescale_end_date),
# Join features data to features date ranges
CTE_features AS (
SELECT
A.seq_idx,
A.timestep_idx,
A.seq_unix_start_date,
IFNULL(B.{value_name},0) AS {value_name}
FROM
CTE_seq_features_date_range AS A
INNER JOIN
CTE_grouped_data AS B
ON
A.timescale_idx = B.timescale_idx),
# Aggregate features data into sequences
CTE_seq_features AS (
SELECT
seq_idx,
seq_unix_start_date,
{feature_pivot_list}
FROM
CTE_features
GROUP BY
seq_idx,
seq_unix_start_date),
# Join labels data to labels date ranges
CTE_labels AS (
SELECT
A.seq_idx,
A.timestep_idx,
A.seq_unix_start_date,
IFNULL(B.{value_name},0) AS {value_name}
FROM
CTE_seq_labels_date_range AS A
INNER JOIN
CTE_grouped_data AS B
ON
A.timescale_idx = B.timescale_idx),
# Aggregate labels data into sequences
CTE_seq_labels AS (
SELECT
seq_idx,
seq_unix_start_date,
{label_pivot_list}
FROM
CTE_labels
GROUP BY
seq_idx,
seq_unix_start_date)
# Join features with labels with horizon in between
SELECT
DATE_FROM_UNIX_DATE(A.seq_unix_start_date) AS feat_seq_start_date,
DATE_FROM_UNIX_DATE(A.seq_unix_start_date + {window_size} * {downsample_size} - 1) AS feat_seq_end_date,
DATE_FROM_UNIX_DATE(B.seq_unix_start_date) AS lab_seq_start_date,
DATE_FROM_UNIX_DATE(B.seq_unix_start_date + {labels_size} * {downsample_size} - 1) AS lab_seq_end_date,
{feature_list},
{label_list}
FROM
CTE_seq_features AS A
INNER JOIN
CTE_seq_labels AS B
ON
A.seq_unix_start_date + ({window_size} * {downsample_size} - 1) + ({downsample_size} * {horizon}) = B.seq_unix_start_date
ORDER BY
A.seq_idx
""".format(project=project,
dataset=dataset,
date_range_table=date_range_table,
sales_dataset_table=sales_dataset_table,
value_name=value_name,
downsample_size=downsample_size,
window_size=window_size,
horizon=horizon,
labels_size=labels_size,
feature_pivot_list=new_line.join(feature_pivot_list),
label_pivot_list=new_line.join(label_pivot_list),
feature_list=new_line.join(feature_list),
label_list=new_line.join(label_list))
return sql_bqml_sub_sequences
def csv_create_rolling_features_label(project, dataset, date_range_table, sales_dataset_table, value_name, downsample_size, window_size, horizon, labels_size=1):
sql_csv_sub_sequences = """
WITH
# Create sequence date ranges
CTE_seq_date_ranges AS (
SELECT
A.timescale_idx AS seq_idx,
ROW_NUMBER() OVER (PARTITION BY A.timescale_idx ORDER BY B.unix_timescale_start_date) - 1 AS timestep_idx,
B.timescale_idx AS timescale_idx,
A.unix_timescale_start_date AS seq_unix_start_date,
B.unix_timescale_start_date AS timestep_unix_start_date,
B.unix_timescale_end_date AS timestep_unix_end_date
FROM
`{project}.{dataset}.{date_range_table}` AS A
INNER JOIN
`{project}.{dataset}.{date_range_table}` AS B
ON
MOD(B.unix_timescale_start_date - A.unix_timescale_start_date, {downsample_size}) = 0
AND B.unix_timescale_start_date >= A.unix_timescale_start_date
WHERE
B.unix_timescale_end_date IS NOT NULL),
# Create sequence date ranges for features data
CTE_seq_features_date_range AS (
SELECT
*
FROM
CTE_seq_date_ranges
WHERE
timestep_idx < {window_size}),
# Create sequence date ranges for labels data
CTE_seq_labels_date_range AS (
SELECT
*
FROM
CTE_seq_date_ranges
WHERE
timestep_idx < {labels_size}),
# Join timescale information with data to be aggregated over timescale
CTE_timescale_joined_data AS (
SELECT
timescale_idx,
unix_timescale_start_date,
unix_timescale_end_date,
all_sales.sale_price
FROM
`{project}.{dataset}.{date_range_table}` AS start_end_timescale_date_range
INNER JOIN
`{project}.{sales_dataset_table}` AS all_sales
ON
start_end_timescale_date_range.unix_timescale_start_date <= UNIX_DATE(CAST(all_sales.sale_date AS DATE))
AND UNIX_DATE(CAST(all_sales.sale_date AS DATE)) <= start_end_timescale_date_range.unix_timescale_end_date),
# Group data we want aggregated over timescale
CTE_grouped_data AS (
SELECT
timescale_idx,
unix_timescale_start_date,
unix_timescale_end_date,
APPROX_QUANTILES(sale_price, 100)[
OFFSET
(50)] AS {value_name}
FROM
CTE_timescale_joined_data
GROUP BY
timescale_idx,
unix_timescale_start_date,
unix_timescale_end_date),
# Join features data to features date ranges
CTE_features AS (
SELECT
A.seq_idx,
A.timestep_idx,
A.seq_unix_start_date,
IFNULL(B.{value_name},
0) AS {value_name}
FROM
CTE_seq_features_date_range AS A
INNER JOIN
CTE_grouped_data AS B
ON
A.timescale_idx = B.timescale_idx),
# Aggregate features data into sequences
CTE_seq_features AS (
SELECT
seq_idx,
seq_unix_start_date,
STRING_AGG(CAST({value_name} AS STRING), ';'
ORDER BY
timestep_idx) AS {value_name}_agg
FROM
CTE_features
GROUP BY
seq_idx,
seq_unix_start_date),
# Join labels data to labels date ranges
CTE_labels AS (
SELECT
A.seq_idx,
A.timestep_idx,
A.seq_unix_start_date,
IFNULL(B.{value_name},
0) AS {value_name}
FROM
CTE_seq_labels_date_range AS A
INNER JOIN
CTE_grouped_data AS B
ON
A.timescale_idx = B.timescale_idx),
# Aggregate labels data into sequences
CTE_seq_labels AS (
SELECT
seq_idx,
seq_unix_start_date,
STRING_AGG(CAST({value_name} AS STRING), ';'
ORDER BY
timestep_idx) AS labels_agg
FROM
CTE_labels
GROUP BY
seq_idx,
seq_unix_start_date)
# Join features with labels with horizon in between
SELECT
DATE_FROM_UNIX_DATE(A.seq_unix_start_date) AS feat_seq_start_date,
DATE_FROM_UNIX_DATE(A.seq_unix_start_date + {window_size} * {downsample_size} - 1) AS feat_seq_end_date,
DATE_FROM_UNIX_DATE(B.seq_unix_start_date) AS lab_seq_start_date,
DATE_FROM_UNIX_DATE(B.seq_unix_start_date + {labels_size} * {downsample_size} - 1) AS lab_seq_end_date,
{value_name}_agg,
labels_agg
FROM
CTE_seq_features AS A
INNER JOIN
CTE_seq_labels AS B
ON
A.seq_unix_start_date + ({window_size} * {downsample_size} - 1) + ({downsample_size} * {horizon}) = B.seq_unix_start_date
ORDER BY
A.seq_idx
""".format(project=project,
dataset=dataset,
date_range_table=date_range_table,
sales_dataset_table=sales_dataset_table,
value_name=value_name,
downsample_size=downsample_size,
window_size=window_size,
horizon=horizon,
labels_size=labels_size)
return sql_csv_sub_sequences