Spaces:
Running
Running
Commit ·
64e560c
1
Parent(s): 882717c
add county level us map
Browse files- app.py +41 -9
- counties.json +0 -0
- uscities.csv +0 -0
app.py
CHANGED
|
@@ -10,6 +10,7 @@ from google.analytics.data_v1beta.types import (
|
|
| 10 |
import gradio as gr
|
| 11 |
import os
|
| 12 |
import json
|
|
|
|
| 13 |
import pandas as pd
|
| 14 |
import plotly.express as px
|
| 15 |
|
|
@@ -33,6 +34,18 @@ iso['Alpha-3 code'] = iso['Alpha-3 code'].str.strip()
|
|
| 33 |
iso.set_index('Alpha-2 code', inplace=True)
|
| 34 |
alpha_2_map = iso['Alpha-3 code'].to_dict()
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def full_report():
|
| 37 |
client = BetaAnalyticsDataClient()
|
| 38 |
|
|
@@ -42,17 +55,15 @@ def full_report():
|
|
| 42 |
Dimension(name='eventName'),
|
| 43 |
Dimension(name="continent"),
|
| 44 |
Dimension(name="country"),
|
| 45 |
-
Dimension(name="countryId")
|
|
|
|
| 46 |
metrics=[Metric(name="eventValue")],
|
| 47 |
#return_property_quota=True,
|
| 48 |
date_ranges=[DateRange(start_date="2023-06-30", end_date="today")],
|
| 49 |
)
|
| 50 |
response = client.run_report(request)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
total_jumps = 0
|
| 55 |
-
res = {'day': [], 'jumps': [], 'continent': [], 'country': [], 'iso': []}
|
| 56 |
|
| 57 |
for row in response.rows:
|
| 58 |
event_name = row.dimension_values[1].value
|
|
@@ -61,18 +72,22 @@ def full_report():
|
|
| 61 |
continent = row.dimension_values[2].value
|
| 62 |
country = row.dimension_values[3].value
|
| 63 |
country_iso = row.dimension_values[4].value
|
|
|
|
| 64 |
event_value = float(row.metric_values[0].value)
|
| 65 |
-
total_jumps += int(event_value)
|
| 66 |
res['day'].append(day)
|
| 67 |
res['jumps'].append(event_value)
|
| 68 |
res['continent'].append(continent)
|
| 69 |
res['country'].append(country)
|
| 70 |
res['iso'].append(country_iso)
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
|
| 74 |
df = pd.DataFrame.from_dict(res)
|
|
|
|
|
|
|
| 75 |
df['iso'] = df['iso'].map(alpha_2_map)
|
|
|
|
| 76 |
country_df = df.groupby(['country', 'iso']).sum().reset_index()
|
| 77 |
country_df = country_df.sort_values(by=['jumps'], ascending=False)
|
| 78 |
|
|
@@ -96,8 +111,23 @@ def full_report():
|
|
| 96 |
template="plotly_dark")
|
| 97 |
# remove the legend
|
| 98 |
total_map.update_layout(showlegend=False)
|
|
|
|
| 99 |
total_map.update(layout_coloraxis_showscale=False)
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
df = df.groupby(['day', 'continent']).sum().reset_index()
|
| 102 |
df = df.sort_values(by=['day'])
|
| 103 |
df['total_jumps'] = df.groupby('continent')['jumps'].cumsum()
|
|
@@ -106,7 +136,7 @@ def full_report():
|
|
| 106 |
color='continent',
|
| 107 |
template="plotly_dark")
|
| 108 |
|
| 109 |
-
return f"## Total Jumps: {total_jumps:,}", total, avg, total_map, jumps_over_time
|
| 110 |
|
| 111 |
|
| 112 |
with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
|
|
@@ -114,6 +144,8 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
|
|
| 114 |
total_jumps_label = gr.Markdown("Total Jumps: 0")
|
| 115 |
with gr.Row():
|
| 116 |
map_fig = gr.Plot(label="Map")
|
|
|
|
|
|
|
| 117 |
with gr.Row():
|
| 118 |
jumps_over_time = gr.Plot(label="Jumps Over Time")
|
| 119 |
with gr.Row():
|
|
@@ -121,7 +153,7 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
|
|
| 121 |
with gr.Row():
|
| 122 |
avg_plot = gr.Plot(label="Average Jumps per Day")
|
| 123 |
|
| 124 |
-
outputs = [total_jumps_label, total_plot, avg_plot, map_fig, jumps_over_time]
|
| 125 |
dep = demo.load(full_report, None, outputs)
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
|
|
|
| 10 |
import gradio as gr
|
| 11 |
import os
|
| 12 |
import json
|
| 13 |
+
import time
|
| 14 |
import pandas as pd
|
| 15 |
import plotly.express as px
|
| 16 |
|
|
|
|
| 34 |
iso.set_index('Alpha-2 code', inplace=True)
|
| 35 |
alpha_2_map = iso['Alpha-3 code'].to_dict()
|
| 36 |
|
| 37 |
+
# read counties json file
|
| 38 |
+
with open('counties.json') as f:
|
| 39 |
+
counties = json.load(f)
|
| 40 |
+
|
| 41 |
+
cities = pd.read_csv('uscities.csv')
|
| 42 |
+
cities.set_index('city', inplace=True)
|
| 43 |
+
city_county_map = cities['county_fips'].to_dict()
|
| 44 |
+
city_county_name_map = cities['county_name'].to_dict()
|
| 45 |
+
|
| 46 |
+
cached_report = None
|
| 47 |
+
cache_time = 0
|
| 48 |
+
|
| 49 |
def full_report():
|
| 50 |
client = BetaAnalyticsDataClient()
|
| 51 |
|
|
|
|
| 55 |
Dimension(name='eventName'),
|
| 56 |
Dimension(name="continent"),
|
| 57 |
Dimension(name="country"),
|
| 58 |
+
Dimension(name="countryId"),
|
| 59 |
+
Dimension(name="city")],
|
| 60 |
metrics=[Metric(name="eventValue")],
|
| 61 |
#return_property_quota=True,
|
| 62 |
date_ranges=[DateRange(start_date="2023-06-30", end_date="today")],
|
| 63 |
)
|
| 64 |
response = client.run_report(request)
|
| 65 |
|
| 66 |
+
res = {'day': [], 'jumps': [], 'continent': [], 'country': [], 'iso': [], 'city': []}
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
for row in response.rows:
|
| 69 |
event_name = row.dimension_values[1].value
|
|
|
|
| 72 |
continent = row.dimension_values[2].value
|
| 73 |
country = row.dimension_values[3].value
|
| 74 |
country_iso = row.dimension_values[4].value
|
| 75 |
+
city = row.dimension_values[5].value
|
| 76 |
event_value = float(row.metric_values[0].value)
|
|
|
|
| 77 |
res['day'].append(day)
|
| 78 |
res['jumps'].append(event_value)
|
| 79 |
res['continent'].append(continent)
|
| 80 |
res['country'].append(country)
|
| 81 |
res['iso'].append(country_iso)
|
| 82 |
+
res['city'].append(city)
|
| 83 |
|
| 84 |
+
|
| 85 |
|
| 86 |
df = pd.DataFrame.from_dict(res)
|
| 87 |
+
total_jumps = df['jumps'].sum()
|
| 88 |
+
print(f"Total jumps: {total_jumps}")
|
| 89 |
df['iso'] = df['iso'].map(alpha_2_map)
|
| 90 |
+
|
| 91 |
country_df = df.groupby(['country', 'iso']).sum().reset_index()
|
| 92 |
country_df = country_df.sort_values(by=['jumps'], ascending=False)
|
| 93 |
|
|
|
|
| 111 |
template="plotly_dark")
|
| 112 |
# remove the legend
|
| 113 |
total_map.update_layout(showlegend=False)
|
| 114 |
+
total_map.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
|
| 115 |
total_map.update(layout_coloraxis_showscale=False)
|
| 116 |
|
| 117 |
+
county_df = df.copy()
|
| 118 |
+
county_df['county'] = county_df['city'].map(city_county_map)
|
| 119 |
+
county_df['count_name'] = county_df['city'].map(city_county_name_map)
|
| 120 |
+
county_df = county_df.groupby(['county', 'count_name']).sum().reset_index()
|
| 121 |
+
print(county_df)
|
| 122 |
+
|
| 123 |
+
county_map = px.choropleth(county_df, geojson=counties, locations='county', color='jumps',
|
| 124 |
+
color_continuous_scale="plasma",
|
| 125 |
+
scope="usa",
|
| 126 |
+
hover_name="count_name",
|
| 127 |
+
template="plotly_dark"
|
| 128 |
+
)
|
| 129 |
+
county_map.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
|
| 130 |
+
|
| 131 |
df = df.groupby(['day', 'continent']).sum().reset_index()
|
| 132 |
df = df.sort_values(by=['day'])
|
| 133 |
df['total_jumps'] = df.groupby('continent')['jumps'].cumsum()
|
|
|
|
| 136 |
color='continent',
|
| 137 |
template="plotly_dark")
|
| 138 |
|
| 139 |
+
return f"## Total Jumps: {total_jumps:,}", total, avg, total_map, jumps_over_time, county_map
|
| 140 |
|
| 141 |
|
| 142 |
with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
|
|
|
|
| 144 |
total_jumps_label = gr.Markdown("Total Jumps: 0")
|
| 145 |
with gr.Row():
|
| 146 |
map_fig = gr.Plot(label="Map")
|
| 147 |
+
with gr.Row():
|
| 148 |
+
county_map = gr.Plot(label="US Map")
|
| 149 |
with gr.Row():
|
| 150 |
jumps_over_time = gr.Plot(label="Jumps Over Time")
|
| 151 |
with gr.Row():
|
|
|
|
| 153 |
with gr.Row():
|
| 154 |
avg_plot = gr.Plot(label="Average Jumps per Day")
|
| 155 |
|
| 156 |
+
outputs = [total_jumps_label, total_plot, avg_plot, map_fig, jumps_over_time, county_map]
|
| 157 |
dep = demo.load(full_report, None, outputs)
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
counties.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
uscities.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|