1 # -*- coding: utf-8 -*-
2 # Author: Pierre Ratinaud
4 # copyright : 2012-2013 (c) Pierre Ratinaud
7 from chemins import PathOut
8 from functions import exec_rcode, check_Rresult, DoConf, ReadDicoAsDico, progressbar
9 from shutil import copy
10 from time import time, sleep
11 from uuid import uuid4
13 from openanalyse import OpenAnalyse
14 from dialog import StatDialog
16 log = logging.getLogger('iramuteq.analyse')
19 def __init__(self, ira, corpus, parametres=None, dlg=False, lemdial=True) :
25 self.parametres = parametres
27 self.lemdial = lemdial
29 self.keys = DoConf(self.ira.ConfigPath['key']).getoptions()
30 if not 'pathout' in self.parametres :
31 self.pathout = PathOut(corpus.parametres['originalpath'], analyse_type=parametres['type'], dirout=corpus.parametres['pathout'])
33 self.pathout = PathOut(filename=corpus.parametres['originalpath'], dirout=self.parametres['pathout'], analyse_type=self.parametres['type'])
34 self.parametres = self.lemparam()
35 if self.parametres is not None :
36 self.parametres = self.make_config(parametres)
37 #print 'paramtre aprs make_config', self.parametres
38 log.info(self.pathout.dirout)
39 if self.parametres is not None :
40 self.keys = DoConf(self.ira.ConfigPath['key']).getoptions()
41 gramact = [k for k in self.keys if self.keys[k] == 1]
42 gramsup = [k for k in self.keys if self.keys[k] == 2]
43 self.parametres['pathout'] = self.pathout.mkdirout()
44 self.pathout = PathOut(dirout=self.parametres['pathout'])
45 self.pathout.createdir(self.parametres['pathout'])
46 self.parametres['corpus'] = self.corpus.parametres['uuid']
47 self.parametres['uuid'] = str(uuid4())
48 self.parametres['name'] = os.path.split(self.parametres['pathout'])[1]
49 self.parametres['type'] = parametres['type']
50 self.parametres['encoding'] = self.ira.syscoding
52 if not self.parametres.get('dictionary', False) :
54 self.corpus.make_lems(lem=self.parametres['lem'])
57 dico = ReadDicoAsDico(self.parametres['dictionary'])
58 self.corpus.make_lems_from_dict(dico, dolem=self.parametres['lem'])
59 dictname = os.path.basename(self.parametres['dictionary'])
60 dictpath = os.path.join(self.pathout.dirout, dictname)
61 copy(self.parametres['dictionary'], dictpath)
62 self.parametres['dictionary'] = dictpath
63 self.corpus.parse_active(gramact, gramsup)
65 self.dlg = progressbar(self.ira, dlg)
66 result_analyse = self.doanalyse()
67 if result_analyse is None :
68 self.time = time() - self.t1
69 minutes, seconds = divmod(self.time, 60)
70 hours, minutes = divmod(minutes, 60)
71 self.parametres['time'] = '%.0fh %.0fm %.0fs' % (hours, minutes, seconds)
72 self.parametres['ira'] = self.pathout['Analyse.ira']
73 DoConf().makeoptions([self.parametres['type']], [self.parametres], self.pathout['Analyse.ira'])
74 self.ira.history.add(self.parametres)
76 if not isinstance(dlg, int) :
79 OpenAnalyse(self.parent, self.parametres['ira'])
80 self.ira.tree.AddAnalyse(self.parametres)
90 #if isinstance(dlg, wx.ProgressDialog) :
98 if self.dlg and self.lemdial:
99 dial = StatDialog(self.parent, self.keys)
100 dial.CenterOnParent()
101 dial.corpus = self.corpus
102 val = dial.ShowModal()
104 if dial.radio_lem.GetSelection() == 0 :
108 self.parametres['lem'] = lem
109 if dial.radio_dictchoice.GetSelection() == 1 :
110 self.parametres['dictionary'] = dial.dictpath.GetValue()
112 return self.parametres
117 return self.parametres
119 def make_config(self, config) :
120 if config is not None :
124 return self.preferences()
128 def readconfig(self, config) :
131 def preferences(self) :
132 return self.parametres
134 def printRscript(self) :
137 def doR(self, Rscript, wait=False, dlg=None, message='') :
138 log.info('R code...')
139 pid = exec_rcode(self.ira.RPath, Rscript, wait=wait)
140 while pid.poll() is None :
142 self.dlg.Pulse(message)
146 return check_Rresult(self.ira, pid)