a10 commited on
Commit
6e598b9
·
1 Parent(s): d218c5e

Update app.py

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