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-2009 Paul Brossier <piem@aubio.org> |
---|
7 | |
---|
8 | This file is part of aubio. |
---|
9 | |
---|
10 | aubio is free software: you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation, either version 3 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | aubio is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | """ |
---|
23 | |
---|
24 | """ this file contains routines to compare two lists of onsets or notes. |
---|
25 | it somewhat implements the Receiver Operating Statistic (ROC). |
---|
26 | see http://en.wikipedia.org/wiki/Receiver_operating_characteristic |
---|
27 | """ |
---|
28 | |
---|
29 | def onset_roc(ltru, lexp, eps): |
---|
30 | """ compute differences between two lists |
---|
31 | orig = hits + missed + merged |
---|
32 | expc = hits + bad + doubled |
---|
33 | returns orig, missed, merged, expc, bad, doubled |
---|
34 | """ |
---|
35 | orig, expc = len(ltru), len(lexp) |
---|
36 | # if lexp is empty |
---|
37 | if expc == 0 : return orig,orig,0,0,0,0 |
---|
38 | missed, bad, doubled, merged = 0, 0, 0, 0 |
---|
39 | # find missed and doubled ones first |
---|
40 | for x in ltru: |
---|
41 | correspond = 0 |
---|
42 | for y in lexp: |
---|
43 | if abs(x-y) <= eps: correspond += 1 |
---|
44 | if correspond == 0: missed += 1 |
---|
45 | elif correspond > 1: doubled += correspond - 1 |
---|
46 | # then look for bad and merged ones |
---|
47 | for y in lexp: |
---|
48 | correspond = 0 |
---|
49 | for x in ltru: |
---|
50 | if abs(x-y) <= eps: correspond += 1 |
---|
51 | if correspond == 0: bad += 1 |
---|
52 | elif correspond > 1: merged += correspond - 1 |
---|
53 | # check consistancy of the results |
---|
54 | assert ( orig - missed - merged == expc - bad - doubled) |
---|
55 | return orig, missed, merged, expc, bad, doubled |
---|
56 | |
---|
57 | def onset_diffs(ltru, lexp, eps): |
---|
58 | """ compute differences between two lists |
---|
59 | orig = hits + missed + merged |
---|
60 | expc = hits + bad + doubled |
---|
61 | returns orig, missed, merged, expc, bad, doubled |
---|
62 | """ |
---|
63 | orig, expc = len(ltru), len(lexp) |
---|
64 | # if lexp is empty |
---|
65 | l = [] |
---|
66 | if expc == 0 : return l |
---|
67 | # find missed and doubled ones first |
---|
68 | for x in ltru: |
---|
69 | correspond = 0 |
---|
70 | for y in lexp: |
---|
71 | if abs(x-y) <= eps: l.append(y-x) |
---|
72 | # return list of diffs |
---|
73 | return l |
---|
74 | |
---|
75 | def onset_rocloc(ltru, lexp, eps): |
---|
76 | """ compute differences between two lists |
---|
77 | orig = hits + missed + merged |
---|
78 | expc = hits + bad + doubled |
---|
79 | returns orig, missed, merged, expc, bad, doubled |
---|
80 | """ |
---|
81 | orig, expc = len(ltru), len(lexp) |
---|
82 | l = [] |
---|
83 | labs = [] |
---|
84 | mean = 0 |
---|
85 | # if lexp is empty |
---|
86 | if expc == 0 : return orig,orig,0,0,0,0,l,mean |
---|
87 | missed, bad, doubled, merged = 0, 0, 0, 0 |
---|
88 | # find missed and doubled ones first |
---|
89 | for x in ltru: |
---|
90 | correspond = 0 |
---|
91 | for y in lexp: |
---|
92 | if abs(x-y) <= eps: correspond += 1 |
---|
93 | if correspond == 0: missed += 1 |
---|
94 | elif correspond > 1: doubled += correspond - 1 |
---|
95 | # then look for bad and merged ones |
---|
96 | for y in lexp: |
---|
97 | correspond = 0 |
---|
98 | for x in ltru: |
---|
99 | if abs(x-y) <= eps: |
---|
100 | correspond += 1 |
---|
101 | l.append(y-x) |
---|
102 | labs.append(abs(y-x)) |
---|
103 | if correspond == 0: bad += 1 |
---|
104 | elif correspond > 1: merged += correspond - 1 |
---|
105 | # check consistancy of the results |
---|
106 | assert ( orig - missed - merged == expc - bad - doubled) |
---|
107 | return orig, missed, merged, expc, bad, doubled, l, labs |
---|
108 | |
---|
109 | def notes_roc (la, lb, eps): |
---|
110 | from numpy import transpose, add, resize |
---|
111 | """ creates a matrix of size len(la)*len(lb) then look for hit and miss |
---|
112 | in it within eps tolerance windows """ |
---|
113 | gdn,fpw,fpg,fpa,fdo,fdp = 0,0,0,0,0,0 |
---|
114 | m = len(la) |
---|
115 | n = len(lb) |
---|
116 | x = resize(la[:][0],(n,m)) |
---|
117 | y = transpose(resize(lb[:][0],(m,n))) |
---|
118 | teps = (abs(x-y) <= eps[0]) |
---|
119 | x = resize(la[:][1],(n,m)) |
---|
120 | y = transpose(resize(lb[:][1],(m,n))) |
---|
121 | tpitc = (abs(x-y) <= eps[1]) |
---|
122 | res = teps * tpitc |
---|
123 | res = add.reduce(res,axis=0) |
---|
124 | for i in range(len(res)) : |
---|
125 | if res[i] > 1: |
---|
126 | gdn+=1 |
---|
127 | fdo+=res[i]-1 |
---|
128 | elif res [i] == 1: |
---|
129 | gdn+=1 |
---|
130 | fpa = n - gdn - fpa |
---|
131 | return gdn,fpw,fpg,fpa,fdo,fdp |
---|
132 | |
---|
133 | def load_onsets(filename) : |
---|
134 | """ load onsets targets / candidates files in arrays """ |
---|
135 | l = []; |
---|
136 | |
---|
137 | f = open(filename,'ro') |
---|
138 | while 1: |
---|
139 | line = f.readline().split() |
---|
140 | if not line : break |
---|
141 | l.append(float(line[0])) |
---|
142 | |
---|
143 | return l |
---|