multisplit
[iramuteq] / tabchi2.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 string
12 import sys
13 import tempfile
14 from time import sleep
15
16 #------------------------------------
17 # import des modules wx
18 #------------------------------------
19 import wx
20 import wx.lib.sized_controls as sc
21
22 #------------------------------------
23 # import des fichiers du projet
24 #------------------------------------
25 import HTML
26 from chemins import ffr,FFF
27 from functions import exec_rcode, check_Rresult
28 from dialog import ChiDialog, PrefChi
29 from analysematrix import AnalyseMatrix
30
31
32 chioption = { 'valobs' : True,
33               'valtheo' : True,
34               'resi' : False,
35               'contrib' : True,
36               'pourcent' : False,
37               'pourcentl' : True,
38               'pourcentc' : True,
39               'graph' : True,
40               'bw' : False,
41               }
42
43
44 def make_res(line) :
45     if float(line[5]) <= 0.05 and line[6] != 'warning':
46         line.append('green')
47     elif float(line[5]) <= 0.05 and line[6] == 'warning':
48         line.append('blue')
49     else :
50         line.append('red')
51     return line
52
53 def clean_line(result) :
54     return [[val for val in line if val != '**'] for line in result]
55
56 def make_table(tabs, tab_title, res) :
57     return ['<br>'.join(['<font color=%s>%s</font>' % (res[i][-1], tab_title), HTML.table(tab)]) for i,tab in enumerate(tabs)]
58
59 def make_restab(res) :
60     return ['<br>'.join(['<font color=%s>%s</font>'% (line[-1], 'Résultats'),HTML.table([['chi', line[3]],['p', line[5]]])]) for i,line in enumerate(res)]
61
62 def make_htmlgraphs(graphs) :
63     return ['<img src=%s>' % os.path.basename(val) for val in graphs]
64
65 def make_link_list(res, text) :
66     return ['<a name=back_%i></a><a href=#%i><font color=%s>%s</font></a>' % (i, i, chi[-1], text[i]) for i, chi in enumerate(res)]
67
68 def make_title(res, text) :
69     return ['<a name=%i></a><br><font color=%s>%s</font><br><a href=#back_%i>retour</a><br>' % (i, val[-1], text[i], i) for i, val in enumerate(res)] 
70
71
72 class ChiSquare(AnalyseMatrix):
73
74     def doparametres(self, dlg = None):
75         if dlg is None :
76             return
77         dial = ChiDialog(self.parent, -1, "Chi2", chioption, self.tableau, size=(400, 350),
78                      style = wx.DEFAULT_DIALOG_STYLE
79                      )
80         dial.CenterOnParent()
81         val = dial.ShowModal()
82         if val==wx.ID_OK :     
83             self.colsel1 = dial.list_box_1.GetSelections()
84             self.colsel2 = dial.list_box_2.GetSelections()
85             if dial.chiopt :
86                 chioption['valobs'] = dial.dial.check1.GetValue()
87                 chioption['valtheo'] = dial.dial.check2.GetValue()
88                 chioption['resi'] = dial.dial.check3.GetValue()
89                 chioption['contrib'] = dial.dial.check4.GetValue()
90                 chioption['pourcent'] = dial.dial.check5.GetValue()
91                 chioption['pourcentl'] = dial.dial.check6.GetValue()
92                 chioption['pourcentc'] = dial.dial.check7.GetValue()
93                 chioption['graph'] = dial.dial.check8.GetValue()
94                 chioption['bw'] = dial.dial.checkbw.GetValue()
95                 dial.dial.Destroy()
96             dial.Destroy()
97             self.parametres.update(chioption)
98             self.chioption = chioption
99         else :
100             if dial.chiopt :
101                 dial.dial.Destroy()
102             dial.Destroy()
103             self.parametres = None
104
105     def doanalyse(self):
106         self.count = 1
107         keepGoing = self.dlg.Update(self.count,"Analyse dans R...")
108         self.OutFrame=tempfile.mktemp(dir=self.parent.TEMPDIR)
109         self.encode=self.parent.encode
110         self.TEMPDIR=self.parent.TEMPDIR
111         self.RPath=self.parent.PathPath.get('PATHS','rpath')
112         self.TextCroise=[]
113         for i in self.colsel1 :
114             for j in self.colsel2 :
115                 self.TextCroise.append(self.tableau.colnames[i] + ' / ' + self.tableau.colnames[j])
116         rchioption = {}
117         for val in self.chioption :
118             if self.chioption[val]:
119                 rchioption[val] = 'TRUE'
120             else :
121                 rchioption[val] = 'FALSE'
122         txt="""
123         source("%s")
124         """%ffr(self.parent.RscriptsPath['Rfunct'])
125         txt += """
126         source("%s")
127         """ % ffr(self.parent.RscriptsPath['Rgraph'])
128         txt += """
129         doobs <- %s
130         doexp <- %s
131         docontrib <- %s
132         doresi <- %s
133         dopr <- %s
134         doprl <- %s
135         doprc <- %s
136         dograph <- %s
137         bw <- %s
138         """ % (rchioption['valobs'], rchioption['valtheo'], rchioption['contrib'], rchioption['resi'], rchioption['pourcent'], rchioption['pourcentl'], rchioption['pourcentc'], rchioption['graph'], rchioption['bw'])
139         txt+="""
140         datadm <- read.csv2("%s", encoding="%s", header = TRUE, row.names = 1, sep='\\t', quote = '"', na.string = '')
141         listres<-list()
142         listcol<-list()
143         cont<-1
144         """%(ffr(self.tableau.parametres['csvfile']), self.tableau.parametres['syscoding'])
145         if len(self.colsel1)==1:
146             strsel1=str(tuple(self.colsel1)).replace(',','')
147         else:
148             strsel1=str(tuple(self.colsel1))
149         if len(self.colsel2)==1:
150             strsel2=str(tuple(self.colsel2)).replace(',','')
151         else:
152             strsel2=str(tuple(self.colsel2))
153         txt+="""
154         for (i in c%s) {""" % strsel1
155         txt+="""
156             for (j in c%s) {""" % strsel2
157         txt+="""
158                 tab<-table(datadm[,i+1],datadm[,j+1])
159                 if (min(dim(tab)) != 1) {
160                     chi<-chisq.test(tab)
161                     CS<-colSums(tab)
162                     RS<-rowSums(tab)
163                     GT<-sum(tab)
164                     chi$contrib<-(tab-chi$expected)/sqrt(chi$expected * ((1 - RS/GT) %%*%% t(1 - CS/GT)))
165                     listres[[cont]]<-chi
166                     listcol[[cont]]<-ncol(tab)
167                     cont<-cont+1
168                 } else {
169                     chi <- list(observed = tab, residuals = tab, contrib = tab, statistic = 0, p.value = 1, expected = tab, message = 'pas de calcul')
170                     listres[[cont]] <- chi
171                     listcol[[cont]]<-ncol(tab)
172                     cont <- cont + 1
173                 }
174             }
175         }
176         maxcol<-max(unlist(listcol))+1
177         if (maxcol<7) {maxcol<-7}
178         frameout<-matrix('*',1,maxcol)
179         count<-0
180         for (chi in listres) {
181             if (min(chi$expected)<5) {
182                 att<-"warning"
183             } else {
184                 att<-""
185             }
186             if ('message' %%in%% attributes(chi)$names) {
187                 att <- "Ce chi2 n\'a pas été calculé"
188                 nom_colresi<-colnames(chi$observed)
189                 chi$prl <- chi$expected
190                 chi$prc <- chi$expected
191                 st <- sum(chi$observed)
192             } else {
193                 nom_colresi<-colnames(chi$observed)
194                 st <- sum(chi$observed)
195                 sc <- colSums(chi$observed)
196                 sr <- rowSums(chi$observed)
197                 chi$prl <- round((chi$observed/sr)*100,2)
198                 chi$prc <- t(round((t(chi$observed)/sc)*100,2))
199             }
200             fileout<-paste('histo_',count,sep='')
201             fileout<-paste(fileout,'.png',sep='')
202             count<-count+1
203             fileout<-file.path("%s",fileout)
204             if (max(nchar(colnames(chi$observed)))>15) {
205                 leg <- 1:length(colnames(chi$observed))
206             } else {
207                 leg <- colnames(chi$observed)
208             }
209             if (dograph) {
210                 width<-ncol(chi$observed)*100
211                 if (width < 350) {width <- 350}
212                 open_file_graph(fileout,width = width, height = 300)
213                 par(mar=c(0,0,0,0))
214                 layout(matrix(c(1,2),1,2, byrow=TRUE),widths=c(3,1))
215                 par(mar=c(2,2,1,0))
216                 par(cex=0.8)
217                 if (!bw) colors <- rainbow(length(rownames(chi$observed)))
218                 else colors <- gray.colors(length(rownames(chi$observed)))
219                 barplot(chi$prl,names.arg = leg, beside=TRUE,border=NA, col=colors)
220                 par(mar=c(0,0,0,0))
221                 par(cex=0.8)
222                 plot(0, axes = FALSE, pch = '')
223                 legend(x = 'center' , rownames(chi$observed), fill = colors)
224                 dev.off()
225             }
226             chi$prl <- cbind(chi$prl, total = rowSums(chi$prl))
227             chi$prc <- rbind(chi$prc, total = colSums(chi$prc))
228             chi$observed<-rbind(chi$observed,total=colSums(chi$observed))
229             chi$observed<-cbind(chi$observed,total=rowSums(chi$observed))
230             chi$pr <- round((chi$observed/st)*100,2)
231             chi$expected<-rbind(chi$expected,total=colSums(chi$expected))
232             chi$expected<-cbind(chi$expected,total=rowSums(chi$expected))
233             chi$expected<-round(chi$expected,digits=2)
234             chi$residuals<-round(chi$residuals,digits=2)
235             chi$contrib<-round(chi$contrib, digits=2)
236             nom_col<-colnames(chi$observed)
237            
238             if (ncol(chi$observed)<maxcol) {
239                 for (i in 1:(maxcol-ncol(chi$observed))) {
240                     chi$observed<-cbind(chi$observed,'**')
241                     chi$pr<-cbind(chi$pr,'**')
242                     chi$prl<-cbind(chi$prl,'**')
243                     chi$prc<-cbind(chi$prc,'**')
244                     chi$expected<-cbind(chi$expected,'**')
245                     chi$residuals<-cbind(chi$residuals,'**')
246                     chi$contrib<-cbind(chi$contrib,'**')
247                     nom_col<-append(nom_col,'**')
248                     nom_colresi<-append(nom_colresi,'**')
249                 }
250                 chi$residuals<-cbind(chi$residuals,'**')
251                 chi$contrib<-cbind(chi$contrib,'**')
252                 nom_colresi<-append(nom_colresi,'**')
253                 chi$prc<-cbind(chi$prc,'**')
254             } else if (ncol(chi$observed)==maxcol) {
255                 chi$residuals<-cbind(chi$residuals,'**')
256                 chi$contrib<-cbind(chi$contrib,'**')
257                 nom_colresi<-append(nom_colresi,'**')
258                 chi$prc<-cbind(chi$prc,'**')
259             }
260             if (doobs) {
261                 li<-matrix('*obs*',1,maxcol)
262                 frameout<-rbind(frameout,li)
263                 frameout<-rbind(frameout,nom_col)
264                 frameout<-rbind(frameout,chi$observed)
265             }
266             if (doexp) {
267                 li<-matrix('*exp*',1,maxcol)
268                 frameout<-rbind(frameout,li)
269                 frameout<-rbind(frameout,nom_col)
270                 frameout<-rbind(frameout,chi$expected)
271             }
272             if (doresi) {
273                 li<-matrix('*resi*',1,maxcol)
274                 frameout<-rbind(frameout,li)
275                 frameout<-rbind(frameout,nom_colresi)
276                 frameout<-rbind(frameout,chi$residuals)
277             }
278             if (docontrib) {
279                 li<-matrix('*contrib*',1,maxcol)
280                 frameout<-rbind(frameout,li)
281                 frameout<-rbind(frameout,nom_colresi)
282                 frameout<-rbind(frameout,chi$contrib)
283             }
284             if (dopr) {
285                 li<-matrix('*pr*', 1, maxcol)
286                 frameout<-rbind(frameout,li)
287                 frameout<-rbind(frameout,nom_col)
288                 frameout<-rbind(frameout,chi$pr)
289             }
290             if (doprl) {
291                 li<-matrix('*prl*', 1, maxcol)
292                 frameout<-rbind(frameout,li)
293                 frameout<-rbind(frameout,nom_col)
294                 frameout<-rbind(frameout,chi$prl)
295             }
296             if (doprc) {
297                 li<-matrix('*prc*', 1, maxcol)
298                 frameout<-rbind(frameout,li)
299                 frameout<-rbind(frameout,nom_colresi)
300                 frameout<-rbind(frameout,chi$prc)
301             }
302             res<-c('****','chi',chi$statistic,'p',chi$p.value,att,fileout)
303             frameout<-rbind(frameout,res)
304         }
305         li<-matrix('fin_analyse',1,maxcol)
306         frameout<-rbind(frameout,li)
307         write.csv2(frameout,file="%s")
308         """ % (ffr(self.parametres['pathout']),ffr(self.OutFrame))
309         tmpfile=tempfile.mktemp(dir=self.TEMPDIR)
310         print(tmpfile)
311         tmpscript=open(tmpfile,'w', encoding='utf8')
312         tmpscript.write(txt)
313         tmpscript.close()
314         pid = exec_rcode(self.RPath, tmpfile, wait = False)
315         while pid.poll() == None :
316             sleep(0.2)
317         check_Rresult(self.parent, pid)            
318         self.count += 1
319         keepGoing = self.dlg.Update(self.count,"Ecriture des résultats")
320         listfileout = self.dolayout(self.chioption)
321         #listfileout=dlg.ShowChi2(ColSel1,ColSel2)
322         #parent.FreqNum += 1
323         #parent.DictTab[u"Chi2_%s*"%parent.FreqNum]=listfileout
324         #parent.newtab = wx.html.HtmlWindow(parent.nb, -1)
325         #if "gtk2" in wx.PlatformInfo:
326             #parent.newtab.SetStandardFonts()
327             #parent.newtab.LoadPage(listfileout[len(listfileout)-1])
328             #parent.nb.AddPage(parent.newtab,u"Chi2_%s*"%parent.FreqNum)
329             #parent.nb.SetSelection(parent.nb.GetPageCount()-1)
330             #parent.ShowTab(wx.EVT_BUTTON)
331             #parent.DisEnSaveTabAs(True)
332         #self.count += 1
333         #keepGoing = self.dlg.Update(self.count,u"Fini")
334
335     def dolayout(self, option):
336         ListFile=[False]
337         file=open(self.OutFrame,'r', encoding='utf8')
338         content=file.readlines()
339         file.close()
340         lcont = [line.replace('"','').replace('\n','').split(';') for line in content]
341         lcont.pop(0)
342         lcont.pop(0)
343         allcoord = []
344         names = []
345         res = [chi for chi in lcont if chi[0]=='res']
346         res = [make_res(line) for line in res]
347         coord_res = [i for i,chi in enumerate(lcont) if chi[0]=='res']
348         if option['valobs']:
349             allcoord.append([i for i,chi in enumerate(lcont) if chi[1]=='*obs*'])
350             names.append('Valeurs observées')
351         if option['valtheo'] :
352             allcoord.append([i for i,chi in enumerate(lcont) if chi[1]=='*exp*'])
353             names.append('Valeurs théoriques')
354         if option['resi'] :
355             allcoord.append([i for i,chi in enumerate(lcont) if chi[1]=='*resi*'])
356             names.append('Residuals')
357         if option['contrib'] :
358             allcoord.append([i for i,chi in enumerate(lcont) if chi[1]=='*contrib*'])
359             names.append('Contributions a posteriori')
360         if option['pourcent'] : 
361             allcoord.append([i for i,chi in enumerate(lcont) if chi[1]=='*pr*'])
362             names.append('Pourcentages')
363         if option['pourcentl'] :
364             allcoord.append([i for i,chi in enumerate(lcont) if chi[1]=='*prl*'])
365             names.append('Pourcentages en ligne')
366         if option['pourcentc'] :
367             allcoord.append([i for i,chi in enumerate(lcont) if chi[1]=='*prc*'])
368             names.append('Pourcentages en colonne')
369         allcoord.append(coord_res)
370         allhtml = [[clean_line(lcont[allcoord[i][j]+1:allcoord[i+1][j]]) for j, line in enumerate(allcoord[i])] for i, tab in enumerate(allcoord) if i!=len(allcoord)-1]
371         allhtml = [make_table(val,names[i],res) for i,val in enumerate(allhtml)]
372         links = make_link_list(res, self.TextCroise)
373         html_res = make_restab(res)
374         allhtml.insert(0,html_res)
375         titles = make_title(res, self.TextCroise)
376         allhtml.insert(0,titles)
377         if option['graph'] :
378             graphs = [line[7] for line in res]
379             ListFile += graphs
380             html_graphs = make_htmlgraphs(graphs)
381             allhtml.append(html_graphs)
382         header="""
383         <html>\n
384         <meta http-equiv="content-Type" content="text/html; charset=utf8" />\n
385         <body>\n
386         <h1>Test du Chi2</h1>\n
387         <br>
388         <table border=1><tr><td>
389         Légende : <br>
390         <font color=green>p &lt;= 0.05</font><br>
391         <font color=blue>p &lt;= 0.05 mais il y a des valeurs théoriques &lt; 5</font><br>
392         <font color=red>p &gt; 0.05</font>
393         </td></tr></table><br><br>
394         """
395         pretxt = '<br>\n'.join(links)+'<br><hr><br>\n'
396         txt = '<br><hr><br>\n'.join(['<br><br>'.join([tab[i] for tab in allhtml]) for i,val in enumerate(res)])
397         txt = header + pretxt + txt + '\n</body></html>'
398         fileout=os.path.join(self.parametres['pathout'],'resultats-chi2.html')
399         with open(fileout, 'w',encoding='utf8') as f :
400             f.write(txt)
401         ListFile.append(fileout)         
402         return ListFile