-
Notifications
You must be signed in to change notification settings - Fork 0
/
TEST filter.py
355 lines (268 loc) · 13.8 KB
/
TEST filter.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
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 10 18:00:08 2015
@author: erblast
"""
import time
import threading
import pandas as pd
import sqlite3
import requests
from copy import deepcopy
from createDB import *
from lastify_v6 import cleanup_db_dates, calc_interest_score
from call_controler_v2 import *
from output_controler import *
from Filter import *
url = 'http://ws.audioscrobbler.com/2.0/'
baserequest = {'api_key': '43e826d47e1fc381ac3686f374ee34b5', 'format': 'json'}
path = r'd:\Dropbox\work\python\lastify'
sk = str()
secret = 'e47ef29dda1b81c1f865d12a89ad28b8'
# Database Connection
db_path = '%s\\test.db' % path
createDB(db_path)
con = sqlite3.connect(db_path)
c = con.cursor()
db_lock = threading.RLock()
user = 'erblast'
cleanup_db_dates(db_path,db_lock) # takes 1 s on large db
print 'dates cleaned up'
calc_interest_score(user,db_path,db_lock) # takes 1,5 min on large db
print 'interest_score calculated'
# TRACK Filters
filt_manager_track = FilterManager(db_path=db_path, db_lock=db_lock,user= user, category='track',freq_init=True)
# tag filter 1, no threshold
f1 = FilterTrackTag(name='f1',tag= 'indie', thresh=0, db_path=db_path, db_lock=db_lock)
print 'n filtered tracks by tag_track: %d' % len(f1.IDs)
filt_manager_track.log_filter(f1)
# test if filtering was successfull
freq_init=filt_manager_track.calc_freq_tag(thresh=0,con=con)
count_init=freq_init[ freq_init['tagName'] == 'indie' ]['count']
assert len(f1.IDs) == count_init.iloc[0]
# delete filter and check for successful deletion
assert 'f1' in filt_manager_track.filter_log
filt_manager_track.del_filter(f1)
assert 'f1' not in filt_manager_track.filter_log
# 2nd tag filter with threshold
f1_1 = FilterTrackTag(name='f1_1',tag= 'alternative',thresh= 75, db_path=db_path, db_lock=db_lock)
print 'n filtered tracks by tag_track: %d' % len(f1_1.IDs)
filt_manager_track.log_filter(f1_1)
# test if thresholded filtering was successfull
freq_75=filt_manager_track.calc_freq_tag(thresh=75, con=con)
count_init=freq_75[ freq_75['tagName'] == 'alternative' ]['count']
assert len(f1_1.IDs) == count_init.iloc[0]
# add artisttag filter
f2 = FilterArtistTag(name='f2', tag='alternative', thresh=75, category='track', db_path=db_path, db_lock=db_lock)
print 'n filtered tracks by tag_artist: %d' % len(f2.IDs)
filt_manager_track.log_filter(f2)
# test if filter is correct
DF = pd.read_sql('SELECT ID_tag.tagName, COUNT (ID_tag.tagName) AS count, ID_tag.url\
FROM tag_artist \
LEFT JOIN ID_track ON tag_artist.artistID=ID_track.artistID\
LEFT JOIN ID_tag ON ID_tag.tagID=tag_artist.tagID \
WHERE tag_artist.count>=? \
GROUP BY ID_tag.tagID' , con, params=(75,))
assert len(f2.IDs) == DF['count'][ DF['tagName']=='alternative'].iloc[0]
# add plays filter
f3_1 = FilterPlaysTrack(name='f3_1',user= user, db_path=db_path,db_lock= db_lock, minim=10)
print 'n filtered track by plays : %d' % len(f3_1.IDs)
filt_manager_track.log_filter(f3_1)
f3_2 = FilterPlaysTrack(name='f3_2', user=user, db_path=db_path,db_lock= db_lock, minim=10, maxim=100)
print 'n filtered track by plays : %d' % len(f3_2.IDs)
filt_manager_track.log_filter(f3_2)
f3_3 = FilterPlaysTrack(name='f3_3', user=user, db_path=db_path, db_lock=db_lock, minim=0, maxim=100)
print 'n filtered track by plays : %d' % len(f3_3.IDs)
filt_manager_track.log_filter(f3_3)
assert len(f3_1.IDs) >= len(f3_2.IDs)
assert len(f3_3.IDs) >= len(f3_2.IDs)
# add artist plays filter
f4_1 = FilterPlaysArtist(name='f4_1', user=user, db_path=db_path, db_lock=db_lock, category='track', minim=10)
print 'n filtered tracks by artist plays : %d' % len(f4_1.IDs)
filt_manager_track.log_filter(f4_1)
f4_2 = FilterPlaysArtist(name='f4_2',user= user, db_path=db_path, db_lock=db_lock, category='track', minim=10, maxim=900)
print 'n filtered tracks by artist plays : %d' % len(f4_2.IDs)
filt_manager_track.log_filter(f4_2)
f4_3 = FilterPlaysArtist(name='f4_3', user=user, db_path=db_path, db_lock=db_lock, category='track', minim=0, maxim=900)
print 'n filtered tracks by artist plays : %d' % len(f4_3.IDs)
filt_manager_track.log_filter(f4_3)
assert len(f4_1.IDs) >= len(f4_2.IDs)
assert len(f4_3.IDs) >= len(f4_2.IDs)
#add date filter
f5_1 = FilterDateTrack(name='f5_1', db_path=db_path, db_lock=db_lock, minim=1990,include_undated=False)
print 'n filtered Tracks by date : %d' % len(f5_1.IDs)
filt_manager_track.log_filter(f5_1)
f5_2 = FilterDateTrack(name='f5_2', db_path=db_path, db_lock=db_lock, minim=1990, maxim=2014, include_undated=False)
print 'n filtered Tracks by date : %d' % len(f5_2.IDs)
filt_manager_track.log_filter(f5_2)
f5_3 = FilterDateTrack(name='f5_3', db_path=db_path, db_lock=db_lock, minim=1990, maxim=2014, include_undated=True)
print 'n filtered Tracks by date : %d' % len(f5_3.IDs)
filt_manager_track.log_filter(f5_3)
assert len(f5_1.IDs) >= len(f5_2.IDs)
assert len(f5_3.IDs) >= len(f5_2.IDs)
#add date filter artist
f6_1 =FilterDateArtist(name='f6_1', db_path=db_path, db_lock=db_lock, category='track', minim=1990, include_undated=False)
print 'n filtered tracks by artist date : %d' % len(f6_1.IDs)
filt_manager_track.log_filter(f6_1)
f6_2 =FilterDateArtist(name='f6_2', db_path=db_path, db_lock=db_lock, category='track', minim=1990, maxim=2014, include_undated=False)
print 'n filtered tracks by artist date : %d' % len(f6_2.IDs)
filt_manager_track.log_filter(f6_2)
f6_3 =FilterDateArtist(name='f6_3', db_path=db_path, db_lock=db_lock, category='track', minim=1990, maxim=2014, include_undated=True)
print 'n filtered tracks by artist date : %d' % len(f6_3.IDs)
filt_manager_track.log_filter(f6_3)
assert len(f6_1.IDs) >= len(f6_2.IDs)
assert len(f6_3.IDs) >= len(f6_2.IDs)
assert len(f6_1.IDs) != len(f5_1.IDs)
assert len(f6_2.IDs) != len(f5_2.IDs)
assert len(f6_3.IDs) != len(f5_3.IDs)
filt_manager_track.update_filterIDs(con=con)
print 'n all filters combined: %d' % len(filt_manager_track.filtered_IDs)
print 'deleting tag filters'
filt_manager_track.del_filter(f1_1)
filt_manager_track.del_filter(f2)
filt_manager_track.update_filterIDs(con=con)
print 'n all filters combined: %d' % len(filt_manager_track.filtered_IDs)
# ALBUM FILTERS
filt_manager_album = FilterManager(db_path=db_path, db_lock=db_lock,user= user, category='album',freq_init=True)
# tag filter 1, no threshold
f1 = FilterAlbumTag(name='f1',tag= 'indie', thresh=0, db_path=db_path, db_lock=db_lock)
print 'n filtered albums by tag_album: %d' % len(f1.IDs)
filt_manager_album.log_filter(f1)
# test if filtering was successfull
freq_init=filt_manager_album.calc_freq_tag(thresh=0, con=con)
count_init=freq_init[ freq_init['tagName'] == 'indie' ]['count']
assert len(f1.IDs) == count_init.iloc[0]
# delete filter and check for successful deletion
assert 'f1' in filt_manager_album.filter_log
filt_manager_album.del_filter(f1)
assert 'f1' not in filt_manager_album.filter_log
# 2nd tag filter with threshold
f1_1 = FilterAlbumTag(name='f1_1',tag= 'alternative',thresh= 75, db_path=db_path, db_lock=db_lock)
print 'n filtered albums by tag_album: %d' % len(f1_1.IDs)
filt_manager_album.log_filter(f1_1)
# test if thresholded filtering was successfull
freq_75=filt_manager_album.calc_freq_tag(thresh=75, con=con)
count_init=freq_75[ freq_75['tagName'] == 'alternative' ]['count']
assert len(f1_1.IDs) == count_init.iloc[0]
# add artisttag filter
f2 = FilterArtistTag(name='f2',tag= 'alternative', thresh=75, category='album', db_path=db_path, db_lock=db_lock)
print 'n filtered albums by tag_artist: %d' % len(f2.IDs)
filt_manager_album.log_filter(f2)
# test if filter is correct
DF = pd.read_sql('SELECT ID_tag.tagName, COUNT (ID_tag.tagName) AS count, ID_tag.url\
FROM tag_artist \
LEFT JOIN ID_album ON tag_artist.artistID=ID_album.artistID\
LEFT JOIN ID_tag ON ID_tag.tagID=tag_artist.tagID \
WHERE tag_artist.count>=? \
GROUP BY ID_tag.tagID' , con, params=(75,))
assert len(f2.IDs) == DF['count'][ DF['tagName']=='alternative'].iloc[0]
# add plays filter
f3_1 = FilterPlaysAlbum(name='f3_1', user=user, db_path=db_path, db_lock=db_lock, minim=10)
print 'n filtered album by plays : %d' % len(f3_1.IDs)
filt_manager_album.log_filter(f3_1)
f3_2 = FilterPlaysAlbum(name='f3_2',user= user, db_path=db_path, db_lock= db_lock, minim=400, maxim=550)
print 'n filtered album by plays : %d' % len(f3_2.IDs)
filt_manager_album.log_filter(f3_2)
f3_3 = FilterPlaysAlbum(name='f3_3', user=user, db_path=db_path, db_lock=db_lock, minim=400, maxim=550)
print 'n filtered album by plays : %d' % len(f3_3.IDs)
filt_manager_album.log_filter(f3_3)
assert len(f3_1.IDs) >= len(f3_2.IDs)
assert len(f3_3.IDs) >= len(f3_2.IDs)
# add artist plays filter
f4_1 = FilterPlaysArtist(name='f4_1',user= user, db_path=db_path, db_lock=db_lock, category='album', minim=10)
print 'n filtered albums by artist plays : %d' % len(f4_1.IDs)
filt_manager_album.log_filter(f4_1)
f4_2 = FilterPlaysArtist(name='f4_2', user=user, db_path=db_path, db_lock=db_lock, category='album', minim=10, maxim=900)
print 'n filtered albums by artist plays : %d' % len(f4_2.IDs)
filt_manager_album.log_filter(f4_2)
f4_3 = FilterPlaysArtist(name='f4_3', user=user, db_path=db_path, db_lock=db_lock, category='album', minim=0, maxim=900)
print 'n filtered albums by artist plays : %d' % len(f4_3.IDs)
filt_manager_album.log_filter(f4_3)
assert len(f4_1.IDs) >= len(f4_2.IDs)
assert len(f4_3.IDs) >= len(f4_2.IDs)
#add date filter
f5_1 = FilterDateAlbum(name='f5_1', db_path=db_path, db_lock=db_lock, minim=1990,include_undated=False)
print 'n filtered album by date : %d' % len(f5_1.IDs)
filt_manager_album.log_filter(f5_1)
f5_2 = FilterDateAlbum(name='f5_2', db_path=db_path, db_lock=db_lock, minim=1990, maxim=2014, include_undated=False)
print 'n filtered album by date : %d' % len(f5_2.IDs)
filt_manager_album.log_filter(f5_2)
f5_3 = FilterDateAlbum(name='f5_3', db_path=db_path, db_lock=db_lock, minim=1990, maxim=2014, include_undated=True)
print 'n filtered album by date : %d' % len(f5_3.IDs)
filt_manager_album.log_filter(f5_3)
assert len(f5_1.IDs) >= len(f5_2.IDs)
assert len(f5_3.IDs) >= len(f5_2.IDs)
#add date filter artist
f6_1 =FilterDateArtist(name='f6_1', db_path=db_path, db_lock=db_lock, category='album', minim=1990, include_undated=False)
print 'n filtered albums by artist date : %d' % len(f6_1.IDs)
filt_manager_album.log_filter(f6_1)
f6_2 =FilterDateArtist(name='f6_2', db_path=db_path, db_lock=db_lock, category='album', minim=1990, maxim=2014, include_undated=False)
print 'n filtered albums by artist date : %d' % len(f6_2.IDs)
filt_manager_album.log_filter(f6_2)
f6_3 =FilterDateArtist(name='f6_3', db_path=db_path, db_lock=db_lock, category='album', minim=1990, maxim=2014, include_undated=True)
print 'n filtered albums by artist date : %d' % len(f6_3.IDs)
filt_manager_album.log_filter(f6_3)
assert len(f6_1.IDs) >= len(f6_2.IDs)
assert len(f6_3.IDs) >= len(f6_2.IDs)
assert len(f6_1.IDs) != len(f5_1.IDs)
assert len(f6_2.IDs) != len(f5_2.IDs)
assert len(f6_3.IDs) != len(f5_3.IDs)
filt_manager_album.update_filterIDs(con=con)
print 'n all filters combined: %d' % len(filt_manager_album.filtered_IDs)
print 'deleting tag filters'
filt_manager_album.del_filter(f1_1)
filt_manager_album.del_filter(f2)
filt_manager_album.update_filterIDs(con=con)
print 'n all filters combined: %d' % len(filt_manager_album.filtered_IDs)
# Track initial frequencies
print '\n'
print 'initial track tag frequency'
print filt_manager_track.freq_tag_init[['tagName','count']].sort_values(by='count',ascending=False ).head(5)
print '\n'
print 'initial track release date frequency'
print filt_manager_track.freq_date_init.sort_values(by='count',ascending=False).head(5)
print '\n'
print 'initial artist tag frequency'
print filt_manager_track.freq_tag_artist_init[['tagName','count']].sort_values(by='count',ascending=False).head(5)
print '\n'
print 'initial artist release date frequency'
print filt_manager_track.freq_date_artist_init.sort_values(by='count',ascending=False).head(5)
# Track filtered frequencies
print '\n'
print 'filtered track tag frequency'
print filt_manager_track.calc_freq_tag(con=con)[['tagName','count']].sort_values(by='count',ascending=False).head(5)
print '\n'
print 'filtered track release date frequency'
print filt_manager_track.calc_freq_date(con=con).sort_values(by='count',ascending=False).head(5)
print '\n'
print 'filtered artist tag frequency'
print filt_manager_track.calc_freq_tag(artist=True, con=con)[['tagName','count']].sort_values(by='count',ascending=False).head(5)
print '\n'
print 'filtered artist release date frequency'
print filt_manager_track.calc_freq_date(artist=True, con=con).sort_values(by='count',ascending=False).head(5)
# Album initial frequencies
print '\n'
print 'initial album tag frequency'
print filt_manager_album.freq_tag_init[['tagName','count']].sort_values(by='count',ascending=False ).head(5)
print '\n'
print 'initial album release date frequency'
print filt_manager_album.freq_date_init.sort_values(by='count',ascending=False).head(5)
print '\n'
print 'initial artist tag frequency'
print filt_manager_album.freq_tag_artist_init[['tagName','count']].sort_values(by='count',ascending=False).head(5)
print '\n'
print 'initial artist release date frequency'
print filt_manager_album.freq_date_artist_init.sort_values(by='count',ascending=False).head(5)
# Album filtered frequencies
print '\n'
print 'filtered album tag frequency'
print filt_manager_album.calc_freq_tag(con=con)[['tagName','count']].sort_values(by='count',ascending=False).head(5)
print '\n'
print 'filtered album release date frequency'
print filt_manager_album.calc_freq_date().sort_values(by='count',ascending=False).head(5)
print '\n'
print 'filtered artist tag frequency'
print filt_manager_album.calc_freq_tag(artist=True, con=con)[['tagName','count']].sort_values(by='count',ascending=False).head(5)
print '\n'
print 'filtered artist release date frequency'
print filt_manager_album.calc_freq_date(artist=True).sort_values(by='count',ascending=False).head(5)