Merge branch 'master' of http://www.iramuteq.org/git/iramuteq
[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 = 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                         print self.parametres['pathout']
113                     else :
114                         self.parametres['pathout'] = self.dirout
115                     self.pathout.createdir(self.parametres['pathout'])
116                     self.pathout.dirout = self.parametres['pathout']
117                     self.dial.Destroy()
118                     #self.doanalyse2()  
119                 else :
120                     self.dial.Destroy()
121                     self.parametres = None
122                     return False
123             else :
124                 dialcol.Destroy()
125                 self.parametres = None
126                 return False
127
128     def doanalyse(self) :
129         self.pathout.basefiles(simipath)
130         with open(normpath_win32(self.pathout['selected.csv']), 'w') as f :
131             f.write('\n'.join([`val` for val in self.column]))
132
133         count = 1
134         keepGoing = self.dlg.Update(count)
135     #----------------------------------------------------------------
136         self.DictForme = {}
137         self.Min = 10
138         self.Linecontent = []
139     #--------------------------------------------------------
140         count += 1
141         #if not self.fromprof :
142             #self.pathout = ConstructPathOut(self.tableau.parametres['filename'], 'Simi')
143             #self.DictPathOut = construct_simipath(self.pathout)
144         self.tableau.dictpathout = self.pathout
145         self.dlg.Update(count, u"passage en O/1")
146         if not self.fromprof :
147             self.tableau.make_01_from_selection(self.tableau.selected_col)
148             #self.Linecontent = parent.table
149             #self.ListTo01Form()
150         #else :
151             #self.pathout = pathout
152             #self.DictPathOut = construct_simipath(self.pathout)
153         self.DictPathOut = self.pathout
154             #self.DictPathOut['mat01'] = fromprof
155         self.script = PrintSimiScript(self)
156         self.script.make_script()
157         #self.PrintScript()
158         count += 1
159         self.dlg.Update(count, u"R...") 
160         #self.DoR(script.scriptout, dlg = self.dlg, message = 'R...')
161         self.tmpfile = self.script.scriptout
162         self.DoR(self.dlg)
163         self.addgraph()
164         self.tableau.save_tableau(self.pathout['db'])
165         #self.make_ira()
166         #count += 1
167         #self.dlg.Update(count, u"") 
168         self.dlg.Destroy()
169         #self.dial.Destroy()
170         #self.dolayout()
171         if self.fromprof :
172             fromprof = True
173         else:
174             fromprof = False
175          
176
177     def make_param(self) :
178         if self.paramsimi['first'] :
179             keep_coord = False
180         else :
181             keep_coord = self.dial.check_coord.GetValue()
182         #self.select = self.dial.check_colch.GetValue()
183
184         paramsimi = {'coeff' : self.dial.choice1.GetSelection(),
185                           'layout' : self.dial.choice2.GetSelection(),
186                           'type_graph' : self.dial.choice3.GetSelection(),
187                           'arbremax' : self.dial.check1.GetValue(),
188                           'coeff_tv' : self.dial.check_s_size.GetValue(),
189                           'coeff_tv_nb' : self.dial.spin_tv.GetValue(),
190                           'tvprop' : self.dial.check2.GetValue(),
191                           'tvmin' : self.dial.spin_tvmin.GetValue(),
192                           'tvmax' : self.dial.spin_tvmax.GetValue(),
193                           'coeff_te' : self.dial.check3.GetValue(),
194                           'coeff_temin' : self.dial.spin_temin.GetValue(),
195                           'coeff_temax' : self.dial.spin_temax.GetValue(),
196                           'label_e' : self.dial.check_elab.GetValue(),
197                           'label_v' : self.dial.check_vlab.GetValue(),
198                           'vcex' : self.dial.check_vcex.GetValue(),
199                           'vcexmin' : self.dial.spin_vcexmin.GetValue(),
200                           'vcexmax' : self.dial.spin_vcexmax.GetValue(),
201                           'cex' : self.dial.spin_cex.GetValue(),
202                           'seuil_ok' : self.dial.check_seuil.GetValue(),
203                           'seuil' : self.dial.spin_seuil.GetValue(),
204                           'cols' : self.dial.cols.GetColour(),
205                           'cola' : self.dial.cola.GetColour(),
206                           'width' : self.dial.spin_width.GetValue(),
207                           'height' : self.dial.spin_height.GetValue(),
208                           'first' : False,
209                           'keep_coord' : keep_coord,
210                           'alpha' : self.dial.slider_sphere.GetValue(),
211                           'film' : self.dial.film.GetValue(),
212                           'svg' : self.dial.choix_format.GetSelection(),
213                           'halo' : self.dial.halo.GetValue(),
214                           'com' : self.dial.comcheck.GetValue(),
215                           'communities' :self.dial.choix_com.GetSelection(),
216                           }
217         if 'cexfromchi' in self.paramsimi :
218             paramsimi['cexfromchi'] = self.dial.checkit.GetValue()
219         if 'sfromchi' in self.paramsimi :
220             paramsimi['sfromchi'] = self.dial.checki.GetValue()
221         if 'vlabcolor' in self.paramsimi :
222             paramsimi['vlabcolor'] = self.paramsimi['vlabcolor']
223         if 'check_bystar' in dir(self.dial) :
224             paramsimi['bystar'] = self.dial.check_bystar.GetValue()
225             paramsimi['stars'] = self.paramsimi['stars']
226         if 'tmpchi' in self.paramsimi :
227             paramsimi['tmpchi'] = self.paramsimi['tmpchi']
228         return paramsimi
229         
230 #        
231     def addgraph(self) :
232         if self.parametres['type_graph'] == 1:
233             if self.parametres['svg'] :
234                 filename, ext = os.path.splitext(self.script.filename)
235                 fileout = filename + '.svg'
236             else :
237                 fileout = self.script.filename
238             if os.path.exists(self.DictPathOut['liste_graph']):
239                 graph_simi = read_list_file(self.DictPathOut['liste_graph'])
240                 graph_simi.append([os.path.basename(fileout), self.script.txtgraph])
241             else :
242                 graph_simi = [[os.path.basename(fileout), self.script.txtgraph]]
243             print_liste(self.DictPathOut['liste_graph'], graph_simi)
244         
245     def DoR(self, dlg):
246         if self.paramsimi['type_graph'] == 1 :
247             graph = False
248             wait = False
249         else : 
250             graph = True
251             wait = True
252         pid = exec_rcode(self.ira.RPath, self.tmpfile, wait = wait, graph = graph)
253         if self.paramsimi['type_graph'] == 1 :
254             while pid.poll() == None :
255                     if not self.cmd :
256                         dlg.Pulse(u'R ...')
257                         sleep(0.2)
258                     else :
259                         sleep(0.2)
260             check_Rresult(self.parent, pid)
261     
262