...
[iramuteq] / tabstudent.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008 Pierre Ratinaud
5 #License: 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 functions import exec_rcode, check_Rresult
16 from time import sleep
17
18
19 class StudentDialog(wx.Dialog):
20     def __init__(
21             self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition, 
22             style=wx.DEFAULT_DIALOG_STYLE
23             ):
24
25         pre = wx.PreDialog()
26         pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
27         pre.Create(parent, ID, title, pos, size, style)
28
29         self.PostCreate(pre)
30         
31         Filename=parent.PATH[0]
32         self.content=parent.table[:]
33         self.HEADER=parent.header[:]
34         fnb={}
35         inb={}
36         cnb={}
37         vide={}
38         #FIXME : assume une premiere ligne contenant les noms de colonnes
39         for line in self.content:
40             i=0
41             for val in line :
42                 if val==u'':
43                     if vide.has_key(i):
44                         vide[i]+=1
45                     else:
46                         vide[i]=1
47                 else:
48                     try:
49                         int(val)
50                         if inb.has_key(i):
51                             inb[i]+=1
52                         else:
53                             inb[i]=1
54                     except:
55                         try:
56                             float(val)
57                             if fnb.has_key(i):
58                                 fnb[i]+=1
59                             else:
60                                 fnb[i]=1
61                         except:
62                             if cnb.has_key(i):
63                                 cnb[i]+=1
64                             else:
65                                 cnb[i]=1
66                 i+=1
67         dicttot={}
68         for key,nb in vide.iteritems():
69             dicttot[key]=['vide',nb]
70         print dicttot
71         for key,nb in inb.iteritems() :
72             if dicttot.has_key(key):
73                 dicttot[key]=['int',dicttot[key][1]+nb]
74             else:
75                 dicttot[key]=['int',nb]
76         for key,nb in fnb.iteritems():
77             if dicttot.has_key(key):
78                 dicttot[key]=['float',dicttot[key][1]+nb]
79             else:
80                 dicttot[key]=['float',nb]
81         for key,nb in cnb.iteritems():
82             if dicttot.has_key(key):
83                 dicttot[key]=['char',dicttot[key][1]+nb]
84             else:
85                 dicttot[key]=['char',nb]
86
87         acontent=array(self.content)
88         self.ListGrp=[]
89
90         lg=[i for i,descr in dicttot.iteritems() if descr[0]=='char']
91         for i in lg:
92             if len(unique(acontent[:,i]))==2:
93                 self.ListGrp.append(i)
94             elif (u'' in unique(acontent[:,i]).tolist()) and len(unique(acontent[:,i]))==3:
95                 self.ListGrp.append(i)
96
97         li=[i for i,descr in dicttot.iteritems() if descr[0]=='int']
98         lf=[i for i,descr in dicttot.iteritems() if descr[0]=='float']
99         self.ListNum=li+lf
100         print self.ListGrp, self.ListNum    
101         LABELLIST=[]
102         for i in self.HEADER:
103             if len(i)>60 :
104                 LABELLIST.append(i[0:60])
105             else:
106                 LABELLIST.append(i)
107
108         self.LabelListGrp=[]
109         self.LabelListNum=[]
110         for i in self.ListGrp :
111             self.LabelListGrp.append(LABELLIST[i])
112         for i in self.ListNum :
113             self.LabelListNum.append(LABELLIST[i])
114         self.list_box_1 = wx.ListBox(self, -1, choices=self.LabelListGrp, style=wx.LB_MULTIPLE|wx.LB_HSCROLL)
115         self.list_box_2 = wx.ListBox(self, -1, choices=self.LabelListNum, style=wx.LB_MULTIPLE|wx.LB_HSCROLL)
116         self.button_1 = wx.Button(self, wx.ID_OK)
117         self.button_cancel = wx.Button(self, wx.ID_CANCEL)
118
119         self.__set_properties()
120         self.__do_layout()
121
122         self.Bind(wx.EVT_LISTBOX, self.Select1, self.list_box_1)
123         # end wxGlade
124         
125         self.TEMPDIR=parent.TEMPDIR
126         self.parent=parent
127         self.Filename=parent.fileforR
128 #--------------FIXME
129         self.num=parent.FreqNum
130 #-------------------------------
131     def __set_properties(self):
132         # begin wxGlade: ConfChi2.__set_properties
133         self.SetTitle(u"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
257         file=open(restmp,'rU')
258         res=file.readlines()
259         file.close()
260         resl=[line.replace('\n','').replace('"','').split(';') for line in res]
261         resl.pop(0)
262         i=0
263         student={}
264         listr=[]
265         for line in resl :
266             if i==8 :
267                 i=0
268             if i==0 :
269                 student['grp1']=line[0].replace('mean in group ','')
270                 student['mean1']=float(line[1])
271             if i==1 :
272                 student['grp2']=line[0].replace('mean in group ','')
273                 student['mean2']=float(line[1])     
274             if i==2:
275                 student['p.value']=float(line[1]) 
276             if i==3:
277                 student['method']=line[1]
278             if i==4 :
279                 student['t']=float(line[1])
280             if i==5 :
281                 student['df']=float(line[1])
282             if i==6:
283                 student['graph']=line[1]
284             if i==7:
285                 listr.append(student)
286                 student={}
287             i+=1
288         
289         txt2=''
290         ancre=0
291         LISTFILE=[]
292         LISTFILE.append(False)
293         txt=MakeHeader('T de Student', self.encode)
294         ListGraph=[]
295         for i in select1 :
296             for j in select2:
297                 ancre+=1
298                 Student=listr[ancre-1]
299                 pvalue=Student['p.value']
300                 Colname=self.LabelListNum[j]
301                 Colgrp=self.LabelListGrp[i]
302                 LISTFILE.append(Student['graph'])
303                 if pvalue<0.05:
304                     color='green'
305                 else:
306                     color='red'
307                 txt+="<a href=#%s><font color=%s>%s</font></a><br />"%(ancre,color,Colname+' / '+Colgrp) 
308                 txt2+=MakeStudentTable(Student,self.num,ancre,Graph,os.path.basename(Student['graph']),Colname,self.encode)                
309         txt+=txt2
310         fileout=os.path.join(self.TEMPDIR,'resultats-student_%s.html'%str(self.num))
311         File=open(fileout,'w')
312         File.write(txt)
313         File.close()
314         LISTFILE.append(fileout)
315 #        dlg.Destroy()
316         return LISTFILE
317
318
319 class MakeStudent():
320     def __init__(self,parent):
321         dlg = StudentDialog(parent, -1, u"Student", size=(350, 400),
322                          style = wx.DEFAULT_DIALOG_STYLE
323                          )
324         dlg.CenterOnScreen()
325         val = dlg.ShowModal()
326         if val==wx.ID_OK :
327             ColSel1=dlg.list_box_1.GetSelections()
328             ColSel2=dlg.list_box_2.GetSelections()
329             listfileout=dlg.ShowStudent(ColSel1,ColSel2)
330             parent.FreqNum+=1
331             parent.DictTab[u"t de student_%s*"%parent.FreqNum]=listfileout
332             parent.FileTabList.append(listfileout)
333             parent.newtab=wx.html.HtmlWindow(parent.nb, -1)
334             if "gtk2" in wx.PlatformInfo:
335                 parent.newtab.SetStandardFonts()
336             parent.newtab.LoadPage(listfileout[len(listfileout)-1])
337             parent.nb.AddPage(parent.newtab,u"t de student_%s*"%parent.FreqNum)
338             parent.nb.SetSelection(parent.nb.GetPageCount()-1)
339             parent.ShowTab(wx.EVT_BUTTON)
340             parent.DisEnSaveTabAs(True)