-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_algo.py
81 lines (49 loc) · 2.41 KB
/
test_algo.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
from core.WriteNewColOnCsv import WriteNewColOnCsv
from matplotlib.pyplot import show
from core.JamendoPlotFuncs import plot_rating_stats, compareJCRColumns
from core.JamendoCsvReader import JamendoCsvReader
from imp import load_module, find_module
from sys import argv
import numpy
def normalizeCol(newrateiter):
absoluteratesarray = numpy.fromiter( newrateiter, float )
M = numpy.max(absoluteratesarray)
m = numpy.min(absoluteratesarray)
for absolutevalue in absoluteratesarray:
normalizedvalue = (absolutevalue -m) / (M - m)
yield normalizedvalue
def printChart(JCR, N, newfield, oldfield):
#print a new chart for each field considered
for field in [newfield, oldfield]:
chart = JCR.getColumns(['id', field], sortkey=field, reverse=True)
print '******* %s TOP %s CHART *******' % (field.upper(), N)
print 'pos: id - %s ' % field.upper()
for i in range(0,N):
print ' %s: %s - %s' % (i+1, chart['id'][i], chart[field][i])
print '%s items out of the chart (%s rate = 0)' % (chart[field].count(0), field)
def test_algo(algo, originalfile, newfile, plot=1):
# parameters you can change
normalize = True
oldfield = 'rating'
newfield = 'NEWrating'
chartN = 20
nozero = True
try:
file, pathname, description = find_module(algo, ['rating_algo'])
assert file
ratingalgorithm = load_module(algo, file, pathname, description).__dict__[algo]
except Exception, e:
raise e
if normalize:
newcolumniter = normalizeCol( ratingalgorithm(file=originalfile) )
else:
newcolumniter = ratingalgorithm(file=originalfile)
WriteNewColOnCsv(newfield, newcolumniter, newfile, originalfile)
JCR = JamendoCsvReader(newfile)
printChart(JCR, chartN, newfield, oldfield )
if int(plot):
plot_rating_stats(JCR, oldfield, title=oldfield, show=False, nozero=nozero)
plot_rating_stats(JCR, newfield, title=newfield, show=False, nozero=nozero)
compareJCRColumns(JCR, [oldfield, 'weighted_avg_agreed_note', newfield], sortkey=newfield, \
title='comparare %s and %s (values aligned by same ids)' % (oldfield, newfield))
test_algo(*argv[1:])