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, -1, self.tableau.get_colnames(), u"Fréquences", size=(350, 200))
25 val = dial.ShowModal()
27 self.parametres['colsel'] = dial.list_box_1.GetSelections()
28 self.parametres['header'] = dial.header
30 self.parametres = None
34 self.pathout.createdir(self.parametres['pathout'])
35 header = self.tableau.get_colnames()
36 select = self.parametres['colsel']
37 self.listtitre = [header[i] for i in select]
38 b, self.outframe = tempfile.mkstemp()
39 self.fileforR = [ffr(os.path.join(self.pathout.dirout, 'freq_%i.png' % i)) for i in range(len(select))]
40 self.Rscript = PrintRScript(self)
41 sel = 'c(' + ','.join([str(val + 1) for val in select]) + ')'
42 listfiles = 'c("' + '","'.join(self.fileforR) + '")'
43 titles = 'c("' + '","'.join(self.listtitre) + '")'
47 dm <- read.csv2(filein, encoding = encoding, header = TRUE, row.names = 1, sep='\\t', quote = '"', na.string = '')
48 """ %(ffr(self.tableau.parametres['csvfile']), self.tableau.parametres['syscoding'])
50 outframe <- data.frame(cbind('***','****','****'))
51 colnames(outframe)<-c('effectif','pourcentage', 'labels')
56 """ % (sel, listfiles, titles)
62 pour <- prop.table(as.matrix(freq), 2) * 100
64 ntable <- cbind(as.matrix(freq), pour)
65 graphout <- listfiles[compteur]
66 if (Sys.info()["sysname"]=='Darwin') {
67 quartz(file=graphout,type='png')
73 if (max(nchar(rownames(ntable))) > 15) {
74 lab.bar <- 1:nrow(ntable)
76 lab.bar <- rownames(ntable)
78 barplot(ntable[,2],border=NA,beside=TRUE,names.arg=lab.bar)
79 ntable <- cbind(ntable, rownames(as.matrix(freq)))
80 colnames(ntable) <- c('effectif','pourcentage', 'labels')
81 title(main=titles[compteur])
83 ntable<-rbind(ntable,total=c(sumfreq,sumpour,''))
84 outframe<-rbind(outframe,c('***','****','****'))
85 #datasum[,1]<-as.character(datasum[,1])
86 #datasum[,2]<-as.character(datasum[,2])
87 outframe<-rbind(outframe,ntable)
88 compteur <- compteur + 1
90 outframe<-rbind(outframe,c('***','****','****'))
91 write.table(outframe, file="%s", sep="\\t")
92 """ % ffr(self.outframe)
95 self.doR(self.Rscript.scriptout)
101 with open(self.outframe) as f :
102 content = f.read().splitlines()
106 for ligne in content:
107 ligne = ligne.replace('"', '')
108 ligne = ligne.split('\t')
109 if ligne[1] == u'***' :
115 pretexte = u'''<html>
116 <meta http-equiv="content-Type" content="text/html; charset=%s" />
117 <body>\n<h1>Fréquences</h1>
118 <a name="deb"></a><br>
119 ''' % self.parent.SysEncoding
120 for i in range(0, len(listtab)):
121 pretexte += '<p><a href="#%s">%s</a></p>' % (str(i), self.listtitre[i])
122 texte += '<hr size="5" align="center" width="50%" color="green">\n'
123 texte += '<p><a href="#deb">Retour</a></p>\n'
124 texte += '<a name="%s"></a><h2>%s</h2>\n' % (str(i), self.listtitre[i])
125 texte += '<table>\n<tr><td>\n'
126 texte += '<table border=1><tr><td></td><td>Effectifs</td><td>pourcentage</td></tr>'
127 for line in listtab[i] :
130 <td>%s</td><td align=center>%s</td><td align=center>%s %%</td>
131 """ % (line[3], line[1], line[2])
133 texte += '</table></td>'
135 <td><img src="%s" alt="graph"/></td></tr></table>\n
136 """ % os.path.basename(self.fileforR[i])
137 texte += '</body>\n</html>'
138 fileout = os.path.join(self.pathout.dirout, 'resultats.html')
139 with open(fileout, 'w') as f :
140 f.write(pretexte + texte)