-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusters.py
429 lines (304 loc) · 10.2 KB
/
clusters.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import json
from hearthstone.enums import CardClass
from copy import deepcopy #we want new values not reference values on copy
from datetime import date
import matplotlib.pyplot as plt
import csv
from deckVector import *
from deckWrapper import *
from cardDB import *
from getClusterCounts import *
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import os
from tfKMeans import *
from sklearn.preprocessing import RobustScaler
global CLUSTER_NUMBERS
CLASSES=["DEMONHUNTER", 'DRUID', 'HUNTER', 'MAGE', 'PALADIN', 'PRIEST', 'ROGUE', 'SHAMAN', 'WARLOCK', 'WARRIOR']
db = card_db()
#Clustering process wants a more precise log for debug purposes
# Copied from example at @ https://docs.python.org/3/howto/logging.html
import logging
#create logger
logging.basicConfig(
level=logging.INFO,
format= '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
datefmt='%H:%M:%S'
)
formatter = logging.Formatter('%(levelname)s %(asctime)s:\t %(message)s',
datefmt='%m/%d/%Y%I:%M:%S %p')
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(formatter)
logger = logging.getLogger('clustering')
logger.info("START NEW\n--------------------------------------------------------------------------")
"""
cluster of decks
"""
class Cluster:
# Initializer, defines no Factory style to use
def __init__(self, *args, **kwargs):
self._factory = None
self._superCluster = None
#Function to Create New Cluster with variable Factory function
@staticmethod
def create(factory, superCluster, clusterID, decks, name="NEW"):
self = factory()
self._factor = factory
self._superCluster = superCluster
self.clusterID = clusterID
self.decks = decks or []
self.name = name
#initialize decks to this cluster
self._initializeDecks()
return self
#Assign each deck to this cluster on creation
def _initializeDecks(self):
for deck in self.decks:
deck.clusterID = self.clusterID
deck.classification = self.name
def getCount(self):
return len(self.decks)
def __str__(self):
clustID = self.clusterID
clustName = self.name
return("Cluster Id: {} Name: {}".format(clustID, clustName))
def __repr__(self):
return str(self)
def updateNames(self):
for deck in self.decks:
#print(self.name)
deck.classification = "{} {}".format(self.name, deck.ingameClass)
#print(deck.classification)
# Collection of clusters sorted by class
class ClassCluster:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._factory = None
self._superCluster = None
#create function
@staticmethod
def create(factory, superCluster, inGameClass, clusters):
self = factory()
self._factory = factory
self._superCluster = superCluster
self.inGameClass = inGameClass
self.clusters = clusters
return self
#string conversion
def __str__(self):
return "{} Set, contains {} clusters".format(self.inGameClass, len(self.clusters))
def __repr__(self):
return str(self)
@property
def getInGameClass(self):
return CardClass(self.inGameClass).name
# Function to convert to a dictionary for input usage
def convertToDict(self):
for cluster in self.clusters:
yield(cluster.clusterID, cluster.decks)
plt.ion()
#Collection of ClassClusters
#Used to save a configuration for later Classification
class SuperCluster:
CLASS_FACTORY = ClassCluster
CLUSTER_FACTORY = Cluster
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._factory = None
def __str__(self):
result = "Cluster Set: "
myCCs = self.myClassClusters
for classCluster in myCCs:
result += "\n{}".format(str(classCluster))
return result
def __repr__(self):
return str(self)
def getClassClusterByName(self, gameClassName):
myClass = int(CardClass[gameClassName])
#print(myClass)
#print(self.myClassClusters)
for cc in self.myClassClusters:
#print(cc.getInGameClass)
if cc.inGameClass == myClass:
return cc
print("FAIL")
# Function to convert to a dictionary for input usage
def convertToDict(self):
for classCluster in self.myClassClusters:
yield(classCluster.inGameClass, classCluster.clusters)
def chartifyData(self, theDateUpdated=""):
result = []
i = 1
for aCC in self.myClassClusters:
plt.figure(i)
myXs = []
myYs = []
myLabels = []
clusters= {}
for cluster in aCC.clusters:
if cluster.name not in clusters:
clusters[cluster.name] = []
for dp in cluster.decks:
myXs.append(dp["x"])
myYs.append(dp["y"])
myLabels.append(dp.classification)
clusters[cluster.name].append(tuple((dp["x"], dp["y"])))
#print(clusters)
for c in clusters:
first = [t[0] for t in clusters[c]]
second = [t[1] for t in clusters[c]]
plt.scatter(first, second, label=c)
plt.title(CardClass(aCC.inGameClass).name)
plt.ylabel("y")
plt.xlabel("x")
plt.legend()
i+=1
result.append(tuple((myXs, myYs, myLabels)))
plt.show()
return result
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import RobustScaler
def rescaleVector(myCC):
CLUSTER_NUMBERS = clusterNumbers
aCC = mySuperCluster.getClassClusterByName(hero)
X=[]
reducedSetVector = getReducedSetVector(hero=hero)
for cluster in aCC.clusters:
for dp in cluster.decks:
vector = []
#add all vectors for comparisons
cards = dp.deck.cards
cardDict = defaultdict(int)
for (i,j) in cards:
cardDict[i] = j
#Check if card is present in deck
vector = [float(cardDict.get(dbId, 0)) / 30 for dbId in reducedSetVector]
manaVector = (getManaCurveVector(dp))
vector.extend(manaVector)
cardTypeVector = getCardTypeVector(dp)
vector.extend(cardTypeVector)
keyWordVector = getKeyWordVector(dp)
vector.extend(keyWordVector)
classNeutralVector = getClassNeutralVector(dp)
vector.extend(classNeutralVector)
cardSetVector = getCardSetVector(dp)
vector.extend(cardSetVector)
for i in range(0,100):
vector.append(np.array(isHighlander(dp)))
vector = np.array(vector)
#We want highlander to be very important so it gets more than one spot
X.append(vector)
#if hero == "MAGE":
#print(vector)
#if len(vector) != 2018:
#print(dp.teamName)
#print(len(vector))
#print(X)
X = np.array(X, dtype=float)
X = StandardScaler().fit_transform(X)
return X
from updateWindow import *
def createSuperCluster(inData, scFact=SuperCluster, clusterNumbers=[3,3,3,3,3,3,3,3,3,3], window=None):
from sklearn import manifold
from sklearn.cluster import KMeans
windowUpdate = True
if window == None:
windowUpdate=False
superCluster = scFact()
superCluster._factory= scFact
# deep copy because we need new instances
data = deepcopy(inData)
clusterCountMover = 0;
classClusters = []
CLUSTER_NUMBERS = clusterNumbers
for hero, dataPoints in zip(CLASSES, data):
logger.info("Start Clustering for: {}".format(hero))
X = []
if windowUpdate:
updateTextWindow(window, "Clustering {} Decks".format(hero))
#fix weird bug where DH is only class not displaying
if hero == "DEMONHUNTER":
updateTextWindow(window, "Clustering DH Decks".format(hero))
#Generate Vectors used in Classifications
reducedSetVector = getReducedSetVector(hero=hero)
logger.info("Base Cluster Length: %s" % len(reducedSetVector))
for dp in dataPoints:
#add all vectors for comparisons
cards = dp.deck.cards
cardDict = defaultdict(int)
for (i,j) in cards:
cardDict[i] = j
#Check if card is present in deck
vector = [float(cardDict.get(dbId, 0)) / 30 for dbId in reducedSetVector]
manaVector = (getManaCurveVector(dp))
vector.extend(manaVector)
cardTypeVector = getCardTypeVector(dp)
vector.extend(cardTypeVector)
keyWordVector = getKeyWordVector(dp)
vector.extend(keyWordVector)
classNeutralVector = getClassNeutralVector(dp)
vector.extend(classNeutralVector)
cardSetVector = getCardSetVector(dp)
vector.extend(cardSetVector)
vector = np.array(vector)
#We want highlander to be very important so it gets more than one spot
X.append(vector)
#if hero == "MAGE":
#print(vector)
#if len(vector) != 2018:
#print(dp.teamName)
#print(len(vector))
#print(X)
X = np.array(X, dtype=float)
logger.info("Full Feature Vector Length: %s" % len(X[0]))
#do machine learning
# Use TSNE to help visualize the high dimensonal data
if len(dataPoints) > 1:
tsne = manifold.TSNE(n_components=2, init='pca', random_state=0)
xy = tsne.fit_transform(deepcopy(X))
for (x,y), dp in zip(xy, dataPoints):
dp.x = float(x)
dp.y = float(y)
elif len(dataPoints) == 1:
#in case of only one deck just dump it at origin
dataPoints[0].x = 0.0
dataPoints[0].y = 0.0
else:
#Nothing here
continue
#Do Machine Learning
X = RobustScaler().fit_transform(X)
#print(X.shape)
#labels = KmeansTF(X, CLUSTER_NUMBERS[clusterCountMover])
print("")
myClusterMaker = KMeans(n_clusters=min(CLUSTER_NUMBERS[clusterCountMover], len(X)))
myClusterMaker.fit(X)
labels = myClusterMaker.labels_
#myClusterMaker.fit(X)
if windowUpdate:
updateTextWindow(window, "Labeling {} decks".format(hero))
#fix weird bug where DH is only class not displaying
if hero == "DEMONHUNTER":
updateTextWindow(window, "Labeling DH Decks".format(hero))
if not os.path.exists("outputs/labels/NEW_labels"):
os.mkdir("outputs/labels/NEW_labels")
df = pd.DataFrame(X)
df["cluster"]= labels
df = df.sort_values("cluster")
df.to_csv("outputs/labels/NEW_labels/{}_labels.csv".format(hero), mode="w+", encoding='utf-8', index=False)
dpsInCluster = defaultdict(list)
for dp, cID in zip(dataPoints, labels):#myClusterMaker.labels_):
dpsInCluster[int(cID)].append(dp)
clusters = []
for id, dataPointIter in dpsInCluster.items():
clusters.append(Cluster.create(superCluster.CLUSTER_FACTORY, superCluster, id, dataPointIter))
#print(len(clusters)
#print(type(clusters))
classCluster = ClassCluster.create(superCluster.CLASS_FACTORY, superCluster, int(CardClass[hero]), clusters)
classClusters.append(classCluster)
clusterCountMover +=1
logger.info("END Clustering for: {}\n\n".format(hero))
superCluster.myClassClusters = classClusters
return superCluster