/
cluster_module.py
430 lines (334 loc) · 11.4 KB
/
cluster_module.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
428
429
"""
conducts the clustering function using scikit-learn
"""
import json
import operator
from database_wrapper import DatabaseWrapper
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction import FeatureHasher
from sklearn.cluster import KMeans
from shoutcast_wrapper import ShoutcastWrapper
class ClusterModule:
"""
clusters stations by artist using scikit-learn.
"""
numberOfClusters = 3
minimumArtistsToCluster = 10
maximumArtistsToCluster = 30
def getMoreArtists(self, currentdataset):
"""
downloads more artists to cluster by checking for stations
playing similar artists
parameters
----------
currentdataset - the current artist data set
returns
-------
dataset with a number of artists above the threshold
"""
def cluster(self, dataset):
"""
clusters the data provided into the number of
clusters set by self.numberOfClusters
stationToArtist: dict where
dict['data'] = data in a 2d array
dict['labels'] = labels for each array
returns
-------
a list of clusters, where a cluster is a
list of station names
"""
outputlabels = [] #the set of stations per cluster
outputdata = [] #list of set of artists per cluster
finaloutputdata = []
hasher = FeatureHasher(input_type="string")
transformer = TfidfTransformer()
km = KMeans(n_clusters=self.numberOfClusters, init='k-means++',
max_iter=10, n_init=1, verbose=0)
#edit the dataset so that it contains only artist name and not
#artist popularity
artistdataset = dataset['data']
newartistdataset = []
for i in range(0, len(artistdataset)):
if (len(artistdataset[i]) != 0):
newartistdataset.append(artistdataset[i][0][0])
#if the number of artists is not enough, get more artists
#here!!!
print "clustering " + str(len(artistdataset)) + " artists"
if len(artistdataset) < self.maximumArtistsToCluster:
print "we need more artists to cluster"
self.getMoreArtists(artistdataset)
datacounts = hasher.fit_transform(newartistdataset)
#tfidfcounts = transformer.fit_transform(datacounts)
#disabled tf-idf because too slow
#km.fit(tfidfcounts)
km.fit(datacounts)
labeleddata = km.labels_
#init output array
for i in range(0, len(set(labeleddata))):
outputlabels.append([])
outputdata.append([])
#add items to output array
for i in range (0, len(labeleddata)):
currentcluster = labeleddata[i]
outputlabels[currentcluster].append(dataset['labels'][i])
outputdata[currentcluster].append(dataset['data'][i])
#change the artist list to artist sets
for item in outputdata:
listofartists = []
for artistlist in item:
for artist in artistlist:
listofartists.append(artist)
finaloutputdata.append(list(set(listofartists)))
return {"labels" : outputlabels, "data" : finaloutputdata}
def getPlayingStations(self, artist):
"""
gets the playing stations from last.fm and gets the
stations who have historically played the artist from
the database.
Then, merges the sets and gets the artists for each
station.
Returns a dictionary with 2 lists. 1 is a list of stations
as 3-tuples, the other is the list of artist for each
station
parameters
----------
artist: name of the artist being played
dataset: dataset of stations who have played the artist.
dictionary with two keys:
'labels' contains the set of station 3-tuples
'data' contains the list of artists for each station
"""
sr = ShoutcastWrapper()
db = DatabaseWrapper()
mergedict = {}
mergelist = []
artistsetlist = []
#gets the set of currently playing stations
playingStationSet = sr.getStationPlayingArtist(artist)
#gets the set of historically played stations
historicalStationSet = db.getStationTuplesForArtist(artist)
#merges the two sets of stations, while preserving order of
#listen count
#add all of the historically played stations
itemcount = 0
for item in historicalStationSet:
itemId = item[1]
itemName = item[0]
mergedict[itemId] = itemcount
mergelist.append((itemId, itemName, False))
#mergelist.append(item)
itemcount = itemcount + 1
#add only the unique stations from now playing
for item in playingStationSet:
itemId = item[2]
itemName = item[0]
itemLC = item[1]
itemCT = item[3]
#if the station is already in the list, change
#status to playing
if (mergedict.has_key(itemId)):
itemnumber = mergedict[itemId]
mergelist[itemnumber] = item
#else append the station to the top of the list
#and add the station to the db
else:
#mergelist.insert(0, (itemId, itemName, True, itemCT))
mergelist.insert(0, item)
db.addStationForArtist(artist, (itemName, itemId, itemLC))
#get set of artists for each station
for item in mergelist:
stationID = item[0]
artistset = db.getArtistsForStationID(stationID)
artistsetlist.append(artistset)
return {"data" : artistsetlist, "labels" : mergelist}
def selectTopStationTags(self, data):
"""
selects top tags when given a list of stations
parameters
----------
data: a list of artists
returns: top 3 tags for each artist
"""
db = DatabaseWrapper()
taglist = []
output = []
for station in data:
topTags = db.getTagsForStation(station[0])
taglist.append(topTags)
if (len(taglist) > 2):
#calculate set differences
output.append(list(set(taglist[0]) - (set (taglist[1] + taglist[2])))[:3])
#if the first set difference is empty, just return the first three tags
if (output[0] == []):
output[0] = taglist[0][:3]
output.append(list(set(taglist[1]) - (set (output[0] + taglist[2])))[:3])
output.append(list(set(taglist[2]) - (set (output[1] + output[0])))[:3])
else:
print "Not enough tags to calculate tag difference"
output = [[],[],[]]
return output
def selectRepresentativeArtists(self, data, seedartist):
"""
selects a representative set of artists using the set difference
clean up this method!!
parameters
----------
data: the dataset of 3 clusters of artist sets
seedartist: name of the seed artist
"""
setlist = [] #list of unique sets
differencelist = [] #list of set differences
outputlist = []
datadicts = []
outputdicts = []
sortedoutputlist = []
#make sure there is enough data
if (len(data) > 2):
#calculate set differences
setlist.append(list(set(data[0]) - (set (data[1] + data[2]))))
setlist.append(list(set(data[1]) - (set (data[0] + data[2]))))
setlist.append(list(set(data[2]) - (set (data[1] + data[0]))))
#calculate the artist score
#add the artist scores to 3 dicts
for cluster in data:
currentdict = {}
outputdicts.append({})
for artist in cluster:
currentdict[artist[0]] = artist[1]
datadicts.append(currentdict)
#calculate the new score for each artist
for i in range(0, len(datadicts)):
currentcluster = datadicts[i]
for artist in currentcluster:
artistscore = currentcluster[artist]
for j in range (0, len(datadicts)):
if i != j and datadicts[j].has_key(artist):
artistscore = artistscore - datadicts[j][artist]
outputdicts[i][artist] = artistscore
#do a pass to check if the seed artist is in the dicts
if seedartist in outputdicts[i]: del outputdicts[i][seedartist]
for outputdict in outputdicts:
sortedoutputlist.append(sorted(outputdict.iteritems(), key=operator.itemgetter(1), reverse=True)[:3])
print sortedoutputlist
else:
print "Not enough artists to recommend"
sortedoutputlist = [[[],[],[]],[[],[],[]],[[],[],[]]]
#sort the lists in order of popularity
for item in setlist:
outputlist.append(sorted(item, key=lambda tup: tup[1], reverse=True)[:3])
return sortedoutputlist
def getDataResponseForArtist(self, artist):
"""
creates a response for a given artist but not in JSON
parameters
----------
artist: name of artist to search for
returns
-------
a list of 3 stations.
each station contains:
a representative artist
3 tags representing the station
"""
#vars
sr = ShoutcastWrapper()
topartists = []
topstations = []
toptags = []
outputlist = []
#get the data for the artist
dataset = self.getPlayingStations(artist)
#error check - if no artists
if len(dataset['data']) < 4:
return []
#cluster the data
clusteredset = self.cluster(dataset)
#pick the station for each set
stations = clusteredset['labels']
for item in stations:
stationtoappend = item[0]
#check if there is a now playing artist
if (stationtoappend[2] == False):
shoutcastid = stationtoappend[0]
name = stationtoappend[1]
currenttrack = sr.getCurrentTrackForStationWithData(name)
currenttrackname = currenttrack['stationname']
bitrate = currenttrack['br']
encoding = currenttrack['en']
newstationtuple = (shoutcastid, name, False, currenttrackname, bitrate, encoding)
topstations.append(newstationtuple)
else:
topstations.append(stationtoappend)
#append 3 dummy lists to topstations to prevent errors if no stations found
for i in range(0,3):
topstations.append(("",""))
#pick the representative artist for each set
topartists = self.selectRepresentativeArtists(clusteredset['data'], artist)
#pick the top tags for each station
toptags = self.selectTopStationTags(topstations)
for i in range(0,3):
outputlist.append([topstations[i], topartists[i], toptags[i]])
return outputlist
def getJSONResponseForArtist(self, artist):
"""
creates a JSON response for a given artist
parameters
----------
artist: name of artist to search for
returns
-------
a JSON string consisting of:
a list of 3 stations.
each station contains:
a representative artist
3 tags representing the station
"""
#vars
sr = ShoutcastWrapper()
topartists = []
topstations = []
toptags = []
outputlist = []
#get the data for the artist
dataset = self.getPlayingStations(artist)
#error check - if no artists
if len(dataset['data']) < 4:
return json.dumps({"success" : "False", "data" : []})
#cluster the data
clusteredset = self.cluster(dataset)
print "clustering done"
#pick the station for each set
stations = clusteredset['labels']
for item in stations:
stationtoappend = item[0]
#check if there is a now playing artist
if (stationtoappend[2] == False):
shoutcastid = stationtoappend[0]
name = stationtoappend[1]
currenttrack = sr.getCurrentTrackForStationWithData(name)
currenttrackname = currenttrack['stationname']
bitrate = currenttrack['br']
encoding = currenttrack['en']
listencount = currenttrack['lc']
newstationtuple = (shoutcastid, name, False, currenttrackname, bitrate, encoding, listencount)
topstations.append(newstationtuple)
else:
topstations.append(stationtoappend)
#append 3 dummy lists to topstations to prevent errors if no stations found
for i in range(0,3):
topstations.append(("",""))
#pick the representative artist for each set
topartists = self.selectRepresentativeArtists(clusteredset['data'], artist)
#pick the top tags for each station
toptags = self.selectTopStationTags(topstations)
for i in range(0,3):
outputlist.append([topstations[i], topartists[i], toptags[i]])
return json.dumps({"success":"True", "data":outputlist})
def main():
#get dataset for artist sting
db = DatabaseWrapper()
cluster = ClusterModule()
dataset = db.getStationSetForArtist("Coldplay")
output = cluster.cluster(dataset)
if __name__ =='__main__':main()