source: python/aubio/onsetcompare.py @ 8b0595e

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 8b0595e was 8b0595e, checked in by Paul Brossier <piem@altern.org>, 19 years ago

added onset_rocloc simultaneous comparison and diffs
added onset_rocloc simultaneous comparison and diffs

  • Property mode set to 100644
File size: 4.8 KB
Line 
1"""Copyright (C) 2004 Paul Brossier <piem@altern.org>
2print 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.
24it somewhat implements the Receiver Operating Statistic (ROC).
25see http://en.wikipedia.org/wiki/Receiver_operating_characteristic
26"""
27
28def onset_roc(ltru, lexp, eps):
29    """ compute differences between two lists
30          orig = hits + missed + merged
31          expc = hits + bad + doubled
32        returns orig, missed, merged, expc, bad, doubled
33    """
34    orig, expc = len(ltru), len(lexp)
35    # if lexp is empty
36    if expc == 0 : return orig,orig,0,0,0,0
37    missed, bad, doubled, merged = 0, 0, 0, 0
38    # find missed and doubled ones first
39    for x in ltru:
40        correspond = 0
41        for y in lexp:
42            if abs(x-y) <= eps:    correspond += 1
43        if correspond == 0:        missed += 1
44        elif correspond > 1:       doubled += correspond - 1 
45    # then look for bad and merged ones
46    for y in lexp:
47        correspond = 0
48        for x in ltru:
49            if abs(x-y) <= eps:    correspond += 1
50        if correspond == 0:        bad += 1
51        elif correspond > 1:       merged += correspond - 1
52    # check consistancy of the results
53    assert ( orig - missed - merged == expc - bad - doubled)
54    return orig, missed, merged, expc, bad, doubled
55
56def onset_diffs(ltru, lexp, eps):
57    """ compute differences between two lists
58          orig = hits + missed + merged
59          expc = hits + bad + doubled
60        returns orig, missed, merged, expc, bad, doubled
61    """
62    orig, expc = len(ltru), len(lexp)
63    # if lexp is empty
64    l = []
65    if expc == 0 : return l
66    # find missed and doubled ones first
67    for x in ltru:
68        correspond = 0
69        for y in lexp:
70            if abs(x-y) <= eps:    l.append(y-x) 
71    # return list of diffs
72    return l
73
74def onset_rocloc(ltru, lexp, eps):
75    """ compute differences between two lists
76          orig = hits + missed + merged
77          expc = hits + bad + doubled
78        returns orig, missed, merged, expc, bad, doubled
79    """
80    orig, expc = len(ltru), len(lexp)
81    l = []
82    mean = 0
83    # if lexp is empty
84    if expc == 0 : return orig,orig,0,0,0,0,l,mean
85    missed, bad, doubled, merged = 0, 0, 0, 0
86    # find missed and doubled ones first
87    for x in ltru:
88        correspond = 0
89        for y in lexp:
90            if abs(x-y) <= eps:    correspond += 1
91        if correspond == 0:        missed += 1
92        elif correspond > 1:       doubled += correspond - 1 
93    # then look for bad and merged ones
94    for y in lexp:
95        correspond = 0
96        for x in ltru:
97            if abs(x-y) <= eps:   
98                correspond += 1
99                l.append(y-x) 
100        if correspond == 0:        bad += 1
101        elif correspond > 1:       merged += correspond - 1
102    # check consistancy of the results
103    assert ( orig - missed - merged == expc - bad - doubled)
104    return orig, missed, merged, expc, bad, doubled, l, sum(l)/float(max(len(l),1))
105
106def notes_roc (la, lb, eps):
107    from numarray import transpose, add, resize
108    """ creates a matrix of size len(la)*len(lb) then look for hit and miss
109    in it within eps tolerance windows """
110    gdn,fpw,fpg,fpa,fdo,fdp = 0,0,0,0,0,0
111    m = len(la)
112    n = len(lb)
113    x =           resize(la[:][0],(n,m))
114    y = transpose(resize(lb[:][0],(m,n)))
115    teps =  (abs(x-y) <= eps[0]) 
116    x =           resize(la[:][1],(n,m))
117    y = transpose(resize(lb[:][1],(m,n)))
118    tpitc = (abs(x-y) <= eps[1]) 
119    res = teps * tpitc
120    res = add.reduce(res,axis=0)
121    for i in range(len(res)) :
122        if res[i] > 1:
123            gdn+=1
124            fdo+=res[i]-1
125        elif res [i] == 1:
126            gdn+=1
127    fpa = n - gdn - fpa
128    return gdn,fpw,fpg,fpa,fdo,fdp
129
130def load_onsets(filename) :
131    """ load onsets targets / candidates files in arrays """
132    l = [];
133   
134    f = open(filename,'ro')
135    while 1:
136        line = f.readline().split()
137        if not line : break
138        l.append(float(line[0]))
139   
140    return l
Note: See TracBrowser for help on using the repository browser.