1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2011 Pierre Ratinaud
7 from chemins import ffr, PathOut
10 from datetime import datetime
13 log = logging.getLogger('iramuteq.printRscript')
16 def __init__ (self, analyse):
18 self.pathout = analyse.pathout
19 self.analyse = analyse
20 self.parametres = analyse.parametres
21 self.scriptout = self.pathout['lastRscript.R']
22 self.script = u"#Script genere par IRaMuTeQ - %s\n" % datetime.now().ctime()
25 self.script = '\n'.join([self.script, txt])
27 def defvar(self, name, value) :
28 self.add(' <- '.join([name, value]))
30 def defvars(self, lvars) :
32 self.defvar(val[0],val[1])
34 def sources(self, lsources) :
35 for source in lsources :
36 self.add('source("%s", encoding = \'utf8\')' % ffr(source))
38 def packages(self, lpks) :
40 self.add('library(%s)' % pk)
44 self.add('load("%s")' % ffr(val))
47 with open(self.scriptout, 'w') as f :
51 class chdtxt(PrintRScript) :
55 return str(color).replace(')', ', max=255)')
57 class Alceste2(PrintRScript) :
59 self.sources(['chdfunct'])
61 lvars = [['clnb', `self.analyse.clnb`],
62 ['Contout', '"%s"' % self.pathout['Contout']],
63 ['ContSupOut', '"%s"' % self.pathout['ContSupOut']],
64 ['ContEtOut', '"%s"' % self.pathout['ContEtOut']],
65 ['profileout', '"%s"' % self.pathout['profils.csv']],
66 ['antiout', '"%s"' % self.pathout['antiprofils.csv']],
67 ['chisqtable', '"%s"' % self.pathout['chisqtable.csv']],
68 ['ptable', '"%s"' % self.pathout['ptable.csv']]]
74 # txt = "clnb<-%i\n" % clnb
78 #""" % (RscriptsPath['chdfunct'], DictChdTxtOut['RData'])
80 #dataact<-read.csv2("%s", header = FALSE, sep = ';',quote = '\"', row.names = 1, na.strings = 'NA')
81 #datasup<-read.csv2("%s", header = FALSE, sep = ';',quote = '\"', row.names = 1, na.strings = 'NA')
82 #dataet<-read.csv2("%s", header = FALSE, sep = ';',quote = '\"', row.names = 1, na.strings = 'NA')
83 #""" % (DictChdTxtOut['Contout'], DictChdTxtOut['ContSupOut'], DictChdTxtOut['ContEtOut'])
85 #tablesqrpact<-BuildProf(as.matrix(dataact),n1,clnb)
86 #tablesqrpsup<-BuildProf(as.matrix(datasup),n1,clnb)
87 #tablesqrpet<-BuildProf(as.matrix(dataet),n1,clnb)
90 #PrintProfile(n1,tablesqrpact[4],tablesqrpet[4],tablesqrpact[5],tablesqrpet[5],clnb,"%s","%s",tablesqrpsup[4],tablesqrpsup[5])
91 #""" % (DictChdTxtOut['PROFILE_OUT'], DictChdTxtOut['ANTIPRO_OUT'])
93 #colnames(tablesqrpact[[2]])<-paste('classe',1:clnb,sep=' ')
94 #colnames(tablesqrpact[[1]])<-paste('classe',1:clnb,sep=' ')
95 #colnames(tablesqrpsup[[2]])<-paste('classe',1:clnb,sep=' ')
96 #colnames(tablesqrpsup[[1]])<-paste('classe',1:clnb,sep=' ')
97 #colnames(tablesqrpet[[2]])<-paste('classe',1:clnb,sep=' ')
98 #colnames(tablesqrpet[[1]])<-paste('classe',1:clnb,sep=' ')
99 #chistabletot<-rbind(tablesqrpact[2][[1]],tablesqrpsup[2][[1]])
100 #chistabletot<-rbind(chistabletot,tablesqrpet[2][[1]])
101 #ptabletot<-rbind(tablesqrpact[1][[1]],tablesqrpet[1][[1]])
104 #write.csv2(chistabletot,file="%s")
105 #write.csv2(ptabletot,file="%s")
107 #write.csv2(gbcluster,file="%s")
108 #""" % (DictChdTxtOut['chisqtable'], DictChdTxtOut['ptable'], DictChdTxtOut['SbyClasseOut'])
112 def RchdTxt(DicoPath, RscriptPath, mincl, classif_mode, nbt = 9, svdmethod = 'svdR', libsvdc = False, libsvdc_path = None, R_max_mem = False, mode_patate = False):
118 """ % (ffr(RscriptPath['CHD']), ffr(RscriptPath['chdtxt']), ffr(RscriptPath['anacor']), ffr(RscriptPath['Rgraph']))
127 if svdmethod == 'svdlibc' and libsvdc :
129 svd.method <- 'svdlibc'
131 """ % ffr(libsvdc_path)
132 elif svdmethod == 'irlba' :
135 svd.method <- 'irlba'
153 data1 <- readMM("%s")
154 data1 <- as(data1, "dgCMatrix")
155 row.names(data1) <- 1:nrow(data1)
156 """ % ffr(DicoPath['TableUc1'])
158 if classif_mode == 0:
160 data2 <- readMM("%s")
161 data2 <- as(data2, "dgCMatrix")
162 row.names(data2) <- 1:nrow(data2)
163 """ % ffr(DicoPath['TableUc2'])
166 chd1<-CHD(data1, x = nbt, mode.patate = mode.patate, svd.method =
167 svd.method, libsvdc.path = libsvdc.path)#, log.file = log1)
168 """ % ffr(DicoPath['log-chd1.txt'])
170 if classif_mode == 0:
173 chd2<-CHD(data2, x = nbt, mode.patate = mode.patate, svd.method =
174 svd.method, libsvdc.path = libsvdc.path)#, log.file = log2)
175 """ % ffr(DicoPath['log-chd2.txt'])
179 listuce1<-read.csv2("%s")
180 """ % ffr(DicoPath['listeuce1'])
182 if classif_mode == 0:
184 listuce2<-read.csv2("%s")
185 """ % ffr(DicoPath['listeuce2'])
191 if classif_mode == 0:
199 if (classif_mode == 0) {
200 chd.result <- Rchdtxt(uceout, chd1, chd2 = chd2, mincl = mincl,classif_mode = classif_mode, nbt = nbt)
202 chd.result <- Rchdtxt(uceout, chd1, chd2 = chd1, mincl = mincl,classif_mode = classif_mode, nbt = nbt)
205 classeuce1 <- chd.result$cuce1
206 classes<-n1[,ncol(n1)]
207 write.csv2(n1, file="%s")
209 """ % (classif_mode, mincl, ffr(DicoPath['uce']), ffr(DicoPath['n1.csv']))
212 tree.tot1 <- make_tree_tot(chd1)
213 # open_file_graph("%s", widt = 600, height=400)
214 # plot(tree.tot1$tree.cl)
216 """ % ffr(DicoPath['arbre1'])
218 if classif_mode == 0:
220 classeuce2 <- chd.result$cuce2
221 tree.tot2 <- make_tree_tot(chd2)
222 # open_file_graph("%s", width = 600, height=400)
223 # plot(tree.tot2$tree.cl)
225 """ % ffr(DicoPath['arbre2'] )
228 tree.cut1 <- make_dendro_cut_tuple(tree.tot1$dendro_tuple, chd.result$coord_ok, classeuce1, 1, nbt)
229 save(tree.cut1, file="%s")
231 open_file_graph("%s", width = 600, height=400)
232 plot.dendropr(tree.cut1$tree.cl,classes, histo=TRUE)
233 open_file_graph("%s", width = 600, height=400)
234 plot(tree.cut1$dendro_tot_cl)
236 """ % (ffr(DicoPath['Rdendro']), ffr(DicoPath['dendro1']), ffr(DicoPath['arbre1']))
238 if classif_mode == 0:
240 tree.cut2 <- make_dendro_cut_tuple(tree.tot2$dendro_tuple, chd.result$coord_ok, classeuce2, 2, nbt)
241 open_file_graph("%s", width = 600, height=400)
242 plot(tree.cut2$tree.cl)
244 open_file_graph("%s", width = 600, height=400)
245 plot(tree.cut2$dendro_tot_cl)
247 """ % (ffr(DicoPath['dendro2']), ffr(DicoPath['arbre2']))
251 #save.image(file="%s")
252 """ % (ffr(DicoPath['RData']))
254 fileout = open(DicoPath['Rchdtxt'], 'w')
258 def RPamTxt(corpus, RscriptPath):
259 DicoPath = corpus.pathout
260 param = corpus.parametres
263 """ % (RscriptPath['pamtxt'])
266 """ % (RscriptPath['Rgraph'])
268 result <- pamtxt("%s", "%s", "%s", method = "%s", clust_type = "%s", clnb = %i)
270 """ % (DicoPath['TableUc1'], DicoPath['listeuce1'], DicoPath['uce'], param['method'], param['cluster_type'], param['nbcl'] )
272 open_file_graph("%s", width=400, height=400)
275 """ % (DicoPath['arbre1'])
277 save.image(file="%s")
278 """ % DicoPath['RData']
279 fileout = open(DicoPath['Rchdtxt'], 'w')
284 def RchdQuest(DicoPath, RscriptPath, nbcl = 10, mincl = 10):
290 """ % (ffr(RscriptPath['CHD']), ffr(RscriptPath['chdquest']), ffr(RscriptPath['anacor']),ffr(RscriptPath['Rgraph']))
298 chd.result<-Rchdquest("%s","%s","%s", nbt = nbt, mincl = mincl)
300 classeuce1 <- chd.result$cuce1
301 """ % (ffr(DicoPath['mat01.csv']), ffr(DicoPath['listeuce1']), ffr(DicoPath['uce']))
304 tree_tot1 <- make_tree_tot(chd.result$chd)
305 open_file_graph("%s", width = 600, height=400)
306 plot(tree_tot1$tree.cl)
308 """ % ffr(DicoPath['arbre1'])
311 tree_cut1 <- make_dendro_cut_tuple(tree_tot1$dendro_tuple, chd.result$coord_ok, classeuce1, 1, nbt)
312 tree.cut1 <- tree_cut1
313 save(tree.cut1, file="%s")
314 open_file_graph("%s", width = 600, height=400)
315 classes<-n1[,ncol(n1)]
316 plot.dendropr(tree_cut1$tree.cl,classes, histo = TRUE)
317 """ % (ffr(DicoPath['Rdendro']), ffr(DicoPath['dendro1']))
320 save.image(file="%s")
321 """ % ffr(DicoPath['RData'])
322 fileout = open(DicoPath['Rchdquest'], 'w')
326 def ReinertTxtProf(DictChdTxtOut, RscriptsPath, clnb, taillecar):
327 txt = "clnb<-%i\n" % clnb
331 n1 <- read.csv2("%s")
332 """ % (ffr(RscriptsPath['chdfunct']), ffr(DictChdTxtOut['RData']), ffr(DictChdTxtOut['n1.csv']))
334 dataact<-read.csv2("%s", header = FALSE, sep = ';',quote = '\"', row.names = 1, na.strings = 'NA')
335 datasup<-read.csv2("%s", header = FALSE, sep = ';',quote = '\"', row.names = 1, na.strings = 'NA')
336 dataet<-read.csv2("%s", header = FALSE, sep = ';',quote = '\"', row.names = 1, na.strings = 'NA')
337 """ % (ffr(DictChdTxtOut['Contout']), ffr(DictChdTxtOut['ContSupOut']), ffr(DictChdTxtOut['ContEtOut']))
339 tablesqrpact<-BuildProf(as.matrix(dataact),n1,clnb)
340 tablesqrpsup<-BuildProf(as.matrix(datasup),n1,clnb)
341 tablesqrpet<-BuildProf(as.matrix(dataet),n1,clnb)
344 PrintProfile(n1,tablesqrpact[4],tablesqrpet[4],tablesqrpact[5],tablesqrpet[5],clnb,"%s","%s",tablesqrpsup[4],tablesqrpsup[5])
345 """ % (ffr(DictChdTxtOut['PROFILE_OUT']), ffr(DictChdTxtOut['ANTIPRO_OUT']))
347 colnames(tablesqrpact[[2]])<-paste('classe',1:clnb,sep=' ')
348 colnames(tablesqrpact[[1]])<-paste('classe',1:clnb,sep=' ')
349 colnames(tablesqrpsup[[2]])<-paste('classe',1:clnb,sep=' ')
350 colnames(tablesqrpsup[[1]])<-paste('classe',1:clnb,sep=' ')
351 colnames(tablesqrpet[[2]])<-paste('classe',1:clnb,sep=' ')
352 colnames(tablesqrpet[[1]])<-paste('classe',1:clnb,sep=' ')
353 chistabletot<-rbind(tablesqrpact[2][[1]],tablesqrpsup[2][[1]])
354 chistabletot<-rbind(chistabletot,tablesqrpet[2][[1]])
355 ptabletot<-rbind(tablesqrpact[1][[1]],tablesqrpet[1][[1]])
358 write.csv2(chistabletot,file="%s")
359 write.csv2(ptabletot,file="%s")
361 write.csv2(gbcluster,file="%s")
362 """ % (ffr(DictChdTxtOut['chisqtable']), ffr(DictChdTxtOut['ptable']), ffr(DictChdTxtOut['SbyClasseOut']))
366 colnames(dataact)<-paste('classe',1:clnb,sep=' ')
367 colnames(datasup)<-paste('classe',1:clnb,sep=' ')
368 colnames(dataet)<-paste('classe',1:clnb,sep=' ')
369 rowtot<-nrow(dataact)+nrow(dataet)+nrow(datasup)
370 afctable<-rbind(as.matrix(dataact),as.matrix(datasup))
371 afctable<-rbind(afctable,as.matrix(dataet))
372 colnames(afctable)<-paste('classe',1:clnb,sep=' ')
373 afc<-ca(afctable,suprow=((nrow(dataact)+1):rowtot),nd=(ncol(afctable)-1))
374 debsup<-nrow(dataact)+1
375 debet<-nrow(dataact)+nrow(datasup)+1
377 afc<-AddCorrelationOk(afc)
379 #FIXME : split this!!!
382 """ % ffr(RscriptsPath['Rgraph'])
385 afc <- summary.ca.dm(afc)
386 afc_table <- create_afc_table(afc)
387 write.csv2(afc_table$facteur, file = "%s")
388 write.csv2(afc_table$colonne, file = "%s")
389 write.csv2(afc_table$ligne, file = "%s")
390 """ % (ffr(DictChdTxtOut['afc_facteur']), ffr(DictChdTxtOut['afc_col']), ffr(DictChdTxtOut['afc_row']))
396 xyminmax <- PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='coord', deb=1, fin=(debsup-1), xlab = xlab, ylab = ylab)
397 """ % (ffr(DictChdTxtOut['AFC2DL_OUT']))
399 PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='coord', deb=debsup, fin=(debet-1), xlab = xlab, ylab = ylab, xmin = xyminmax$xminmax[1], xmax = xyminmax$xminmax[2], ymin = xyminmax$yminmax[1], ymax = xyminmax$yminmax[2], active=FALSE)
400 """ % (ffr(DictChdTxtOut['AFC2DSL_OUT']))
402 if ((fin - debet) > 2) {
403 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], active = FALSE)
405 """ % (ffr(DictChdTxtOut['AFC2DEL_OUT']))
407 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], active=FALSE)
408 """ % (ffr(DictChdTxtOut['AFC2DCL_OUT']))
410 # PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='crl', deb=1, fin=(debsup-1), xlab = xlab, ylab = ylab)
411 # PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='crl', deb=debsup, fin=(debet-1), xlab = xlab, ylab = ylab)
412 # PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", what='crl', deb=debet, fin=fin, xlab = xlab, ylab = ylab)
413 # PlotAfc2dCoul(afc, as.data.frame(chistabletot), "%s", col=TRUE, what='crl', xlab = xlab, ylab = ylab)
414 # """ % (DictChdTxtOut['AFC2DCoul'], DictChdTxtOut['AFC2DCoulSup'], DictChdTxtOut['AFC2DCoulEt'], DictChdTxtOut['AFC2DCoulCl'])
423 save.image(file="%s")
424 """ % ffr(DictChdTxtOut['RData'])
425 file = open(DictChdTxtOut['RTxtProfGraph'], 'w')
430 def write_afc_graph(self):
431 if self.param['over'] : over = 'TRUE'
432 else : over = 'FALSE'
434 if self.param['do_select_nb'] : do_select_nb = 'TRUE'
435 else : do_select_nb = 'FALSE'
437 if self.param['do_select_chi'] : do_select_chi = 'TRUE'
438 else : do_select_chi = 'FALSE'
440 if self.param['do_select_chi_classe'] : do_select_chi_classe = 'TRUE'
441 else : do_select_chi_classe = 'FALSE'
443 if self.param['cex_txt'] : cex_txt = 'TRUE'
444 else : cex_txt = 'FALSE'
446 if self.param['tchi'] : tchi = 'TRUE'
447 else : tchi = 'FALSE'
449 if self.param['svg'] : svg = 'TRUE'
452 with open(self.RscriptsPath['afc_graph'], 'r') as f:
455 # self.DictPathOut['RData'], \
456 scripts = txt % (ffr(self.RscriptsPath['Rgraph']),\
457 self.param['typegraph'], \
458 self.param['what'], \
459 self.param['facteur'][0],\
460 self.param['facteur'][1], \
461 self.param['facteur'][2], \
463 over, do_select_nb, \
464 self.param['select_nb'], \
466 self.param['select_chi'], \
467 do_select_chi_classe, \
468 self.param['nbchic'], \
470 self.param['txt_min'], \
471 self.param['txt_max'], \
473 self.param['width'], \
474 self.param['height'],\
475 self.param['taillecar'], \
476 self.param['alpha'], \
477 self.param['film'], \
479 self.param['tchi_min'],\
480 self.param['tchi_max'],\
481 ffr(os.path.dirname(self.fileout)),\
485 def print_simi3d(self):
486 simi3d = self.parent.simi3dpanel
487 txt = '#Fichier genere par Iramuteq'
488 if simi3d.movie.GetValue() :
489 movie = "'" + ffr(os.path.dirname(self.DictPathOut['RData'])) + "'"
493 #if self.corpus.parametres['type'] == 'corpus' :
499 dm<-read.csv2("%s",row.names=1,header = %s)
501 """ % (self.DictPathOut['Contout'], header, self.DictPathOut['RData'])
505 """ % self.parent.RscriptsPath['Rgraph']
509 make.simi.afc(dm,chistabletot, lim=%i, alpha = %.2f, movie = %s)
510 """ % (simi3d.spin_1.GetValue(), float(simi3d.slider_1.GetValue())/100, movie)
511 tmpfile = tempfile.mktemp(dir=self.parent.TEMPDIR)
512 tmp = open(tmpfile,'w')
517 def dendroandbarplot(table, rownames, colnames, rgraph, tmpgraph, intxt = False, dendro=False) :
519 txttable = 'c(' + ','.join([','.join(line) for line in table]) + ')'
520 rownb = len(rownames)
521 rownames = 'c("' + '","'.join(rownames) + '")'
522 colnames = 'c("' + '","'.join(colnames) + '")'
526 di <- matrix(data=%s, nrow=%i, byrow = TRUE)
529 """ % (txttable, rownb, rownames, colnames)
536 height <- (30*ncol(di)) + (15*nrow(di))
537 height <- ifelse(height <= 400, 400, height)
539 open_file_graph("%s", width=width, height=height)
540 plot.dendro.lex(tree.cut1$tree.cl, di)
541 """ % (ffr(dendro),ffr(rgraph), ffr(tmpgraph))
544 def barplot(table, parametres, intxt = False) :
546 txttable = 'c(' + ','.join([','.join(line) for line in table]) + ')'
547 #width = 100 + (15 * len(rownames)) + (100 * len(colnames))
548 #height = len(rownames) * 15
549 rownb = len(parametres['rownames'])
552 rownames = 'c("' + '","'.join(parametres['rownames']) + '")'
553 colnames = 'c("' + '","'.join(parametres['colnames']) + '")'
558 di <- matrix(data=%s, nrow=%i, byrow = TRUE)
559 toinf <- which(di == Inf)
560 tominf <- which(di == -Inf)
563 valmax <- max(di, na.rm = TRUE)
571 if (length(tominf)) {
573 valmin <- min(di, na.rm = TRUE)
583 """ % (txttable, rownb, rownames, colnames)
586 if not 'tree' in parametres :
589 color = rainbow(nrow(di))
592 open_file_graph("%s",width = width, height = height, svg = %s)
594 layout(matrix(c(1,2),1,2, byrow=TRUE),widths=c(3,lcm(7)))
596 yp = ifelse(length(toinf), 0.2, 0)
597 ym = ifelse(length(tominf), 0.2, 0)
598 ymin <- ifelse(!length(which(di < 0)), 0, min(di) - ym)
599 coord <- barplot(as.matrix(di), beside = TRUE, col = color, space = c(0.1,0.6), ylim=c(ymin, max(di) + yp), las = 2)
601 coordinf <- coord[toinf]
603 text(x=coordinf, y=valinf + 0.1, 'i')
605 if (length(tominf)) {
606 coordinf <- coord[toinf]
608 text(x=coordinf, y=valinf - 0.1, 'i')
614 lcoord <- apply(cc, 1, mean)
617 amp <- abs(max(di) - min(di))
624 d <- signif(amp%%/%%10,1)
629 if ((i/d) == (i%%/%%d)) {
634 plot(0, axes = FALSE, pch = '')
635 legend(x = 'center' , rownames(di), fill = color)
637 """ % (ffr(parametres['rgraph']), parametres['width'], parametres['height'], ffr(parametres['tmpgraph']), parametres['svg'])
645 open_file_graph("%s", width=width, height=height, svg = %s)
646 plot.dendro.lex(tree.cut1$tree.cl, di)
647 """ % (ffr(parametres['tree']), ffr(parametres['rgraph']), parametres['width'], parametres['height'], ffr(parametres['tmpgraph']), parametres['svg'])
650 #def RAfcUci(DictAfcUciOut, nd=2, RscriptsPath='', PARCEX='0.8'):
656 # dataact<-read.csv2("%s")
657 # """ % (DictAfcUciOut['TableCont'])#, encoding)
659 # datasup<-read.csv2("%s")
660 # """ % (DictAfcUciOut['TableSup'])#, encoding)
662 # dataet<-read.csv2("%s")
663 # """ % (DictAfcUciOut['TableEt'])#, encoding)
665 # datatotsup<-cbind(dataact,datasup)
666 # datatotet<-cbind(dataact,dataet)
667 # afcact<-ca(dataact,nd=nd)
668 # afcsup<-ca(datatotsup,supcol=((ncol(dataact)+1):ncol(datatotsup)),nd=nd)
669 # afcet<-ca(datatotet,supcol=((ncol(dataact)+1):ncol(datatotet)),nd=nd)
670 # afctot<-afcsup$colcoord
671 # rownames(afctot)<-afcsup$colnames
672 # colnames(afctot)<-paste('coord. facteur',1:nd,sep=' ')
673 # afctot<-cbind(afctot,mass=afcsup$colmass)
674 # afctot<-cbind(afctot,distance=afcsup$coldist)
675 # afctot<-cbind(afctot,intertie=afcsup$colinertia)
676 # rcolet<-afcet$colsup
677 # afctmp<-afcet$colcoord[rcolet,]
678 # rownames(afctmp)<-afcet$colnames[rcolet]
679 # afctmp<-cbind(afctmp,afcet$colmass[rcolet])
680 # afctmp<-cbind(afctmp,afcet$coldist[rcolet])
681 # afctmp<-cbind(afctmp,afcet$colinertia[rcolet])
682 # afctot<-rbind(afctot,afctmp)
683 # write.csv2(afctot,file = "%s")
685 # """ % (DictAfcUciOut['afc_row'], RscriptsPath['Rgraph'])
691 # PlotAfc(afcet,filename="%s",toplot=c%s, PARCEX=PARCEX)
692 # """ % (DictAfcUciOut['AfcColAct'], "('none','active')")
694 # PlotAfc(afcsup,filename="%s",toplot=c%s, PARCEX=PARCEX)
695 # """ % (DictAfcUciOut['AfcColSup'], "('none','passive')")
696 # txt += """PlotAfc(afcet,filename="%s", toplot=c%s, PARCEX=PARCEX)
697 # """ % (DictAfcUciOut['AfcColEt'], "('none','passive')")
699 # PlotAfc(afcet,filename="%s", toplot=c%s, PARCEX=PARCEX)
700 # """ % (DictAfcUciOut['AfcRow'], "('all','none')")
701 # f = open(DictAfcUciOut['Rafcuci'], 'w')
705 class PrintSimiScript(PrintRScript) :
706 def make_script(self) :
708 self.packages(['igraph', 'proxy', 'Matrix'])
709 self.sources([self.analyse.parent.RscriptsPath['simi'], self.analyse.parent.RscriptsPath['Rgraph']])
711 if not self.parametres['keep_coord'] and not (self.parametres['type'] == 'simimatrix' or self.parametres['type'] == 'simiclustermatrix') :
716 """ % (ffr(self.pathout['mat01.csv']), ffr(self.pathout['actives.csv']), ffr(self.pathout['selected.csv']))
717 if 'word' in self.parametres :
721 """ % self.parametres['word']
728 cn <- read.table(cn.path, sep='\t', quote='"')
729 colnames(dm) <- cn[,1]
730 if (file.exists(selected.col)) {
731 sel.col <- read.csv2(selected.col, header = FALSE)
732 sel.col <- sel.col[,1] + 1
734 sel.col <- 1:ncol(dm)
739 forme <- colnames(dm)[index]
740 if (!index %in% sel.col) {
741 sel.col <- append(sel.col, index)
744 index <- which(colnames(dm) == forme)
747 elif not self.parametres['keep_coord'] and (self.parametres['type'] == 'simimatrix' or self.parametres['type'] == 'simiclustermatrix'):
751 """ % (ffr(self.pathout['mat01.csv']), ffr(self.pathout['selected.csv']))
752 if 'word' in self.parametres :
756 """ % self.parametres['word']
762 dm <-read.csv2(dm.path)
764 if (file.exists(selected.col)) {
765 sel.col <- read.csv2(selected.col, header = FALSE)
766 sel.col <- sel.col[,1] + 1
768 sel.col <- 1:ncol(dm)
773 forme <- colnames(dm)[index]
774 if (!index %in% sel.col) {
775 sel.col <- append(sel.col, index)
778 index <- which(colnames(dm) == forme)
784 """ % ffr(self.pathout['RData.RData'])
786 if self.parametres['coeff'] == 0 :
788 if not self.parametres['keep_coord'] :
794 if not self.parametres['keep_coord'] :
798 if self.parametres['coeff'] == 1 :
802 mat <- simil(dm, method = 'Russel', diag = TRUE, upper = TRUE, by_rows = FALSE)
804 elif self.analyse.indices[self.parametres['coeff']] == 'binomial' :
806 if not self.parametres['keep_coord'] :
811 elif self.parametres['coeff'] != 0 :
812 method = self.analyse.indices[self.parametres['coeff']]
813 if not self.parametres['keep_coord'] :
816 mat <- simil(dm, method = method, diag = TRUE, upper = TRUE, by_rows = FALSE)
817 """ % self.analyse.indices[self.parametres['coeff']]
818 if not self.parametres['keep_coord'] :
820 mat <- as.matrix(stats::as.dist(mat,diag=TRUE,upper=TRUE))
822 if (length(which(mat == Inf))) {
823 infp <- which(mat == Inf)
825 maxmat <- max(mat, na.rm = TRUE)
833 if (length(which(mat == -Inf))) {
834 infm <- which(mat == -Inf)
836 minmat <- min(mat, na.rm = TRUE)
845 if 'word' in self.parametres and not self.parametres['keep_coord'] :
847 mat <- graph.word(mat, index)
849 if (length(cs)) mat <- mat[,-which(cs==0)]
851 if (length(rs)) mat <- mat[-which(rs==0),]
852 if (length(cs)) dm <- dm[, -which(cs==0)]
855 if self.parametres['layout'] == 0 : layout = 'random'
856 if self.parametres['layout'] == 1 : layout = 'circle'
857 if self.parametres['layout'] == 2 : layout = 'frutch'
858 if self.parametres['layout'] == 3 : layout = 'kawa'
859 if self.parametres['layout'] == 4 : layout = 'graphopt'
863 if self.parametres['type_graph'] == 0 : type = 'tkplot'
864 if self.parametres['type_graph'] == 1 :
867 dirout = os.path.dirname(self.pathout['mat01.csv'])
868 while os.path.exists(os.path.join(dirout,'graph_simi_'+str(graphnb)+'.png')):
870 self.filename = ffr(os.path.join(dirout,'graph_simi_'+str(graphnb)+'.png'))
871 if self.parametres['type_graph'] == 2 : type = 'rgl'
872 if self.parametres['type_graph'] == 3 :
875 dirout = os.path.dirname(self.pathout['mat01.csv'])
876 while os.path.exists(os.path.join(dirout,'web_'+str(graphnb))):
878 self.filename = ffr(os.path.join(dirout,'web_'+str(graphnb)))
879 os.mkdir(self.filename)
880 self.filename = os.path.join(self.filename, 'gexf.gexf')
881 if self.parametres['type_graph'] == 4 :
884 dirout = os.path.dirname(self.pathout['mat01.csv'])
885 while os.path.exists(os.path.join(dirout,'webrgl_'+str(graphnb))):
887 self.filename = ffr(os.path.join(dirout,'webrgl_'+str(graphnb)))
888 os.mkdir(self.filename)
890 if self.parametres['arbremax'] :
892 self.txtgraph += ' - arbre maximum'
893 else : arbremax = 'FALSE'
895 if self.parametres['coeff_tv'] :
896 coeff_tv = self.parametres['coeff_tv_nb']
897 tvminmax = 'c(NULL,NULL)'
898 elif not self.parametres['coeff_tv'] or self.parametres.get('sformchi', False) :
900 tvminmax = 'c(%i, %i)' %(self.parametres['tvmin'], self.parametres['tvmax'])
901 if self.parametres['coeff_te'] : coeff_te = 'c(%i,%i)' % (self.parametres['coeff_temin'], self.parametres['coeff_temax'])
902 else : coeff_te = 'NULL'
904 if self.parametres['vcex'] or self.parametres.get('cexfromchi', False) :
905 vcexminmax = 'c(%i/10,%i/10)' % (self.parametres['vcexmin'],self.parametres['vcexmax'])
907 vcexminmax = 'c(NULL,NULL)'
908 if not self.parametres['label_v'] : label_v = 'FALSE'
909 else : label_v = 'TRUE'
911 if not self.parametres['label_e'] : label_e = 'FALSE'
912 else : label_e = 'TRUE'
914 if self.parametres['seuil_ok'] : seuil = str(self.parametres['seuil'])
915 else : seuil = 'NULL'
917 if not self.parametres.get('edgecurved', False) :
926 cols = str(self.parametres['cols']).replace(')',', max=255)')
927 cola = str(self.parametres['cola']).replace(')',',max=255)')
937 """ % self.parametres['cex']
939 if self.parametres['film'] :
942 """ % self.pathout['film']
949 if (!is.null(seuil)) {
950 if (method!='cooc') {
959 """ % (label_v, label_e)
967 """ % (self.parametres['width'], self.parametres['height'])
968 if self.parametres['keep_coord'] :
970 coords <- try(coords, TRUE)
971 if (!is.matrix(coords)) {
981 """ % self.parametres['alpha']
984 """ % self.parametres['alpha']
985 #############################################
986 if self.parametres.get('bystar',False) :
990 for i, line in enumerate(self.parametres['listet']) :
993 """ % (i+1, ','.join([`val + 1` for val in line]))
996 """ % ("','".join([val for val in self.parametres['selectedstars']]))
1000 for (i in 1:length(unetoile)) {
1003 if (length(tosum) > 1) {
1004 fsum <- cbind(fsum, colSums(dm[tosum,]))
1006 fsum <- cbind(fsum, dm[tosum,])
1010 lex <- AsLexico2(fsum, chip=TRUE)
1011 dcol <- apply(lex[[4]],1,which.max)
1012 toblack <- apply(lex[[4]],1,max)
1013 gcol <- rainbow(length(unetoile))
1014 #gcol[2] <- 'orange'
1015 vertex.label.color <- gcol[dcol]
1016 vertex.label.color[which(toblack <= 3.84)] <- 'black'
1017 leg <- list(unetoile=unetoile, gcol=gcol)
1018 cols <- vertex.label.color
1019 chivertex.size <- norm.vec(toblack, vcexminmax[1], vcexminmax[2])
1021 """ % (ffr(self.analyse.parent.RscriptsPath['chdfunct']))
1024 vertex.label.color <- 'black'
1028 #############################################
1031 # eff <- colSums(dm)
1032 # g.ori <- graph.adjacency(mat, mode='lower', weighted = TRUE)
1033 # w.ori <- E(g.ori)$weight
1035 # if (method == 'cooc') {
1036 # E(g.ori)$weight <- 1 / w.ori
1038 # E(g.ori)$weigth <- 1 - w.ori
1040 # g.max <- minimum.spanning.tree(g.ori)
1041 # if (method == 'cooc') {
1042 # E(g.max)$weight <- 1 / E(g.max)$weight
1044 # E(g.max)$weight <- 1 - E(g.max)$weight
1051 if self.parametres['com'] :
1052 com = `self.parametres['communities']`
1055 if self.parametres['halo'] :
1065 x <- list(mat = mat, eff = eff)
1066 graph.simi <- do.simi(x, method='%s', seuil = seuil, p.type = '%s', layout.type = '%s', max.tree = %s, coeff.vertex=%s, coeff.edge = %s, minmaxeff = minmaxeff, vcexminmax = vcexminmax, cex = cex, coords = coords, communities = communities, halo = halo)
1067 """ % (method, type, layout, arbremax, coeff_tv, coeff_te)
1069 if self.parametres.get('bystar',False) :
1070 if self.parametres.get('cexfromchi', False) :
1072 label.cex<-chivertex.size
1078 if self.parametres.get('sfromchi', False) :
1080 vertex.size <- norm.vec(toblack, minmaxeff[1], minmaxeff[2])
1087 #print self.parametres
1088 if (self.parametres['type'] == 'clustersimitxt' and self.parametres.get('tmpchi', False)) or (self.parametres['type'] in ['simimatrix','simiclustermatrix'] and 'tmpchi' in self.parametres):
1090 lchi <- read.table("%s")
1092 """ % ffr(self.parametres['tmpchi'])
1094 lchi <- lchi[sel.col]
1096 if self.parametres['type'] in ['clustersimitxt', 'simimatrix', 'simiclustermatrix'] and self.parametres.get('cexfromchi', False) :
1098 label.cex <- norm.vec(lchi, vcexminmax[1], vcexminmax[2])
1102 if (is.null(vcexminmax[1])) {
1105 label.cex <- graph.simi$label.cex
1108 if (self.parametres['type'] in ['clustersimitxt', 'simimatrix', 'simiclustermatrix']) and self.parametres.get('sfromchi', False):
1110 vertex.size <- norm.vec(lchi, minmaxeff[1], minmaxeff[2])
1111 if (!length(vertex.size)) vertex.size <- 0
1115 if (is.null(minmaxeff[1])) {
1118 vertex.size <- graph.simi$eff
1121 #txt += """ vertex.size <- NULL """
1122 if self.parametres['svg'] : svg = 'TRUE'
1123 else : svg = 'FALSE'
1129 if (!is.null(graph.simi$com)) {
1130 com <- graph.simi$com
1131 colm <- rainbow(length(com))
1132 if (vertex.size != 0 || graph.simi$halo) {
1133 vertex.label.color <- 'black'
1134 vertex.col <- colm[membership(com)]
1136 vertex.label.color <- colm[membership(com)]
1139 coords <- plot.simi(graph.simi, p.type='%s',filename="%s", vertex.label = label.v, edge.label = label.e, vertex.col = vertex.col, vertex.label.color = vertex.label.color, vertex.label.cex=label.cex, vertex.size = vertex.size, edge.col = cola, leg=leg, width = width, height = height, alpha = alpha, movie = film, edge.curved = edge.curved, svg = svg)
1140 save.image(file="%s")
1141 """ % (type, self.filename, ffr(self.pathout['RData']))
1146 class WordCloudRScript(PrintRScript) :
1147 def make_script(self) :
1148 self.sources([self.analyse.parent.RscriptsPath['Rgraph']])
1149 self.packages(['wordcloud'])
1150 bg_col = Rcolor(self.parametres['col_bg'])
1151 txt_col = Rcolor(self.parametres['col_text'])
1152 if self.parametres['svg'] :
1160 act <- read.csv2("%s", header = FALSE, row.names=1, sep='\t')
1161 selected.col <- read.table("%s")
1162 toprint <- as.matrix(act[selected.col[,1] + 1,])
1163 rownames(toprint) <- rownames(act)[selected.col[,1] + 1]
1165 if (nrow(toprint) > maxword) {
1166 toprint <- as.matrix(toprint[order(toprint[,1], decreasing=TRUE),])
1167 toprint <- as.matrix(toprint[1:maxword,])
1169 open_file_graph("%s", width = %i, height = %i , svg = svg)
1171 wordcloud(row.names(toprint), toprint[,1], scale=c(%f,%f), random.order=FALSE, colors=rgb%s)
1173 """ % (ffr(self.analyse.pathout['actives_eff.csv']), ffr(self.analyse.pathout['selected.csv']), self.parametres['maxword'], ffr(self.parametres['graphout']), self.parametres['width'], self.parametres['height'], bg_col, self.parametres['maxcex'], self.parametres['mincex'], txt_col)
1177 class ProtoScript(PrintRScript) :
1178 def make_script(self) :
1179 self.sources([self.analyse.parent.RscriptsPath['Rgraph'], self.analyse.parent.RscriptsPath['prototypical.R']])
1180 self.packages(['wordcloud'])
1181 if self.parametres.get('cloud', False) :
1186 errorn <- function(x) {
1187 qnorm(0.975)*sd(x)/sqrt(lenght(n))
1189 errort <- function(x) {
1190 qt(0.975,df=lenght(x)-1)*sd(x)/sqrt(lenght(x))
1192 mat <- read.csv2("%s", header = FALSE, row.names=1, sep='\t', quote='"', dec='.')
1193 open_file_graph("%s",height=800, width=1000)
1194 prototypical(mat, mfreq = %s, mrank = %s, cloud = FALSE, cexrange=c(1,2.4), cexalpha= c(0.4, 1), type = '%s')
1196 """ % (ffr(self.analyse.pathout['table.csv']), ffr(self.analyse.pathout['proto.png']), self.parametres['limfreq'], self.parametres['limrang'], self.parametres['typegraph'])
1201 class ExportAfc(PrintRScript) :
1202 def make_script(self) :
1203 self.source([self.analyse.parent.RscriptsPath['Rgraph']])
1204 self.packages(['rgexf'])
1208 class MergeGraphes(PrintRScript) :
1209 def __init__(self, parametres):
1210 self.script = u"#Script genere par IRaMuTeQ - %s\n" % datetime.now().ctime()
1211 self.pathout = PathOut()
1212 self.parametres = parametres
1213 self.scriptout = self.pathout['temp']
1215 def make_script(self) :
1225 g <- graph.simi$graph
1226 V(g)$weight <- (graph.simi$mat.eff/nrow(dm))*100
1229 for i, graph in enumerate(self.parametres['lgraphes']) :
1230 path = os.path.dirname(graph)
1231 gname = ''.join(['g', `i`])
1232 RData = os.path.join(path,'RData.RData')
1233 txt += load % (ffr(RData), gname)
1235 self.sources([self.analyse.parent.RscriptsPath['simi']])
1237 ng <- merge.graph(graphs)
1238 ngraph <- list(graph=ng, layout=layout.fruchterman.reingold(ng, dim=3), labex.cex=V(ng)$weight)
1239 write.graph(ng, "%s", format = 'graphml')
1240 """ % ffr(self.parametres['grapheout'])
1243 class TgenSpecScript(PrintRScript):
1244 def make_script(self):
1245 self.packages(['textometry'])
1247 tgen <- read.csv2("%s", row.names = 1, sep = '\\t')
1248 """ % ffr(self.parametres['tgeneff'])
1250 tot <- tgen[nrow(tgen), ]
1252 tgen <- tgen[-nrow(tgen),]
1253 for (i in 1:nrow(tgen)) {
1254 mat <- rbind(tgen[i,], tot - tgen[i,])
1255 specmat <- specificities(mat)
1256 result <- rbind(result, specmat[1,])
1258 colnames(result) <- colnames(tgen)
1259 row.names(result) <- rownames(tgen)
1260 write.table(result, file = "%s", sep='\\t', col.names = NA)
1261 """ % ffr(self.pathout['tgenspec.csv'])
1264 class TgenProfScript(PrintRScript):
1265 def make_script(self):
1266 self.sources([self.analyse.ira.RscriptsPath['chdfunct']])
1268 tgen <- read.csv2("%s", row.names = 1, sep = '\\t')
1269 """ % ffr(self.parametres['tgeneff'])
1271 tgenlem <- read.csv2("%s", row.names = 1, sep = '\\t')
1272 """ % ffr(self.parametres['tgenlemeff'])
1274 res <- build.prof.tgen(tgen)
1275 write.table(res$chi2, file = "%s", sep='\\t', col.names = NA)
1276 write.table(res$pchi2, file = "%s", sep='\\t', col.names = NA)
1277 """ % (ffr(self.pathout['tgenchi2.csv']), ffr(self.pathout['tgenpchi2.csv']))
1279 reslem <- build.prof.tgen(tgenlem)
1280 write.table(reslem$chi2, file = "%s", sep='\\t', col.names = NA)
1281 write.table(reslem$pchi2, file = "%s", sep='\\t', col.names = NA)
1282 """ % (ffr(self.pathout['tgenlemchi2.csv']), ffr(self.pathout['tgenlempchi2.csv']))
1285 class FreqMultiScript(PrintRScript):
1286 def make_script(self):
1287 self.sources([self.analyse.parent.RscriptsPath['Rgraph']])
1289 freq <- read.csv2("%s", row.names=1, sep='\\t', dec='.')
1290 """ % ffr(self.pathout['frequences.csv'])
1292 toplot <- freq[order(freq[,2]) ,2]
1293 toplot.names = rownames(freq)[order(freq[,2])]
1294 h <- 80 + (20 * nrow(freq))
1295 open_file_graph("%s",height=h, width=500)
1296 par(mar=c(3,20,3,3))
1297 barplot(toplot, names = toplot.names, horiz=TRUE, las =1, col = rainbow(nrow(freq)))
1299 """ % ffr(self.pathout['barplotfreq.png'])
1301 toplot <- freq[order(freq[,4]) ,4]
1302 toplot.names = rownames(freq)[order(freq[,4])]
1303 open_file_graph("%s",height=h, width=500)
1304 par(mar=c(3,20,3,3))
1305 barplot(toplot, names = toplot.names, horiz=TRUE, las =1, col = rainbow(nrow(freq)))
1307 """ % ffr(self.pathout['barplotrow.png'])