[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 | |
---|
[9cf2833] | 28 | def 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: |
---|
[96fb8ad] | 40 | correspond = 0 |
---|
[9cf2833] | 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: |
---|
[96fb8ad] | 47 | correspond = 0 |
---|
[9cf2833] | 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 | |
---|
| 56 | def 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 | |
---|
[96fb8ad] | 74 | def notes_roc (la, lb, eps): |
---|
[cd9b142] | 75 | from numarray import * |
---|
[96fb8ad] | 76 | """ creates a matrix of size len(la)*len(lb) then look for hit and miss |
---|
| 77 | in it within eps tolerance windows """ |
---|
| 78 | gdn,fpw,fpg,fpa,fdo,fdp = 0,0,0,0,0,0 |
---|
| 79 | m = len(la) |
---|
| 80 | n = len(lb) |
---|
| 81 | x = resize(la[:,0],(n,m)) |
---|
| 82 | y = transpose(resize(lb[:,0],(m,n))) |
---|
| 83 | teps = (abs(x-y) <= eps[0]) |
---|
| 84 | x = resize(la[:,1],(n,m)) |
---|
| 85 | y = transpose(resize(lb[:,1],(m,n))) |
---|
| 86 | tpitc = (abs(x-y) <= eps[1]) |
---|
| 87 | res = teps * tpitc |
---|
| 88 | res = add.reduce(res,axis=0) |
---|
| 89 | for i in range(len(res)) : |
---|
| 90 | if res[i] > 1: |
---|
| 91 | gdn+=1 |
---|
| 92 | fdo+=res[i]-1 |
---|
| 93 | elif res [i] == 1: |
---|
| 94 | gdn+=1 |
---|
| 95 | fpa = n - gdn - fpa |
---|
| 96 | return gdn,fpw,fpg,fpa,fdo,fdp |
---|
| 97 | |
---|
| 98 | def load_onsets(filename) : |
---|
| 99 | """ load onsets targets / candidates files in arrays """ |
---|
| 100 | l = []; |
---|
| 101 | |
---|
| 102 | f = open(filename,'ro') |
---|
| 103 | while 1: |
---|
| 104 | line = f.readline().split() |
---|
| 105 | if not line : break |
---|
| 106 | l.append(float(line[0])) |
---|
| 107 | |
---|
| 108 | return l |
---|