-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClimFormer.py
More file actions
189 lines (152 loc) · 4.7 KB
/
ClimFormer.py
File metadata and controls
189 lines (152 loc) · 4.7 KB
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
'''
ClimFormer class, a subclass of InformerForPrediction
=====================================================
This class is a subclass of Informer
It contains classes for time series dataset, future time series dataset, and two subclasses of
InformerForPrediction and TimeSeriesTransformerForPrediction
'''
import numpy as np
import xarray as xr
import pandas as pd
from typing import Optional, List, Union, Tuple
import scipy.linalg as sc
import matplotlib.pyplot as plt
# import datetime
import time as tm
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
import torch.nn.functional as F
import transformers as tr
from sklearn.preprocessing import StandardScaler, MinMaxScaler
import zapata.computation as zcom
import zapata.data as zd
import zapata.lib as zlib
import zapata.mapping as zmap
import zapata.koopman as zkop
class TimeSeriesDataset(Dataset):
'''
Class for time series dataset.
Includes time feature for transformers
PARAMETERS
==========
datasrc: numpy array
Source data
datatgt: numpy array
Target data
TIN: int
Input time steps
MIN: int
Input variables size
T: int
Predictions time steps
K: int
Output variables size
time_features: numpy array (optional)
If not None contain Time Features
ATTRIBUTES
==========
datasrc: numpy array
Source data
datatgt: numpy array
Target data
time_features: numpy array
Time features
TIN: int
Input time steps
MIN: int
Input variables
T: int
Output time steps
K: int
Output variables
'''
def __init__(self, datasrc, datatgt, TIN, MIN, T, K, time_features=None):
self.datasrc = datasrc
self.datatgt = datatgt
self.TIN = TIN
self.MIN = MIN
self.T = T
self.K = K
self.time_features = time_features
def __len__(self):
return len(self.datasrc) - self.TIN - self.T + 1
def __getitem__(self, idx):
input_seq = self.datasrc[idx:idx+self.TIN, :self.MIN]
target_seq = self.datatgt[idx+self.TIN:idx+self.TIN+self.T, :self.K]
pasft = self.time_features[idx:idx+self.TIN,:]
futft = self.time_features[idx+self.TIN:idx+self.TIN+self.T,:]
return input_seq, target_seq, pasft, futft
class TimeSeriesFuture(Dataset):
'''
Class for time series dataset.
Includes future time feature for prediction with informer
PARAMETERS
==========
datasrc: numpy array
Source data
datatgt: numpy array
Target data
TIN: int
Input time steps
MIN: int
Input variables size
T: int
Predictions time steps
K: int
Output variables size
time_features: numpy array (optional)
If not None contain Time Features
shift:
Overlap between source and target, for trasnformers
overlap = 0 for LSTM overlap should be TIN-T
ATTRIBUTES
==========
datasrc: numpy array
Source data
datatgt: numpy array
Target data
time_features: numpy array
Time features
TIN: int
Input time steps
MIN: int
Input variables
T: int
Output time steps
K: int
Output variables
'''
def __init__(self, datasrc, datatgt, TIN, MIN, T, K, Tpredict, time_features=None):
self.datasrc = datasrc
self.datatgt = datatgt
self.TIN = TIN
self.MIN = MIN
self.T = T
self.Tpredict = Tpredict
self.K = K
self.time_features = time_features
def __len__(self):
return len(self.datasrc) - self.TIN - self.Tpredict + 1
def __getitem__(self, idx):
input_seq = self.datasrc[idx:idx+self.TIN, :self.MIN]
target_seq = self.datatgt[idx+self.TIN:idx+self.TIN+self.T, :self.K]
pasft = self.time_features[idx:idx+self.TIN,:]
futft = self.time_features[idx+self.TIN:idx+self.TIN+self.Tpredict,:]
return input_seq, target_seq, pasft, futft
class ClimFormer(tr.InformerForPrediction):
'''
Class for training and prediction with InformerForPrediction model
from ``transformers`` library
'''
def __init__(self, config):
super().__init__(config)
tr.InformerForPrediction(config)
class TrasFormer(tr.TimeSeriesTransformerForPrediction):
'''
Class for training and prediction with TimeSeriesTransformerForPrediction model
from ``transformers`` library
'''
def __init__(self, *args):
tr.TimeSeriesTransformerForPrediction.__init__(self, *args)