première mise à jour pour python 3
[iramuteq] / autres / tabstudent.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2020 Pierre Ratinaud
4 #License: GNU/GPL
5
6 #------------------------------------
7 # import des modules python
8 #------------------------------------
9 import string
10 import os
11 import sys
12 import tempfile
13 from time import sleep
14
15 #------------------------------------
16 # import des modules wx
17 #------------------------------------
18 import wx
19
20 #------------------------------------
21 # import des fichiers du projet
22 #------------------------------------
23 from chemins import ffr
24 from layout import MakeHeader,MakeStudentTable
25 from functions import exec_rcode, check_Rresult
26
27
28 class StudentDialog(wx.Dialog):
29
30     def __init__(
31             self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition, 
32             style=wx.DEFAULT_DIALOG_STYLE
33             ):
34         wx.Dialog.__init__(self)                       # 1
35         self.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)    # 2
36         self.Create(parent, ID, title)                 # 3
37         Filename=parent.PATH[0]
38         self.content=parent.table[:]
39         self.HEADER=parent.header[:]
40         fnb={}
41         inb={}
42         cnb={}
43         vide={}
44         #FIXME : assume une premiere ligne contenant les noms de colonnes
45         for line in self.content:
46             i=0
47             for val in line :
48                 if val=='':
49                     if i in vide:
50                         vide[i]+=1
51                     else:
52                         vide[i]=1
53                 else:
54                     try:
55                         int(val)
56                         if i in inb:
57                             inb[i]+=1
58                         else:
59                             inb[i]=1
60                     except:
61                         try:
62                             float(val)
63                             if i in fnb:
64                                 fnb[i]+=1
65                             else:
66                                 fnb[i]=1
67                         except:
68                             if i in cnb:
69                                 cnb[i]+=1
70                             else:
71                                 cnb[i]=1
72                 i+=1
73         dicttot={}
74         for key,nb in vide.items():
75             dicttot[key]=['vide',nb]
76         print(dicttot)
77         for key,nb in inb.items() :
78             if key in dicttot:
79                 dicttot[key]=['int',dicttot[key][1]+nb]
80             else:
81                 dicttot[key]=['int',nb]
82         for key,nb in fnb.items():
83             if key in dicttot:
84                 dicttot[key]=['float',dicttot[key][1]+nb]
85             else:
86                 dicttot[key]=['float',nb]
87         for key,nb in cnb.items():
88             if key in dicttot:
89                 dicttot[key]=['char',dicttot[key][1]+nb]
90             else:
91                 dicttot[key]=['char',nb]
92         acontent=array(self.content)
93         self.ListGrp=[]
94         lg=[i for i,descr in dicttot.items() if descr[0]=='char']
95         for i in lg:
96             if len(unique(acontent[:,i]))==2:
97                 self.ListGrp.append(i)
98             elif ('' in unique(acontent[:,i]).tolist()) and len(unique(acontent[:,i]))==3:
99                 self.ListGrp.append(i)
100         li=[i for i,descr in dicttot.items() if descr[0]=='int']
101         lf=[i for i,descr in dicttot.items() if descr[0]=='float']
102         self.ListNum=li+lf
103         print(self.ListGrp, self.ListNum)    
104         LABELLIST=[]
105         for i in self.HEADER:
106             if len(i)>60 :
107                 LABELLIST.append(i[0:60])
108             else:
109                 LABELLIST.append(i)
110         self.LabelListGrp=[]
111         self.LabelListNum=[]
112         for i in self.ListGrp :
113             self.LabelListGrp.append(LABELLIST[i])
114         for i in self.ListNum :
115             self.LabelListNum.append(LABELLIST[i])
116         self.list_box_1 = wx.ListBox(self, -1, choices=self.LabelListGrp, style=wx.LB_MULTIPLE|wx.LB_HSCROLL)
117         self.list_box_2 = wx.ListBox(self, -1, choices=self.LabelListNum, style=wx.LB_MULTIPLE|wx.LB_HSCROLL)
118         self.button_1 = wx.Button(self, wx.ID_OK)
119         self.button_cancel = wx.Button(self, wx.ID_CANCEL)
120         self.__set_properties()
121         self.__do_layout()
122         self.Bind(wx.EVT_LISTBOX, self.Select1, self.list_box_1)
123         # end wxGlade
124         self.TEMPDIR=parent.TEMPDIR
125         self.parent=parent
126         self.Filename=parent.fileforR
127         #--------------FIXME
128         self.num=parent.FreqNum
129         #-------------------------------
130
131     def __set_properties(self):
132         # begin wxGlade: ConfChi2.__set_properties
133         self.SetTitle("Sélection des variables")
134         self.list_box_1.SetSelection(0)
135         self.list_box_2.SetSelection(0)
136         # end wxGlade
137
138     def __do_layout(self):
139         # begin wxGlade: ConfChi2.__do_layout
140         sizer_1 = wx.BoxSizer(wx.VERTICAL)
141         sizer_2 = wx.BoxSizer(wx.VERTICAL)
142         sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
143         sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
144         sizer_3.Add(self.list_box_1, 0, wx.EXPAND, 0)
145         sizer_3.Add(self.list_box_2, 0, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL, 0)
146         sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
147         sizer_4.Add(self.button_cancel, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 0)
148         sizer_4.Add(self.button_1, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 0)
149         sizer_2.Add(sizer_4, 0, wx.ALIGN_CENTRE_HORIZONTAL, 0)
150         sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
151         self.SetSizer(sizer_1)
152         sizer_1.Fit(self)
153         self.Layout()
154         # end wxGlade
155
156     def Select1(self, event): # wxGlade: ConfChi2.<event_handler>
157         event.Skip()
158
159     def ShowStudent(self,select1,select2):
160         parent=self.parent
161         self.encode=self.parent.SysEncoding
162         #################################################
163         #max = len(select1)*len(select2)
164         #dlg = wx.ProgressDialog("Traitements",
165         #                         "Veuillez patienter...",
166         #                         maximum = max,
167         #                         parent=self,
168         #                         style = wx.PD_APP_MODAL
169         #                       )
170         #dlg.Center()
171         #count = 0
172         ###############################################
173         Graph=True
174         colgrp=[self.ListGrp[i] for i in select1]
175         colnum=[self.ListNum[i] for i in select2]
176         if len(select1)==1:
177             strcolgrp=str(tuple(colgrp)).replace(',','')
178         else:
179             strcolgrp=str(tuple(colgrp))
180         if len(select2)==1:
181             strcolnum=str(tuple(colnum)).replace(',','')
182         else:
183             strcolnum=str(tuple(colnum))
184         txtR=''
185         txtR+="""
186         source('%s')
187         """%self.parent.RscriptsPath['Rfunct']
188         if parent.g_id: rownames='1'
189         else : rownames='NULL'
190         if parent.g_header : header = 'TRUE'
191         else : header = 'FALSE'
192         #txtR+="""
193         #datadm <- ReadData('%s', encoding='%s',header = %s, sep = '%s',quote = '%s', na.strings = '%s',rownames=%s)
194         txtR += """
195         datadm <- read.csv2('%s', encoding='%s',header = %s, sep = '%s',quote = '%s', na.strings = '%s',row.names=%s, dec='.')
196         """%(ffr(self.Filename),parent.encode,header, parent.colsep,parent.txtsep,parent.nastrings,rownames)
197         txtR+="""
198         num<-%i
199         """%self.num
200         txtR+="""
201         tmpdir<-'%s'
202         """%ffr(self.TEMPDIR)
203         txtR+="""
204         out<-matrix(0,0,1)
205         count<-0
206         for (i in c%s) {
207         """%strcolgrp
208         txtR+="""
209             for (j in c%s) {
210         """%strcolnum
211         txtR+="""
212                 datadm[,(j+1)]<-as.numeric(datadm[,(j+1)])
213                 count<-count+1
214                 fileout<-paste('student',num,sep='')
215                 fileout<-paste(fileout,'_',sep='')
216                 fileout<-paste(fileout,count,sep='')
217                 fileout<-paste(fileout,'.jpeg',sep='')
218                 fileout<-file.path(tmpdir,fileout)
219                 if (Sys.info()["sysname"]=='Darwin') {
220                     quartz(file=fileout,type='jpeg')
221                     par(cex=1)
222                 } else {
223                     jpeg(fileout,res=200)
224                     par(cex=0.4)
225                 }
226                 plot(datadm[,(j+1)] ~ datadm[,(i+1)],data=datadm)
227                 dev.off()
228                 student<-t.test(datadm[,(j+1)] ~ datadm[,(i+1)],data=datadm)
229                 pvalue<-student$p.value
230                 method<-student$method
231                 tvalue<-student$statistic
232                 df<-student$parameter
233                 grmean<-as.matrix(student$estimate)
234                 out<-rbind(out,round(grmean,digits=2))
235                 out<-rbind(out,pvalue)
236                 out<-rbind(out,method)
237                 out<-rbind(out,tvalue)
238                 out<-rbind(out,round(df,digits=2))
239                 out<-rbind(out,fileout)
240                 out<-rbind(out,"**")
241             }
242         }
243         """
244         restmp=tempfile.mktemp(dir=self.TEMPDIR)
245         txtR+="""
246         write.csv2(out,file='%s')
247         """%ffr(restmp)
248         tmpfile=tempfile.mktemp(dir=self.TEMPDIR)
249         tmpscript=open(tmpfile,'w')
250         tmpscript.write(txtR)
251         tmpscript.close()
252         pid = exec_rcode(self.parent.RPath, tmpfile, wait = False)
253         while pid.poll() == None :
254             sleep(0.2)
255         check_Rresult(self.parent, pid)
256         file=open(restmp,'r')
257         res=file.readlines()
258         file.close()
259         resl=[line.replace('\n','').replace('"','').split(';') for line in res]
260         resl.pop(0)
261         i=0
262         student={}
263         listr=[]
264         for line in resl :
265             if i==8 :
266                 i=0
267             if i==0 :
268                 student['grp1']=line[0].replace('mean in group ','')
269                 student['mean1']=float(line[1])
270             if i==1 :
271                 student['grp2']=line[0].replace('mean in group ','')
272                 student['mean2']=float(line[1])     
273             if i==2:
274                 student['p.value']=float(line[1]) 
275             if i==3:
276                 student['method']=line[1]
277             if i==4 :
278                 student['t']=float(line[1])
279             if i==5 :
280                 student['df']=float(line[1])
281             if i==6:
282                 student['graph']=line[1]
283             if i==7:
284                 listr.append(student)
285                 student={}
286             i+=1
287         txt2=''
288         ancre=0
289         LISTFILE=[]
290         LISTFILE.append(False)
291         txt=MakeHeader('T de Student', self.encode)
292         ListGraph=[]
293         for i in select1 :
294             for j in select2:
295                 ancre+=1
296                 Student=listr[ancre-1]
297                 pvalue=Student['p.value']
298                 Colname=self.LabelListNum[j]
299                 Colgrp=self.LabelListGrp[i]
300                 LISTFILE.append(Student['graph'])
301                 if pvalue<0.05:
302                     color='green'
303                 else:
304                     color='red'
305                 txt+="<a href=#%s><font color=%s>%s</font></a><br />"%(ancre,color,Colname+' / '+Colgrp) 
306                 txt2+=MakeStudentTable(Student,self.num,ancre,Graph,os.path.basename(Student['graph']),Colname,self.encode)                
307         txt+=txt2
308         fileout=os.path.join(self.TEMPDIR,'resultats-student_%s.html'%str(self.num))
309         File=open(fileout,'w')
310         File.write(txt)
311         File.close()
312         LISTFILE.append(fileout)
313 #        dlg.Destroy()
314         return LISTFILE
315
316
317 class MakeStudent():
318
319     def __init__(self,parent):
320         dlg = StudentDialog(parent, -1, "Student", size=(350, 400),
321                          style = wx.DEFAULT_DIALOG_STYLE
322                          )
323         dlg.CenterOnScreen()
324         val = dlg.ShowModal()
325         if val==wx.ID_OK :
326             ColSel1=dlg.list_box_1.GetSelections()
327             ColSel2=dlg.list_box_2.GetSelections()
328             listfileout=dlg.ShowStudent(ColSel1,ColSel2)
329             parent.FreqNum+=1
330             parent.DictTab["t de student_%s*"%parent.FreqNum]=listfileout
331             parent.FileTabList.append(listfileout)
332             parent.newtab=wx.html.HtmlWindow(parent.nb, -1)
333             if "gtk2" in wx.PlatformInfo:
334                 parent.newtab.SetStandardFonts()
335             parent.newtab.LoadPage(listfileout[len(listfileout)-1])
336             parent.nb.AddPage(parent.newtab,"t de student_%s*"%parent.FreqNum)
337             parent.nb.SetSelection(parent.nb.GetPageCount()-1)
338             parent.ShowTab(wx.EVT_BUTTON)
339             parent.DisEnSaveTabAs(True)