-
Notifications
You must be signed in to change notification settings - Fork 24
/
run_darima.py
265 lines (208 loc) · 9.83 KB
/
run_darima.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
#! /usr/local/bin/python3.7
import findspark
findspark.init("/usr/lib/spark-current")
import pyspark
# Set Executor Env
conf = pyspark.SparkConf().setAppName("Spark DARIMA App").setExecutorEnv('ARROW_PRE_0_15_IPC_FORMAT', '1')
spark = pyspark.sql.SparkSession.builder.config(conf=conf).getOrCreate()
spark.sparkContext.addPyFile("darima.zip")
import os, sys, time
from datetime import timedelta
# from hurry.filesize import size
import pickle
import numpy as np
import pandas as pd
import string
from math import ceil
from pyspark.sql.types import *
from pyspark.sql import functions
from pyspark.sql.functions import udf, pandas_udf, PandasUDFType, monotonically_increasing_id
from darima.model import sarima2ar_model, darima_model
from darima.dlsa import dlsa_mapreduce
from darima.forecast import forecast_darima, darima_forec
from darima.evaluation import model_eval
import rpy2.robjects as robjects
from rpy2.robjects import numpy2ri
# Enable Arrow-based columnar data transfers
spark.conf.set("spark.sql.execution.arrow.enabled", "true")
spark.conf.set("spark.sql.execution.arrow.fallback.enabled", "true")
##----------------------------------------------------------------------------------------
## SETTINGS
##----------------------------------------------------------------------------------------
# General settings
#-----------------------------------------------------------------------------------------
using_data = "real_hdfs" # ["simulated_pdf", "real_pdf", "real_hdfs"
series_name = 'TOTAL'
model_saved_file_name = 'result/darima_model_' + series_name + '_' + time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()) + '.pkl'
coef_saved_file_name = 'result/darima_coef_' + series_name + '_' + time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()) + '.csv'
# Model settings
#-----------------------------------------------------------------------------------------
period = 24 # Seasonality
tol = 2000
order = [0,0,0]; seasonal = [0,0,0]
max_p = 5; max_q = 5; max_P = 2; max_Q = 2
max_order = 5; max_d = 2; max_D = 1
allowmean = True; allowdrift = True
method = "CSS" # Fitting method
approximation = False; stepwise = True
parallel = False; num_cores = 2
h = 2879; level = 95
# Settings for using real hdfs data
#-----------------------------------------------------------------------------------------
file_train_path = ['/user/student/xiaoqian-darima/darima/data/' + series_name + '_train.csv'] # HDFS file
file_test_path = ['/user/student/xiaoqian-darima/darima/data/' + series_name + '_test.csv'] # HDFS file
forec_saved_file_name = 'result/darima_forec_' + series_name + '_' + time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()) + '.csv'
usecols_x = ['time']
schema_sdf = StructType([
StructField('demand', DoubleType(), True),
StructField('time', StringType(), True)
])
# s = spark.read.schema("col0 INT, col1 DOUBLE")
n_files = len(file_train_path)
partition_num = [150]*n_files # Number of partition subseries
## max_sample_size_per_sdf = 10000 # No effect with `real_hdfs` data
sample_size_per_partition = []
Y_name = "demand"
sample_size = []
# Read or load data chunks into pandas
#-----------------------------------------------------------------------------------------
time_2sdf = []
# time_repartition = []
file_no_i = 0
tic_2sdf = time.perf_counter()
## Using HDFS data
## ------------------------------
isub = 0 # fixed, never changed
# Read HDFS to Spark DataFrame
data_sdf_i = spark.read.csv(file_train_path[file_no_i], header=True, schema=schema_sdf)
data_sdf_i = data_sdf_i.select(usecols_x + [Y_name])
data_sdf_i = data_sdf_i.dropna()
data_test_sdf = spark.read.csv(file_test_path[file_no_i], header=True, schema=schema_sdf)
# Calculate sample size of per partition (except the last sample)
sample_size.append(data_sdf_i.count())
sample_size_per_partition.append(int(sample_size[file_no_i] / partition_num[file_no_i]))
# Add ID
data_sdf_i = data_sdf_i.withColumn(
"id",
monotonically_increasing_id()+1)
# Add partition ID according to ID
def partition_id(idvalue):
if ceil(idvalue/sample_size_per_partition[file_no_i]) <= partition_num[file_no_i]:
return ceil(idvalue/sample_size_per_partition[file_no_i])
else:
return partition_num[file_no_i]
udf_partition_id = udf(partition_id, IntegerType())
data_sdf_i = data_sdf_i.withColumn("partition_id", udf_partition_id("id"))
# data_sdf_i.groupby("partition_id").count().show() # Verify results of udf_partition_id
data_sdf_i = data_sdf_i.select([Y_name] + ["partition_id"]) # Only select demand and partition_id
# data_sdf_i = data_sdf_i.select(usecols_x + [Y_name] + ["partition_id"]) # Delete ID
time_2sdf.append(time.perf_counter() - tic_2sdf)
##----------------------------------------------------------------------------------------
## MODELING ON PARTITIONED DATA
##----------------------------------------------------------------------------------------
'''
tic_repartition = time.perf_counter()
data_sdf_i = data_sdf_i.repartition(partition_num[file_no_i], "partition_id")
time_repartition.append(time.perf_counter() - tic_repartition)
'''
# Register a user defined function via the Pandas UDF
#-----------------------------------------------------------------------------------------
usecoef_ar = ['c0', 'c1'] + ["pi" + str(i+1) for i in np.arange(tol)]
schema_fields = []
for i in usecoef_ar:
schema_fields.append(StructField(i, DoubleType(), True))
## standard output version
#schema_beta = StructType(
# [StructField('par_id', IntegerType(), True),
# StructField('coef', DoubleType(), True),
# StructField('Sig_invMcoef', DoubleType(), True)]
# + schema_fields)
# simplified output version
schema_beta = StructType(
[StructField('par_id', IntegerType(), True),
StructField('Sig_inv_value', DoubleType(), True)]
+ schema_fields)
@pandas_udf(schema_beta, PandasUDFType.GROUPED_MAP)
def darima_model_udf(sample_df):
return darima_model(sample_df = sample_df, Y_name = Y_name, period = period, tol = tol,
order = order, seasonal = seasonal,
max_p = max_p, max_q = max_q, max_P = max_P, max_Q = max_Q,
max_order = max_order, max_d = max_d, max_D = max_D,
allowmean = allowmean, allowdrift = allowdrift, method = method,
approximation = approximation, stepwise = stepwise,
parallel = parallel, num_cores = num_cores)
# Partition the data and run the UDF
#-----------------------------------------------------------------------------------------
model_mapped_sdf = data_sdf_i.groupby("partition_id").apply(darima_model_udf)
#tic_map = time.perf_counter()
#model_mapped_sdf.show()
#time_map = time.perf_counter() - tic_map
##----------------------------------------------------------------------------------------
## AGGREGATING THE MODEL ESTIMATES
##----------------------------------------------------------------------------------------
# Obtain Sig_tilde and Theta_tilde
sample_size = sum(sample_size)
tic_mapred = time.perf_counter()
Sig_Theta = dlsa_mapreduce(model_mapped_sdf, sample_size) # Pandas DataFrame
time_mapred = time.perf_counter() - tic_mapred
##----------------------------------------------------------------------------------------
## FORECASTING
##----------------------------------------------------------------------------------------
data_train = data_sdf_i.toPandas()["demand"]
out_Theta = Sig_Theta["Theta_tilde"]
out_Sigma = Sig_Theta[usecoef_ar]
tic_model_forec = time.perf_counter()
out_model_forec = darima_forec(Theta = out_Theta, Sigma = out_Sigma,
x = data_train, period = period,
h = h, level = level)
time_model_forec = time.perf_counter() - tic_model_forec
##----------------------------------------------------------------------------------------
## EVALUATION
##----------------------------------------------------------------------------------------
data_test = data_test_sdf.toPandas()["demand"]
pred = out_model_forec["pred"]
lower = out_model_forec["lower"]
upper = out_model_forec["upper"]
tic_model_eval = time.perf_counter()
out_model_eval = model_eval(x = data_train, xx = data_test, period = period,
pred = pred, lower = lower, upper = upper, level = level)
time_model_eval = time.perf_counter() - tic_model_eval
score = out_model_eval.mean(axis=0)
##----------------------------------------------------------------------------------------
## PRINT OUTPUT
##----------------------------------------------------------------------------------------
partition_num = sum(partition_num)
# time_repartition = sum(time_repartition)
# time_2sdf = sum(time_2sdf)
# sample_size_per_partition = sample_size / partition_num
out_time = pd.DataFrame({
"sample_size": sample_size,
"sample_size_per_partition": sample_size_per_partition,
"n_par": len(schema_beta) - 3,
"partition_num": partition_num,
# "time_2sdf": time_2sdf,
# "time_repartition": time_repartition,
#"time_map": time_map,
"time_mapred": time_mapred,
# "time_dlsa": time_dlsa,
"time_model_forec": time_model_forec,
"time_model_eval": time_model_eval}, index=[0])
# save the model to pickle, use pd.read_pickle("test.pkl") to load it.
out = [Sig_Theta, out_model_forec, out_model_eval, out_time]
pickle.dump(out, open(os.path.expanduser(model_saved_file_name), 'wb'))
print("Model results are saved to:\t" + model_saved_file_name)
# save the Sig_Theta to csv.
Sig_Theta.to_csv(coef_saved_file_name, index=False)
print("Coefficient results are saved to:\t" + coef_saved_file_name)
# save the point forecasts and prediction intervals to csv.
out_model_forec.to_csv(forec_saved_file_name, index=False)
print("Forecasting results are saved to:\t" + forec_saved_file_name)
# print time.
print("\nModel Summary:\n")
print(out_time.to_string(index=False))
print("\nDLSA Coefficients:\n")
print(out_Theta.to_string(index=False))
print("\nForecasting scores:\n")
print("mase, smape, msis\n")
print(score.to_string(index=False))
print("End")