qsfsdf
[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, normpath_win32
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 doparametres(self, dlg = None) :
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         if self.fromprof:
33             self.paramsimi = self.parametres
34         else :
35             self.paramsimi = {'coeff' : 0,
36                           'layout' : 2,
37                           'type_graph' : 1,
38                           'arbremax' : 1,
39                           'coeff_tv' : 0,
40                           'coeff_tv_nb' : 10,
41                           'tvprop' : 1,
42                           'tvmin' : 5,
43                           'tvmax' : 30,
44                           'coeff_te' : 1,
45                           'coeff_temin' : 1,
46                           'coeff_temax' : 10,
47                           'label_v': 1,
48                           'label_e': 1,
49                           'vcex' : 0,
50                           'vcexmin' : 8,
51                           'vcexmax' : 25,
52                           'cex' : 10,
53                           'seuil_ok' : 0,
54                           'seuil' : 1,
55                           'cols' : (255,0,0),
56                           'cola' : (200,200,200),
57                           'width' : 800,
58                           'height' : 800,
59                           'first' : True,
60                           'keep_coord' : False,
61                           'alpha' : 10,
62                           'film' : False,
63                           'svg' : 0,
64                           'halo' : 0,
65                           'com' : 0,
66                           'communities' : 0,
67                           }
68         self.indices = indices_simi
69         self.Source = None
70         if self.dirout :
71             self.pathout = PathOut(dirout = self.dirout)
72
73         if not self.parametres.get('isopen', False) :
74             if self.tableau is None :
75                 self.tableau = parent.tableau
76             self.tableau.parametres['mineff'] = 0
77             if not self.fromprof :
78                 dialcol = FreqDialog(self.parent, self.tableau.get_colnames(), _(u"Select columns").decode('utf8'), size=(600, 250), showNA = False)
79                 dialcol.CenterOnParent()            
80                 res = dialcol.ShowModal()
81             else :
82                 res = wx.ID_OK
83             if res == wx.ID_OK :
84                 if not self.actives :
85                     self.tableau.selected_col = dialcol.m_listBox1.GetSelections()
86                     actives = self.tableau.getactlistfromselection(self.tableau.selected_col)
87                 else :
88                     actives = self.actives
89                 if isinstance(actives, dict) :
90                     actives = [[val, actives[val][0]] for val in actives]
91                     self.tableau.actives = dict(actives)
92                 self.tableau.make_listactives()
93                 actives = dict([[i, val] for i, val in enumerate(actives)])
94                 self.dial = PrefSimi(self.parent, -1, self.paramsimi, self.indices, wordlist = actives)
95                 self.dial.CenterOnParent()
96                 self.val = self.dial.ShowModal()
97                 if self.val == wx.ID_OK :
98                     last = self.dial.listcol.GetFirstSelected()
99                     lastl = [self.dial.listcol.GetFirstSelected()]
100                     indexes = [self.dial.listcol.getColumnText(self.dial.listcol.GetFirstSelected(),0)]
101                     while self.dial.listcol.GetNextSelected(last) != -1:
102                         last = self.dial.listcol.GetNextSelected(last)
103                         lastl.append(last)
104                         indexes.append(self.dial.listcol.getColumnText(last,0))
105                     self.column = [self.tableau.listactives.index(val) for val in indexes]
106                     self.column.sort()
107                     self.paramsimi = self.make_param()
108                     self.parametres.update(self.paramsimi)
109                     #self.parametres['type'] = 'simimatrix'
110                     if not self.pathout : 
111                         self.parametres['pathout'] = ConstructPathOut(self.parametres['pathout'], 'SimiMatrix')
112                     else :
113                         self.parametres['pathout'] = self.dirout
114                     self.pathout.createdir(self.parametres['pathout'])
115                     self.pathout.dirout = self.parametres['pathout']
116                     self.dial.Destroy()
117                     #self.doanalyse2()  
118                 else :
119                     self.dial.Destroy()
120                     self.parametres = None
121                     return False
122             else :
123                 dialcol.Destroy()
124                 self.parametres = None
125                 return False
126
127     def doanalyse(self) :
128         self.pathout.basefiles(simipath)
129         with open(normpath_win32(self.pathout['selected.csv']), 'w') as f :
130             f.write('\n'.join([`val` for val in self.column]))
131
132         count = 1
133         keepGoing = self.dlg.Update(count)
134     #----------------------------------------------------------------
135         self.DictForme = {}
136         self.Min = 10
137         self.Linecontent = []
138     #--------------------------------------------------------
139         count += 1
140         #if not self.fromprof :
141             #self.pathout = ConstructPathOut(self.tableau.parametres['filename'], 'Simi')
142             #self.DictPathOut = construct_simipath(self.pathout)
143         self.tableau.dictpathout = self.pathout
144         self.dlg.Update(count, u"passage en O/1")
145         if not self.fromprof :
146             self.tableau.make_01_from_selection(self.tableau.selected_col)
147             #self.Linecontent = parent.table
148             #self.ListTo01Form()
149         #else :
150             #self.pathout = pathout
151             #self.DictPathOut = construct_simipath(self.pathout)
152         self.DictPathOut = self.pathout
153             #self.DictPathOut['mat01'] = fromprof
154         self.script = PrintSimiScript(self)
155         self.script.make_script()
156         #self.PrintScript()
157         count += 1
158         self.dlg.Update(count, u"R...") 
159         #self.DoR(script.scriptout, dlg = self.dlg, message = 'R...')
160         self.tmpfile = self.script.scriptout
161         self.DoR(self.dlg)
162         self.addgraph()
163         self.tableau.save_tableau(self.pathout['db'])
164         #self.make_ira()
165         #count += 1
166         #self.dlg.Update(count, u"") 
167         self.dlg.Destroy()
168         #self.dial.Destroy()
169         #self.dolayout()
170         if self.fromprof :
171             fromprof = True
172         else:
173             fromprof = False
174          
175
176     def make_param(self) :
177         if self.paramsimi['first'] :
178             keep_coord = False
179         else :
180             keep_coord = self.dial.check_coord.GetValue()
181         #self.select = self.dial.check_colch.GetValue()
182
183         paramsimi = {'coeff' : self.dial.choice1.GetSelection(),
184                           'layout' : self.dial.choice2.GetSelection(),
185                           'type_graph' : self.dial.choice3.GetSelection(),
186                           'arbremax' : self.dial.check1.GetValue(),
187                           'coeff_tv' : self.dial.check_s_size.GetValue(),
188                           'coeff_tv_nb' : self.dial.spin_tv.GetValue(),
189                           'tvprop' : self.dial.check2.GetValue(),
190                           'tvmin' : self.dial.spin_tvmin.GetValue(),
191                           'tvmax' : self.dial.spin_tvmax.GetValue(),
192                           'coeff_te' : self.dial.check3.GetValue(),
193                           'coeff_temin' : self.dial.spin_temin.GetValue(),
194                           'coeff_temax' : self.dial.spin_temax.GetValue(),
195                           'label_e' : self.dial.check_elab.GetValue(),
196                           'label_v' : self.dial.check_vlab.GetValue(),
197                           'vcex' : self.dial.check_vcex.GetValue(),
198                           'vcexmin' : self.dial.spin_vcexmin.GetValue(),
199                           'vcexmax' : self.dial.spin_vcexmax.GetValue(),
200                           'cex' : self.dial.spin_cex.GetValue(),
201                           'seuil_ok' : self.dial.check_seuil.GetValue(),
202                           'seuil' : self.dial.spin_seuil.GetValue(),
203                           'cols' : self.dial.cols.GetColour(),
204                           'cola' : self.dial.cola.GetColour(),
205                           'width' : self.dial.spin_width.GetValue(),
206                           'height' : self.dial.spin_height.GetValue(),
207                           'first' : False,
208                           'keep_coord' : keep_coord,
209                           'alpha' : self.dial.slider_sphere.GetValue(),
210                           'film' : self.dial.film.GetValue(),
211                           'svg' : self.dial.choix_format.GetSelection(),
212                           'halo' : self.dial.halo.GetValue(),
213                           'com' : self.dial.comcheck.GetValue(),
214                           'communities' :self.dial.choix_com.GetSelection(),
215                           }
216         if 'cexfromchi' in self.paramsimi :
217             paramsimi['cexfromchi'] = self.dial.checkit.GetValue()
218         if 'sfromchi' in self.paramsimi :
219             paramsimi['sfromchi'] = self.dial.checki.GetValue()
220         if 'vlabcolor' in self.paramsimi :
221             paramsimi['vlabcolor'] = self.paramsimi['vlabcolor']
222         if 'check_bystar' in dir(self.dial) :
223             paramsimi['bystar'] = self.dial.check_bystar.GetValue()
224             paramsimi['stars'] = self.paramsimi['stars']
225         if 'tmpchi' in self.paramsimi :
226             paramsimi['tmpchi'] = self.paramsimi['tmpchi']
227         return paramsimi
228         
229 #        
230     def addgraph(self) :
231         if self.parametres['type_graph'] == 1:
232             if self.parametres['svg'] :
233                 filename, ext = os.path.splitext(self.script.filename)
234                 fileout = filename + '.svg'
235             else :
236                 fileout = self.script.filename
237             if os.path.exists(self.DictPathOut['liste_graph']):
238                 graph_simi = read_list_file(self.DictPathOut['liste_graph'])
239                 graph_simi.append([os.path.basename(fileout), self.script.txtgraph])
240             else :
241                 graph_simi = [[os.path.basename(fileout), self.script.txtgraph]]
242             print_liste(self.DictPathOut['liste_graph'], graph_simi)
243         
244     def DoR(self, dlg):
245         if self.paramsimi['type_graph'] == 1 :
246             graph = False
247             wait = False
248         else : 
249             graph = True
250             wait = True
251         pid = exec_rcode(self.ira.RPath, self.tmpfile, wait = wait, graph = graph)
252         if self.paramsimi['type_graph'] == 1 :
253             while pid.poll() == None :
254                     if not self.cmd :
255                         dlg.Pulse(u'R ...')
256                         sleep(0.2)
257                     else :
258                         sleep(0.2)
259             check_Rresult(self.parent, pid)
260     
261