2022-09-15 23:24:10 +08:00
|
|
|
import gradio as gr
|
2022-01-21 21:44:12 +08:00
|
|
|
from math import sqrt
|
2022-04-19 03:26:30 +08:00
|
|
|
import matplotlib
|
2022-10-13 00:30:42 +08:00
|
|
|
|
|
|
|
matplotlib.use("Agg")
|
|
|
|
|
2021-05-24 23:31:44 +08:00
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import numpy as np
|
2022-04-19 03:26:30 +08:00
|
|
|
import plotly.express as px
|
|
|
|
import pandas as pd
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2021-05-24 23:31:44 +08:00
|
|
|
|
2022-04-19 03:26:30 +08:00
|
|
|
def outbreak(plot_type, r, month, countries, social_distancing):
|
2021-05-24 23:31:44 +08:00
|
|
|
months = ["January", "February", "March", "April", "May"]
|
|
|
|
m = months.index(month)
|
|
|
|
start_day = 30 * m
|
|
|
|
final_day = 30 * (m + 1)
|
2022-01-21 21:44:12 +08:00
|
|
|
x = np.arange(start_day, final_day + 1)
|
2021-05-24 23:31:44 +08:00
|
|
|
pop_count = {"USA": 350, "Canada": 40, "Mexico": 300, "UK": 120}
|
|
|
|
if social_distancing:
|
|
|
|
r = sqrt(r)
|
2022-10-13 00:30:42 +08:00
|
|
|
df = pd.DataFrame({"day": x})
|
2022-04-19 03:26:30 +08:00
|
|
|
for country in countries:
|
2022-10-13 00:30:42 +08:00
|
|
|
df[country] = x ** (r) * (pop_count[country] + 1)
|
2022-04-19 03:26:30 +08:00
|
|
|
|
|
|
|
if plot_type == "Matplotlib":
|
|
|
|
fig = plt.figure()
|
2022-10-13 00:30:42 +08:00
|
|
|
plt.plot(df["day"], df[countries].to_numpy())
|
2022-04-19 03:26:30 +08:00
|
|
|
plt.title("Outbreak in " + month)
|
|
|
|
plt.ylabel("Cases")
|
|
|
|
plt.xlabel("Days since Day 0")
|
|
|
|
plt.legend(countries)
|
|
|
|
return fig
|
|
|
|
elif plot_type == "Plotly":
|
2022-10-13 00:30:42 +08:00
|
|
|
fig = px.line(df, x="day", y=countries)
|
|
|
|
fig.update_layout(
|
|
|
|
title="Outbreak in " + month,
|
|
|
|
xaxis_title="Cases",
|
|
|
|
yaxis_title="Days Since Day 0",
|
|
|
|
)
|
2022-04-19 03:26:30 +08:00
|
|
|
return fig
|
|
|
|
else:
|
2022-08-23 23:31:04 +08:00
|
|
|
raise ValueError("A plot type must be selected")
|
2022-04-19 03:26:30 +08:00
|
|
|
|
2022-10-13 00:30:42 +08:00
|
|
|
|
2022-04-19 03:26:30 +08:00
|
|
|
inputs = [
|
2022-10-13 00:30:42 +08:00
|
|
|
gr.Dropdown(["Matplotlib", "Plotly"], label="Plot Type"),
|
|
|
|
gr.Slider(1, 4, 3.2, label="R"),
|
|
|
|
gr.Dropdown(["January", "February", "March", "April", "May"], label="Month"),
|
|
|
|
gr.CheckboxGroup(
|
|
|
|
["USA", "Canada", "Mexico", "UK"], label="Countries", value=["USA", "Canada"]
|
|
|
|
),
|
|
|
|
gr.Checkbox(label="Social Distancing?"),
|
|
|
|
]
|
2022-05-14 13:45:44 +08:00
|
|
|
outputs = gr.Plot()
|
2022-04-19 03:26:30 +08:00
|
|
|
|
2022-10-13 00:30:42 +08:00
|
|
|
demo = gr.Interface(
|
|
|
|
fn=outbreak,
|
|
|
|
inputs=inputs,
|
|
|
|
outputs=outputs,
|
|
|
|
examples=[
|
2022-06-21 02:13:35 +08:00
|
|
|
["Matplotlib", 2, "March", ["Mexico", "UK"], True],
|
|
|
|
["Plotly", 3.6, "February", ["Canada", "Mexico", "UK"], False],
|
2022-10-13 00:30:42 +08:00
|
|
|
],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
2022-04-19 03:26:30 +08:00
|
|
|
|
2022-10-13 00:30:42 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
demo.launch()
|