[xiph-cvs] cvs commit: positron/test detect_test.py

Stan Seibert volsung at xiph.org
Tue Jun 10 14:57:09 PDT 2003



volsung     03/06/10 17:57:09

  Modified:    test     detect_test.py
  Log:
  Much improved audiofile.detect() exerciser.  Found a unicode handling bug
  already!

Revision  Changes    Path
1.2       +81 -68    positron/test/detect_test.py

Index: detect_test.py
===================================================================
RCS file: /usr/local/cvsroot/positron/test/detect_test.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- detect_test.py	2 Jun 2003 02:19:36 -0000	1.1
+++ detect_test.py	10 Jun 2003 21:57:09 -0000	1.2
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 # -*- Mode: python -*-
 #
-# positron - main frontend
+# detect_test.py - tests the accuracy of positron.audiofile.detect()
 #
 # Copyright (C) 2003, Xiph.org Foundation
 #
@@ -15,106 +15,119 @@
 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
 # or FITNESS FOR A PARTICULAR PURPOSE.  See the license for more details.
 
-import positron.MP3Info as MP3Info
+import positron.audiofile
 import sys
 from os import path
 import time
 
+# Mappings from audio types to extensions
+type_mapping = { "mp3" : ".mp3",
+                  "oggvorbis" : ".ogg"}
+ext_mapping = { ".mp3" : "mp3",
+                 ".ogg" : "oggvorbis"}
+type_names = {"mp3" : "MP3",
+              "oggvorbis" : "Ogg Vorbis"}
+
+
 def check_dir(arg, dirname, names):
 
     for name in names:
         fullname = path.join(dirname, name)
         if path.isfile(fullname):
             try:
-                # Figure out if this is an MP3
-                f = file(fullname)
-                try:
-                    mp3info = MP3Info.MP3Info(f)
-                    arg.append((fullname, "mp3"))
-                except MP3Info.Error:
+                metadata = positron.audiofile.detect(fullname)
+                if metadata != None:
+                    arg.append((fullname, metadata["type"]))
+                else:
                     arg.append((fullname, None))
-                f.close()
             except Exception, e:
-                # This means MP3Info has an uncaught exception and needs
+                # This means detect() has an uncaught exception and needs
                 # to be fixed
-                print "ERROR: %s crashed MP3Info:" % (fullname,), e
+                print "ERROR: %s crashed detect():" % (fullname,), e
+
+def print_results(name, correct, incorrect):
+
+    correct_num = len(correct)
+    incorrect_num = len(incorrect)
+    total_num = correct_num + incorrect_num
+    
+    if total_num == 0:
+        print "No %s files found!" % (name,)
+    else:
+        print "%s files correctly identified: %d/%d (%3.1f%%)" % \
+              (name, correct_num, total_num,
+               100.0 * correct_num / total_num)
+        print "%s files not identified: %d/%d" % (name, incorrect_num,
+                                                  total_num)
+        if len(incorrect) > 0:
+            print "Filenames:"
+            for filename,detected_type in incorrect:
+                print "  (%s) %s" % (detected_type, filename)
+        print
+
 
 if __name__ == "__main__":
 
     if len(sys.argv) == 1:
-        print """Tests MP3Info on a test set of files.  Assumes test set has .mp3 for true MP3
-files and some other extension, or no extension, for other files.
+        print """Tests positron.audiofile.detect() on a test set of files.
+
+Assumes test set has .mp3 for true MP3, .ogg for Ogg Vorbis files and some
+other extension, or no extension, for other files.
 
 Usage: detect_test.py dir1 dir2 dir3 ..."""
 
         sys.exit(0)
 
     starttime = time.time()
-    results = []
+    files = []
     for dir in sys.argv[1:]:
-        path.walk(dir, check_dir, results)
+        path.walk(dir, check_dir, files)
     endtime = time.time()
-    
-    # Now let's see how MP3Info did.  We assume all MP3s have the .mp3
-    # extension and all non-MP3s do not.  So only use this on test sets
-    # with those extensions.
-
-    correct_mp3 = []
-    incorrect_mp3 = []
-    correct_other = []
-    incorrect_other = []
-    
-    for filename, detected_type in results:
+
+
+    # None is for the results of non-music files
+    results = {}
+    for key in type_mapping.keys()+[None]:
+        results[key] = [[], []]  # Correctly IDed, Incorrectly IDed
+
+    for filename, detected_type in files:
         (dummy, ext) = path.splitext(filename.lower())
-        
-        if ext == ".mp3" and detected_type == "mp3":
-            correct_mp3.append(filename)
-        elif ext == ".mp3" and detected_type != "mp3":
-            incorrect_mp3.append(filename)
-        elif ext != ".mp3" and detected_type != "mp3":
-            correct_other.append(filename)
-        elif ext != ".mp3" and detected_type == "mp3":
-            incorrect_other.append(filename)
 
+        if detected_type != None:            
+            if ext == type_mapping[detected_type]:
+                results[detected_type][0].append(filename)
+            elif ext in ext_mapping.keys():
+                results[ext_mapping[ext]][1].append((filename, detected_type))
+            else:
+                results[None][1].append((filename, detected_type))
+        else:
+            if ext not in ext_mapping.keys():
+                results[None][0].append(filename)
+            else:
+                results[ext_mapping[ext]][1].append((filename, detected_type))
 
+    # Print results
     print
     print "Results"
     print "-------"
 
-    correct_mp3_num = len(correct_mp3)
-    incorrect_mp3_num = len(incorrect_mp3)
-    total_mp3_num = correct_mp3_num + incorrect_mp3_num
-
-    if total_mp3_num == 0:
-        print "No MP3s found!"
-    else:
-        print "MP3s correctly identified: %d/%d (%3.1f%%)" % \
-              (correct_mp3_num, total_mp3_num,
-               100.0 * correct_mp3_num / total_mp3_num)
-        print "MP3s not identified: %d/%d" % (incorrect_mp3_num, total_mp3_num)
-        if len(incorrect_mp3) > 0:
-            print "Filenames:"
-            for filename in incorrect_mp3:
-                print " ", filename
-        print
 
-    correct_other_num = len(correct_other)
-    incorrect_other_num = len(incorrect_other)
-    total_other_num = correct_other_num + incorrect_other_num
-    if total_other_num == 0:
-        print "No other files found!"
-    else:
-        print "Other files correctly identified: %d/%d (%3.1f%%)" % \
-              (correct_other_num, total_other_num,
-               100.0 * correct_other_num / total_other_num)
-        print "Other files incorrectly identified as MP3s: %d/%d" \
-              % (incorrect_other_num, total_other_num)
-        if len(incorrect_other) > 0:
-            print "Filenames:"
-            for filename in incorrect_other:
-                print " ", filename
+    keys = results.keys()
+    keys.sort()
+    keys.reverse()
+
+    for filetype in keys:
+
+        if filetype == None:
+            name = "Other"
+        else:
+            name = type_names[filetype]
+            
+        print_results(name, results[filetype][0], results[filetype][1])
+        
 
-    if len(results) > 0:
+    if len(files) > 0:
         totaltime = endtime - starttime
+        print
         print "Total time spent checking files: %1.1f seconds" % (totaltime,)
-        print "Rate: %1.1f files/sec" % (len(results) / totaltime)
+        print "Rate: %1.1f files/sec" % (len(files) / totaltime)

<p><p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list