source: python/aubio/onsetcompare.py @ d57c879

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

python: finish getting rid of numarray

  • 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    labs = []
83    mean = 0
84    # if lexp is empty
85    if expc == 0 : return orig,orig,0,0,0,0,l,mean
86    missed, bad, doubled, merged = 0, 0, 0, 0
87    # find missed and doubled ones first
88    for x in ltru:
89        correspond = 0
90        for y in lexp:
91            if abs(x-y) <= eps:    correspond += 1
92        if correspond == 0:        missed += 1
93        elif correspond > 1:       doubled += correspond - 1 
94    # then look for bad and merged ones
95    for y in lexp:
96        correspond = 0
97        for x in ltru:
98            if abs(x-y) <= eps:   
99                correspond += 1
100                l.append(y-x) 
101                labs.append(abs(y-x))
102        if correspond == 0:        bad += 1
103        elif correspond > 1:       merged += correspond - 1
104    # check consistancy of the results
105    assert ( orig - missed - merged == expc - bad - doubled)
106    return orig, missed, merged, expc, bad, doubled, l, labs
107
108def notes_roc (la, lb, eps):
109    from numpy import transpose, add, resize
110    """ creates a matrix of size len(la)*len(lb) then look for hit and miss
111    in it within eps tolerance windows """
112    gdn,fpw,fpg,fpa,fdo,fdp = 0,0,0,0,0,0
113    m = len(la)
114    n = len(lb)
115    x =           resize(la[:][0],(n,m))
116    y = transpose(resize(lb[:][0],(m,n)))
117    teps =  (abs(x-y) <= eps[0]) 
118    x =           resize(la[:][1],(n,m))
119    y = transpose(resize(lb[:][1],(m,n)))
120    tpitc = (abs(x-y) <= eps[1]) 
121    res = teps * tpitc
122    res = add.reduce(res,axis=0)
123    for i in range(len(res)) :
124        if res[i] > 1:
125            gdn+=1
126            fdo+=res[i]-1
127        elif res [i] == 1:
128            gdn+=1
129    fpa = n - gdn - fpa
130    return gdn,fpw,fpg,fpa,fdo,fdp
131
132def load_onsets(filename) :
133    """ load onsets targets / candidates files in arrays """
134    l = [];
135   
136    f = open(filename,'ro')
137    while 1:
138        line = f.readline().split()
139        if not line : break
140        l.append(float(line[0]))
141   
142    return l
Note: See TracBrowser for help on using the repository browser.