-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgen_ts_from_folder.py
158 lines (129 loc) · 5.48 KB
/
gen_ts_from_folder.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
# ▄▄▌ ▐ ▄▌ ▄▄ •
#▪ ██· █▌▐█▐█ ▀ ▪
# ▄█▀▄ ██▪▐█▐▐▌▄█ ▀█▄
#▐█▌.▐▌▐█▌██▐█▌▐█▄▪▐█
# ▀█▄▀▪ ▀▀▀▀ ▀▪·▀▀▀▀
#
## gen_ts_from_folder.py
## A script to use a model on a folder of images
## and compile a time-series of predictions
## modify config_test.json with relevant inputs
## Written by Daniel Buscombe,
## Northern Arizona University
## daniel.buscombe.nau.edu
# import libraries
import sys, getopt, os
import numpy as np
import json
from glob import glob
import pandas as pd
from datetime import datetime
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
os.environ['CUDA_VISIBLE_DEVICES'] = '-1' ##use CPU
from utils import *
#==============================================================
## script starts here
if __name__ == '__main__':
#==============================================================
## user inputs
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv,"h:i:")
except getopt.GetoptError:
print('python gen_ts_from_folder.py -w path/to/folder')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('Example usage: python gen_ts_from_folder.py -i snap_images/data')
sys.exit()
elif opt in ("-i"):
image_path = arg
if not os.path.isdir(image_path):
print('Provided image directory apparently does not exist ... exiting')
sys.exit(1)
with open(os.getcwd()+os.sep+'config'+os.sep+'config_test.json') as f:
config = json.load(f)
# config variables
im_size = int(config["im_size"])
category = config["category"]
weights_path = config["weights_path"]
samplewise_std_normalization = config["samplewise_std_normalization"]
samplewise_center = config["samplewise_center"]
file_ext = config["file_ext"]
input_csv_file = config["input_csv_file"]
IMG_SIZE = (im_size, im_size)
#==============================================================
# load json and create model
# call the utils.py function load_OWG_json
OWG = load_OWG_json(os.getcwd()+os.sep+weights_path)
# call the utils.py function get_and_tidy_df
_, df = get_and_tidy_df(os.path.normpath(os.getcwd()), input_csv_file, image_path, category)
# call the utils.py function im_gen_noaug
im_gen = im_gen_noaug(samplewise_std_normalization, samplewise_center)
print ("[INFO] Reading images ...")
# call the utils.py function gen_from_def
test_X, test_Y = gen_from_def(IMG_SIZE, df, image_path, category, im_gen)
print ("[INFO] Predicting ...")
if len(test_X)<2:
print('Insufficient imagery - check your config_test.json file ... exiting')
sys.exit(1)
V = OWG.predict(test_X, batch_size = 128, verbose = True)
files = sorted(glob(image_path+os.sep+'*.'+file_ext))
T = [file.split(os.sep)[-1].split('.')[0] for file in files]
print ("[INFO] Making plots...")
T = np.array(T, dtype=np.float32)
V = np.array(V.squeeze(), dtype=np.float32)
# interpolate onto a regular small timestamp
df = df.sort_values('time')
x = np.arange(T.min(), T.max(),len(T)*5)
Vi = np.interp(x,T,V)
# make a dataframe
if category=='H':
d = {'dates': [datetime.fromtimestamp(t) for t in x], 'H': np.interp(x,df.time.values,df[category])}
else:
d = {'dates': [datetime.fromtimestamp(t) for t in x], 'T': np.interp(x,df.time.values,df[category])}
new_df = pd.DataFrame(data=d)
# remove night-time hour samples (before 7am and after 7pm)
ind = np.where((new_df.dates.dt.hour<7) | (new_df.dates.dt.hour>19))[0]
new_df[category][ind] = np.nan
Vi[ind] = np.nan
new_df[category+'_est'] = Vi
# make a time-series plot showing actual and estimated
# for just a few days
n1 = 0; n2=130
fig = plt.figure(figsize=(8,6))
ax=plt.subplot(211)
ax.plot_date(mdate.epoch2num(x)[n1:n2], new_df[category][n1:n2],'k', lw=2, label='Measured')
ax.plot_date(mdate.epoch2num(x)[n1:n2], Vi[n1:n2], 'b.-', lw=2, alpha=0.5, label='Estimated from Image')
if category=='H':
plt.ylabel(r'$H_s$ (m)')
else:
plt.ylabel(r'$T_p$ (s)')
date_formatter = mdate.DateFormatter('%m-%d-%y')
ax.xaxis.set_major_formatter(date_formatter)
fig.autofmt_xdate()
plt.legend()
plt.savefig(image_path.split(os.sep)[0]+'_short_time_series_'+str(category)+'.png', dpi=300, bbox_inches='tight')
plt.close()
# make a time-series plot showing actual and estimated
# for the whole time period
fig = plt.figure(figsize=(8,6))
ax=plt.subplot(211)
ax.plot_date(mdate.epoch2num(x), new_df[category],'k', lw=2, label='Measured')
ax.plot_date(mdate.epoch2num(x), Vi, 'b.-', lw=2, alpha=0.5, label='Estimated from Image')
if category=='H':
plt.ylabel(r'$H_s$ (m)')
else:
plt.ylabel(r'$T_p$ (s)')
date_formatter = mdate.DateFormatter('%m-%d-%y')
ax.xaxis.set_major_formatter(date_formatter)
fig.autofmt_xdate()
plt.legend()
plt.savefig(image_path.split(os.sep)[0]+'_time_series_'+str(category)+'.png', dpi=300, bbox_inches='tight')
plt.close()
new_df.to_csv(image_path.split(os.sep)[0]+'_obs_est_time_series.csv')