forked from KasperGroesLudvigsen/influenza_transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
215 lines (153 loc) · 7.71 KB
/
utils.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os
import numpy as np
from torch import nn, Tensor
from typing import Optional, Any, Union, Callable, Tuple
import torch
import pandas as pd
from pathlib import Path
def generate_square_subsequent_mask(dim1: int, dim2: int) -> Tensor:
"""
Generates an upper-triangular matrix of -inf, with zeros on diag.
Modified from:
https://pytorch.org/tutorials/beginner/transformer_tutorial.html
Args:
dim1: int, for both src and tgt masking, this must be target sequence
length
dim2: int, for src masking this must be encoder sequence length (i.e.
the length of the input sequence to the model),
and for tgt masking, this must be target sequence length
Return:
A Tensor of shape [dim1, dim2]
"""
return torch.triu(torch.ones(dim1, dim2) * float('-inf'), diagonal=1)
def get_indices_input_target(num_obs, input_len, step_size, forecast_horizon, target_len):
"""
Produce all the start and end index positions of all sub-sequences.
The indices will be used to split the data into sub-sequences on which
the models will be trained.
Returns a tuple with four elements:
1) The index position of the first element to be included in the input sequence
2) The index position of the last element to be included in the input sequence
3) The index position of the first element to be included in the target sequence
4) The index position of the last element to be included in the target sequence
Args:
num_obs (int): Number of observations in the entire dataset for which
indices must be generated.
input_len (int): Length of the input sequence (a sub-sequence of
of the entire data sequence)
step_size (int): Size of each step as the data sequence is traversed.
If 1, the first sub-sequence will be indices 0-input_len,
and the next will be 1-input_len.
forecast_horizon (int): How many index positions is the target away from
the last index position of the input sequence?
If forecast_horizon=1, and the input sequence
is data[0:10], the target will be data[11:taget_len].
target_len (int): Length of the target / output sequence.
"""
input_len = round(input_len) # just a precaution
start_position = 0
stop_position = num_obs-1 # because of 0 indexing
subseq_first_idx = start_position
subseq_last_idx = start_position + input_len
target_first_idx = subseq_last_idx + forecast_horizon
target_last_idx = target_first_idx + target_len
print("target_last_idx is {}".format(target_last_idx))
print("stop_position is {}".format(stop_position))
indices = []
while target_last_idx <= stop_position:
indices.append((subseq_first_idx, subseq_last_idx, target_first_idx, target_last_idx))
subseq_first_idx += step_size
subseq_last_idx += step_size
target_first_idx = subseq_last_idx + forecast_horizon
target_last_idx = target_first_idx + target_len
return indices
def get_indices_entire_sequence(data: pd.DataFrame, window_size: int, step_size: int) -> list:
"""
Produce all the start and end index positions that is needed to produce
the sub-sequences.
Returns a list of tuples. Each tuple is (start_idx, end_idx) of a sub-
sequence. These tuples should be used to slice the dataset into sub-
sequences. These sub-sequences should then be passed into a function
that slices them into input and target sequences.
Args:
num_obs (int): Number of observations (time steps) in the entire
dataset for which indices must be generated, e.g.
len(data)
window_size (int): The desired length of each sub-sequence. Should be
(input_sequence_length + target_sequence_length)
E.g. if you want the model to consider the past 100
time steps in order to predict the future 50
time steps, window_size = 100+50 = 150
step_size (int): Size of each step as the data sequence is traversed
by the moving window.
If 1, the first sub-sequence will be [0:window_size],
and the next will be [1:window_size].
Return:
indices: a list of tuples
"""
stop_position = len(data)-1 # 1- because of 0 indexing
# Start the first sub-sequence at index position 0
subseq_first_idx = 0
subseq_last_idx = window_size
indices = []
while subseq_last_idx <= stop_position:
indices.append((subseq_first_idx, subseq_last_idx))
subseq_first_idx += step_size
subseq_last_idx += step_size
return indices
def read_data(data_dir: Union[str, Path] = "data",
timestamp_col_name: str="timestamp") -> pd.DataFrame:
"""
Read data from csv file and return pd.Dataframe object
Args:
data_dir: str or Path object specifying the path to the directory
containing the data
target_col_name: str, the name of the column containing the target variable
timestamp_col_name: str, the name of the column or named index
containing the timestamps
"""
# Ensure that `data_dir` is a Path object
data_dir = Path(data_dir)
# Read csv file
csv_files = list(data_dir.glob("*.csv"))
if len(csv_files) > 1:
raise ValueError("data_dir contains more than 1 csv file. Must only contain 1")
elif len(csv_files) == 0:
raise ValueError("data_dir must contain at least 1 csv file.")
data_path = csv_files[0]
print("Reading file in {}".format(data_path))
data = pd.read_csv(
data_path,
parse_dates=[timestamp_col_name],
index_col=[timestamp_col_name],
infer_datetime_format=True,
low_memory=False
)
# Make sure all "n/e" values have been removed from df.
if is_ne_in_df(data):
raise ValueError("data frame contains 'n/e' values. These must be handled")
data = to_numeric_and_downcast_data(data)
# Make sure data is in ascending order by timestamp
data.sort_values(by=[timestamp_col_name], inplace=True)
return data
def is_ne_in_df(df:pd.DataFrame):
"""
Some raw data files contain cells with "n/e". This function checks whether
any column in a df contains a cell with "n/e". Returns False if no columns
contain "n/e", True otherwise
"""
for col in df.columns:
true_bool = (df[col] == "n/e")
if any(true_bool):
return True
return False
def to_numeric_and_downcast_data(df: pd.DataFrame):
"""
Downcast columns in df to smallest possible version of it's existing data
type
"""
fcols = df.select_dtypes('float').columns
icols = df.select_dtypes('integer').columns
df[fcols] = df[fcols].apply(pd.to_numeric, downcast='float')
df[icols] = df[icols].apply(pd.to_numeric, downcast='integer')
return df