Implémentation de méthodes d'analyse.
authorpin <pin@fe552daf-6dbe-4428-90eb-1537e0879342>
Wed, 7 Jul 2010 16:19:34 +0000 (16:19 +0000)
committerpin <pin@fe552daf-6dbe-4428-90eb-1537e0879342>
Wed, 7 Jul 2010 16:19:34 +0000 (16:19 +0000)
git-svn-id: https://svn.cri.ensmp.fr/svn/minwii/trunk@278 fe552daf-6dbe-4428-90eb-1537e0879342

src/minwii/loganalyse.py

index 4a0460a..1772849 100755 (executable)
@@ -5,3 +5,103 @@ Module d'analyse des fichiers de log minwii.
 $Id$
 $URL$
 """
+
+from logfilereader import LogFileReader
+from pprint import pprint
+from musicxml import musicXml2Song
+from statlib import stats
+
+DEFAULT_STATS = ('geometricmean',
+                 'harmonicmean',
+                 'mean',
+                 'median',
+                 'medianscore',
+                 'mode',
+                 'moment',
+                 'variation',
+                 'skew',
+                 'kurtosis',
+                 'itemfreq',
+                 'histogram',
+                 'cumfreq',
+                 'relfreq',
+                 )
+
+def statsresults(m) :
+    def computeList(self):
+        l = m(self)
+        ret = {}
+        for name in DEFAULT_STATS :
+            ret[name] = getattr(stats, name)(l)
+        return ret
+    return computeList
+
+class LogFileAnalyser(LogFileReader) :
+
+    POSSIBLE_ANALYSES = {'BEGINNER' : ('songDuration',
+                                       'playingDuration',
+                                       'noteEndNoteOnLatency')}
+    
+    def analyse(self) :
+        mode = self.getMode()
+        print 'Mode :', mode
+        
+        results = {}
+        
+        for name in self.POSSIBLE_ANALYSES[mode] :
+            meth = getattr(self, name)
+            results[name] = meth()
+        
+        pprint(results)
+    
+    def playingDuration(self) :
+        last = self.getLastEventTicks()
+        first = self.getFirstEventTicks()
+        return last - first
+    
+    def songDuration(self) :
+        songFile = self.getSongFile()
+        song = musicXml2Song(songFile)
+        duration = 0
+        for note, verseIndex in song.iterNotes(indefinitely=False) :
+            duration = duration + note.duration
+        return duration * song.quarterNoteDuration
+    
+    @statsresults
+    def noteEndNoteOnLatency(self) :
+        eIter = self.getEventsIterator()
+        latencies = []
+        lastnoteEndT = 0
+        
+        for ticks, eventName, message in eIter :
+            if eventName == 'NOTEEND':
+                lastnoteEndT = ticks
+            if eventName == 'NOTEON' and lastnoteEndT :
+                latencies.append(ticks - lastnoteEndT)
+        
+        return latencies
+    
+        
+        
+        
+
+def main() :
+    from optparse import OptionParser
+    usage = "%prog logfile"
+    op = OptionParser(usage)
+    options, args = op.parse_args()
+    if len(args) != 1 :
+        op.error("incorrect number of arguments")
+
+
+    lfa = LogFileAnalyser(args[0])
+    lfa.analyse()
+
+if __name__ == "__main__" :
+    from os.path import realpath, sep
+    import sys
+    minwiipath = realpath(__file__).split(sep)
+    minwiipath = minwiipath[:-2]
+    minwiipath = sep.join(minwiipath)
+    sys.path.insert(1, minwiipath)
+    main()