...
[iramuteq] / tabsimi.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2009-2010 Pierre Ratinaud
5 #License: GNU/GPL
6
7 from chemins import ConstructPathOut, simipath, ffr, PathOut
8 from functions import print_liste, exec_rcode, read_list_file, check_Rresult, indices_simi, treat_var_mod
9 from dialog import SelectColDial, FreqDialog
10 from guifunct import PrefSimi
11 from analysematrix import AnalyseMatrix
12 from PrintRScript import PrintSimiScript
13 from listlex import *
14 import wx
15 import os
16 import tempfile
17 import datetime
18 from ConfigParser import RawConfigParser
19 from time import sleep
20 from uuid import uuid4
21
22
23 class DoSimi(AnalyseMatrix):
24     def doanalyse(self) :
25         self.fromprof = self.parametres.get('fromprof', False)
26         self.wordgraph = self.parametres.get('wordgraph', False)
27         self.listactives = self.parametres.get('listactives', False)
28         self.actives = self.parametres.get('actives', False)
29         self.openfromprof = self.parametres.get('openfromprof', False)
30         self.cmd = self.parametres.get('cmd', False)
31         self.dirout = self.parametres.get('pathout', False)
32      #parent, matrix = None, parametres = None, isopen = False, fromprof = False, pathout = False, filename ='', gparent = False, wordgraph = False, listactives = False, actives = False, cmd = False, openfromprof=False, tableau = None):
33 #------------------------------------------------------------------- 
34     #    self.fromprof = fromprof
35     #    self.wordgraph = wordgraph
36     #    self.listactives = listactives
37     #    self.actives = actives
38     #    self.openfromprof = openfromprof
39     #    self.cmd = cmd
40     #    self.dirout = pathout
41     #    if parametres is not None and fromprof:
42         if self.fromprof:
43             self.paramsimi = parametres
44         else :
45             self.paramsimi = {'coeff' : 0,
46                           'layout' : 2,
47                           'type_graph' : 1,
48                           'arbremax' : 1,
49                           'coeff_tv' : 0,
50                           'coeff_tv_nb' : 10,
51                           'tvprop' : 1,
52                           'tvmin' : 5,
53                           'tvmax' : 30,
54                           'coeff_te' : 1,
55                           'coeff_temin' : 1,
56                           'coeff_temax' : 10,
57                           'label_v': 1,
58                           'label_e': 1,
59                           'vcex' : 0,
60                           'vcexmin' : 8,
61                           'vcexmax' : 25,
62                           'cex' : 10,
63                           'seuil_ok' : 0,
64                           'seuil' : 1,
65                           'cols' : (255,0,0),
66                           'cola' : (200,200,200),
67                           'width' : 800,
68                           'height' : 800,
69                           'first' : True,
70                           'keep_coord' : False,
71                           'alpha' : 10,
72                           'film' : False,
73                           'svg' : 0,
74                           'halo' : 0,
75                           'com' : 0,
76                           'communities' : 0,
77                           }
78         self.indices = indices_simi
79         #if fromprof :
80         #    self.parent = parent.parent
81         #    self.Source = parent
82         #else :
83         self.Source = None
84         if self.dirout :
85             self.pathout = PathOut(dirout = self.dirout)
86
87         #self.RPath = self.parent.PathPath.get('PATHS', 'rpath')
88         if not self.parametres.get('isopen', False) :
89             #if not fromprof :
90             #    self.tableau = self.parent.tableau
91             #else :
92             #    self.tableau = parent.tableau
93             if self.tableau is None :
94                 self.tableau = parent.tableau
95             self.tableau.parametres['mineff'] = 0
96             if not self.fromprof :
97                 dialcol = FreqDialog(self.parent, -1, self.tableau.get_colnames(), _(u"Select columns").decode('utf8'), size=(600, 250))
98                 dialcol.CenterOnParent()            
99                 res = dialcol.ShowModal()
100             else :
101                 res = wx.ID_OK
102             if res == wx.ID_OK :
103                 if not self.actives :
104                     self.tableau.selected_col = dialcol.list_box_1.GetSelections()
105                     actives = self.tableau.getactlistfromselection(self.tableau.selected_col)
106                 else :
107                     actives = self.actives
108                 if isinstance(actives, dict) :
109                     actives = [[val, actives[val][0]] for val in actives]
110                     self.tableau.actives = dict(actives)
111                 self.tableau.make_listactives()
112                 actives = dict([[i, val] for i, val in enumerate(actives)])
113                 self.dial = PrefSimi(self.parent, -1, self.paramsimi, self.indices, wordlist = actives)
114                 self.dial.CenterOnParent()
115                 self.val = self.dial.ShowModal()
116                 if self.val == wx.ID_OK :
117                     last = self.dial.listcol.GetFirstSelected()
118                     lastl = [self.dial.listcol.GetFirstSelected()]
119                     indexes = [self.dial.listcol.getColumnText(self.dial.listcol.GetFirstSelected(),0)]
120                     while self.dial.listcol.GetNextSelected(last) != -1:
121                         last = self.dial.listcol.GetNextSelected(last)
122                         lastl.append(last)
123                         indexes.append(self.dial.listcol.getColumnText(last,0))
124                     self.column = [self.tableau.listactives.index(val) for val in indexes]
125                     self.column.sort()
126                     self.paramsimi = self.make_param()
127                     self.parametres.update(self.paramsimi)
128                     #self.parametres['type'] = 'simimatrix'
129                     if not self.pathout : 
130                         self.parametres['pathout'] = ConstructPathOut(self.parametres['pathout'], 'SimiMatrix')
131                         print self.parametres['pathout']
132                     else :
133                         self.parametres['pathout'] = self.dirout
134                     self.pathout.createdir(self.parametres['pathout'])
135                     self.pathout.dirout = self.parametres['pathout']
136                     #self.parametres['filename'] = self.tableau.parametres['filename']
137                     self.dial.Destroy()
138                     self.doanalyse2()  
139                     #dlg = wx.ProgressDialog("Traitements",
140                     #               "Veuillez patienter...",
141                     #               maximum=4,
142                     #               parent=self.parent,
143                     #               style=wx.PD_APP_MODAL | wx.PD_AUTO_HIDE | wx.PD_ELAPSED_TIME
144                     #                )
145                     #dlg.Center()
146                     #AnalyseMatrix.__init__(self, parent, self.tableau, self.paramsimi, dlg = dlg)
147                 else :
148                     self.dial.Destroy()
149                     self.parametres = None
150                     return False
151             else :
152                 dialcol.Destroy()
153                 self.parametres = None
154                 return False
155
156     def doanalyse2(self) :
157         self.pathout.basefiles(simipath)
158         with open(self.pathout['selected.csv'], 'w') as f :
159             f.write('\n'.join([`val` for val in self.column]))
160
161         count = 1
162         keepGoing = self.dlg.Update(count)
163     #----------------------------------------------------------------
164         self.DictForme = {}
165         self.Min = 10
166         self.Linecontent = []
167     #--------------------------------------------------------
168         count += 1
169         #if not self.fromprof :
170             #self.pathout = ConstructPathOut(self.tableau.parametres['filename'], 'Simi')
171             #self.DictPathOut = construct_simipath(self.pathout)
172         self.tableau.dictpathout = self.pathout
173         self.dlg.Update(count, u"passage en O/1")
174         if not self.fromprof :
175             self.tableau.make_01_from_selection(self.tableau.selected_col)
176             #self.Linecontent = parent.table
177             #self.ListTo01Form()
178         #else :
179             #self.pathout = pathout
180             #self.DictPathOut = construct_simipath(self.pathout)
181         self.DictPathOut = self.pathout
182             #self.DictPathOut['mat01'] = fromprof
183         self.script = PrintSimiScript(self)
184         self.script.make_script()
185         #self.PrintScript()
186         count += 1
187         self.dlg.Update(count, u"R...") 
188         #self.DoR(script.scriptout, dlg = self.dlg, message = 'R...')
189         self.tmpfile = self.script.scriptout
190         self.DoR(self.dlg)
191         self.addgraph()
192         self.tableau.save_tableau(self.pathout['db'])
193         #self.make_ira()
194         count += 1
195         self.dlg.Update(count, u"") 
196         self.dlg.Destroy()
197         #self.dial.Destroy()
198         #self.dolayout()
199         if self.fromprof :
200             fromprof = True
201         else:
202             fromprof = False
203          
204
205     def make_param(self) :
206         if self.paramsimi['first'] :
207             keep_coord = False
208         else :
209             keep_coord = self.dial.check_coord.GetValue()
210         #self.select = self.dial.check_colch.GetValue()
211
212         paramsimi = {'coeff' : self.dial.choice1.GetSelection(),
213                           'layout' : self.dial.choice2.GetSelection(),
214                           'type_graph' : self.dial.choice3.GetSelection(),
215                           'arbremax' : self.dial.check1.GetValue(),
216                           'coeff_tv' : self.dial.check_s_size.GetValue(),
217                           'coeff_tv_nb' : self.dial.spin_tv.GetValue(),
218                           'tvprop' : self.dial.check2.GetValue(),
219                           'tvmin' : self.dial.spin_tvmin.GetValue(),
220                           'tvmax' : self.dial.spin_tvmax.GetValue(),
221                           'coeff_te' : self.dial.check3.GetValue(),
222                           'coeff_temin' : self.dial.spin_temin.GetValue(),
223                           'coeff_temax' : self.dial.spin_temax.GetValue(),
224                           'label_e' : self.dial.check_elab.GetValue(),
225                           'label_v' : self.dial.check_vlab.GetValue(),
226                           'vcex' : self.dial.check_vcex.GetValue(),
227                           'vcexmin' : self.dial.spin_vcexmin.GetValue(),
228                           'vcexmax' : self.dial.spin_vcexmax.GetValue(),
229                           'cex' : self.dial.spin_cex.GetValue(),
230                           'seuil_ok' : self.dial.check_seuil.GetValue(),
231                           'seuil' : self.dial.spin_seuil.GetValue(),
232                           'cols' : self.dial.cols.GetColour(),
233                           'cola' : self.dial.cola.GetColour(),
234                           'width' : self.dial.spin_width.GetValue(),
235                           'height' : self.dial.spin_height.GetValue(),
236                           'first' : False,
237                           'keep_coord' : keep_coord,
238                           'alpha' : self.dial.slider_sphere.GetValue(),
239                           'film' : self.dial.film.GetValue(),
240                           'svg' : self.dial.choix_format.GetSelection(),
241                           'halo' : self.dial.halo.GetValue(),
242                           'com' : self.dial.comcheck.GetValue(),
243                           'communities' :self.dial.choix_com.GetSelection(),
244                           }
245         if 'cexfromchi' in self.paramsimi :
246             paramsimi['cexfromchi'] = self.dial.checkit.GetValue()
247         if 'sfromchi' in self.paramsimi :
248             paramsimi['sfromchi'] = self.dial.checki.GetValue()
249         if 'vlabcolor' in self.paramsimi :
250             paramsimi['vlabcolor'] = self.paramsimi['vlabcolor']
251         if 'check_bystar' in dir(self.dial) :
252             paramsimi['bystar'] = self.dial.check_bystar.GetValue()
253             paramsimi['stars'] = self.paramsimi['stars']
254         if 'tmpchi' in self.paramsimi :
255             paramsimi['tmpchi'] = self.paramsimi['tmpchi']
256         return paramsimi
257         
258 #        
259     def addgraph(self) :
260         if self.parametres['type_graph'] == 1:
261             if self.parametres['svg'] :
262                 filename, ext = os.path.splitext(self.script.filename)
263                 fileout = filename + '.svg'
264             else :
265                 fileout = self.script.filename
266             if os.path.exists(self.DictPathOut['liste_graph']):
267                 graph_simi = read_list_file(self.DictPathOut['liste_graph'])
268                 graph_simi.append([os.path.basename(fileout), self.script.txtgraph])
269             else :
270                 graph_simi = [[os.path.basename(fileout), self.script.txtgraph]]
271             print_liste(self.DictPathOut['liste_graph'], graph_simi)
272         
273     def DoR(self, dlg):
274         if self.paramsimi['type_graph'] == 1 :
275             graph = False
276             wait = False
277         else : 
278             graph = True
279             wait = True
280         pid = exec_rcode(self.ira.RPath, self.tmpfile, wait = wait, graph = graph)
281         if self.paramsimi['type_graph'] == 1 :
282             while pid.poll() == None :
283                     if not self.cmd :
284                         dlg.Pulse(u'R ...')
285                         sleep(0.2)
286                     else :
287                         sleep(0.2)
288             check_Rresult(self.parent, pid)
289     
290