Implémentation de méthodes d'analyse.
[minwii.git] / src / minwii / loganalyse.py
1 # -*- coding: utf-8 -*-
2 """
3 Module d'analyse des fichiers de log minwii.
4
5 $Id$
6 $URL$
7 """
8
9 from logfilereader import LogFileReader
10 from pprint import pprint
11 from musicxml import musicXml2Song
12 from statlib import stats
13
14 DEFAULT_STATS = ('geometricmean',
15 'harmonicmean',
16 'mean',
17 'median',
18 'medianscore',
19 'mode',
20 'moment',
21 'variation',
22 'skew',
23 'kurtosis',
24 'itemfreq',
25 'histogram',
26 'cumfreq',
27 'relfreq',
28 )
29
30 def statsresults(m) :
31 def computeList(self):
32 l = m(self)
33 ret = {}
34 for name in DEFAULT_STATS :
35 ret[name] = getattr(stats, name)(l)
36 return ret
37 return computeList
38
39 class LogFileAnalyser(LogFileReader) :
40
41 POSSIBLE_ANALYSES = {'BEGINNER' : ('songDuration',
42 'playingDuration',
43 'noteEndNoteOnLatency')}
44
45 def analyse(self) :
46 mode = self.getMode()
47 print 'Mode :', mode
48
49 results = {}
50
51 for name in self.POSSIBLE_ANALYSES[mode] :
52 meth = getattr(self, name)
53 results[name] = meth()
54
55 pprint(results)
56
57 def playingDuration(self) :
58 last = self.getLastEventTicks()
59 first = self.getFirstEventTicks()
60 return last - first
61
62 def songDuration(self) :
63 songFile = self.getSongFile()
64 song = musicXml2Song(songFile)
65 duration = 0
66 for note, verseIndex in song.iterNotes(indefinitely=False) :
67 duration = duration + note.duration
68 return duration * song.quarterNoteDuration
69
70 @statsresults
71 def noteEndNoteOnLatency(self) :
72 eIter = self.getEventsIterator()
73 latencies = []
74 lastnoteEndT = 0
75
76 for ticks, eventName, message in eIter :
77 if eventName == 'NOTEEND':
78 lastnoteEndT = ticks
79 if eventName == 'NOTEON' and lastnoteEndT :
80 latencies.append(ticks - lastnoteEndT)
81
82 return latencies
83
84
85
86
87
88 def main() :
89 from optparse import OptionParser
90 usage = "%prog logfile"
91 op = OptionParser(usage)
92 options, args = op.parse_args()
93 if len(args) != 1 :
94 op.error("incorrect number of arguments")
95
96
97 lfa = LogFileAnalyser(args[0])
98 lfa.analyse()
99
100 if __name__ == "__main__" :
101 from os.path import realpath, sep
102 import sys
103 minwiipath = realpath(__file__).split(sep)
104 minwiipath = minwiipath[:-2]
105 minwiipath = sep.join(minwiipath)
106 sys.path.insert(1, minwiipath)
107 main()