2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008 Pierre Ratinaud
8 from chemins import ffr
9 from layout import MakeHeader,MakeStudentTable
16 from functions import exec_rcode, check_Rresult
17 from time import sleep
20 class StudentDialog(wx.Dialog):
22 self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition,
23 style=wx.DEFAULT_DIALOG_STYLE
27 pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
28 pre.Create(parent, ID, title, pos, size, style)
32 Filename=parent.PATH[0]
33 self.content=parent.table[:]
34 self.HEADER=parent.header[:]
39 #FIXME : assume une premiere ligne contenant les noms de colonnes
40 for line in self.content:
69 for key,nb in vide.iteritems():
70 dicttot[key]=['vide',nb]
72 for key,nb in inb.iteritems() :
73 if dicttot.has_key(key):
74 dicttot[key]=['int',dicttot[key][1]+nb]
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]
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]
86 dicttot[key]=['char',nb]
88 acontent=array(self.content)
91 lg=[i for i,descr in dicttot.iteritems() if descr[0]=='char']
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)
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']
101 print self.ListGrp, self.ListNum
103 for i in self.HEADER:
105 LABELLIST.append(i[0:60])
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)
120 self.__set_properties()
123 self.Bind(wx.EVT_LISTBOX, self.Select1, self.list_box_1)
126 self.TEMPDIR=parent.TEMPDIR
128 self.Filename=parent.fileforR
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)
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)
157 def Select1(self, event): # wxGlade: ConfChi2.<event_handler>
160 def ShowStudent(self,select1,select2):
162 self.encode=self.parent.SysEncoding
163 #################################################
164 # max = len(select1)*len(select2)
165 # dlg = wx.ProgressDialog("Traitements",
166 # "Veuillez patienter...",
169 # style = wx.PD_APP_MODAL
173 ###############################################
175 colgrp=[self.ListGrp[i] for i in select1]
176 colnum=[self.ListNum[i] for i in select2]
178 strcolgrp=str(tuple(colgrp)).replace(',','')
180 strcolgrp=str(tuple(colgrp))
182 strcolnum=str(tuple(colnum)).replace(',','')
184 strcolnum=str(tuple(colnum))
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'
194 #datadm <- ReadData('%s', encoding='%s',header = %s, sep = '%s',quote = '%s', na.strings = '%s',rownames=%s)
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)
203 """%ffr(self.TEMPDIR)
213 datadm[,(j+1)]<-as.numeric(datadm[,(j+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')
224 jpeg(fileout,res=200)
227 plot(datadm[,(j+1)] ~ datadm[,(i+1)],data=datadm)
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)
245 restmp=tempfile.mktemp(dir=self.TEMPDIR)
247 write.csv2(out,file='%s')
249 tmpfile=tempfile.mktemp(dir=self.TEMPDIR)
250 tmpscript=open(tmpfile,'w')
251 tmpscript.write(txtR)
253 pid = exec_rcode(self.parent.RPath, tmpfile, wait = False)
254 while pid.poll() == None :
256 check_Rresult(self.parent, pid)
258 file=open(restmp,'rU')
261 resl=[line.replace('\n','').replace('"','').split(';') for line in res]
270 student['grp1']=line[0].replace('mean in group ','')
271 student['mean1']=float(line[1])
273 student['grp2']=line[0].replace('mean in group ','')
274 student['mean2']=float(line[1])
276 student['p.value']=float(line[1])
278 student['method']=line[1]
280 student['t']=float(line[1])
282 student['df']=float(line[1])
284 student['graph']=line[1]
286 listr.append(student)
293 LISTFILE.append(False)
294 txt=MakeHeader('T de Student', self.encode)
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'])
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)
311 fileout=os.path.join(self.TEMPDIR,'resultats-student_%s.html'%str(self.num))
312 File=open(fileout,'w')
315 LISTFILE.append(fileout)
321 def __init__(self,parent):
322 dlg = StudentDialog(parent, -1, u"Student", size=(350, 400),
323 style = wx.DEFAULT_DIALOG_STYLE
326 val = dlg.ShowModal()
328 ColSel1=dlg.list_box_1.GetSelections()
329 ColSel2=dlg.list_box_2.GetSelections()
330 listfileout=dlg.ShowStudent(ColSel1,ColSel2)
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)