-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
34 lines (26 loc) · 996 Bytes
/
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
import numpy as np
from numpy import isnan
import os
import matplotlib.pyplot as plt
import torch
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def fill_missing_one_day(values):
one_day = 1
for row in range(values.shape[0]):
for col in range(values.shape[1]):
if isnan(values[row, col]):
values[row, col] = values[row - one_day, col]
def fill_missing_mean(values):
for row in range(values.shape[0]):
for col in range(values.shape[1]):
if isnan(values[row, col]):
values[row, col] = np.mean(values)
def save_or_show_plot(file_nm: str, save: bool):
if save:
plt.savefig(os.path.join(os.path.dirname(__file__), "plots", file_nm))
else:
plt.show()
def numpy_to_tensor(x):
return torch.from_numpy(x).type(torch.FloatTensor).to(device)
def model_saver (file_nm: str, model):
torch.save(model, os.path.join(os.path.dirname(__file__), "model", file_nm))