-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
100 lines (77 loc) · 3.3 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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# import plotly.express as px # to plot the time series plot
from sklearn import metrics # for the evaluation
from sklearn.preprocessing import LabelEncoder,MinMaxScaler,StandardScaler
# import tensorflow as tf
import torch
import torch.nn as nn
from torch.autograd import Variable
import imageio
import natsort
import os
# This is the sliding window for the LSTM
def sliding_windows(data, seq_length):
x = []
predicted_ls = []
for i in range(len(data)-seq_length-1):
_x = data[i:(i+seq_length)]
predicted = data[i+seq_length]
x.append(_x)
predicted_ls.append(predicted)
return np.array(x), np.array(predicted_ls)
# This method is to convert X and Y values into tensors and reshape them
def conv_and_reshape(x, y):
X = torch.tensor(x, dtype=torch.float32)
Y = torch.tensor(y, dtype=torch.float32)
if len(X.shape) == 1:
X = torch.reshape(X, (X.shape[0],1))
Y = torch.reshape(Y, (Y.shape[0],1))
elif len(X.shape) == 2:
X = torch.reshape(X, (X.shape[0],X.shape[1],1))
Y = torch.reshape(Y, (Y.shape[0],Y.shape[1],1))
return X, Y
# This method is to get data for X and Y
def get_data(df, inst_id, seq_length, cols):
target_df = df[df.instance_id == inst_id]
df_cols_training = target_df[cols]
# normalize features
scaler = MinMaxScaler(feature_range=(-1, 1))
df_cols_training = scaler.fit_transform(df_cols_training)
df_cols_training_x = df_cols_training[:,0]
df_cols_training_y = df_cols_training[:,1]
x, expected_x = sliding_windows(df_cols_training_x, seq_length)
y, expected_y = sliding_windows(df_cols_training_y, seq_length)
dataX, dataY = conv_and_reshape(x,y)
expected_x, expected_y = conv_and_reshape(expected_x, expected_y)
return dataX, dataY, expected_x, expected_y
# This method is to get data for X,Y,Z and heading angle
def get_all_data(df, inst_id, seq_length, cols):
target_df = df[df.instance_id == inst_id]
df_cols_training = target_df[cols]
# normalize features
scaler = MinMaxScaler(feature_range=(-1, 1))
df_cols_training = scaler.fit_transform(df_cols_training)
df_cols_training_x = df_cols_training[:,0]
df_cols_training_y = df_cols_training[:,1]
df_cols_training_z = df_cols_training[:,2]
df_cols_training_r = df_cols_training[:,3]
x, expected_x = sliding_windows(df_cols_training_x, seq_length)
y, expected_y = sliding_windows(df_cols_training_y, seq_length)
z, expected_z = sliding_windows(df_cols_training_z, seq_length)
r, expected_r = sliding_windows(df_cols_training_r, seq_length)
dataX, dataY = conv_and_reshape(x,y)
dataZ, dataR = conv_and_reshape(z,r)
expected_x, expected_y = conv_and_reshape(expected_x, expected_y)
expected_z, expected_r = conv_and_reshape(expected_z, expected_r)
return dataX, dataY, expected_x, expected_y, dataZ, dataR, expected_z, expected_r
def conv_to_gif(path):
images = []
for filename in natsort.natsorted(os.listdir("./pictures/")):
print(filename)
for i in range(4):
images.append(imageio.imread("./pictures/"+filename))
imageio.mimsave(path, images)
for filename in natsort.natsorted(os.listdir("./pictures/")):
os.remove("./pictures/"+filename)