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