-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_analysis_3c.py
199 lines (146 loc) · 6.13 KB
/
data_analysis_3c.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 11 01:25:40 2023
@author: st_ko
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from functools import reduce
import os
import keras as ks
from utils import *
import tensorflow as tf
# function to plot resampled momthly (mean of all month) observations for all years for all currencies
# for each currency draw different plot
def plot_all_currencies_monthly(data,size,num_currencies,ann=False,save=False):
# annotate a subpart of the series
for col in data.columns[0:num_currencies]:
fig=plt.figure()
for i in range(size):
if(ann):
plt.annotate(data[col][i], (data[col].index[i], data[col].values[i]),
textcoords="offset points", xytext=(0, 10), ha='center',fontsize=8,rotation = 45)
plt.plot(data[col][:size],'o-',label=col)
plt.legend(loc='lower right',bbox_to_anchor=(1.05,0.2))
plt.title("Monthly sampled All years " + col )
if(save):
# 1000 means all years but monthly
# just a random chosen number
save_image(fig, col,None)
plt.close()
# plot the currencies within a given range of observations
# added option to select currencies subrange as well
# this plots chosen subset of series and observations all together in one plot
def plot_Series(data,size,num_currencies,ann=False):
# annotate a subpart of the series
plt.figure()
for col in data.columns[0:num_currencies]:
for i in range(size):
if(ann):
plt.annotate(data[col][i], (data[col].index[i], data[col].values[i]),
textcoords="offset points", xytext=(0, 10), ha='center',fontsize=8,rotation = 45)
plt.plot(data[col][:size],'o-',label=col)
plt.legend(loc='lower right',bbox_to_anchor=(1.05,0.2))
# ! More cuztomized
# create a nested dictionary of year -> dictionary of months --> observations of this month in this year
def create_nested_dict(data):
total_time= [x for x in data.index]
year_dict = {x: {x2 : [] for x2 in iter(range(1,13)) } for x in range(data.index.min().year,data.index.max().year+ 1)}
m_step = data.index.min().month
y_step = data.index.min().year
y=True
m = True
# create nested dictionary
# i will turn this later into a function
for i in range(len(total_time)-1) :
if(total_time[i].year == total_time[i+1].year):
y = True
if(total_time[i].month == total_time[i+1].month):
year_dict[y_step][m_step].append(total_time[i])
m = True
else :
if(m==True):
year_dict[y_step][m_step].append(total_time[i])
m=False
m_step +=1
else :
# reset months to set the last observation
m_step = 12
# add the remaining same year observation
year_dict[y_step][m_step].append(total_time[i])
# reset month index
m_step=data.index.min().month
y_step+=1
y=False
return year_dict
# for a certain year , for a certain currency plot the monthy series of this currency (one subplot per month of the year)
def plot_one_year_one_currency(year_dict,data,year,currency,save=False):
# total plots
fig, axes = plt.subplots(nrows=3, ncols=4, figsize=(15, 10)) # 3x4 grid for subplots
#select currency from dataset
# for each month
for month in range(1, 13):
row = (month - 1) // 4 # Determine row index
col = (month - 1) % 4 # Determine column index
#pick year and currency
dollar_month = data.loc[year_dict[year][month],currency]
ax = axes[row,col]
# rotate txt
ax.xaxis.set_tick_params(rotation=60)
ax.plot(dollar_month,'o-',label= currency + ' : ' + str(month))
ax.set_title(f"{year}-{month}-{currency}")
#general title for whole plot
plt.suptitle(f"Time Series Observations for Year {year}", fontsize=16)
plt.tight_layout()
#plt.show()
# close plot
#plt.close()
# save image (optional)
if(save):
save_image(fig,currency,year)
# same as above but do it for all currencies (17 currencies -> 17 plots )
def plot_one_year_all_currencies(year_dict,data,year,save=False):
for cu in data.columns:
plot_one_year_one_currency(year_dict,data,year,cu,save)
# plot all years one currency
def plot_all_years_one_currency(year_dict,data,currency,save=False):
# total plots
fig, axes = plt.subplots(nrows=5, ncols=5, figsize=(15, 10)) # 3x4 grid for subplots
#select currency from dataset
# for each month
inyear = data.index.min().year
for year in range(1, 26):
row = (year - 1) // 5 # Determine row index
col = (year - 1) % 5 # Determine column index
# take the yearly indices
indices = [k for k2 in [x for x in y_dict[inyear].values()] for k in k2]
dollar_month = data.loc[ indices ,currency]
ax = axes[row,col]
# rotate txt
ax.xaxis.set_tick_params(rotation=60)
ax.plot(dollar_month,'o-',label= currency + ' : ' + str(inyear))
ax.set_title(f"{inyear}-{currency}")
inyear += 1
#general title for whole plot
plt.suptitle(f"Time Series Observations for All years - {currency}", fontsize=16)
plt.tight_layout()
#plt.show()
#plt.close()
if(save):
# i use 25 to denote that we plot all the years
save_image(fig,currency,25)
# function to plot all years for all currencies
def plot_all_years_all_currencies(year_dict,data,save=False):
for cur in data.columns:
plot_all_years_one_currency(year_dict, data, cur,save)
# function to save images
def save_image(fig,currency,year):
if not (os.path.exists('plots')):
os.makedirs('plots')
plt.savefig(os.path.join('plots', currency + ' ' +str(year) + '-forecast.png'))
plt.close()
# only called as main for testing
if __name__=="__main__" :
pass