#!/bin/env python # -*- coding: utf-8 -*- #Author: Pierre Ratinaud #Copyright (c) 2010 Pierre Ratinaud #Lisense: GNU/GPL import HTML import os import string import wx import os import sys import tempfile from chemins import ffr,FFF import wx.lib.sized_controls as sc from time import sleep from functions import exec_rcode, check_Rresult from dialog import ChiDialog, PrefChi def make_res(line) : if float(line[5]) <= 0.05 and line[6] != 'warning': line.append('green') elif float(line[5]) <= 0.05 and line[6] == 'warning': line.append('blue') else : line.append('red') return line def clean_line(result) : return [[val for val in line if val != '**'] for line in result] def make_table(tabs, tab_title, res) : return [''.join(['%s' % (res[i][-1], tab_title), HTML.table(tab)]) for i,tab in enumerate(tabs)] def make_restab(res) : return [''.join(['%s'% (line[-1], u'Résultats'),HTML.table([['chi', line[3]],['p', line[5]]])]) for i,line in enumerate(res)] def make_htmlgraphs(graphs) : return ['' % os.path.basename(val) for val in graphs] def make_link_list(res, text) : return ['%s' % (i, i, chi[-1], text[i]) for i, chi in enumerate(res)] def make_title(res, text) : return ['%sretour' % (i, val[-1], text[i], i) for i, val in enumerate(res)] class MakeChi2(): def __init__(self, parent, select1, select2, chioption): self.OutFrame=tempfile.mktemp(dir=parent.TEMPDIR) print self.OutFrame self.parent=parent self.encode=self.parent.encode self.TEMPDIR=parent.TEMPDIR self.RPath=parent.PathPath.get('PATHS','rpath') self.TextCroise=[] for i in select1 : for j in select2 : self.TextCroise.append(parent.tableau.colnames[i] + ' / ' + parent.tableau.colnames[j]) rchioption = {} for val in chioption : if chioption[val]: rchioption[val] = 'TRUE' else : rchioption[val] = 'FALSE' txt=""" source("%s") """%self.parent.RscriptsPath['Rfunct'] # if parent.tableau.: rownames=1 # else : rownames='NULL' # if parent.g_header : header = 'TRUE' # else : header = 'FALSE' txt += """ source("%s") """ % ffr(self.parent.RscriptsPath['Rgraph']) txt += """ doobs <- %s doexp <- %s docontrib <- %s doresi <- %s dopr <- %s doprl <- %s doprc <- %s dograph <- %s bw <- %s """ % (rchioption['valobs'], rchioption['valtheo'], rchioption['contrib'], rchioption['resi'], rchioption['pourcent'], rchioption['pourcentl'], rchioption['pourcentc'], rchioption['graph'], rchioption['bw']) txt+=""" datadm <- ReadData("%s", encoding="%s", header = TRUE, sep = "%s",quote = '%s', na.strings = "%s",rownames= 1) listres<-list() listcol<-list() cont<-1 """%(ffr(parent.tableau.parametre['csvfile']),self.parent.encode, parent.tableau.parametre['colsep'], parent.tableau.parametre['txtsep'], self.parent.nastrings) if len(select1)==1: strsel1=str(select1).replace(',','') else: strsel1=str(select1) if len(select2)==1: strsel2=str(select2).replace(',','') else: strsel2=str(select2) txt+=""" for (i in c%s) {"""%strsel1 txt+=""" for (j in c%s) {"""%strsel2 txt+=""" tab<-table(datadm[,i+1],datadm[,j+1]) if (min(dim(tab)) != 1) { chi<-chisq.test(tab) CS<-colSums(tab) RS<-rowSums(tab) GT<-sum(tab) chi$contrib<-(tab-chi$expected)/sqrt(chi$expected * ((1 - RS/GT) %%*%% t(1 - CS/GT))) listres[[cont]]<-chi listcol[[cont]]<-ncol(tab) cont<-cont+1 } else { chi <- list(observed = tab, residuals = tab, contrib = tab, statistic = 0, p.value = 1, expected = tab, message = 'pas de calcul') listres[[cont]] <- chi listcol[[cont]]<-ncol(tab) cont <- cont + 1 } } } maxcol<-max(unlist(listcol))+1 if (maxcol<7) {maxcol<-7} frameout<-matrix('*',1,maxcol) count<-0 for (chi in listres) { if (min(chi$expected)<5) { att<-"warning" } else { att<-"" } if ('message' %%in%% attributes(chi)$names) { att <- "Ce chi2 n\'a pas été calculé" nom_colresi<-colnames(chi$observed) chi$prl <- chi$expected chi$prc <- chi$expected st <- sum(chi$observed) } else { nom_colresi<-colnames(chi$observed) st <- sum(chi$observed) sc <- colSums(chi$observed) sr <- rowSums(chi$observed) chi$prl <- round((chi$observed/sr)*100,2) chi$prc <- t(round((t(chi$observed)/sc)*100,2)) } fileout<-paste('histo',%i,sep='') fileout<-paste(fileout,'_',sep='') fileout<-paste(fileout,count,sep='') fileout<-paste(fileout,'.png',sep='') count<-count+1 fileout<-file.path("%s",fileout) if (max(nchar(colnames(chi$observed)))>15) { leg <- 1:length(colnames(chi$observed)) } else { leg <- colnames(chi$observed) } if (dograph) { width<-ncol(chi$observed)*100 if (width < 350) {width <- 350} open_file_graph(fileout,width = width, height = 300) par(mar=c(0,0,0,0)) layout(matrix(c(1,2),1,2, byrow=TRUE),widths=c(3,1)) par(mar=c(2,2,1,0)) par(cex=0.8) if (!bw) colors <- rainbow(length(rownames(chi$observed))) else colors <- gray.colors(length(rownames(chi$observed))) barplot(chi$prl,names.arg = leg, beside=TRUE,border=NA, col=colors) par(mar=c(0,0,0,0)) par(cex=0.8) plot(0, axes = FALSE, pch = '') legend(x = 'center' , rownames(chi$observed), fill = colors) dev.off() } chi$prl <- cbind(chi$prl, total = rowSums(chi$prl)) chi$prc <- rbind(chi$prc, total = colSums(chi$prc)) chi$observed<-rbind(chi$observed,total=colSums(chi$observed)) chi$observed<-cbind(chi$observed,total=rowSums(chi$observed)) chi$pr <- round((chi$observed/st)*100,2) chi$expected<-rbind(chi$expected,total=colSums(chi$expected)) chi$expected<-cbind(chi$expected,total=rowSums(chi$expected)) chi$expected<-round(chi$expected,digits=2) chi$residuals<-round(chi$residuals,digits=2) chi$contrib<-round(chi$contrib, digits=2) nom_col<-colnames(chi$observed) if (ncol(chi$observed)\n \n \n Test du Chi2\n Légende : p <= 0.05 p <= 0.05 mais il y a des valeurs théoriques < 5 p > 0.05 """%self.parent.SysEncoding pretxt = '\n'.join(links)+'\n' txt = '\n'.join([''.join([tab[i] for tab in allhtml]) for i,val in enumerate(res)]) txt = header + pretxt + txt + '\n