[96fb8ad] | 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 | |
---|
[9cf2833] | 30 | def onset_roc(ltru, lexp, eps): |
---|
| 31 | """ compute differences between two lists |
---|
| 32 | orig = hits + missed + merged |
---|
| 33 | expc = hits + bad + doubled |
---|
| 34 | returns orig, missed, merged, expc, bad, doubled |
---|
| 35 | """ |
---|
| 36 | orig, expc = len(ltru), len(lexp) |
---|
| 37 | # if lexp is empty |
---|
| 38 | if expc == 0 : return orig,orig,0,0,0,0 |
---|
| 39 | missed, bad, doubled, merged = 0, 0, 0, 0 |
---|
| 40 | # find missed and doubled ones first |
---|
| 41 | for x in ltru: |
---|
[96fb8ad] | 42 | correspond = 0 |
---|
[9cf2833] | 43 | for y in lexp: |
---|
| 44 | if abs(x-y) <= eps: correspond += 1 |
---|
| 45 | if correspond == 0: missed += 1 |
---|
| 46 | elif correspond > 1: doubled += correspond - 1 |
---|
| 47 | # then look for bad and merged ones |
---|
| 48 | for y in lexp: |
---|
[96fb8ad] | 49 | correspond = 0 |
---|
[9cf2833] | 50 | for x in ltru: |
---|
| 51 | if abs(x-y) <= eps: correspond += 1 |
---|
| 52 | if correspond == 0: bad += 1 |
---|
| 53 | elif correspond > 1: merged += correspond - 1 |
---|
| 54 | # check consistancy of the results |
---|
| 55 | assert ( orig - missed - merged == expc - bad - doubled) |
---|
| 56 | return orig, missed, merged, expc, bad, doubled |
---|
| 57 | |
---|
| 58 | def onset_diffs(ltru, lexp, eps): |
---|
| 59 | """ compute differences between two lists |
---|
| 60 | orig = hits + missed + merged |
---|
| 61 | expc = hits + bad + doubled |
---|
| 62 | returns orig, missed, merged, expc, bad, doubled |
---|
| 63 | """ |
---|
| 64 | orig, expc = len(ltru), len(lexp) |
---|
| 65 | # if lexp is empty |
---|
| 66 | l = [] |
---|
| 67 | if expc == 0 : return l |
---|
| 68 | # find missed and doubled ones first |
---|
| 69 | for x in ltru: |
---|
| 70 | correspond = 0 |
---|
| 71 | for y in lexp: |
---|
| 72 | if abs(x-y) <= eps: l.append(y-x) |
---|
| 73 | # return list of diffs |
---|
| 74 | return l |
---|
| 75 | |
---|
[96fb8ad] | 76 | def notes_roc (la, lb, eps): |
---|
| 77 | """ creates a matrix of size len(la)*len(lb) then look for hit and miss |
---|
| 78 | in it within eps tolerance windows """ |
---|
| 79 | gdn,fpw,fpg,fpa,fdo,fdp = 0,0,0,0,0,0 |
---|
| 80 | m = len(la) |
---|
| 81 | n = len(lb) |
---|
| 82 | x = resize(la[:,0],(n,m)) |
---|
| 83 | y = transpose(resize(lb[:,0],(m,n))) |
---|
| 84 | teps = (abs(x-y) <= eps[0]) |
---|
| 85 | x = resize(la[:,1],(n,m)) |
---|
| 86 | y = transpose(resize(lb[:,1],(m,n))) |
---|
| 87 | tpitc = (abs(x-y) <= eps[1]) |
---|
| 88 | res = teps * tpitc |
---|
| 89 | res = add.reduce(res,axis=0) |
---|
| 90 | for i in range(len(res)) : |
---|
| 91 | if res[i] > 1: |
---|
| 92 | gdn+=1 |
---|
| 93 | fdo+=res[i]-1 |
---|
| 94 | elif res [i] == 1: |
---|
| 95 | gdn+=1 |
---|
| 96 | fpa = n - gdn - fpa |
---|
| 97 | return gdn,fpw,fpg,fpa,fdo,fdp |
---|
| 98 | |
---|
| 99 | def load_onsets(filename) : |
---|
| 100 | """ load onsets targets / candidates files in arrays """ |
---|
| 101 | l = []; |
---|
| 102 | |
---|
| 103 | f = open(filename,'ro') |
---|
| 104 | while 1: |
---|
| 105 | line = f.readline().split() |
---|
| 106 | if not line : break |
---|
| 107 | l.append(float(line[0])) |
---|
| 108 | |
---|
| 109 | return l |
---|