# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import tensorflow.contrib.legacy_seq2seq

# +
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
#import tensorflow.contrib.seq2seq 

#from tensorflow.contrib import legacy_seq2seq

from lib.metrics import masked_mae_loss
from model.dcrnn_cell import DCGRUCell

import tensorflow.contrib.legacy_seq2seq


from model.dcrnn_model import DCRNNModel
from model.dcrnn_supervisor import DCRNNSupervisor

n_f=7
n_node=106

info={'base_dir': 'data/model', 
      'log_level': 'INFO', 
      'data': {'batch_size': 4, 'dataset_dir': 'data/', 
               'test_batch_size': 4, 'val_batch_size': 4, 
               'graph_pkl_filename': 'data/sensor_graph/adj_mx_bay.pkl'},#not used? 
      'model': {'cl_decay_steps': 2000, 'filter_type': 'dual_random_walk', 
                'horizon': 12, 'input_dim': n_f, 'l1_decay': 0, 'max_diffusion_step': 2, 
                'num_nodes': n_node, 'num_rnn_layers': 2, 'output_dim': n_f, 'rnn_units': 64, 
                'seq_len': 12, 'use_curriculum_learning': True}, 
      'train': {'base_lr': 0.01, 'dropout': 0, 'epoch': 0, 'epochs': 100, 'epsilon': 0.001, 
                'global_step': 0, 'lr_decay_ratio': 0.1, 'max_grad_norm': 5, 'max_to_keep': 100, 
                'min_learning_rate': 2e-06, 'optimizer': 'adam', 'patience': 50, 'steps': [20, 30, 40, 50],
                'test_every_n_epochs': 10}}

import pickle
with open("data/sensor_graph/adj_mx.pkl", 'rb') as pickle_file:
    adj_mx = pickle.load(pickle_file)
adj_mx=adj_mx[2]
from lib.utils import StandardScaler

supervisor = DCRNNSupervisor(adj_mx=adj_mx, **info)
# -

with tf.Session() as sess:
    supervisor.train(sess=sess)

info['train']['model_filename']

with tf.Session() as sess:
    supervisor.load(sess, 'data/model/dcrnn_DR_2_h_12_64-64_lr_0.01_bs_4_0106094512/models-5.3707-105261')
    outputs = supervisor.evaluate(sess)



outputs['predictions'][0].shape

outputs['groundtruth'][0].shape

outputs.keys()

len(outputs["predictions"])

len(outputs["groundtruth"])

outputs["groundtruth"]

# +
import numpy as np
y_true=np.array(outputs["groundtruth"])
print(y_true.shape)
y_true=np.transpose(y_true,(1,0,2))
print(y_true.shape)

y_true[0]

# +
y_pred=np.array(outputs["predictions"])
print(y_pred.shape)
y_pred=np.transpose(y_pred,(1,0,2))
print(y_pred.shape)

y_pred[0]
# -





# +
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib import animation

node_loc=np.load('data/node_loc.npy')
#_node_signal=np.load('data/node_signal.npy')
#print(node_loc.shape,_node_signal.shape)
x=node_loc[:,0]
y=node_loc[:,1]
z=node_loc[:,2]

y_true_vis=y_true[-150:]
y_true_vis=y_true_vis[:,-1,:]

y_pred_vis=y_pred[-150:]
y_pred_vis=y_pred_vis[:,-1,:]
y_pred_vis.shape, y_true_vis.shape
# -



# +
# %matplotlib notebook

def update_graph(num):
    graph._offsets3d = (x,y,z)
    graph.set_array(y_true_vis[num,:])
    title.set_text('3D Test, time={}'.format(num))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
title = ax.set_title('3D Test')

#i_f=6
graph = ax.scatter(x, y, z,c=y_true_vis[0,:], marker='o',
             vmin=np.min(y_true_vis[:,:]),vmax=np.max(y_true_vis[:,:]),
                  cmap=cm.RdBu_r,edgecolor="k")

ani = animation.FuncAnimation(fig, update_graph, 150,
                               interval=40, blit=False)
fig.colorbar(graph)

ani.save('images/results/true.gif')
plt.show() 


# +
# %matplotlib notebook

def update_graph(num):
    graph._offsets3d = (x,y,z)
    graph.set_array(y_pred_vis[num,:])
    title.set_text('3D Test, time={}'.format(num))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
title = ax.set_title('3D Test')

i_f=6
graph = ax.scatter(x, y, z,c=y_pred_vis[0,:], marker='o',
             vmin=np.min(y_true_vis[:,:]),vmax=np.max(y_true_vis[:,:]),
                  cmap=cm.RdBu_r,edgecolor="k")

ani = animation.FuncAnimation(fig, update_graph, 150,
                               interval=40, blit=False)
fig.colorbar(graph)

ani.save('images/results/pred.gif')
plt.show() 
# -

y_pred_vis.shape

for i in range(106):
    plt.plot(np.arange(150),y_pred_vis[:,i],label="pred")
plt.show()

# +
# %matplotlib notebook

for i in range(106):
    plt.plot(np.arange(150),y_pred_vis[:,i],label="pred")
plt.show()

# +
# %matplotlib notebook

for i in range(106):
    plt.plot(np.arange(150),y_true_vis[:,i],label="pred")
plt.show()
# -

# # %matplotlib notebook
#
# plt.plot(np.arange(150),y_pred_vis[:,70],label="pred")
# plt.plot(np.arange(150),y_true_vis[:,70],label="true")
# plt.legend()
# plt.show()



StarcoderdataJupyterScriptsDedupFiltered