- txt += """
- dm <- read.csv2("%s")
- """ % self.DictPathOut['mat01']
- txt += """
- dm <- as.matrix(dm)
- #dml <- apply(dm, 2, as.logical)
- """
- if not self.paramsimi['keep_coord'] :
- txt += """
- cn <- read.csv("%s", sep='\t', quote='"', headers = FALSE)
- colnames(dm) <- cn[,1]
- #colnames(dml) <- cn[,1]
- """ % ffr(active_file)
- if 'selected_col' in dir(self.tableau) and not self.paramsimi['keep_coord']:
- txt += """
- #dml <- dml[, c%s+1]
- dm <- dm[, c%s+1]
- """ % (datas, datas)
- txt += """
- source("%s")
- source("%s")
- """ % (self.parent.RscriptsPath['simi'],self.parent.RscriptsPath['Rgraph'])
-
- self.txtgraph = ''
- self.txtgraph += self.types[self.paramsimi['coeff']]
- if not self.paramsimi['keep_coord'] :
- if self.paramsimi['coeff'] == 0 :
- method = 'cooc'
- txt += """
- method <- 'cooc'
- mat <- make.a(dm)
- """
- elif self.paramsimi['coeff'] == 1 :
- method = 'prcooc'
- txt += """
- method <- 'Russel'
- mat <- simil(dm, method = 'Russel', diag = TRUE, upper = TRUE, by_rows = FALSE)
- """
- elif self.types[self.paramsimi['coeff']] == 'binomial' :
- method = 'binomial'
- txt += """
- method <- 'binomial'
- mat <- binom.sim(dm)
- """
- else :
- method = self.types[self.paramsimi['coeff']]
- txt += """
- method <-"%s"
- mat <- simil(dm, method = method, diag = TRUE, upper = TRUE, by_rows = FALSE)
- """ % self.types[self.paramsimi['coeff']]
- txt += """
- mat <- as.matrix(stats::as.dist(mat,diag=TRUE,upper=TRUE))
- mat[is.na(mat)] <- 0
- mat[is.infinite(mat)] <- 0
- """
- if self.types[self.paramsimi['coeff']] not in ['cooccurrence', 'Chi-squared', 'Mozley', 'Stiles'] :
- txt += """
- mat <- mat * 100
- """
- if self.wordgraph :
- txt += """
- mat <- graph.word(mat, %i)
- """ % (col + 1)
- txt += """
- eff <- colSums(dm)
- x <- list(mat = mat, eff = eff)
- """
- else :
- method = ''
- if self.paramsimi['layout'] == 0 : layout = 'random'
- if self.paramsimi['layout'] == 1 : layout = 'circle'
- if self.paramsimi['layout'] == 2 : layout = 'frutch'
- if self.paramsimi['layout'] == 3 : layout = 'kawa'
- if self.paramsimi['layout'] == 4 : layout = 'graphopt'
-
- self.filename=''
- if self.paramsimi['type_graph'] == 0 : type = 'tkplot'
- if self.paramsimi['type_graph'] == 1 :
- graphnb = 1
- type = 'nplot'
- dirout = os.path.dirname(self.DictPathOut['mat01'])
- while os.path.exists(os.path.join(dirout,'graph_simi_'+str(graphnb)+'.png')):
- graphnb +=1
- self.filename = ffr(os.path.join(dirout,'graph_simi_'+str(graphnb)+'.png'))
- if self.paramsimi['type_graph'] == 2 : type = 'rgl'
-
- if self.paramsimi['arbremax'] :
- arbremax = 'TRUE'
- self.txtgraph += ' - arbre maximum'
- else : arbremax = 'FALSE'
-
- if self.paramsimi['coeff_tv'] :
- coeff_tv = self.paramsimi['coeff_tv_nb']
- tvminmax = 'c(NULL,NULL)'
- elif not self.paramsimi['coeff_tv'] or self.paramsimi.get('sformchi', False) :
- coeff_tv = 'NULL'
- tvminmax = 'c(%i, %i)' %(self.paramsimi['tvmin'], self.paramsimi['tvmax'])
- txt += """
- minmaxeff <- %s
- """ % tvminmax
-
- if self.paramsimi['coeff_te'] : coeff_te = 'c(%i,%i)' % (self.paramsimi['coeff_temin'], self.paramsimi['coeff_temax'])
- else : coeff_te = 'NULL'
-
- if self.paramsimi['vcex'] or self.paramsimi.get('cexfromchi', False) :
- vcexminmax = 'c(%i/10,%i/10)' % (self.paramsimi['vcexmin'],self.paramsimi['vcexmax'])
- else :
- vcexminmax = 'c(NULL,NULL)'
-
- txt += """
- vcexminmax <- %s
- """ % vcexminmax
-
- txt += """
- cex = %i/10
- """ % self.paramsimi['cex']
-
- if not self.paramsimi['label_v'] : label_v = 'FALSE'
- else : label_v = 'TRUE'
-
- if not self.paramsimi['label_e'] : label_e = 'FALSE'
- else : label_e = 'TRUE'
-
- if self.paramsimi['seuil_ok'] : seuil = str(self.paramsimi['seuil'])
- else : seuil = 'NULL'
-
- if self.paramsimi['film'] :
- txt += """
- film <- "%s"
- """ % self.DictPathOut['film']
- else :
- txt += """
- film <- NULL
- """
-
- txt += """
- seuil <- %s
- """ % seuil
-
- txt += """
- label.v <- %s
- label.e <- %s
- """ % (label_v, label_e)
-
- cols = str(self.paramsimi['cols']).replace(')',', max=255)')
- cola = str(self.paramsimi['cola']).replace(')',',max=255)')
-
- txt += """
- cols <- rgb%s
- cola <- rgb%s
- """ % (cols, cola)
-
- txt += """
- width <- %i
- height <- %i
- """ % (self.paramsimi['width'], self.paramsimi['height'])
-
- if self.paramsimi['keep_coord'] :
- txt += """
- coords <- try(coords, TRUE)
- if (!is.matrix(coords)) {
- coords<-NULL
- }
- """
- else :
- txt += """
- coords <- NULL
- """
- txt += """
- alpha <- %i/100
- """ % self.paramsimi['alpha']
-
- if self.paramsimi.get('bystar',False) :
- txt += """
- et <- list()
- """
- for i,et in enumerate(self.tableau.etline) :
- txt+= """
- et[[%i]] <- c(%s)
- """ % (i+1, ','.join(et[1:]))
- txt+= """
- unetoile <- c('%s')
- """ % ("','".join([val[0] for val in self.tableau.etline]))
- txt += """
- fsum <- NULL
- rs <- rowSums(dm)
- for (i in 1:length(unetoile)) {
- print(unetoile[i])
- tosum <- et[[i]]
- if (length(tosum) > 1) {
- fsum <- cbind(fsum, colSums(dm[tosum,]))
- } else {
- fsum <- cbind(fsum, dm[tosum,])
- }
- }
- source("%s")
- lex <- AsLexico2(fsum, chip=TRUE)
- dcol <- apply(lex[[4]],1,which.max)
- toblack <- apply(lex[[4]],1,max)
- gcol <- rainbow(length(unetoile))
- #gcol[2] <- 'orange'
- vertex.label.color <- gcol[dcol]
- vertex.label.color[which(toblack <= 3.84)] <- 'black'
- leg <- list(unetoile=unetoile, gcol=gcol)
- cols <- vertex.label.color
- chivertex.size <- norm.vec(toblack, vcexminmax[1], vcexminmax[2])
-
- """ % (self.parent.RscriptsPath['chdfunct'])
- else :
- txt += """
- vertex.label.color <- 'black'
- chivertex.size <- 1
- leg<-NULL
- """
- #if not self.paramsimi['keep_coord'] :
- txt += """
- 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)
- """ % (method, type, layout, arbremax, coeff_tv, coeff_te)
- #txt += """
- if self.paramsimi.get('bystar',False) :
- if self.paramsimi.get('cexfromchi', False) :
- txt+="""
- label.cex<-chivertex.size
- """
- else :
- txt+="""
- label.cex <- NULL
- """
- if self.paramsimi.get('sfromchi', False) :
- txt += """
- vertex.size <- norm.vec(toblack, minmaxeff[1], minmaxeff[2])
- """
- else :
- txt += """
- vertex.size <- NULL
- """
- else :
- if tmpchi :
- txt += """
- lchi <- read.table("%s")
- lchi <- lchi[,1]
- """ % ffr(tmpchi)
- if 'selected_col' in dir(self.tableau) :
- txt += """
- lchi <- lchi[c%s+1]
- """ % datas
- if tmpchi and self.paramsimi.get('cexfromchi', False) :
- txt += """
- label.cex <- norm.vec(lchi, vcexminmax[1], vcexminmax[2])
- """
- else :
- txt += """
- if (is.null(vcexminmax[1])) {
- label.cex <- NULL
- } else {
- label.cex <- graph.simi$label.cex
- }
- """
- if tmpchi and self.paramsimi.get('sfromchi', False) :
- txt += """
- vertex.size <- norm.vec(lchi, minmaxeff[1], minmaxeff[2])
- """
- else :
- txt += """
- if (is.null(minmaxeff[1])) {
- vertex.size <- NULL
- } else {
- vertex.size <- graph.simi$eff
- }
- """
- txt += """ vertex.size <- NULL """
- txt += """
- coords <- plot.simi(graph.simi, p.type='%s',filename="%s", vertex.label = label.v, edge.label = label.e, vertex.col = cols, 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)
- save.image(file="%s")
- """ % (type, self.filename, self.DictPathOut['RData'])
-
- self.tmpfile = tempfile.mktemp(dir=self.parent.TEMPDIR)
- tmpscript = open(self.tmpfile, 'w')
- tmpscript.write(txt)
- tmpscript.close()
-
- if type == 'nplot':