Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced visualization for multivariate columns #617

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions exp/exp_long_term_forecasting.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ def train(self, setting):
return self.model

def test(self, setting, test=0):
import pandas as pd
df_raw = pd.read_csv(os.path.join(self.args.root_path,
self.args.data_path))
feature_names = df_raw.columns[1:6]

test_data, test_loader = self._get_data(flag='test')
if test:
print('loading model')
Expand Down Expand Up @@ -220,9 +225,27 @@ def test(self, setting, test=0):
if test_data.scale and self.args.inverse:
shape = input.shape
input = test_data.inverse_transform(input.reshape(shape[0] * shape[1], -1)).reshape(shape)
gt = np.concatenate((input[0, :, -1], true[0, :, -1]), axis=0)
pd = np.concatenate((input[0, :, -1], pred[0, :, -1]), axis=0)
visual(gt, pd, os.path.join(folder_path, str(i) + '.pdf'))
gt = np.concatenate((input[0, :], true[0, :]), axis=0)
pd = np.concatenate((input[0, :], pred[0, :]), axis=0)
np.save(folder_path + 'core_true.npy', gt)
np.save(folder_path + 'core_pred.npy', pd)



for idx, col in enumerate(feature_names):
mae, mse, rmse, mape, mspe = metric(pd[:, idx], gt[:, idx])
print(f'Column: {col}')
print('mse:{}, rmse:{}, mae:{} '.format(mse, rmse, mae))
f = open("result_long_term_forecast.txt", 'a')
f.write(setting + " \n")
f.write(f'Column: {col}')
f.write('mse:{}, rmse:{}, mae:{} '.format(mse, rmse, mae))
f.write('\n')
f.write('\n')
f.close()
np.save(folder_path + 'metrics.npy', np.array([mae, mse, rmse, mape, mspe]))

visual(feature_names, self.args.pred_len, gt, pd, os.path.join(folder_path, str(i) + '.pdf'))

preds = np.concatenate(preds, axis=0)
trues = np.concatenate(trues, axis=0)
Expand Down Expand Up @@ -252,16 +275,9 @@ def test(self, setting, test=0):
dtw = 'not calculated'


mae, mse, rmse, mape, mspe = metric(preds, trues)
print('mse:{}, mae:{}, dtw:{}'.format(mse, mae, dtw))
f = open("result_long_term_forecast.txt", 'a')
f.write(setting + " \n")
f.write('mse:{}, mae:{}, dtw:{}'.format(mse, mae, dtw))
f.write('\n')
f.write('\n')
f.close()

np.save(folder_path + 'metrics.npy', np.array([mae, mse, rmse, mape, mspe]))


np.save(folder_path + 'pred.npy', preds)
np.save(folder_path + 'true.npy', trues)

Expand Down
29 changes: 22 additions & 7 deletions utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,30 @@ def inverse_transform(self, data):
return (data * self.std) + self.mean


def visual(true, preds=None, name='./pic/test.pdf'):
def visual(col_name, pred_len, true, preds=None, name='./pic/test.pdf'):
"""
Results visualization
Results visualization for all columns
"""
plt.figure()
plt.plot(true, label='GroundTruth', linewidth=2)
if preds is not None:
plt.plot(preds, label='Prediction', linewidth=2)
plt.legend()
n_columns = len(col_name)
start_idx = len(true) - pred_len
plt.figure(figsize=(15, 2*n_columns))

for idx, col in enumerate(col_name):
plt.subplot(n_columns, 1, idx+1)
if len(true.shape) > 1:
plt.plot(true[:, idx], label='GroundTruth', linewidth=2)
if preds is not None:
plt.plot(range(start_idx, len(true)), preds[-pred_len:, idx], label='Prediction', linewidth=2)
else:
plt.plot(true, label='GroundTruth', linewidth=2, zorder=2)
if preds is not None:
plt.plot(range(start_idx, len(true)), preds[-pred_len:], label='Prediction', linewidth=2, zorder=1)

plt.legend()
plt.title(col)
plt.grid(True)

plt.tight_layout()
plt.savefig(name, bbox_inches='tight')


Expand Down