num_units = 400 #state size

input_len = 60
target_len = 30

batch_size = 64
with_EOS = False

total_size = 57994
train_size = 46400
test_size = 11584

"""
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_folder = '../../../../Dropbox/data'


ph_data_path = '../data/price_history'

npz_full = ph_data_path + '/price_history_dp_60to30_57994.npz'
npz_train = ph_data_path + '/price_history_dp_60to30_57994_46400_train.npz'
npz_test = ph_data_path + '/price_history_dp_60to30_57994_11584_test.npz'

"""
Explanation: Once generate data
End of explanation
"""


# dp = PriceHistorySeq2SeqDataProvider(npz_path=npz_train, batch_size=batch_size, with_EOS=with_EOS)
# dp.inputs.shape, dp.targets.shape

# aa, bb = dp.next()
# aa.shape, bb.shape

"""
Explanation: Step 1 - collect data
End of explanation
"""


model = PriceHistorySeq2SeqDynDecIns(rng=random_state, dtype=dtype, config=config, with_EOS=with_EOS)

# graph = model.getGraph(batch_size=batch_size,
#                        num_units=num_units,
#                        input_len=input_len,
#                        target_len=target_len)

#show_graph(graph)

"""
Explanation: Step 2 - Build model
End of explanation
"""


best_params = [500,
 tf.nn.tanh,
 0.0001,
 0.62488034788862112,
 0.001]

num_units, activation, lamda2, keep_prob_input, learning_rate = best_params

batch_size

def experiment():
    return model.run(npz_path=npz_train,
             npz_test = npz_test,
             epochs=100,
             batch_size = batch_size,
             num_units = num_units,
             input_len=input_len,
             target_len=target_len,
             learning_rate = learning_rate,
             preds_gather_enabled=True,
             batch_norm_enabled = True,
             activation = activation,
             decoder_first_input = PriceHistorySeq2SeqDynDecIns.DECODER_FIRST_INPUT.ZEROS,
             keep_prob_input = keep_prob_input,
             lamda2 = lamda2,
            )

#%%time
dyn_stats, preds_dict, targets = get_or_run_nn(experiment, filename='024_seq2seq_60to30_002',
                                               nn_runs_folder= data_folder + '/nn_runs')

"""
Explanation: Step 3 training the network
End of explanation
"""


dyn_stats.plotStats()
plt.show()

data_len = len(targets)

mses = np.empty(data_len)
for ii, (pred, target) in enumerate(zip(preds_dict.values(), targets.values())):
    mses[ii] = mean_squared_error(pred, target)

np.mean(mses)

huber_losses = np.empty(data_len)
for ii, (pred, target) in enumerate(zip(preds_dict.values(), targets.values())):
    huber_losses[ii] = np.mean(huber_loss(pred, target))

np.mean(huber_losses)

targets_arr = np.array(targets.values())
targets_arr.shape

preds_arr = np.array(preds_dict.values())
preds_arr.shape

np.mean(huber_loss(y_true=targets_arr, y_pred=preds_arr))

r2_scores = [r2_score(y_true=targets[ind], y_pred=preds_dict[ind])
            for ind in range(len(targets))]

ind = np.argmin(r2_scores)
ind

reals = targets[ind]
preds = preds_dict[ind]

r2_score(y_true=reals, y_pred=preds)

#sns.tsplot(data=dp.inputs[ind].flatten())

fig = plt.figure(figsize=(15,6))
plt.plot(reals, 'b')
plt.plot(preds, 'g')
plt.legend(['reals','preds'])
plt.show()

%%time
dtw_scores = [fastdtw(targets[ind], preds_dict[ind])[0]
             for ind in range(len(targets))]

np.mean(dtw_scores)

coint(preds, reals)

cur_ind = np.random.randint(len(targets))
reals = targets[cur_ind]
preds = preds_dict[cur_ind]
fig = plt.figure(figsize=(15,6))
plt.plot(reals, 'b')
plt.plot(preds, 'g')
plt.legend(['reals','preds'])
plt.show()

"""
Explanation: One epoch takes approximately 268 secs
If we want to let it run for ~8 hours = 8 * 3600 / 268 ~= 107 epochs
So let it run for 100 epochs and see how it behaves
End of explanation
"""


