1 | """Copyright (C) 2004 Paul Brossier <piem@altern.org> |
---|
2 | print aubio.__LICENSE__ for the terms of use |
---|
3 | """ |
---|
4 | |
---|
5 | __LICENSE__ = """\ |
---|
6 | Copyright (C) 2004 Paul Brossier <piem@altern.org> |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | This program is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | GNU General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
21 | """ |
---|
22 | |
---|
23 | """ this file contains routines to compare two lists of onsets or notes. |
---|
24 | it somewhat implements the Receiver Operating Statistic (ROC). |
---|
25 | see http://en.wikipedia.org/wiki/Receiver_operating_characteristic |
---|
26 | """ |
---|
27 | |
---|
28 | from numarray import * |
---|
29 | |
---|
30 | def onset_roc(la, lb, eps): |
---|
31 | """ thanks to nicolas wack for the rewrite""" |
---|
32 | """ compute differences between two lists """ |
---|
33 | """ feature: scalable to huge lists """ |
---|
34 | n, m = len(la), len(lb) |
---|
35 | if m == 0 : |
---|
36 | return 0,0,0,n,0 |
---|
37 | missed, bad = 0, 0 |
---|
38 | # find missed ones first |
---|
39 | for x in la: |
---|
40 | correspond = 0 |
---|
41 | for y in lb: |
---|
42 | if abs(x-y) <= eps: |
---|
43 | correspond += 1 |
---|
44 | if correspond == 0: |
---|
45 | missed += 1 |
---|
46 | # then look for bad ones |
---|
47 | for y in lb: |
---|
48 | correspond = 0 |
---|
49 | for x in la: |
---|
50 | if abs(x-y) <= eps: |
---|
51 | correspond += 1 |
---|
52 | if correspond == 0: |
---|
53 | bad += 1 |
---|
54 | ok = n - missed |
---|
55 | hits = m - bad |
---|
56 | # at this point, we must have ok = hits. if not we had |
---|
57 | # - a case were one onset counted for two labels (ok>hits) |
---|
58 | # - a case were one labels matched two onsets (hits>ok) |
---|
59 | # bad hack for now (fails if both above cases have happened): |
---|
60 | if ok > hits: bad += ok-hits; ok = hits |
---|
61 | if hits > ok: missed += hits-ok; hits = ok |
---|
62 | total = n |
---|
63 | return ok,bad,missed,total,hits |
---|
64 | |
---|
65 | |
---|
66 | def notes_roc (la, lb, eps): |
---|
67 | """ creates a matrix of size len(la)*len(lb) then look for hit and miss |
---|
68 | in it within eps tolerance windows """ |
---|
69 | gdn,fpw,fpg,fpa,fdo,fdp = 0,0,0,0,0,0 |
---|
70 | m = len(la) |
---|
71 | n = len(lb) |
---|
72 | x = resize(la[:,0],(n,m)) |
---|
73 | y = transpose(resize(lb[:,0],(m,n))) |
---|
74 | teps = (abs(x-y) <= eps[0]) |
---|
75 | x = resize(la[:,1],(n,m)) |
---|
76 | y = transpose(resize(lb[:,1],(m,n))) |
---|
77 | tpitc = (abs(x-y) <= eps[1]) |
---|
78 | res = teps * tpitc |
---|
79 | res = add.reduce(res,axis=0) |
---|
80 | for i in range(len(res)) : |
---|
81 | if res[i] > 1: |
---|
82 | gdn+=1 |
---|
83 | fdo+=res[i]-1 |
---|
84 | elif res [i] == 1: |
---|
85 | gdn+=1 |
---|
86 | fpa = n - gdn - fpa |
---|
87 | return gdn,fpw,fpg,fpa,fdo,fdp |
---|
88 | |
---|
89 | def load_onsets(filename) : |
---|
90 | """ load onsets targets / candidates files in arrays """ |
---|
91 | l = []; |
---|
92 | |
---|
93 | f = open(filename,'ro') |
---|
94 | while 1: |
---|
95 | line = f.readline().split() |
---|
96 | if not line : break |
---|
97 | l.append(float(line[0])) |
---|
98 | |
---|
99 | return l |
---|
100 | |
---|
101 | """ |
---|
102 | def onset_roc (la, lb, eps): |
---|
103 | \"\"\" build a matrix of all possible differences between two lists \"\"\" |
---|
104 | \"\"\" bug: not scalable to huge lists \"\"\" |
---|
105 | n, m = len(la), len(lb) |
---|
106 | if m ==0 : |
---|
107 | return 0,0,0,n,0 |
---|
108 | missed, bad = 0, 0 |
---|
109 | x = resize(la[:],(m,n)) |
---|
110 | y = transpose(resize(lb[:],(n,m))) |
---|
111 | teps = (abs(x-y) <= eps) |
---|
112 | resmis = add.reduce(teps,axis = 0) |
---|
113 | for i in range(n) : |
---|
114 | if resmis[i] == 0: |
---|
115 | missed += 1 |
---|
116 | resbad = add.reduce(teps,axis=1) |
---|
117 | for i in range(m) : |
---|
118 | if resbad[i] == 0: |
---|
119 | bad += 1 |
---|
120 | ok = n - missed |
---|
121 | hits = m - bad |
---|
122 | total = n |
---|
123 | return ok,bad,missed,total,hits |
---|
124 | """ |
---|
125 | |
---|