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
15 from functions import exec_rcode, check_Rresult
16 from time import sleep
19 class StudentDialog(wx.Dialog):
21 self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition,
22 style=wx.DEFAULT_DIALOG_STYLE
26 pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
27 pre.Create(parent, ID, title, pos, size, style)
31 Filename=parent.PATH[0]
32 self.content=parent.table[:]
33 self.HEADER=parent.header[:]
38 #FIXME : assume une premiere ligne contenant les noms de colonnes
39 for line in self.content:
68 for key,nb in vide.iteritems():
69 dicttot[key]=['vide',nb]
71 for key,nb in inb.iteritems() :
72 if dicttot.has_key(key):
73 dicttot[key]=['int',dicttot[key][1]+nb]
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]
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]
85 dicttot[key]=['char',nb]
87 acontent=array(self.content)
90 lg=[i for i,descr in dicttot.iteritems() if descr[0]=='char']
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)
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']
100 print self.ListGrp, self.ListNum
102 for i in self.HEADER:
104 LABELLIST.append(i[0:60])
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)
119 self.__set_properties()
122 self.Bind(wx.EVT_LISTBOX, self.Select1, self.list_box_1)
125 self.TEMPDIR=parent.TEMPDIR
127 self.Filename=parent.fileforR
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)
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)
156 def Select1(self, event): # wxGlade: ConfChi2.<event_handler>
159 def ShowStudent(self,select1,select2):
161 self.encode=self.parent.SysEncoding
162 #################################################
163 # max = len(select1)*len(select2)
164 # dlg = wx.ProgressDialog("Traitements",
165 # "Veuillez patienter...",
168 # style = wx.PD_APP_MODAL
172 ###############################################
174 colgrp=[self.ListGrp[i] for i in select1]
175 colnum=[self.ListNum[i] for i in select2]
177 strcolgrp=str(tuple(colgrp)).replace(',','')
179 strcolgrp=str(tuple(colgrp))
181 strcolnum=str(tuple(colnum)).replace(',','')
183 strcolnum=str(tuple(colnum))
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'
193 #datadm <- ReadData('%s', encoding='%s',header = %s, sep = '%s',quote = '%s', na.strings = '%s',rownames=%s)
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)
202 """%ffr(self.TEMPDIR)
212 datadm[,(j+1)]<-as.numeric(datadm[,(j+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')
223 jpeg(fileout,res=200)
226 plot(datadm[,(j+1)] ~ datadm[,(i+1)],data=datadm)
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)
244 restmp=tempfile.mktemp(dir=self.TEMPDIR)
246 write.csv2(out,file='%s')
248 tmpfile=tempfile.mktemp(dir=self.TEMPDIR)
249 tmpscript=open(tmpfile,'w')
250 tmpscript.write(txtR)
252 pid = exec_rcode(self.parent.RPath, tmpfile, wait = False)
253 while pid.poll() == None :
255 check_Rresult(self.parent, pid)
257 file=open(restmp,'rU')
260 resl=[line.replace('\n','').replace('"','').split(';') for line in res]
269 student['grp1']=line[0].replace('mean in group ','')
270 student['mean1']=float(line[1])
272 student['grp2']=line[0].replace('mean in group ','')
273 student['mean2']=float(line[1])
275 student['p.value']=float(line[1])
277 student['method']=line[1]
279 student['t']=float(line[1])
281 student['df']=float(line[1])
283 student['graph']=line[1]
285 listr.append(student)
292 LISTFILE.append(False)
293 txt=MakeHeader('T de Student', self.encode)
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'])
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)
310 fileout=os.path.join(self.TEMPDIR,'resultats-student_%s.html'%str(self.num))
311 File=open(fileout,'w')
314 LISTFILE.append(fileout)
320 def __init__(self,parent):
321 dlg = StudentDialog(parent, -1, u"Student", size=(350, 400),
322 style = wx.DEFAULT_DIALOG_STYLE
325 val = dlg.ShowModal()
327 ColSel1=dlg.list_box_1.GetSelections()
328 ColSel2=dlg.list_box_2.GetSelections()
329 listfileout=dlg.ShowStudent(ColSel1,ColSel2)
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)