-
Notifications
You must be signed in to change notification settings - Fork 1
/
smooth_cnn_visualize.py
154 lines (104 loc) · 4.94 KB
/
smooth_cnn_visualize.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 5 17:15:48 2023
@author: st_ko
"""
import glob
import keras as ks
import pandas as pd
import numpy as np
from collections import namedtuple
import tensorflow as tf
import datetime as dt
import os
from scipy import stats
import matplotlib.pyplot as plt
import glob
from utils import *
from build_multistep_model_2 import *
from sklearn.preprocessing import MinMaxScaler
# for future data (unknown)
def visualize_future_horizon(curs):
# add the list of currencies that we want to plot here
for cur in curs:
# finds all the predictions for all frequencies , training_lengths --> that correspond to a certain currency
y_path = glob.glob(os.path.join('./predictions/scnn/multi_step/slide', cur ,"**/*.csv"),recursive=True)
for p in y_path :
freq = p.split('/')[-3]
series_length = p.split('/')[-2]
# get the predictions
y_model_1 = pd.read_csv(p, header=0, index_col=0).loc[:,'F1':].transpose()
# pick the history (-100) is just to show a lot of previous history , you can set it smaller
history = pd.read_csv(os.path.join('./dataset',f"{freq}.csv"),
header=0,index_col=0).loc[:,cur][-100:].transpose()
fig = plt.figure()
plt.plot(history,'o-',color ='green',label='original history')
plt.plot(y_model_1,'o-',color='purple',label = 'predictions')
plt.title(f'FORECASTING with currency : {cur} , frequency : {freq}, training_length : {series_length}' )
plt.xticks(rotation=90)
plt.legend()
# save images of visualizations of predictions
if not (os.path.exists('./visualizations/scnn/multi_step/slide')):
os.makedirs('./visualizations/scnn/multi_step/slide')
plt.savefig(os.path.join('./visualizations/scnn/multi_step/slide',
cur +'_' + freq + '_' + str(series_length) + '.png'))
plt.close()
# for already existent data (plot predictions vs true data)
def visualize_past_horizon(frequencies):
#------------------ DATA FOR EVALUATION -------------------------#
dseries = pd.DataFrame(frequencies['daily'][1].loc['2009-12-31':])
wseries = pd.DataFrame(frequencies['weekly'][1].loc['2009-12-27':])
mseries = pd.DataFrame(frequencies['monthly'][1].loc['2009-12-31':])
qseries = pd.DataFrame(frequencies['quarterly'][1].loc['2009-12-31':])
yseries = pd.DataFrame(frequencies['yearly'][1].loc['2009-12-31':])
#------------------------------------------------------------------#
pred_dict = {'daily' : (dseries, 14),
'weekly' : (wseries,13),
'monthly' : (mseries,18),
'quarterly' : (qseries,8),
'yearly' : (yseries,6) }
# define the currencies
series = frequencies['daily'][1]
curs = series.columns
for cur in curs:
y_path = glob.glob(os.path.join('./predictions/scnn/multi_step/slide', cur ,"**/*.csv"),recursive=True)
for p in y_path :
#get the frequenxy so i know which dataset to plot with
freq = p.split('/')[-3]
series_length = p.split('/')[-2]
# fix and align the indices
horizon = pred_dict[freq][1]
history = pred_dict[freq][0][cur][: horizon * 2].transpose()
selected_index = history[:horizon].index
# read model predictions and set the predictions index same as the data index to align the true values with the predictions
y_model_1 = pd.read_csv(p, header=0, index_col=0).iloc[:,: horizon].transpose()
y_model_1.index = selected_index
fig = plt.figure()
plt.plot(history,'o-',color ='green',label='original history')
plt.plot(y_model_1,'o-',color='purple',label = 'predictions')
plt.title(f'FORECASTING with currency : {cur} , frequency : {freq}, training_length : {series_length}' )
plt.xticks(rotation=90)
plt.legend()
# save images
if not (os.path.exists('./visualizations/scnn/multi_step/slide')):
os.makedirs('./visualizations/scnn/multi_step/slide')
plt.savefig(os.path.join('./visualizations/scnn/multi_step/slide',
cur +'_' + freq + '_' + str(series_length) + '.png'))
plt.close()
# run as main
if __name__=="__main__":
frequencies = {
'daily': 'D',
'weekly': 'W',
'monthly': 'M',
'quarterly': 'Q',
'yearly': 'Y'
}
for freq_name, freq_code in frequencies.items():
data = pd.read_csv(f"./dataset/{freq_name}.csv",index_col='Date')
frequencies[freq_name] = (frequencies[freq_name],data)
#-------------- PLOT UNKNOWN FUTURE VALUES -----------------#
#visualize_future_horizon(frequencies['daily'][1].columns)
#-------------- PLOT EVALUATION PLOTS, PREDICTED DATA VS ACTUAL DATA-----#
#visualize_past_horizon(frequencies)