2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008-2009 Pierre Ratinaud
7 from chemins import ConstructPathOut, ChdTxtPathOut, ConstructAfcUciPath, ffr, PathOut
8 from functions import sortedby, CreateIraFile, print_liste, exec_rcode, check_Rresult
9 from PrintRScript import RchdQuest
10 from layout import OpenCHDS, PrintRapport
11 from dialog import PrefQuestAlc
12 from analysematrix import AnalyseMatrix
20 class AnalyseQuest(AnalyseMatrix):
21 def doparametres(self, dlg = None):
23 dial = PrefQuestAlc(self.parent, self.tableau)
25 self.val = dial.ShowModal()
26 #parametres = self.tableau.parametre
27 if self.val == wx.ID_OK :
28 self.parametres['nbcl_p1'] = dial.spin_nbcl.GetValue()
29 self.parametres['mincl'] = dial.spin_mincl.GetValue()
30 if dial.m_radioBox1.GetSelection() == 1 :
31 self.parametres['listact'] = dial.nactives
32 self.parametres['listsup'] = dial.varsup
34 self.parametres['formatted'] = 1
36 self.parametres = None
38 # DoQuestAlceste(parent, parametres)
40 #class DoQuestAlceste(AnalyseMatrix):
42 #parametres['pathout'] = ConstructPathOut(parent.tableau.parametre['filename'], 'ReinertMatrix')
43 #self.parametres = parametres
44 #self.parametres['type'] = 'reinertmatrix'
46 self.DictFormeSup = {}
50 #self.RPath = self.parent.PathPath.get('PATHS', 'rpath')
51 #self.dictpathout = PathOut(dirout = self.pathout)
52 #self.dictpathout = self.pathout
53 #self.dictpathout.basefiles(ChdTxtPathOut)
54 #self.pathout = self.dictpathout
56 self.ListAct = self.parametres.get('listact', False)
58 #dlg = wx.ProgressDialog("Traitements",
59 # "Veuillez patienter...",
62 # style=wx.PD_APP_MODAL | wx.PD_AUTO_HIDE | wx.PD_ELAPSED_TIME
65 #AnalyseMatrix.__init__(self, parent, parent.tableau, self.parametres, dlg = dlg)
69 #-----------------------------------------------------------
70 # def doanalyse(self) :
71 #-------------------------------------------------------------------
72 #self.dictpathout = self.pathout
73 self.pathout.basefiles(ChdTxtPathOut)
74 self.tableau.pathout.basefiles(ChdTxtPathOut)
75 # self.parent.tableau.dictpathout = self.dictpathout
79 keepGoing = self.dlg.Update(count)
80 #-------------------------------------------------------------------
82 self.dlg.Update(count, u"passage en O/1")
83 if 'formatted' in self.parametres:
84 self.tableau.make_01_alc_format(self.pathout['mat01.csv'])
86 print self.parametres['listsup']
87 self.tableau.make_01_from_selection(self.parametres['listact'], self.parametres['listsup'])
88 file = open(self.pathout['listeuce1'], 'w')
89 file.write('num uce;num uc\n')
90 for i in range(0, len(self.tableau.linecontent)):
91 file.write('%i;%i\n' % (i, i))
93 self.nbind = len(self.tableau.linecontent)
94 #------------------------------------------------------------
95 RchdQuest(self.pathout, self.parent.RscriptsPath, self.parametres['nbcl_p1'], self.parametres['mincl'])
96 #------------------------------------------------------------
98 self.dlg.Update(count, u"Analyse (patientez...)")
100 pid = exec_rcode(self.parent.RPath, self.pathout['Rchdquest'], wait = False)
101 while pid.poll() == None :
102 self.dlg.Pulse(u"Analyse (patientez...)")
104 check_Rresult(self.parent, pid)
105 #------------------------------------------------------------
107 self.dlg.Update(count, u"Ecriture des résultats")
108 self.tableau.buildprofil()
109 self.clnb = self.tableau.clnb
110 self.parametres['clnb'] = self.clnb
111 self.ucecla = self.tableau.ucecla
113 temps = time.time() - self.t1
114 PrintRapport(self, self, {}, istxt = False)
115 self.tableau.save_tableau(self.pathout['db'])
116 #CreateIraFile(self.dictpathout, self.clnb, corpname = os.path.basename(self.parent.filename), section = 'questionnaire')
117 afc_graph_list = [[os.path.basename(self.pathout['AFC2DL_OUT']), u'Variables actives - coordonnées - facteurs 1 / 2'],
118 [os.path.basename(self.pathout['AFC2DSL_OUT']), u'variables illustratives - coordonnées - facteurs 1 / 2'],
119 [os.path.basename(self.pathout['AFC2DCL_OUT']), u'Classes - Coordonnées - facteur 1 / 2'],]
120 chd_graph_list = [[os.path.basename(self.pathout['dendro1']), u'dendrogramme à partir de chd1']]
121 chd_graph_list.append([os.path.basename(self.pathout['arbre1']), u'chd1'])
122 print_liste(self.pathout['liste_graph_afc'], afc_graph_list)
123 print_liste(self.pathout['liste_graph_chd'], chd_graph_list)
125 #self.tableau = self.parent.tableau
126 #OpenCHDS(self.parent, self, self.dictpathout['ira'], False)
127 #------------------------------------------------------------
128 print 'fini', time.time() - self.t1
130 self.dlg.Update(count, "Fini")
132 def BuildProfile(self):
133 print 'build profile'
137 """ % ffr(self.parent.RscriptsPath['chdfunct'])
140 """ % ffr(self.pathout['RData'])
142 dataact<-read.csv2("%s", header = FALSE, sep = ';',quote = '\"', row.names = 1, na.strings = 'NA')
143 """ % ffr(self.pathout['Contout'])
145 dataet<-read.csv2("%s", header = FALSE, sep = ';',quote = '\"', row.names = 1, na.strings = 'NA')
146 """ % ffr(self.pathout['ContEtOut'])
151 tablesqrpact<-BuildProf(as.matrix(dataact),n1,clnb)
152 tablesqrpet<-BuildProf(as.matrix(dataet),n1,clnb)
153 PrintProfile(n1,tablesqrpact[4],tablesqrpet[4],tablesqrpact[5],tablesqrpet[5],%i,"%s","%s")
154 """ % (self.clnb, ffr(self.pathout['PROFILE_OUT']), ffr(self.pathout['ANTIPRO_OUT']))
156 colnames(tablesqrpact[[2]])<-paste('classe',1:clnb,sep=' ')
157 colnames(tablesqrpact[[1]])<-paste('classe',1:clnb,sep=' ')
158 colnames(tablesqrpet[[2]])<-paste('classe',1:clnb,sep=' ')
159 colnames(tablesqrpet[[1]])<-paste('classe',1:clnb,sep=' ')
160 chistabletot<-rbind(as.data.frame(tablesqrpact[2]),as.data.frame(tablesqrpet[2]))
161 ptabletot<-rbind(as.data.frame(tablesqrpact[1]),as.data.frame(tablesqrpet[1]))
163 write.csv2(chistabletot,file="%s")
164 """ % ffr(self.pathout['chisqtable'])
166 write.csv2(ptabletot,file="%s")
167 """ % ffr(self.pathout['ptable'])
169 write.csv2(gbcluster,file="%s")
170 """ % ffr(self.pathout['SbyClasseOut'])
174 rowtot<-nrow(dataact)+nrow(dataet)
175 afctable<-rbind(as.matrix(dataact),as.matrix(dataet))
176 colnames(afctable)<-paste('classe',1:clnb,sep=' ')
177 afc<-ca(afctable,suprow=((nrow(dataact)+1):rowtot),nd=(ncol(afctable)-1))
178 debet<-nrow(dataact)+1
181 afc<-AddCorrelationOk(afc)
183 """ % ffr(self.parent.RscriptsPath['Rgraph'])
186 afc <- summary.ca.dm(afc)
187 afc_table <- create_afc_table(afc)
188 write.csv2(afc_table$facteur, file = "%s")
189 write.csv2(afc_table$colonne, file = "%s")
190 write.csv2(afc_table$ligne, file = "%s")
191 """ % (ffr(self.pathout['afc_facteur']), ffr(self.pathout['afc_col']), ffr(self.pathout['afc_row']))
194 xlab <- paste('facteur 1 - ', round(afc$facteur[1,2],2), sep = '')
195 ylab <- paste('facteur 2 - ', round(afc$facteur[2,2],2), sep = '')
196 xlab <- paste(xlab, ' %', sep = '')
197 ylab <- paste(ylab, ' %', sep = '')
204 xyminmax <- PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='coord', deb=1, fin=(debet-1), xlab = xlab, ylab = ylab)
205 """ % (ffr(self.pathout['AFC2DL_OUT']))
207 PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='coord', deb=debet, fin=fin, xlab = xlab, ylab = ylab, xmin = xyminmax$xminmax[1], xmax = xyminmax$xminmax[2], ymin = xyminmax$yminmax[1], ymax = xyminmax$yminmax[2])
208 """ % (ffr(self.pathout['AFC2DSL_OUT']))
210 PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", col = TRUE, what='coord', xlab = xlab, ylab = ylab, xmin = xyminmax$xminmax[1], xmax = xyminmax$xminmax[2], ymin = xyminmax$yminmax[1], ymax = xyminmax$yminmax[2])
211 """ % (ffr(self.pathout['AFC2DCL_OUT']))
213 save.image(file="%s")
214 """ % ffr(self.pathout['RData'])
215 tmpfile = tempfile.mktemp(dir=self.parent.TEMPDIR)
216 tmpscript = open(tmpfile, 'w')
219 pid = exec_rcode(self.parent.RPath, tmpfile, wait = False)
220 while pid.poll() == None :
222 check_Rresult(self.parent, pid)
223 temps = time.time() - self.t1
224 self.minutes, self.seconds = divmod(temps, 60)
225 self.hours, self.minutes = divmod(self.minutes, 60)