Update app.py
Browse files
app.py
CHANGED
|
@@ -1,147 +1,184 @@
|
|
| 1 |
-
|
| 2 |
-
import random
|
| 3 |
-
from typing import List, Tuple
|
| 4 |
-
|
| 5 |
-
import aiohttp
|
| 6 |
import panel as pn
|
| 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 |
-
outputs = model(**inputs)
|
| 54 |
-
logits_per_image = outputs.logits_per_image
|
| 55 |
-
class_likelihoods = logits_per_image.softmax(dim=1).detach().numpy()
|
| 56 |
-
return class_likelihoods[0]
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
async def process_inputs(class_names: List[str], image_url: str):
|
| 60 |
-
"""
|
| 61 |
-
High level function that takes in the user inputs and returns the
|
| 62 |
-
classification results as panel objects.
|
| 63 |
-
"""
|
| 64 |
-
try:
|
| 65 |
-
main.disabled = True
|
| 66 |
-
if not image_url:
|
| 67 |
-
yield "##### ⚠️ Provide an image URL"
|
| 68 |
-
return
|
| 69 |
|
| 70 |
-
|
| 71 |
-
try:
|
| 72 |
-
pil_img = await open_image_url(image_url)
|
| 73 |
-
img = pn.pane.Image(pil_img, height=400, align="center")
|
| 74 |
-
except Exception as e:
|
| 75 |
-
yield f"##### 😔 Something went wrong, please try a different URL!"
|
| 76 |
-
return
|
| 77 |
|
| 78 |
-
class_items = class_names.split(",")
|
| 79 |
-
class_likelihoods = get_similarity_scores(class_items, pil_img)
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
yield results
|
| 97 |
-
finally:
|
| 98 |
-
main.disabled = False
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# create widgets
|
| 102 |
-
randomize_url = pn.widgets.Button(name="Randomize URL", align="end")
|
| 103 |
-
|
| 104 |
-
image_url = pn.widgets.TextInput(
|
| 105 |
-
name="Image URL to classify",
|
| 106 |
-
value=pn.bind(random_url, randomize_url),
|
| 107 |
-
)
|
| 108 |
-
class_names = pn.widgets.TextInput(
|
| 109 |
-
name="Comma separated class names",
|
| 110 |
-
placeholder="Enter possible class names, e.g. cat, dog",
|
| 111 |
-
value="cat, dog, parrot",
|
| 112 |
-
)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
)
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
)
|
| 125 |
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
).servable(title=title)
|
|
|
|
| 1 |
+
# load up the libraries
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import panel as pn
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import altair as alt
|
| 5 |
+
import math
|
| 6 |
+
from vega_datasets import data
|
| 7 |
+
|
| 8 |
+
# Define functions
|
| 9 |
+
def plot_event_distribution(df, eventName):
|
| 10 |
+
time_labels = ['0-5', '5-10', '10-15', '15-20', '20-25', '25-30', '30-35', '35-40', '40-45', '45-50', '50-55', '55-60', '60-65', '65-70', '70-75', '75-80', '80-85', '85-90', '>90']
|
| 11 |
+
time_labels_plt = ['0-5', '5-10', '10-15', '15-20', '20-25', '25-30', '30-35', '35-40', '40-45', '>45', '45-50', '50-55', '55-60', '60-65', '65-70', '70-75', '75-80', '80-85', '85-90', '>90']
|
| 12 |
+
|
| 13 |
+
event_data = df[df['eventName'] == eventName].copy()
|
| 14 |
+
event_data['timeLabel'] = "0"
|
| 15 |
+
|
| 16 |
+
for index, row in event_data.iterrows():
|
| 17 |
+
minute = row['minute']
|
| 18 |
+
match_period = row['matchPeriod']
|
| 19 |
+
time_label = '1'
|
| 20 |
+
|
| 21 |
+
if minute > 45 and match_period == '1H':
|
| 22 |
+
time_label = '>45'
|
| 23 |
+
else:
|
| 24 |
+
left = math.floor(minute / 5)
|
| 25 |
+
if left < len(time_labels) - 1:
|
| 26 |
+
time_label = time_labels[left]
|
| 27 |
+
else:
|
| 28 |
+
time_label = '>90'
|
| 29 |
+
|
| 30 |
+
event_data.loc[index, 'timeLabel'] = time_label
|
| 31 |
+
|
| 32 |
+
return event_data
|
| 33 |
+
|
| 34 |
+
def create_event_distribution_df(event_dfs, event_names):
|
| 35 |
+
# 初始化一个空的DataFrame来存储结果
|
| 36 |
+
results_df = pd.DataFrame(columns=['TeamName', 'eventName', 'timeLabel', 'total_counts', 'matchPeriod'])
|
| 37 |
+
for event_df, event_name in zip(event_dfs, event_names):
|
| 38 |
+
group_counts = event_df.groupby(['TeamName', 'timeLabel', 'matchPeriod']).size().reset_index(name='total_counts')
|
| 39 |
+
group_counts['eventName'] = event_name
|
| 40 |
+
results_df = pd.concat([results_df, group_counts], ignore_index=True)
|
| 41 |
+
|
| 42 |
+
return results_df
|
| 43 |
+
|
| 44 |
+
def create_altair_chart(final_df, eventName, order, if_add_xticks=False):
|
| 45 |
+
|
| 46 |
+
selection_interval=alt.selection_interval(encodings=["x"])
|
| 47 |
+
|
| 48 |
+
mouse_hover = alt.selection_point(on="mouseover", empty=True)
|
| 49 |
+
|
| 50 |
+
color_encode = alt.Color('matchPeriod:N', title='', scale=alt.Scale(domain=['1H', '2H'], range=['rgb(76, 114, 176)', 'rgb(85, 168, 104)']))
|
| 51 |
+
|
| 52 |
+
# 这里我们用yellow card来举例
|
| 53 |
+
# 1. Base bar plot
|
| 54 |
+
base1 = alt.Chart(final_df[final_df['eventName'] == '{}'.format(eventName)]).encode(
|
| 55 |
+
x = alt.X('timeLabel:O', scale=alt.Scale(domain=order, paddingInner=0.2), axis=alt.Axis(grid=True, labels=False)),
|
| 56 |
+
y = alt.Y('sum(total_counts):Q', title='{} (n)'.format(eventName), axis=alt.Axis(tickCount=3, titleFontSize=24, labelFontSize=18)),
|
| 57 |
+
color = alt.condition(selection_interval,
|
| 58 |
+
color_encode,
|
| 59 |
+
alt.value("lightgray")),
|
| 60 |
+
opacity=alt.condition(mouse_hover, alt.value(1), alt.value(0.5))
|
| 61 |
+
).add_selection(
|
| 62 |
+
selection_interval,
|
| 63 |
+
mouse_hover
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
if if_add_xticks:
|
| 67 |
+
base1 = base1.encode(
|
| 68 |
+
x = alt.X('timeLabel:O', title='match time (min)' ,scale=alt.Scale(domain=order, paddingInner=0.2), axis=alt.Axis(grid=True, titleFontSize=24, labelFontSize=18)),
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
bar_chart_yellow1 = base1.mark_bar()
|
| 72 |
|
| 73 |
+
# 2. Add vertical line
|
| 74 |
+
vertical_line1 = alt.Chart(final_df[(final_df['eventName'] == '{}'.format(eventName)) & (final_df['timeLabel'] == ' ')]).encode(
|
| 75 |
+
# only show the vertical line at x == ' '
|
| 76 |
+
x = alt.X('timeLabel:O', scale=alt.Scale(domain=[' ']), title=''),
|
| 77 |
)
|
| 78 |
+
|
| 79 |
+
vertical_line1 = vertical_line1.mark_rule(color='orange', strokeWidth=2)
|
| 80 |
+
|
| 81 |
+
# 3. Add text
|
| 82 |
+
text_chart1 = alt.Chart(final_df[(final_df['eventName'] == '{}'.format(eventName)) & (final_df['timeLabel'] == ' ')]).encode(
|
| 83 |
+
# only show the vertical line at x == ' '
|
| 84 |
+
x = alt.X('timeLabel:O', scale=alt.Scale(domain=[' ']), title=''),
|
| 85 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
text_chart1 = text_chart1.mark_text(align='center', baseline='middle', fontSize=23, color='orange', dy=-100, text='Half Time', font='Arial')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# 4. Add the rank bar
|
| 91 |
+
bar_rank_yellow = alt.Chart(final_df[final_df['eventName'] == '{}'.format(eventName)]).transform_filter(selection_interval).transform_aggregate(
|
| 92 |
+
sum_total_counts='sum(total_counts)',
|
| 93 |
+
groupby=['TeamName']
|
| 94 |
+
).transform_window(
|
| 95 |
+
rank='rank(sum_total_counts)',
|
| 96 |
+
sort=[alt.SortField('sum_total_counts', order='descending')]
|
| 97 |
+
).transform_filter(
|
| 98 |
+
alt.datum.rank < 10 # Note: Change this to <= if you want to include the 10th position
|
| 99 |
+
).encode(
|
| 100 |
+
x=alt.X('sum_total_counts:Q', title='', axis=alt.Axis(labelFontSize=9)),
|
| 101 |
+
y=alt.Y('TeamName:N', sort='-x', title='Team Name', axis=alt.Axis(titleFontSize=24, labelFontSize=12, orient='right'))
|
| 102 |
+
)
|
| 103 |
|
| 104 |
+
if if_add_xticks:
|
| 105 |
+
bar_rank_yellow = bar_rank_yellow.encode(
|
| 106 |
+
x=alt.X('sum_total_counts:Q', title='Average {}'.format(eventName), axis=alt.Axis(titleFontSize=24,labelFontSize=9)),
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
bar_rank_yellow = bar_rank_yellow.mark_bar(color='orange').properties(width=300, height=250)
|
| 110 |
+
|
| 111 |
+
# 5. Combine all the charts
|
| 112 |
+
first = (bar_chart_yellow1 + vertical_line1 + text_chart1)
|
| 113 |
+
first = first.encode(tooltip=alt.Tooltip('sum(total_counts):Q', format='.0f'))
|
| 114 |
+
# bar_rank_yellow = bar_rank_yellow.encode(tooltip=alt.Tooltip('sum(total_counts):Q', format='.0f'))
|
| 115 |
+
yellow = first.properties(width=700, height=250) | bar_rank_yellow
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
return yellow
|
| 118 |
+
|
| 119 |
+
# we want to use bootstrap/template, tell Panel to load up what we need
|
| 120 |
+
pn.extension(design='bootstrap')
|
|
|
|
| 121 |
|
| 122 |
+
# we want to use vega, tell Panel to load up what we need
|
| 123 |
+
pn.extension('vega')
|
| 124 |
+
|
| 125 |
+
# create a basic template using bootstrap
|
| 126 |
+
template = pn.template.BootstrapTemplate(
|
| 127 |
+
title='SI649 Scientific Visualization Project',
|
| 128 |
)
|
| 129 |
|
| 130 |
+
# 0. the main column will hold our key content
|
| 131 |
+
maincol = pn.Column()
|
| 132 |
+
|
| 133 |
+
# 1. Load the Data
|
| 134 |
+
url = 'https://raw.githubusercontent.com/yanzhuo2001/SI_649_Projects/main/scientific%20viz%20project/final_data.csv'
|
| 135 |
+
df = pd.read_csv(url)
|
| 136 |
+
|
| 137 |
+
for index, row in df.iterrows():
|
| 138 |
+
minute = row['minute']
|
| 139 |
+
minute1 = math.floor(minute)
|
| 140 |
+
df.loc[index, 'minute1'] = minute1
|
| 141 |
+
|
| 142 |
+
YC_df = plot_event_distribution(df, 'Yellow_Card')
|
| 143 |
+
RC_df = plot_event_distribution(df, 'Red_Card')
|
| 144 |
+
Goal_df = plot_event_distribution(df, 'Goal')
|
| 145 |
+
|
| 146 |
+
event_dfs = [YC_df, RC_df, Goal_df]
|
| 147 |
+
event_names = ['Yellow_Card', 'Red_Card', 'Goal']
|
| 148 |
+
final_df = create_event_distribution_df(event_dfs, event_names)
|
| 149 |
+
final_df = final_df[final_df['matchPeriod'].isin(['1H', '2H'])]
|
| 150 |
+
|
| 151 |
+
for name in final_df['TeamName'].unique():
|
| 152 |
+
new = pd.DataFrame({
|
| 153 |
+
'eventName': ['Yellow_Card', 'Red_Card', 'Goal'],
|
| 154 |
+
'timeLabel': [' '] * 3,
|
| 155 |
+
'total_counts': [0] * 3,
|
| 156 |
+
'matchPeriod': ['1H']*3,
|
| 157 |
+
'TeamName': [name] *3
|
| 158 |
+
})
|
| 159 |
+
final_df = pd.concat([final_df, new], ignore_index=True)
|
| 160 |
+
|
| 161 |
+
order = ['0-5', '5-10', '10-15', '15-20', '20-25', '25-30', '30-35', '35-40', '40-45', '>45', ' ', '45-50', '50-55', '55-60', '60-65', '65-70', '70-75', '75-80', '80-85', '85-90', '>90']
|
| 162 |
+
|
| 163 |
+
yellow = create_altair_chart(final_df, 'Yellow_Card', order, if_add_xticks=False)
|
| 164 |
+
red = create_altair_chart(final_df, 'Red_Card', order, if_add_xticks=True)
|
| 165 |
+
goal = create_altair_chart(final_df, 'Goal', order, if_add_xticks=False)
|
| 166 |
+
|
| 167 |
+
final = (goal & yellow & red).configure_legend(
|
| 168 |
+
orient='top-left', # 图例位置在左上角
|
| 169 |
+
labelFontSize=18, # 图例标签的字体大小
|
| 170 |
+
symbolSize=250, # 图例符号的大小
|
| 171 |
+
fillColor='white', # 图例背景颜色
|
| 172 |
+
strokeWidth=2, # 图例边框粗细
|
| 173 |
+
padding=10, # 图例内的填充
|
| 174 |
+
).configure_view(
|
| 175 |
+
strokeWidth=1, # 图表边框粗细
|
| 176 |
+
stroke='black'
|
| 177 |
)
|
| 178 |
|
| 179 |
+
# 2. append the plot
|
| 180 |
+
maincol.append(final)
|
| 181 |
+
template.main.append(maincol)
|
| 182 |
+
|
| 183 |
+
# Indicate that the template object is the "application" and serve it
|
| 184 |
+
template.servable(title="SI649 Scientific Visualization Project")
|
|
|