input_len = 60
target_len = 30
batch_size = 50
with_EOS = False

csv_in = '../price_history_03_seq_start_suddens_trimmed.csv'

"""
Explanation: Step 0 - hyperparams
vocab_size is all the potential words you could have (classification for translation case)
and max sequence length are the SAME thing
decoder RNN hidden units are usually same size as encoder RNN hidden units in translation but for our case it does not seem really to be a relationship there but we can experiment and find out later, not a priority thing right now
End of explanation
"""


data_path = '../../../../Dropbox/data'
ph_data_path = data_path + '/price_history'
assert path.isdir(ph_data_path)

npz_full = ph_data_path + '/price_history_per_mobile_phone.npz'

dataset_gen = PriceHistoryDatasetPerMobilePhone(random_state=random_state)

dic = dataset_gen.genSaveDictionary(csv_in=csv_in, window_len=90, npz_out=npz_full)
dic.keys()

"""
Explanation: Actual Run
End of explanation
"""


from arima.arima_estimator import ArimaEstimator
import warnings
from collections import OrderedDict
from mylibs.py_helper import cartesian_coord
from arima.arima_cv import ArimaCV

parameters = OrderedDict([
    ('p_auto_regression_order', range(6)), #0-5
    ('d_integration_level', range(3)), #0-2
    ('q_moving_average', range(6)), #0-5
])

cart = cartesian_coord(*parameters.values())
cart.shape

cur_sku = dic.values()[0]
cur_sku.keys()

full_mat = cur_sku['train']
full_mat.shape

target_len

inputs = full_mat[:, :-target_len]
inputs.shape

targets = full_mat[:, -target_len:]
targets.shape

%%time
with warnings.catch_warnings():
    warnings.filterwarnings("ignore")
    ae = ArimaEstimator(p_auto_regression_order=0, d_integration_level=1, q_moving_average=0, easy_mode=True)
    ae.fit(inputs, targets).score(inputs, targets)

score_dic_filepath = data_path + "/arima/scoredic_testing.npy"

path.abspath(score_dic_filepath)

%%time
with warnings.catch_warnings():
    warnings.filterwarnings("ignore")
    scoredic = ArimaCV.cross_validate(inputs=inputs, targets=targets, cartesian_combinations=cart,
                                      score_dic_filepath=score_dic_filepath, easy_mode=True)

#4h 4min 51s / 108 cases => ~= 136 seconds per case !

arr = np.array(list(scoredic.iteritems()))
arr.shape

np.NaN

import math

float('nan') == np.NaN

#np.isnan()
filtered_arr = arr[ np.logical_not(arr[:, 1] != arr[:, 1]) ]
filtered_arr.shape

plt.plot(filtered_arr[:, 1])

minarg = np.argmin(filtered_arr[:, 1])
minarg

best_params = filtered_arr[minarg, 0]
best_params

test_mat = cur_sku['test']
test_ins = test_mat[:-target_len]
test_ins.shape

test_tars = test_mat[-target_len:]
test_tars.shape

test_ins_vals = test_ins.values.reshape(1, -1)
test_ins_vals.shape

test_tars_vals = test_tars.values.reshape(1, -1)
test_tars_vals.shape

"""
Explanation: Arima
End of explanation
"""


%%time
with warnings.catch_warnings():
    warnings.filterwarnings("ignore")
    ae = ArimaEstimator(p_auto_regression_order=best_params[0],
                        d_integration_level=best_params[1],
                        q_moving_average=best_params[2],
                        easy_mode=True)
    score = ae.fit(test_ins_vals, test_tars_vals).score(test_ins_vals, test_tars_vals)

score

plt.figure(figsize=(15,7))
plt.plot(ae.preds.flatten(), label='preds')
test_tars.plot(label='real')
plt.legend()
plt.show()

"""
Explanation: Testing with easy mode on
End of explanation
"""


%%time
with warnings.catch_warnings():
    warnings.filterwarnings("ignore")
    ae = ArimaEstimator(p_auto_regression_order=best_params[0],
                        d_integration_level=best_params[1],
                        q_moving_average=best_params[2],
                        easy_mode=False)
    score = ae.fit(test_ins_vals, test_tars_vals).score(test_ins_vals, test_tars_vals)

score

plt.figure(figsize=(15,7))
plt.plot(ae.preds.flatten(), label='preds')
test_tars.plot(label='real')
plt.legend()
plt.show()

"""
Explanation: Testing with easy mode off
End of explanation
"""


