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