...
[iramuteq] / PrintRScript.py
index b80c899..d81d070 100644 (file)
@@ -33,7 +33,7 @@ class PrintRScript :
 
     def sources(self, lsources) :
         for source in lsources :
-            self.add('source("%s")' % source)
+            self.add('source("%s", encoding = \'utf8\')' % source)
 
     def packages(self, lpks) :
         for pk in lpks :
@@ -253,7 +253,6 @@ def RchdTxt(DicoPath, RscriptPath, mincl, classif_mode, nbt = 9, svdmethod = 'sv
 def RPamTxt(corpus, RscriptPath):
     DicoPath = corpus.pathout
     param = corpus.parametres
-    print param
     txt = """
     source("%s")
     """ % (RscriptPath['pamtxt'])
@@ -394,7 +393,9 @@ write.csv2(gbcluster,file="%s")
     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])
     """ % (DictChdTxtOut['AFC2DSL_OUT'])
         txt += """
+        if ((fin - debet) > 2) {
     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])
+        }
     """ % (DictChdTxtOut['AFC2DEL_OUT'])
         txt += """
     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])
@@ -544,6 +545,7 @@ def barplot(table, rownames, colnames, rgraph, tmpgraph, intxt = False) :
     #    height = 400
     rownames = 'c("' + '","'.join(rownames) + '")'
     colnames = 'c("' + '","'.join(colnames) + '")'
+
     if not intxt :
         #FIXME
         txt = """
@@ -709,8 +711,12 @@ class PrintSimiScript(PrintRScript) :
             dm <-readMM(dm.path)
             cn <- read.table(cn.path, sep='\t', quote='"')
             colnames(dm) <- cn[,1]
-            sel.col <- read.csv2(selected.col, header = FALSE)
-            sel.col <- sel.col[,1] + 1
+            if (file.exists(selected.col)) {
+                sel.col <- read.csv2(selected.col, header = FALSE)
+                sel.col <- sel.col[,1] + 1
+            } else {
+                sel.col <- 1:ncol(dm)
+            }
             if (!word) {
                 dm <- dm[, sel.col]
             } else {
@@ -739,8 +745,12 @@ class PrintSimiScript(PrintRScript) :
             txt += """
             dm <-read.csv2(dm.path)
             dm <- as.matrix(dm)
