File size: 2,813 Bytes
068afd6
1fb31ed
1c25f86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fb31ed
 
1c25f86
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
import pickle
import gradio as gr
import geopandas as gpd
from shapely.geometry import Point
import matplotlib.pyplot as plt
import numpy as np
with open("earthquake_model.pkl", 'rb') as file:
    model = pickle.load(file)
def time2num(x):
    try:
        (h, m, s) = str(x).split(':')
        result = int(h) * 3600 + int(m) * 60 + int(s)
        return result
    except:
        return 0

def date2num(x):
    try:
        (m, d, y) = str(x).split("/")
        result = int(y) * 365 + int(m) * 30 + int(d)
        return result
    except:
        return 0

def datetime2num(date_str, time_str):
    date_value = date2num(date_str)
    time_value = time2num(time_str)
    return date_value ,time_value
def test(model, date_str, time_str,all_points):
    data_list = []
    for lat, lon in all_points:
        date, time = datetime2num(date_str, time_str)
        data_list.append([date, time, lat, lon])
    np_array = np.array(data_list)
    res = model.predict_proba(np_array)
    return res
def create_geodf(all_points, model, date_str, time_str):
    res = test(model, date_str, time_str,all_points)

    data_list = []
    for lat, lon in all_points:
        date, time = datetime2num(date_str, time_str)
        data_list.append([date, time, lat, lon])
    np_array = np.array(data_list)

    df = pd.DataFrame(np_array, columns=['Date', 'Time', 'Latitude', 'Longitude'])
    df['Probability_2'] = [i[1] for i in res]
    df['geometry'] = [Point(lon, lat) for lat, lon in zip(df['Latitude'], df['Longitude'])]

    crs = "EPSG:4326"
    gdf = gpd.GeoDataFrame(df, crs=crs, geometry='geometry')

    return gdf

def plot_func(date_str, time_str):
    min_latitude = -90
    max_latitude = 90
    latitude_step = 1
    min_longitude = -180
    max_longitude = 180
    longitude_step = 1
    latitudes = np.arange(min_latitude, max_latitude + latitude_step, latitude_step)
    longitudes = np.arange(min_longitude, max_longitude + longitude_step, longitude_step)
    all_points = np.array(np.meshgrid(latitudes, longitudes)).T.reshape(-1, 2)
    gdf = create_geodf(all_points, model, date_str, time_str)

    top = gdf.nlargest(100, 'Probability_2')
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
    fig, ax = plt.subplots(figsize=(12, 12))
    ax.imshow(np.ones((180, 360)), cmap='gray', extent=[-180, 180, -90, 90])
    world.plot(ax=ax, color='lightgray', edgecolor='black')
    top.plot(ax=ax, markersize=50, color='red', legend=True, alpha=0.5)

    plt.xlabel('Longitude')
    plt.ylabel('Latitude')
    plt.title('Possible Earthquake Map')
    plt.grid(True)

    return plt.gcf()

inputs = [gr.inputs.Textbox(label="Date: (MM/DD/YYYY)"), gr.inputs.Textbox(label="Time: (HH:MM:SS) GMT-4")]


gr.Interface(fn=plot_func, inputs=inputs, outputs="plot",debugging=True).launch()