shithub: aubio

ref: fff7b92ffe9dd9d37d27aaf681af8572588c735b
dir: /python/aubiocompare-onset/

View raw version
#! /usr/bin/python

"""Copyright (C) 2004 Paul Brossier <piem@altern.org>

print aubio.__LICENSE__ for the terms of use

or see LICENSE.txt in the aubio installation directory.
"""
__LICENSE__ = """\
  Copyright (C) 2004-2009 Paul Brossier <piem@aubio.org>

  This file is part of aubio.

  aubio is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  aubio is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
"""            


__HELP__ = """\
# required arguments
 -c targetfilename 
 -o detectfilename 
(both must be text files with 1 time a line expressed in seconds)

# optional arguments
 -D <delay>  	delay in seconds
 -v 		verbose mode
 -d 		debug mode

# output 
results:number of correct detections
        number of incorrect detections
        number of doubled detections
        number of total detections
        number of total targets

# example:
$ aubioonset-comp -c checked-onsets.txt -o handlab-onsets.txt -v
( gd fp dd ) tot / real
( 5 4 0 ) 9 / 9
55.5555555556 %GD       44.4444444444 %FP       0.0 %OD

# bugs
does not scale to very long lists
"""

import sys
from aubio.onsetcompare import onset_roc, onset_diffs
from aubio.txtfile import read_datafile

# default values
fileo=None;filec=None;vmode=None;dmode=None;delay=0.
# default tolerance is 50 ms
#tol = 0.050
tol = 0.048
# default mode is onset
mode = 'onset'

while len(sys.argv) >=2:
    option = sys.argv[1]; del sys.argv[1]
    if option == '-h': print __HELP__; sys.exit()
    if option == '-o': fileo = sys.argv[1]; del sys.argv[1]
    if option == '-c': filec = sys.argv[1]; del sys.argv[1]
    if option == '-v': vmode = 'verbose'
    if option == '-d': dmode = 'debug'
    if option == '-D': delay = float(sys.argv[1]); del sys.argv[1] 
    if option == '-tol': tol = float(sys.argv[1]); del sys.argv[1] 
    if option == '-l': mode = 'localisation'

# arguments required
if (not fileo) or (not filec):
    print 'wrong set of arguments. use \'-h\' for help' 
    sys.exit('error: needs at least \'-c targets.txt -o detected.txt\'')

# load files
ltru, lres = read_datafile(fileo,depth=0),read_datafile(filec,depth=0)

# delay onsets as required with -D
if delay:
    for i in range(len(lres)):
        lres[i] = lres[i] + delay
# compute errors types
if mode == 'localisation':
	l = onset_diffs(ltru,lres,tol)
	for i in l: print "%.3f" % i
else:
	orig, missed, merged, expc, bad, doubled = onset_roc(ltru,lres,tol)
	
	# print results
	#print "orig, missed, merged, expc, bad, doubled:"
	if vmode=='verbose':
	    print "orig", orig
            print "expc", expc
            print "missed",missed
            print "merged", merged
            print "bad", bad
            print "doubled", doubled
            print "correct", orig-missed-merged
	    print "GD %2.8f\t"        % (100*float(orig-missed-merged)/(orig)),
	    print "FP %2.8f\t"        % (100*float(bad+doubled)/(orig))       , 
	    print "GD-merged %2.8f\t" % (100*float(orig-missed)/(orig))       , 
	    print "FP-pruned %2.8f\t" % (100*float(bad)/(orig))                
	else:
	    print  orig, missed, merged, expc, bad, doubled