Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,284 +1,13 @@
|
|
1 |
-
|
2 |
-
from matplotlib.pyplot import title
|
3 |
-
import tensorflow as tf
|
4 |
-
from tensorflow import keras
|
5 |
-
from huggingface_hub import from_pretrained_keras
|
6 |
-
import pandas as pd
|
7 |
-
import matplotlib.pyplot as plt
|
8 |
import streamlit as st
|
9 |
-
from zipfile import ZipFile
|
10 |
-
import os
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
import warnings
|
17 |
-
warnings.filterwarnings("ignore")
|
18 |
-
|
19 |
-
if ("0" == "mycustom"):
|
20 |
-
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
|
21 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
22 |
-
|
23 |
-
if ("0" == "mycustom"):
|
24 |
-
uri = "https://storage.googleapis.com/tensorflow/tf-keras-datasets/jena_climate_2009_2016.csv.zip"
|
25 |
-
zip_path = keras.utils.get_file(origin=uri, fname="jena_climate_2009_2016.csv.zip")
|
26 |
-
zip_file = ZipFile(zip_path)
|
27 |
-
zip_file.extractall()
|
28 |
-
csv_path = "jena_climate_2009_2016.csv"
|
29 |
-
df = pd.read_csv(csv_path)
|
30 |
-
|
31 |
-
if ("0" == "mycustom"):
|
32 |
-
mybacklogmax = 10000
|
33 |
-
df = df.head(n=mybacklogmax)
|
34 |
-
st.dataframe(df)
|
35 |
-
|
36 |
-
if ("0" != "mycustom"):
|
37 |
-
myfields = [0, 1, 5, 7, 8, 10, 11]
|
38 |
-
myfields = [1]
|
39 |
-
mytitles = ["Date Time","p (mbar)","T (degC)","Tpot (K)","Tdew (degC)","rh (%)","VPmax (mbar)","VPact (mbar)","VPdef (mbar)","sh (g/kg)","H2OC (mmol/mol)","rho (g/m**3)","wv (m/s)","max. wv (m/s)","wd (deg)"]
|
40 |
-
|
41 |
-
mybacklogmax = 10
|
42 |
-
|
43 |
-
atoday = datetime.date.today()
|
44 |
-
|
45 |
-
ayear = int(atoday.strftime("%Y"))-0
|
46 |
-
amonth = int(atoday.strftime("%m"))
|
47 |
-
amonthday = int(atoday.strftime("%d"))
|
48 |
-
|
49 |
-
csvString = ""
|
50 |
-
csvString += (",").join(mytitles)
|
51 |
-
adf = pd.DataFrame(columns=mytitles)
|
52 |
-
for i in range((ayear-mybacklogmax),ayear,1):
|
53 |
-
alink = ("https://data.weather.gov.hk/weatherAPI/opendata/opendata.php?dataType=CLMTEMP&year={}&rformat=csv&station=HKO").format(str(i))
|
54 |
-
df = pd.read_csv(alink, skiprows=[0,1,2], skipfooter=3, engine='python', on_bad_lines='skip')
|
55 |
-
|
56 |
-
df = df.reset_index() # make sure indexes pair with number of rows
|
57 |
-
for index, row in df.iterrows():
|
58 |
-
if (row[2]!=amonth) or (row[3]!=amonthday):
|
59 |
-
continue
|
60 |
-
|
61 |
-
adate = ("{:02d}.{:02d}.{} 00:00:00").format(row[3], row[2], row[1])
|
62 |
-
csvString += '\n'+(",").join([adate,"",str(row[4]),"","","","","","","","","","","",""])
|
63 |
-
st.write(row[0],adate)
|
64 |
-
adf = adf.append({"Date Time":adate,"T (degC)":(row[4]),}, ignore_index=True)
|
65 |
-
break
|
66 |
-
adf = pd.read_csv(StringIO(csvString), sep=",")
|
67 |
-
df = adf
|
68 |
-
st.dataframe(df)
|
69 |
-
|
70 |
-
#%%
|
71 |
-
|
72 |
-
title = "Timeseries forecasting for weather prediction"
|
73 |
-
|
74 |
-
st.title('Timeseries forecasting for weather prediction')
|
75 |
-
|
76 |
-
st.write("Demonstrates how to do timeseries forecasting using a [LSTM model.](https://keras.io/api/layers/recurrent_layers/lstm/#lstm-class)This space demonstration is forecasting for weather prediction. *n* observation is selected from validation dataset." )
|
77 |
-
st.write("Keras example authors: [Prabhanshu Attri, Yashika Sharma, Kristi Takach, Falak Shah](https://keras.io/examples/timeseries/timeseries_weather_forecasting/)")
|
78 |
-
|
79 |
-
|
80 |
-
# %% model
|
81 |
-
|
82 |
-
titles = [
|
83 |
-
"Pressure",
|
84 |
-
"Temperature",
|
85 |
-
"Temperature in Kelvin",
|
86 |
-
"Temperature (dew point)",
|
87 |
-
"Relative Humidity",
|
88 |
-
"Saturation vapor pressure",
|
89 |
-
"Vapor pressure",
|
90 |
-
"Vapor pressure deficit",
|
91 |
-
"Specific humidity",
|
92 |
-
"Water vapor concentration",
|
93 |
-
"Airtight",
|
94 |
-
"Wind speed",
|
95 |
-
"Maximum wind speed",
|
96 |
-
"Wind direction in degrees",
|
97 |
-
]
|
98 |
-
|
99 |
-
feature_keys = [
|
100 |
-
"p (mbar)",
|
101 |
-
"T (degC)",
|
102 |
-
"Tpot (K)",
|
103 |
-
"Tdew (degC)",
|
104 |
-
"rh (%)",
|
105 |
-
"VPmax (mbar)",
|
106 |
-
"VPact (mbar)",
|
107 |
-
"VPdef (mbar)",
|
108 |
-
"sh (g/kg)",
|
109 |
-
"H2OC (mmol/mol)",
|
110 |
-
"rho (g/m**3)",
|
111 |
-
"wv (m/s)",
|
112 |
-
"max. wv (m/s)",
|
113 |
-
"wd (deg)",
|
114 |
]
|
115 |
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
past = 720
|
122 |
-
future = 72
|
123 |
-
learning_rate = 0.001
|
124 |
-
batch_size = 256
|
125 |
-
epochs = 10
|
126 |
-
|
127 |
-
if ("0" != "mycustom"):
|
128 |
-
past = 0
|
129 |
-
future = 1
|
130 |
-
batch_size = 1
|
131 |
-
step = 1
|
132 |
-
|
133 |
-
def normalize(data, train_split):
|
134 |
-
data_mean = data[:train_split].mean(axis=0)
|
135 |
-
data_std = data[:train_split].std(axis=0)
|
136 |
-
return (data - data_mean) / data_std
|
137 |
-
|
138 |
-
print(
|
139 |
-
"The selected parameters are:",
|
140 |
-
", ".join([titles[i] for i in [0, 1, 5, 7, 8, 10, 11]]),
|
141 |
-
)
|
142 |
-
selected_features = [feature_keys[i] for i in [0, 1, 5, 7, 8, 10, 11]]
|
143 |
-
|
144 |
-
if ("0" != "mycustom"):
|
145 |
-
print(
|
146 |
-
"The selected parameters are:",
|
147 |
-
", ".join([titles[i] for i in myfields]),
|
148 |
-
)
|
149 |
-
selected_features = [feature_keys[i] for i in myfields]
|
150 |
-
|
151 |
-
features = df[selected_features]
|
152 |
-
features.index = df[date_time_key]
|
153 |
-
features.head()
|
154 |
-
|
155 |
-
features = normalize(features.values, train_split)
|
156 |
-
features = pd.DataFrame(features)
|
157 |
-
features.head()
|
158 |
-
|
159 |
-
train_data = features.loc[0 : train_split - 1]
|
160 |
-
val_data = features.loc[train_split:]
|
161 |
-
|
162 |
-
|
163 |
-
split_fraction = 0.715
|
164 |
-
train_split = int(split_fraction * int(df.shape[0]))
|
165 |
-
step = 6
|
166 |
-
|
167 |
-
past = 720
|
168 |
-
future = 72
|
169 |
-
learning_rate = 0.001
|
170 |
-
batch_size = 256
|
171 |
-
epochs = 10
|
172 |
-
|
173 |
-
if ("0" != "mycustom"):
|
174 |
-
past = 0
|
175 |
-
future = 1
|
176 |
-
batch_size = 1
|
177 |
-
step = 1
|
178 |
-
|
179 |
-
def normalize(data, train_split):
|
180 |
-
data_mean = data[:train_split].mean(axis=0)
|
181 |
-
data_std = data[:train_split].std(axis=0)
|
182 |
-
return (data - data_mean) / data_std
|
183 |
-
print(
|
184 |
-
"The selected parameters are:",
|
185 |
-
", ".join([titles[i] for i in [0, 1, 5, 7, 8, 10, 11]]),
|
186 |
-
)
|
187 |
-
selected_features = [feature_keys[i] for i in [0, 1, 5, 7, 8, 10, 11]]
|
188 |
-
|
189 |
-
if ("0" != "mycustom"):
|
190 |
-
print(
|
191 |
-
"The selected parameters are:",
|
192 |
-
", ".join([titles[i] for i in myfields]),
|
193 |
-
)
|
194 |
-
selected_features = [feature_keys[i] for i in myfields]
|
195 |
-
|
196 |
-
features = df[selected_features]
|
197 |
-
features.index = df[date_time_key]
|
198 |
-
features.head()
|
199 |
-
|
200 |
-
features = normalize(features.values, train_split)
|
201 |
-
features = pd.DataFrame(features)
|
202 |
-
features.head()
|
203 |
-
|
204 |
-
train_data = features.loc[0 : train_split - 1]
|
205 |
-
val_data = features.loc[train_split:]
|
206 |
-
start = past + future
|
207 |
-
end = start + train_split
|
208 |
-
|
209 |
-
if ("0" == "mycustom"):
|
210 |
-
x_train = train_data[[i for i in range(7)]].values
|
211 |
-
y_train = features.iloc[start:end][[1]]
|
212 |
-
if ("0" != "mycustom"):
|
213 |
-
x_train = train_data[[i for i in range(len(myfields))]].values
|
214 |
-
y_train = features.iloc[start:end][[0]]
|
215 |
-
|
216 |
-
sequence_length = int(past / step)
|
217 |
-
x_end = len(val_data) - past - future
|
218 |
-
|
219 |
-
label_start = train_split + past + future
|
220 |
-
st.write(label_start)
|
221 |
-
|
222 |
-
if ("0" == "mycustom"):
|
223 |
-
x_val = val_data.iloc[:x_end][[i for i in range(7)]].values
|
224 |
-
y_val = features.iloc[label_start:][[1]]
|
225 |
-
if ("0" != "mycustom"):
|
226 |
-
x_val = val_data.iloc[:x_end][[i for i in range(len(myfields))]].values
|
227 |
-
y_val = features.iloc[label_start:][[0]]
|
228 |
-
|
229 |
-
dataset_val = keras.preprocessing.timeseries_dataset_from_array(
|
230 |
-
x_val,
|
231 |
-
y_val,
|
232 |
-
sequence_length=sequence_length,
|
233 |
-
sampling_rate=step,
|
234 |
-
batch_size=batch_size,
|
235 |
-
)
|
236 |
-
#%%
|
237 |
-
model = from_pretrained_keras("keras-io/timeseries_forecasting_for_weather")
|
238 |
-
|
239 |
-
#%%
|
240 |
-
st.set_option('deprecation.showPyplotGlobalUse', False)
|
241 |
-
def plot():
|
242 |
-
n = st.sidebar.slider("Step", min_value = 1, max_value=5, value = 1)
|
243 |
-
def show_plot(plot_data, delta, title):
|
244 |
-
labels = ["History", "True Future", "Model Prediction"]
|
245 |
-
marker = [".-", "rx", "go"]
|
246 |
-
time_steps = list(range(-(plot_data[0].shape[0]), 0))
|
247 |
-
if delta:
|
248 |
-
future = delta
|
249 |
-
else:
|
250 |
-
future = 0
|
251 |
-
|
252 |
-
plt.title(title)
|
253 |
-
for i, val in enumerate(plot_data):
|
254 |
-
if i:
|
255 |
-
plt.plot(future, plot_data[i], marker[i], markersize=10, label=labels[i])
|
256 |
-
else:
|
257 |
-
plt.plot(time_steps, plot_data[i].flatten(), marker[i], label=labels[i])
|
258 |
-
plt.legend(loc='lower center', bbox_to_anchor=(0.5, 1.05),
|
259 |
-
ncol=3, fancybox=True, shadow=True)
|
260 |
-
plt.xlim([time_steps[0], (future + 5) * 2])
|
261 |
-
plt.xlabel("Time-Step")
|
262 |
-
plt.show()
|
263 |
-
return
|
264 |
-
|
265 |
-
|
266 |
-
for x, y in dataset_val.take(n):
|
267 |
-
if ("0" == "mycustom"):
|
268 |
-
show_plot(
|
269 |
-
[x[0][:, 1].numpy(), y[0].numpy(), model.predict(x)[0]],
|
270 |
-
12,
|
271 |
-
f"{n} Step Prediction",
|
272 |
-
)
|
273 |
-
if ("0" != "mycustom"):
|
274 |
-
show_plot(
|
275 |
-
[x[0][:, 0].numpy(), y[0].numpy(), model.predict(x)[0]],
|
276 |
-
12,
|
277 |
-
f"{n} Step Prediction",
|
278 |
-
)
|
279 |
-
|
280 |
-
|
281 |
-
fig = plot()
|
282 |
-
st.pyplot(fig)
|
283 |
-
|
284 |
-
# %%
|
|
|
1 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import streamlit as st
|
|
|
|
|
3 |
|
4 |
+
astations = [
|
5 |
+
["hongkongobservatory","HKO",22.3022566,114.1722662,"Hong Kong Observatory"],
|
6 |
+
["kingspark","KP",22.3115408,114.1685675,"Observatory Meteorological Station, King's Park"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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