Update app.py
Browse files
app.py
CHANGED
@@ -1,125 +1,127 @@
|
|
1 |
-
import
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
import folium
|
5 |
-
import
|
6 |
-
from
|
7 |
-
import
|
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 |
-
# Load the model and make predictions
|
35 |
-
loaded_model = load_model()
|
36 |
-
predictions = loaded_model.predict(input_data_flat)
|
37 |
-
|
38 |
-
# Reshape the predictions back to (6, 4) format
|
39 |
-
predictions_reshaped = predictions.reshape(6, 4)
|
40 |
-
|
41 |
-
# Create a DataFrame for the predictions
|
42 |
-
columns = ['LAT', 'LON', 'DIST2LAND', 'STORM_SPEED']
|
43 |
-
df_predictions = pd.DataFrame(predictions_reshaped, columns=columns)
|
44 |
-
|
45 |
-
# Add the 'Hour' column, incrementing by 3 hours from the present time
|
46 |
-
df_predictions['Hour'] = [(hour_present + (i + 1) * 3) % 24 for i in range(6)] # Ensure the hour wraps around 24
|
47 |
-
|
48 |
return df_predictions
|
49 |
|
50 |
-
#
|
51 |
def plot_predictions_on_map(df_predictions):
|
52 |
-
# Extract LAT and LON from the predictions
|
53 |
latitudes = df_predictions['LAT'].tolist()
|
54 |
longitudes = df_predictions['LON'].tolist()
|
55 |
|
56 |
-
# Create a folium map centered at the first predicted point
|
57 |
m = folium.Map(location=[latitudes[0], longitudes[0]], zoom_start=6)
|
58 |
-
|
59 |
-
# Add the predicted points to the map and connect them with a polyline
|
60 |
locations = list(zip(latitudes, longitudes))
|
61 |
|
62 |
-
# Add the points to the map
|
63 |
for lat, lon in locations:
|
64 |
folium.Marker([lat, lon]).add_to(m)
|
65 |
|
66 |
-
# Add a polyline to connect the points
|
67 |
folium.PolyLine(locations, color='blue', weight=2.5, opacity=0.7).add_to(m)
|
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 |
-
predict_button.click(
|
118 |
-
main_interface,
|
119 |
-
inputs=[lat_present, lon_present, dist2land_present, storm_speed_present,
|
120 |
-
year_present, month_present, day_present, hour_present,
|
121 |
-
lat_prev, lon_prev, dist2land_prev, storm_speed_prev],
|
122 |
-
outputs=[prediction_output, map_output]
|
123 |
-
)
|
124 |
-
|
125 |
-
cyclone_predictor.launch()
|
|
|
1 |
+
import joblib
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
import folium
|
5 |
+
import streamlit as st
|
6 |
+
from streamlit_folium import folium_static
|
7 |
+
import warnings
|
8 |
+
warnings.filterwarnings("ignore")
|
9 |
+
|
10 |
+
# Define model paths
|
11 |
+
model_paths = {
|
12 |
+
'Path': {
|
13 |
+
'3 hours': 'lr_3H_lat_lon.pkl',
|
14 |
+
'6 hours': 'lr_6H_lat_lon.pkl',
|
15 |
+
'9 hours': 'lr_9H_lat_lon.pkl',
|
16 |
+
'12 hours': 'lr_12H_lat_lon.pkl',
|
17 |
+
'15 hours': 'lr_15H_lat_lon.pkl',
|
18 |
+
'18 hours': 'lr_18H_lat_lon.pkl',
|
19 |
+
'21 hours': 'lr_21H_lat_lon.pkl',
|
20 |
+
'24 hours': 'lr_24H_lat_lon.pkl',
|
21 |
+
'27 hours': 'lr_27H_lat_lon.pkl',
|
22 |
+
'30 hours': 'lr_30H_lat_lon.pkl',
|
23 |
+
'33 hours': 'lr_33H_lat_lon.pkl',
|
24 |
+
'36 hours': 'lr_36H_lat_lon.pkl'
|
25 |
+
}
|
26 |
+
}
|
27 |
+
|
28 |
+
# Define scaler paths
|
29 |
+
scaler_paths = {
|
30 |
+
'Path': {
|
31 |
+
'3 hours': 'lr_3H_lat_lon_scaler.pkl',
|
32 |
+
'6 hours': 'lr_6H_lat_lon_scaler.pkl',
|
33 |
+
'9 hours': 'lr_9H_lat_lon_scaler.pkl',
|
34 |
+
'12 hours': 'lr_12H_lat_lon_scaler.pkl',
|
35 |
+
'15 hours': 'lr_15H_lat_lon_scaler.pkl',
|
36 |
+
'18 hours': 'lr_18H_lat_lon_scaler.pkl',
|
37 |
+
'24 hours': 'lr_24H_lat_lon_scaler.pkl',
|
38 |
+
'27 hours': 'lr_27H_lat_lon_scaler.pkl',
|
39 |
+
'30 hours': 'lr_30H_lat_lon_scaler.pkl',
|
40 |
+
'33 hours': 'lr_33H_lat_lon_scaler.pkl',
|
41 |
+
'36 hours': 'lr_36H_lat_lon_scaler.pkl'
|
42 |
+
}
|
43 |
+
}
|
44 |
+
|
45 |
+
# Load model and scaler based on time interval
|
46 |
+
def load_model(time_interval):
|
47 |
+
model = joblib.load(model_paths['Path'][time_interval])
|
48 |
+
scaler = joblib.load(scaler_paths['Path'][time_interval])
|
49 |
+
return model, scaler
|
50 |
+
|
51 |
+
def process_input(input_data, scaler):
|
52 |
+
input_data = np.array(input_data).reshape(-1, 7)
|
53 |
+
processed_data = input_data[:2].reshape(1, -1)
|
54 |
+
processed_data = scaler.transform(processed_data)
|
55 |
+
return processed_data
|
56 |
+
|
57 |
+
def predict_path(time_interval, input_data):
|
58 |
+
model, scaler = load_model(time_interval)
|
59 |
+
processed_data = process_input(input_data, scaler)
|
60 |
+
prediction = model.predict(processed_data)
|
61 |
|
62 |
+
# Create DataFrame for predictions
|
63 |
+
df_predictions = pd.DataFrame(prediction, columns=['LAT', 'LON'])
|
64 |
+
df_predictions['Time'] = [time_interval]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
return df_predictions
|
66 |
|
67 |
+
# Function to plot predictions on a folium map and return the HTML representation
|
68 |
def plot_predictions_on_map(df_predictions):
|
|
|
69 |
latitudes = df_predictions['LAT'].tolist()
|
70 |
longitudes = df_predictions['LON'].tolist()
|
71 |
|
|
|
72 |
m = folium.Map(location=[latitudes[0], longitudes[0]], zoom_start=6)
|
|
|
|
|
73 |
locations = list(zip(latitudes, longitudes))
|
74 |
|
|
|
75 |
for lat, lon in locations:
|
76 |
folium.Marker([lat, lon]).add_to(m)
|
77 |
|
|
|
78 |
folium.PolyLine(locations, color='blue', weight=2.5, opacity=0.7).add_to(m)
|
79 |
+
return m
|
80 |
+
|
81 |
+
# Streamlit App
|
82 |
+
def main():
|
83 |
+
st.title("Cyclone Path Prediction")
|
84 |
+
st.write("Input current and previous cyclone data to predict the path and visualize it on a map.")
|
85 |
+
|
86 |
+
# User inputs
|
87 |
+
time_interval = st.selectbox("Select Prediction Time Interval", [
|
88 |
+
'3 hours', '6 hours', '9 hours', '12 hours', '15 hours', '18 hours',
|
89 |
+
'21 hours', '24 hours', '27 hours', '30 hours', '33 hours', '36 hours'
|
90 |
+
])
|
91 |
+
|
92 |
+
previous_lat = st.number_input("Previous Latitude", format="%f")
|
93 |
+
previous_lon = st.number_input("Previous Longitude", format="%f")
|
94 |
+
previous_speed = st.number_input("Previous Speed", format="%f")
|
95 |
+
previous_year = st.number_input("Previous Year", format="%d")
|
96 |
+
previous_month = st.number_input("Previous Month", format="%d")
|
97 |
+
previous_day = st.number_input("Previous Day", format="%d")
|
98 |
+
previous_hour = st.number_input("Previous Hour", format="%d")
|
99 |
+
|
100 |
+
present_lat = st.number_input("Present Latitude", format="%f")
|
101 |
+
present_lon = st.number_input("Present Longitude", format="%f")
|
102 |
+
present_speed = st.number_input("Present Speed", format="%f")
|
103 |
+
present_year = st.number_input("Present Year", format="%d")
|
104 |
+
present_month = st.number_input("Present Month", format="%d")
|
105 |
+
present_day = st.number_input("Present Day", format="%d")
|
106 |
+
present_hour = st.number_input("Present Hour", format="%d")
|
107 |
+
|
108 |
+
if st.button("Predict"):
|
109 |
+
# Process input into array format
|
110 |
+
previous_data = [previous_lat, previous_lon, previous_speed, previous_year, previous_month, previous_day, previous_hour]
|
111 |
+
present_data = [present_lat, present_lon, present_speed, present_year, present_month, present_day, present_hour]
|
112 |
+
input_data = [previous_data, present_data]
|
113 |
+
|
114 |
+
# Predict path
|
115 |
+
df_predictions = predict_path(time_interval, input_data)
|
116 |
+
|
117 |
+
# Display predicted path DataFrame
|
118 |
+
st.write("Predicted Path DataFrame:")
|
119 |
+
st.write(df_predictions)
|
120 |
+
|
121 |
+
# Plot map with predictions
|
122 |
+
st.write("Cyclone Path Map:")
|
123 |
+
map_ = plot_predictions_on_map(df_predictions)
|
124 |
+
folium_static(map_)
|
125 |
+
|
126 |
+
if __name__ == "__main__":
|
127 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|