-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappWardFinder.py
311 lines (219 loc) · 8.42 KB
/
appWardFinder.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import streamlit as st
import requests
import io
import pandas as pd
from io import BytesIO
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.cluster import DBSCAN
#this line must come first
st.set_page_config(layout="wide")
#colors used in plotting
colors = [
#'b',
#'g',
'r',
'c',
'm',
'y',
'darkorange',
'peru',
'gold',
'silver',
'indigo',
'navy',
'crimson',
'darkmagenta',
'lime'
]
@st.cache(persist=True)
def load_data():
"""
Loads data from public S3 bucket
"""
#read from S3
df_obs = pd.read_csv('https://nadim-kawwa-dota-bucket.s3.us-west-2.amazonaws.com/df_obs.csv')
df_sen = pd.read_csv('https://nadim-kawwa-dota-bucket.s3.us-west-2.amazonaws.com/df_sentry.csv')
#background image to be used
img_url = 'https://nadim-kawwa-dota-bucket.s3.us-west-2.amazonaws.com/map_detailed_723.jpeg'
img_response = requests.get(img_url)
img = Image.open(BytesIO(img_response.content))
#apply translation of coordinates
df_obs['x'] = df_obs['x'] - 64
df_obs['y'] = df_obs['y'] - 64
df_sen['x'] = df_sen['x'] - 64
df_sen['y'] = df_sen['y'] - 64
#convert time to minutes
df_obs['time'] = df_obs['time']/60
df_sen['time'] = df_sen['time']/60
return df_obs, df_sen, img
def getLabels(df, eps=3, min_samples=100):
"""
Returns the labels of a dataframe after dbscan clustering
Parameters
----------
df: Pandas dataframe
Data to be used for fitting. Must have columns 'x' and 'y'
eps : float, default=0.5
The maximum distance between two samples for one to be considered
as in the neighborhood of the other. This is not a maximum bound
on the distances of points within a cluster. This is the most
important DBSCAN parameter to choose appropriately for your data set
and distance function.
"""
#instantiate dbscan
db = DBSCAN(eps=eps,
min_samples=min_samples,
metric='euclidean',
n_jobs=-1
)
#fit and predict to data
db.fit_predict(df[['x', 'y']])
#Returns the sorted unique elements of an array
labels_unique = np.unique(db.labels_)
#drop the -1 labels which are unlabeled
labels_unique = labels_unique[labels_unique != -1]
return db.labels_, labels_unique
def populateSubPlot(df,
eps=3,
min_samples=50,
fig=None,
axs=None,
row=None,
col=None,
title='Some Ward',
img=Image.open('map_detailed_723.jpeg')
):
"""
Populates the subplots to show clustering for each kind
Parameters
----------
df: Pandas dataframe
Data to be used for fitting. Must have columns 'x' and 'y'
eps : float, default=0.5
The maximum distance between two samples for one to be considered
as in the neighborhood of the other. This is not a maximum bound
on the distances of points within a cluster. This is the most
important DBSCAN parameter to choose appropriately for your data set
and distance function.
axs: pyplot object, default=None
Axis of pyplot
fig: pyplot object, default=None
Figure of subplots
row: integer, default=None
Row of subplot to be filled
col: integer, default=None
Column of subplot to be filled
title: string, default=None
Title of subplot
img: string, default=presaved image
Image to be filled in background
"""
#assign labels to data and get unique labels
db_labels, unique_labels = getLabels(df,
eps=eps,
min_samples=min_samples)
#set the title
axs[row,col].set_title(title)
#slap image on background
axs[row,col].imshow(img, extent=[0, 128, 0, 128])
#for each label and color
for label, color in zip(unique_labels, colors):
#places where label matches
label_arg = np.argwhere(db_labels==label).ravel()
#reduced version of where labels occur
df_label_cluster = df.iloc[label_arg]
#add scatter to plot
axs[row,col].scatter(df_label_cluster['x'],
df_label_cluster['y'], label=str(label),
color=color)
def makeQuadSubplots(df_rad_obs,
df_dir_obs,
df_rad_sen,
df_dir_sen,
suptitle='Big title',
eps=3,
min_samples=50):
"""
Makes 4 subplots and fills each using data from the 4 dataframes.
"""
fig, axs = plt.subplots(2, 2,
figsize=(10,10)
)
fig.suptitle('Clustering Output', fontsize=20)
populateSubPlot(df=df_rad_obs,
eps=eps,
min_samples=min_samples,
fig=fig,
axs=axs,
row=0,
col=0, title='Obsever Wards Radiant')
populateSubPlot(df=df_dir_obs,
eps=eps,
min_samples=min_samples,
fig=fig,
axs=axs,
row=0,
col=1, title='Obsever Wards Dire')
populateSubPlot(df=df_rad_sen,
eps=eps,
min_samples=min_samples,
fig=fig,
axs=axs,
row=1,
col=0, title='Sentry Wards Radiant')
populateSubPlot(df=df_dir_sen,
eps=eps,
min_samples=min_samples,
fig=fig,
axs=axs,
row=1,
col=1, title='Sentry Wards Dire')
return fig, axs
def timeSeparation(df_rad_obs,
df_dir_obs,
df_rad_sen,
df_dir_sen,
t1=0,
t2=10):
df1 = df_rad_obs[(df_rad_obs['time']>t1) & (df_rad_obs['time']<=t2)]
df2 = df_dir_obs[(df_dir_obs['time']>t1) & (df_dir_obs['time']<=t2)]
df3 = df_rad_sen[(df_rad_sen['time']>t1) & (df_rad_sen['time']<=t2)]
df4 = df_dir_sen[(df_dir_sen['time']>t1) & (df_dir_sen['time']<=t2)]
return df1, df2, df3, df4
if __name__ == '__main__':
### BEGIN CALLING FUNCTIONS & WORK HERE ###
st.title('DOTA2 Ward Explorer')
st.subheader('An Interactive Tool To Explore Vision')
# ASK FOR USER INPUT #
eps_slide_value = st.slider(label = 'Select Range for Epsilon',
min_value=0.0,
max_value=5.0, value=2.0, step=0.5)
t1, t2 = st.slider(label = 'Select Time Frame',
min_value=-5,
max_value=100, value=(10, 20), step=1)
min_samples = st.slider(label = 'Select Minimum Samples Per Cluster',
min_value=0,
max_value=2_000, value=200, step=50)
# READ AND LOAD DATA#
df_obs, df_sen, img = load_data()
#separate by team
df_rad_obs = df_obs[(df_obs['is_radiant']==1)]
df_dir_obs = df_obs[(df_obs['is_radiant']==0)]
df_rad_sen = df_sen[(df_sen['is_radiant']==1)]
df_dir_sen = df_sen[(df_sen['is_radiant']==0)]
# APPLY USER INPUT #
df1, df2, df3, df4 = timeSeparation(df_rad_obs,
df_dir_obs,
df_rad_sen,
df_dir_sen,
t1=t1,
t2=t2)
fig, axs = makeQuadSubplots(df1,
df2,
df3,
df4,
eps=eps_slide_value,
min_samples=50)
st.pyplot(fig)