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
7 #------------------------------------
8 # import des modules python
9 #------------------------------------
14 from time import sleep
16 #------------------------------------
17 # import des modules wx
18 #------------------------------------
20 import wx.lib.sized_controls as sc
22 #------------------------------------
23 # import des fichiers du projet
24 #------------------------------------
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
32 chioption = { 'valobs' : True,
45 if float(line[5]) <= 0.05 and line[6] != 'warning':
47 elif float(line[5]) <= 0.05 and line[6] == 'warning':
53 def clean_line(result) :
54 return [[val for val in line if val != '**'] for line in result]
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)]
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)]
62 def make_htmlgraphs(graphs) :
63 return ['<img src=%s>' % os.path.basename(val) for val in graphs]
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)]
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)]
72 class ChiSquare(AnalyseMatrix):
74 def doparametres(self, dlg = None):
77 dial = ChiDialog(self.parent, -1, "Chi2", chioption, self.tableau, size=(400, 350),
78 style = wx.DEFAULT_DIALOG_STYLE
81 val = dial.ShowModal()
83 self.colsel1 = dial.list_box_1.GetSelections()
84 self.colsel2 = dial.list_box_2.GetSelections()
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()
97 self.parametres.update(chioption)
98 self.chioption = chioption
103 self.parametres = None
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')
113 for i in self.colsel1 :
114 for j in self.colsel2 :
115 self.TextCroise.append(self.tableau.colnames[i] + ' / ' + self.tableau.colnames[j])
117 for val in self.chioption :
118 if self.chioption[val]:
119 rchioption[val] = 'TRUE'
121 rchioption[val] = 'FALSE'
124 """%ffr(self.parent.RscriptsPath['Rfunct'])
127 """ % ffr(self.parent.RscriptsPath['Rgraph'])
138 """ % (rchioption['valobs'], rchioption['valtheo'], rchioption['contrib'], rchioption['resi'], rchioption['pourcent'], rchioption['pourcentl'], rchioption['pourcentc'], rchioption['graph'], rchioption['bw'])
140 datadm <- read.csv2("%s", encoding="%s", header = TRUE, row.names = 1, sep='\\t', quote = '"', na.string = '')
144 """%(ffr(self.tableau.parametres['csvfile']), self.tableau.parametres['syscoding'])
145 if len(self.colsel1)==1:
146 strsel1=str(tuple(self.colsel1)).replace(',','')
148 strsel1=str(tuple(self.colsel1))
149 if len(self.colsel2)==1:
150 strsel2=str(tuple(self.colsel2)).replace(',','')
152 strsel2=str(tuple(self.colsel2))
154 for (i in c%s) {""" % strsel1
156 for (j in c%s) {""" % strsel2
158 tab<-table(datadm[,i+1],datadm[,j+1])
159 if (min(dim(tab)) != 1) {
164 chi$contrib<-(tab-chi$expected)/sqrt(chi$expected * ((1 - RS/GT) %%*%% t(1 - CS/GT)))
166 listcol[[cont]]<-ncol(tab)
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)
176 maxcol<-max(unlist(listcol))+1
177 if (maxcol<7) {maxcol<-7}
178 frameout<-matrix('*',1,maxcol)
180 for (chi in listres) {
181 if (min(chi$expected)<5) {
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)
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))
200 fileout<-paste('histo_',count,sep='')
201 fileout<-paste(fileout,'.png',sep='')
203 fileout<-file.path("%s",fileout)
204 if (max(nchar(colnames(chi$observed)))>15) {
205 leg <- 1:length(colnames(chi$observed))
207 leg <- colnames(chi$observed)
210 width<-ncol(chi$observed)*100
211 if (width < 350) {width <- 350}
212 open_file_graph(fileout,width = width, height = 300)
214 layout(matrix(c(1,2),1,2, byrow=TRUE),widths=c(3,1))
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)
222 plot(0, axes = FALSE, pch = '')
223 legend(x = 'center' , rownames(chi$observed), fill = colors)
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)
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,'**')
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,'**')
261 li<-matrix('*obs*',1,maxcol)
262 frameout<-rbind(frameout,li)
263 frameout<-rbind(frameout,nom_col)
264 frameout<-rbind(frameout,chi$observed)
267 li<-matrix('*exp*',1,maxcol)
268 frameout<-rbind(frameout,li)
269 frameout<-rbind(frameout,nom_col)
270 frameout<-rbind(frameout,chi$expected)
273 li<-matrix('*resi*',1,maxcol)
274 frameout<-rbind(frameout,li)
275 frameout<-rbind(frameout,nom_colresi)
276 frameout<-rbind(frameout,chi$residuals)
279 li<-matrix('*contrib*',1,maxcol)
280 frameout<-rbind(frameout,li)
281 frameout<-rbind(frameout,nom_colresi)
282 frameout<-rbind(frameout,chi$contrib)
285 li<-matrix('*pr*', 1, maxcol)
286 frameout<-rbind(frameout,li)
287 frameout<-rbind(frameout,nom_col)
288 frameout<-rbind(frameout,chi$pr)
291 li<-matrix('*prl*', 1, maxcol)
292 frameout<-rbind(frameout,li)
293 frameout<-rbind(frameout,nom_col)
294 frameout<-rbind(frameout,chi$prl)
297 li<-matrix('*prc*', 1, maxcol)
298 frameout<-rbind(frameout,li)
299 frameout<-rbind(frameout,nom_colresi)
300 frameout<-rbind(frameout,chi$prc)
302 res<-c('****','chi',chi$statistic,'p',chi$p.value,att,fileout)
303 frameout<-rbind(frameout,res)
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)
311 tmpscript=open(tmpfile,'w', encoding='utf8')
314 pid = exec_rcode(self.RPath, tmpfile, wait = False)
315 while pid.poll() == None :
317 check_Rresult(self.parent, pid)
319 keepGoing = self.dlg.Update(self.count,"Ecriture des résultats")
320 listfileout = self.dolayout(self.chioption)
321 #listfileout=dlg.ShowChi2(ColSel1,ColSel2)
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)
333 #keepGoing = self.dlg.Update(self.count,u"Fini")
335 def dolayout(self, option):
337 file=open(self.OutFrame,'r', encoding='utf8')
338 content=file.readlines()
340 lcont = [line.replace('"','').replace('\n','').split(';') for line in content]
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']
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')
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)
378 graphs = [line[7] for line in res]
380 html_graphs = make_htmlgraphs(graphs)
381 allhtml.append(html_graphs)
384 <meta http-equiv="content-Type" content="text/html; charset=utf8" />\n
386 <h1>Test du Chi2</h1>\n
388 <table border=1><tr><td>
390 <font color=green>p <= 0.05</font><br>
391 <font color=blue>p <= 0.05 mais il y a des valeurs théoriques < 5</font><br>
392 <font color=red>p > 0.05</font>
393 </td></tr></table><br><br>
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 :
401 ListFile.append(fileout)