2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008 Pierre Ratinaud
7 #from __future__ import division
10 from chemins import ffr, FFF
12 from time import sleep
13 from analysematrix import AnalyseMatrix
14 from functions import exec_rcode, check_Rresult
15 from dialog import FreqDialog
16 from PrintRScript import PrintRScript
18 class Frequences(AnalyseMatrix) :
19 def doparametres(self, dlg=None) :
23 dial = FreqDialog(self.parent, self.tableau.get_colnames(), u"Fréquences")
25 val = dial.ShowModal()
27 self.parametres['colsel'] = dial.m_listBox1.GetSelections()
28 self.parametres['header'] = dial.header
29 self.parametres['NA'] = dial.includeNA.GetValue()
31 self.parametres = None
35 self.pathout.createdir(self.parametres['pathout'])
36 header = self.tableau.get_colnames()
37 select = self.parametres['colsel']
38 self.listtitre = [header[i] for i in select]
39 b, self.outframe = tempfile.mkstemp()
40 self.fileforR = [ffr(os.path.join(self.pathout.dirout, 'freq_%i.png' % i)) for i in range(len(select))]
41 self.Rscript = PrintRScript(self)
42 sel = 'c(' + ','.join([str(val + 1) for val in select]) + ')'
43 listfiles = 'c("' + '","'.join(self.fileforR) + '")'
44 titles = 'c("' + '","'.join(self.listtitre) + '")'
48 dm <- read.csv2(filein, encoding = encoding, header = TRUE, row.names = 1, sep='\\t', quote = '"', na.string = '')
49 """ %(ffr(self.tableau.parametres['csvfile']), self.tableau.parametres['syscoding'])
51 outframe <- data.frame(cbind('***','****','****'))
52 colnames(outframe)<-c('effectif','pourcentage', 'labels')
57 """ % (sel, listfiles, titles)
59 if self.parametres['NA'] :
71 freq <- table(dm[,i], useNA = 'ifany')
76 pour <- prop.table(as.matrix(freq), 2) * 100
78 pour <- round(pour, 2)
79 ntable <- cbind(as.matrix(freq), pour)
80 graphout <- listfiles[compteur]
81 if (Sys.info()["sysname"]=='Darwin') {
82 quartz(file=graphout,type='png')
88 if (max(nchar(rownames(ntable))) > 15) {
89 lab.bar <- 1:nrow(ntable)
91 lab.bar <- rownames(ntable)
93 barplot(ntable[,2],border=NA,beside=TRUE,names.arg=lab.bar)
94 ntable <- cbind(ntable, rownames(as.matrix(freq)))
95 colnames(ntable) <- c('effectif','pourcentage', 'labels')
96 title(main=titles[compteur])
98 ntable<-rbind(ntable,total=c(sumfreq,sumpour,''))
99 outframe<-rbind(outframe,c('***','****','****'))
100 #datasum[,1]<-as.character(datasum[,1])
101 #datasum[,2]<-as.character(datasum[,2])
102 outframe<-rbind(outframe,ntable)
103 compteur <- compteur + 1
105 outframe<-rbind(outframe,c('***','****','****'))
106 write.table(outframe, file="%s", sep="\\t")
107 """ % ffr(self.outframe)
108 self.Rscript.add(txt)
110 self.doR(self.Rscript.scriptout)
116 with open(self.outframe) as f :
117 content = f.read().splitlines()
120 content = ['\t'.join(line.split('\t')[1:]).replace('"','') for line in content]
121 content = '\n'.join(content)
122 content = content.split(u'***\t****\t****')
123 content = [[line.split('\t') for line in tab.splitlines() if line.split('\t') != ['']] for tab in content]
124 listtab = [tab for tab in content if tab != []]
126 #for ligne in content:
127 # ligne = ligne.replace('"', '')
128 # ligne = ligne.split('\t')
129 # if ligne[1] == u'***' :
131 # listtab.append(tab)
135 pretexte = u'''<html>
136 <meta http-equiv="content-Type" content="text/html; charset=%s" />
137 <body>\n<h1>Fréquences</h1>
138 <a name="deb"></a><br>
139 ''' % self.parent.SysEncoding
140 for i in range(0, len(listtab)):
141 pretexte += '<p><a href="#%s">%s</a></p>' % (str(i), self.listtitre[i])
142 texte += '<hr size="5" align="center" width="50%" color="green">\n'
143 texte += '<p><a href="#deb">Retour</a></p>\n'
144 texte += '<a name="%s"></a><h2>%s</h2>\n' % (str(i), self.listtitre[i])
145 texte += '<table>\n<tr><td>\n'
146 texte += '<table border=1><tr><td></td><td>Effectifs</td><td>pourcentage</td></tr>'
147 for line in listtab[i] :
150 <td>%s</td><td align=center>%s</td><td align=center>%s %%</td>
151 """ % (line[2], line[0], line[1])
153 texte += '</table></td>'
155 <td><img src="%s" alt="graph"/></td></tr></table>\n
156 """ % os.path.basename(self.fileforR[i])
157 texte += '</body>\n</html>'
158 fileout = os.path.join(self.pathout.dirout, 'resultats.html')
159 with open(fileout, 'w') as f :
160 f.write(pretexte + texte)