-            sel.col <- read.csv2(selected.col, header = FALSE)
-            sel.col <- sel.col[,1] + 1
+            if (file.exists(selected.col)) {
+                sel.col <- read.csv2(selected.col, header = FALSE)
+                sel.col <- sel.col[,1] + 1
+            } else {
+                sel.col <- 1:ncol(dm)
+            }
             if (!word) {
                 dm <- dm[, sel.col]
             } else {
@@ -793,7 +803,28 @@ class PrintSimiScript(PrintRScript) :
             txt += """
             mat <- as.matrix(stats::as.dist(mat,diag=TRUE,upper=TRUE))
             mat[is.na(mat)] <- 0
-            mat[is.infinite(mat)] <- 0
+            if (length(which(mat == Inf))) {
+                infp <- which(mat == Inf)
+                mat[infp] <- NA
+                maxmat <- max(mat, na.rm = TRUE)
+                if (maxmat > 0) {
+                maxmat <- maxmat + 1
+                } else {
+                    maxmat <- 0
+                }
+                mat[infp] <- maxmat
+            }
+            if (length(which(mat == -Inf))) {
+                infm <- which(mat == -Inf)
+                mat[infm] <- NA
+                minmat <- min(mat, na.rm = TRUE)
+                if (maxmat < 0) {
+                minmat <- minmat - 1
+                } else {
+                    minmat <- 0
+                }
+                mat[infm] <- minmat
+            }
             """
         if 'word' in self.parametres and not self.parametres['keep_coord'] :
             txt += """
@@ -822,6 +853,14 @@ class PrintSimiScript(PrintRScript) :
                 graphnb +=1
             self.filename = ffr(os.path.join(dirout,'graph_simi_'+str(graphnb)+'.png'))
         if self.parametres['type_graph'] == 2 : type = 'rgl'
+        if self.parametres['type_graph'] == 3 : 
+            graphnb = 1
+            type = 'rglweb'
+            dirout = os.path.dirname(self.pathout['mat01'])
+            while os.path.exists(os.path.join(dirout,'webrgl_'+str(graphnb))):
+                graphnb +=1
+            self.filename = ffr(os.path.join(dirout,'webrgl_'+str(graphnb)))
+            os.mkdir(self.filename)
 
         if self.parametres['arbremax'] : 
             arbremax = 'TRUE'
@@ -995,7 +1034,7 @@ class PrintSimiScript(PrintRScript) :
                     """
             else :
                 txt+="""
-                label.cex <- NULL
+                label.cex <- cex
                 """
             if self.parametres.get('sfromchi', False) :
                 txt += """
@@ -1006,7 +1045,8 @@ class PrintSimiScript(PrintRScript) :
                 vertex.size <- NULL
                 """
         else :
-            if self.parametres['type'] == 'clustersimitxt' : 
+            print self.parametres
+            if (self.parametres['type'] == 'clustersimitxt' and self.parametres.get('tmpchi', False)) or (self.parametres['type'] == 'simimatrix' and 'tmpchi' in self.parametres): 
                 txt += """
                 lchi <- read.table("%s")
                 lchi <- lchi[,1]
@@ -1021,31 +1061,43 @@ class PrintSimiScript(PrintRScript) :
             else :
                 txt += """
             if (is.null(vcexminmax[1])) {
-                label.cex <- NULL
+                label.cex <- cex
             } else {
                 label.cex <- graph.simi$label.cex
             }
             """
-            if self.parametres['type'] == 'clustersimitxt' and self.parametres.get('sfromchi', False) :
+            if (self.parametres['type'] == 'clustersimitxt' or self.parametres['type'] == 'simimatrix') and self.parametres.get('sfromchi', False):
                 txt += """ 
                 vertex.size <- norm.vec(lchi, minmaxeff[1], minmaxeff[2])
+                if (!length(vertex.size)) vertex.size <- 0
                 """
             else :
                 txt += """
             if (is.null(minmaxeff[1])) {
-                vertex.size <- NULL
+                vertex.size <- 0
             } else {
                 vertex.size <- graph.simi$eff
             }
             """
-        txt += """ vertex.size <- NULL """
+        #txt += """ vertex.size <- NULL """
         if self.parametres['svg'] : svg = 'TRUE'
         else : svg = 'FALSE'
         txt += """
         svg <- %s
         """ % svg
         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, svg = svg)
+        vertex.col <- cols
+        if (!is.null(graph.simi$com)) {
+            com <- graph.simi$com
+            colm <- rainbow(length(com))
+            if (vertex.size != 0 || graph.simi$halo) {
+                vertex.label.color <- 'black'
+                vertex.col <- colm[membership(com)]
+            } else {
+                vertex.label.color <- colm[membership(com)]
+            }
+        }
+        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, svg = svg)
         save.image(file="%s")
         """ % (type, self.filename, self.pathout['RData'])
         
@@ -1058,7 +1110,14 @@ class WordCloudRScript(PrintRScript) :
         self.packages(['wordcloud'])
         bg_col = Rcolor(self.parametres['col_bg'])
         txt_col = Rcolor(self.parametres['col_text'])
+        if self.parametres['svg'] :
+            svg = 'TRUE'
+        else :
+            svg = 'FALSE'
         txt = """
+        svg <- %s
+        """ % svg
+        txt += """
         act <- read.csv2("%s", header = FALSE, row.names=1, sep='\t')
         selected.col <- read.table("%s")
         toprint <- as.matrix(act[selected.col[,1] + 1,])
@@ -1068,10 +1127,32 @@ class WordCloudRScript(PrintRScript) :
             toprint <- as.matrix(toprint[order(toprint[,1], decreasing=TRUE),])
             toprint <- as.matrix(toprint[1:maxword,])
         }
-        open_file_graph("%s", width = %i, height = %i)
+        open_file_graph("%s", width = %i, height = %i , svg = svg)
         par(bg=rgb%s)
         wordcloud(row.names(toprint), toprint[,1], scale=c(%f,%f), random.order=FALSE, colors=rgb%s)
         dev.off()
         """ % (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)
         self.add(txt)
         self.write()
+
+class ProtoScript(PrintRScript) :
+    def make_script(self) :
+        self.sources([self.analyse.parent.RscriptsPath['Rgraph'], self.analyse.parent.RscriptsPath['prototypical.R']])
+        self.packages(['wordcloud'])
+        txt = """
+        mat <- read.csv2("%s", header = FALSE, row.names=1, sep='\t', quote='"', dec='.')
+        open_file_graph("%s",height=800, width=1000)
+        prototypical(mat, mfreq = %s, mrank = %s, cloud = FALSE, cexrange=c(1,2.4), cexalpha= c(0.4, 1))
+        dev.off()
+        """ % (self.analyse.pathout['table.csv'], self.analyse.pathout['proto.png'], self.parametres['limfreq'], self.parametres['limrang'])
+        self.add(txt)
+        self.write()
+
+
+class ExportAfc(PrintRScript) :
+    def make_script(self) :
+        self.source([self.analyse.parent.RscriptsPath['Rgraph']])
+        self.packages(['rgexf'])
+        txt = """
+        """
+