Showing the changes to python (2 of 2 files). Show all
Unified · Split
| @@ -27,42 +27,52 @@ see http://en.wikipedia.org/wiki/Receiver_operating_characteristic · 26 unmodified lines | |||
| 27 | 27 | ||
| 28 | from numarray import * | 28 | from numarray import * |
| 29 | 29 | ||
| 30 | def onset_roc(la, lb, eps): | 30 | def onset_roc(ltru, lexp, eps): |
| 31 | """ thanks to nicolas wack for the rewrite""" | 31 | """ compute differences between two lists |
| 32 | """ compute differences between two lists """ | 32 | orig = hits + missed + merged |
| 33 | """ feature: scalable to huge lists """ | 33 | expc = hits + bad + doubled |
| 34 | n, m = len(la), len(lb) | 34 | returns orig, missed, merged, expc, bad, doubled |
| 35 | if m == 0 : | 35 | """ |
| 36 | return 0,0,0,n,0 | 36 | orig, expc = len(ltru), len(lexp) |
| 37 | missed, bad = 0, 0 | 37 | # if lexp is empty |
| 38 | # find missed ones first | 38 | if expc == 0 : return orig,orig,0,0,0,0 |
| 39 | for x in la: | 39 | missed, bad, doubled, merged = 0, 0, 0, 0 |
| 40 | # find missed and doubled ones first | ||
| 41 | for x in ltru: | ||
| 40 | correspond = 0 | 42 | correspond = 0 |
| 41 | for y in lb: | 43 | for y in lexp: |
| 42 | if abs(x-y) <= eps: | 44 | if abs(x-y) <= eps: correspond += 1 |
| 43 | correspond += 1 | 45 | if correspond == 0: missed += 1 |
| 44 | if correspond == 0: | 46 | elif correspond > 1: doubled += correspond - 1 |
| 45 | missed += 1 | 47 | # then look for bad and merged ones |
| 46 | # then look for bad ones | 48 | for y in lexp: |
| 47 | for y in lb: | ||
| 48 | correspond = 0 | 49 | correspond = 0 |
| 49 | for x in la: | 50 | for x in ltru: |
| 50 | if abs(x-y) <= eps: | 51 | if abs(x-y) <= eps: correspond += 1 |
| 51 | correspond += 1 | 52 | if correspond == 0: bad += 1 |
| 52 | if correspond == 0: | 53 | elif correspond > 1: merged += correspond - 1 |
| 53 | bad += 1 | 54 | # check consistancy of the results |
| 54 | ok = n - missed | 55 | assert ( orig - missed - merged == expc - bad - doubled) |
| 55 | hits = m - bad | 56 | return orig, missed, merged, expc, bad, doubled |
| 56 | # at this point, we must have ok = hits. if not we had | 57 | |
| 57 | # - a case were one onset counted for two labels (ok>hits) | 58 | def onset_diffs(ltru, lexp, eps): |
| 58 | # - a case were one labels matched two onsets (hits>ok) | 59 | """ compute differences between two lists |
| 59 | # bad hack for now (fails if both above cases have happened): | 60 | orig = hits + missed + merged |
| 60 | if ok > hits: bad += ok-hits; ok = hits | 61 | expc = hits + bad + doubled |
| 61 | if hits > ok: missed += hits-ok; hits = ok | 62 | returns orig, missed, merged, expc, bad, doubled |
| 62 | total = n | 63 | """ |
| 63 | return ok,bad,missed,total,hits | 64 | orig, expc = len(ltru), len(lexp) |
| 64 | 65 | # if lexp is empty | |
| 65 | 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 | |||
| 66 | def notes_roc (la, lb, eps): | 76 | def notes_roc (la, lb, eps): |
| 67 | """ creates a matrix of size len(la)*len(lb) then look for hit and miss | 77 | """ creates a matrix of size len(la)*len(lb) then look for hit and miss |
| 68 | in it within eps tolerance windows """ | 78 | in it within eps tolerance windows """ |
| @@ -97,29 +107,3 @@ def load_onsets(filename) : · 28 unmodified lines | |||
| 97 | l.append(float(line[0])) | 107 | l.append(float(line[0])) |
| 98 | 108 | ||
| 99 | return l | 109 | return l |
| 100 | |||
| 101 | """ | ||
| 102 | def onset_roc (la, lb, eps): | ||
| 103 | \"\"\" build a matrix of all possible differences between two lists \"\"\" | ||
| 104 | \"\"\" bug: not scalable to huge lists \"\"\" | ||
| 105 | n, m = len(la), len(lb) | ||
| 106 | if m ==0 : | ||
| 107 | return 0,0,0,n,0 | ||
| 108 | missed, bad = 0, 0 | ||
| 109 | x = resize(la[:],(m,n)) | ||
| 110 | y = transpose(resize(lb[:],(n,m))) | ||
| 111 | teps = (abs(x-y) <= eps) | ||
| 112 | resmis = add.reduce(teps,axis = 0) | ||
| 113 | for i in range(n) : | ||
| 114 | if resmis[i] == 0: | ||
| 115 | missed += 1 | ||
| 116 | resbad = add.reduce(teps,axis=1) | ||
| 117 | for i in range(m) : | ||
| 118 | if resbad[i] == 0: | ||
| 119 | bad += 1 | ||
| 120 | ok = n - missed | ||
| 121 | hits = m - bad | ||
| 122 | total = n | ||
| 123 | return ok,bad,missed,total,hits | ||
| 124 | """ | ||
| 125 | |||
| @@ -35,6 +35,9 @@ def parse_args(): · 34 unmodified lines | |||
| 35 | parser.add_option("-o","--outplot", | 35 | parser.add_option("-o","--outplot", |
| 36 | action="store", dest="outplot", default=None, | 36 | action="store", dest="outplot", default=None, |
| 37 | help="be quiet [default=None]") | 37 | help="be quiet [default=None]") |
| 38 | parser.add_option("-M","--mintol", | ||
| 39 | action="store", dest="mintol", default=-70, | ||
| 40 | help="mintol [default=0.48]") | ||
| 38 | (options, args) = parser.parse_args() | 41 | (options, args) = parser.parse_args() |
| 39 | if not len(args): | 42 | if not len(args): |
| 40 | print "no file name given\n", usage | 43 | print "no file name given\n", usage |
| @@ -49,4 +52,13 @@ silence = float(options.silence) · 8 unmodified lines | |||
| 49 | 52 | ||
| 50 | #onsets = getonsets(filename,threshold,silence,mode=options.mode) | 53 | #onsets = getonsets(filename,threshold,silence,mode=options.mode) |
| 51 | onsets = getonsetscausal(filename,threshold,silence,mode=options.mode) | 54 | onsets = getonsetscausal(filename,threshold,silence,mode=options.mode) |
| 52 | for i in onsets: print i*512./44100. | 55 | |
| 56 | # print all | ||
| 57 | #for i in onsets: print i*512./44100. | ||
| 58 | # prune doubled | ||
| 59 | last = -10. | ||
| 60 | mintol = float(options.mintol) | ||
| 61 | for i in onsets: | ||
| 62 | new = i*512./44100. | ||
| 63 | if (new - last > mintol): print "%f" % new | ||
| 64 | last = new | ||