Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from turtle import title
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from sklearn.datasets import fetch_species_distributions
|
5 |
+
from sklearn.neighbors import KernelDensity
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
def construct_grids(batch):
|
9 |
+
xmin = batch.x_left_lower_corner + batch.grid_size
|
10 |
+
xmax = xmin + (batch.Nx * batch.grid_size)
|
11 |
+
ymin = batch.y_left_lower_corner + batch.grid_size
|
12 |
+
ymax = ymin + (batch.Ny * batch.grid_size)
|
13 |
+
xgrid = np.arange(xmin, xmax, batch.grid_size)
|
14 |
+
ygrid = np.arange(ymin, ymax, batch.grid_size)
|
15 |
+
return (xgrid, ygrid)
|
16 |
+
|
17 |
+
def plot_species_distributions(bandwidth):
|
18 |
+
data = fetch_species_distributions()
|
19 |
+
species_names = ["Bradypus Variegatus", "Microryzomys Minutus"]
|
20 |
+
Xtrain = np.vstack([data["train"]["dd lat"], data["train"]["dd long"]]).T
|
21 |
+
ytrain = np.array(
|
22 |
+
[d.decode("ascii").startswith("micro") for d in data["train"]["species"]],
|
23 |
+
dtype="int",
|
24 |
+
)
|
25 |
+
Xtrain *= np.pi / 180.0
|
26 |
+
|
27 |
+
xgrid, ygrid = construct_grids(data)
|
28 |
+
X, Y = np.meshgrid(xgrid[::5], ygrid[::5][::-1])
|
29 |
+
land_reference = data.coverages[6][::5, ::5]
|
30 |
+
land_mask = (land_reference > -9999).ravel()
|
31 |
+
|
32 |
+
xy = np.vstack([Y.ravel(), X.ravel()]).T
|
33 |
+
xy = xy[land_mask]
|
34 |
+
xy *= np.pi / 180.0
|
35 |
+
|
36 |
+
fig = plt.figure()
|
37 |
+
fig.subplots_adjust(left=0.05, right=0.95, wspace=0.05)
|
38 |
+
|
39 |
+
for i in range(2):
|
40 |
+
plt.subplot(1, 2, i + 1)
|
41 |
+
print(" - computing KDE in spherical coordinates")
|
42 |
+
kde = KernelDensity(
|
43 |
+
bandwidth=bandwidth, metric="haversine", kernel="gaussian", algorithm="ball_tree"
|
44 |
+
)
|
45 |
+
kde.fit(Xtrain[ytrain == i])
|
46 |
+
Z = np.full(land_mask.shape[0], -9999, dtype="int")
|
47 |
+
Z[land_mask] = np.exp(kde.score_samples(xy))
|
48 |
+
Z = Z.reshape(X.shape)
|
49 |
+
levels = np.linspace(0, Z.max(), 25)
|
50 |
+
plt.contourf(X, Y, Z, levels=levels, cmap=plt.cm.Reds)
|
51 |
+
plt.contour(
|
52 |
+
X, Y, land_reference, levels=[-9998], colors="k", linestyles="solid"
|
53 |
+
)
|
54 |
+
plt.xticks([])
|
55 |
+
plt.yticks([])
|
56 |
+
plt.title(species_names[i])
|
57 |
+
|
58 |
+
return plt
|
59 |
+
|
60 |
+
bandwidth_input = gr.inputs.Slider(minimum=0.01, maximum=0.3, default=0.01, step=0.01, label="Bandwidth")
|
61 |
+
title="Kernel Density Estimate of Species Distributions"
|
62 |
+
description="This shows an example of a neighbors-based query (in particular a kernel density estimate) on geospatial data, using a Ball Tree built upon the Haversine distance metric – i.e. distances over points in latitude/longitude. The dataset is provided by Phillips et. al. (2006). If available, the example uses basemap to plot the coast lines and national boundaries of South America. See the original scikit-learn example here: https://scikit-learn.org/stable/auto_examples/neighbors/plot_species_kde.html"
|
63 |
+
iface = gr.Interface(fn=plot_species_distributions, title = title, description=description, inputs=bandwidth_input, outputs="plot")
|
64 |
+
iface.launch()
|