-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model_Predictions.py
114 lines (87 loc) · 2.58 KB
/
Model_Predictions.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
# mport the required cbrain functions
from imports import *
from data_generator import *
from models import *
#shouldn't need on a local computer?
from utils import limit_mem
#Module to save and load models
import h5py
import netCDF4
import numpy as np
#Keras
from keras.models import Sequential
from keras.layers import *
import time
#added by Griffin
import xarray as xr
t0 = time.time()
output_vector = 65
input_vector = 64
print('Starting')
#If running on the GPU or GPU-shared partition uncomment
limit_mem()
DATADIR = 'Preprocessed_Data/RG_Spaced_10_Years/'
valid_gen = DataGenerator(
data_dir=DATADIR,
feature_fn='full_physics_essentials_test_month02_features.nc',
target_fn='full_physics_essentials_test_month02_targets.nc',
batch_size=512,
norm_fn='full_physics_essentials_train_month01_norm.nc', # SAME NORMALIZATION FILE!
fsub='feature_means',
fdiv='feature_stds',
tmult='target_conv',
shuffle=False,
)
fsub = valid_gen.feature_norms[0]
fdiv = valid_gen.feature_norms[1]
tsub = valid_gen.target_norms[0]
tdiv = valid_gen.target_norms[1]
model = keras.models.load_model('Models/8_Years_Linear.h5')
path_to_file = 'Preprocessed_Data/RG_Spaced_10_Years/full_physics_essentials_test_month02_features.nc'
real_ds = xr.open_dataset(path_to_file)
features = real_ds.features[:, :].values
print(features.shape)
print('files imported')
model_data = np.zeros(shape=(len(features), output_vector))
model_data[:,:] = np.nan
segments = int(len(features)/100000)
steps = segments+1
start = 0
gap = 100000
for i in range(steps):
if i <= steps-2:
print(i)
feature_here=features[start:gap]
f = feature_here-fsub
f = f/fdiv
f=f.reshape(-1,1)
x = np.reshape(f, (100000,64))
p = model.predict_on_batch(x)
p = p/tdiv
p = p+tsub
model_data[start:gap,:] = p
start = start+100000
gap = gap+ 100000
else:
feature_here=features[start:]
f = feature_here-fsub
f = f/fdiv
f=f.reshape(-1,1)
x = np.reshape(f, (len(features[start:]),64))
p = model.predict_on_batch(x)
p = p/tdiv
p = p+tsub
model_data[start:,:] = p
print('made it')
print('creating nc file')
lev = np.arange(len(model_data[0]))
sample = np.arange(len(model_data))
myda = xr.DataArray(model_data, coords = {'sample': sample, 'lev': lev}, dims=('sample', 'lev'))
myda.name = 'Prediction'
myds = myda.to_dataset()
myds.to_netcdf('Models/Test_Final_Linear_DNN_Year.nc')
t1 = time.time()
total = t1-t0
total = total/(60.0*60.0)
print(total)
print('done')