Bonjour à tous,
Je souhaite utiliser le script suivant à partir d'un fichier .txt avec des colonnes time A1 B1 [...] H12.
Cependant, je me heurte à une erreur "objet 'height' introuvable" et je n'arrive pas à trouver l'origine du problème car il me semble que 'height' est bien défini partout.
Pourriez-vous m'aider svp ?
Question bonus, mon fichier arrive dans l'ordre A1 A2 A3 ... A12 B1 B2 ... H12 et j'ai besoin de l'avoir dans l'ordre A1 B1 C1 ... H12. Auriez vous une idée pour automatiser ce changement d'ordre ? Je n'y parviens pas.
Merci d'avance
library(lattice)
DrawSummary<-function(fichier,MGR,TMax,ODMax,MaxOD,C,M)
{
puits=ncol(C)-1
if ( puits == 96 )
{
width = 12
height = 8
rowname = c("A","B","C","D","E","F","G","H")
}
if ( puits == 384 )
{
width = 24
height = 16
rowname = c("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P")
}
else {
width = 12
height = 8
rowname = c("A","B","C","D","E","F","G","H")
}
RR<-matrix(ncol=width,nrow=height);
for(i in 1:height){
for(j in 1:width){
#RR[i,j]<-abs(i-8.5)+abs(j-12.5);
RR[i,j]<-sqrt((i-8.5)^2+(j-12.5)^2);
}
}
levelplot(t(RR),main="Distance")
RRR<-RR[1:puits]
RRb<-matrix(ncol=width,nrow=height,data=NA)
RRa<-matrix(ncol=width,nrow=height,data=NA)
RRc<-matrix(ncol=width,nrow=height,data=NA)
MM<-matrix(ncol=width,nrow=height,data=MGR)
TT<-matrix(ncol=width,nrow=height,data=TMax)
OO<-matrix(ncol=width,nrow=height,data=ODMax)
DD<-matrix(ncol=width,nrow=height,data=MaxOD)
rownames(MM)<-rowname
rownames(TT)<-rowname
rownames(OO)<-rowname
rownames(DD)<-rowname
MMb<-matrix(ncol=width,nrow=height,data=NA)
TTb<-matrix(ncol=width,nrow=height,data=NA)
OOb<-matrix(ncol=width,nrow=height,data=NA)
DDb<-matrix(ncol=width,nrow=height,data=NA)
rownames(MMb)<-rowname
rownames(TTb)<-rowname
rownames(OOb)<-rowname
rownames(DDb)<-rowname
MMa<-matrix(ncol=width,nrow=height,data=NA)
TTa<-matrix(ncol=width,nrow=height,data=NA)
OOa<-matrix(ncol=width,nrow=height,data=NA)
DDa<-matrix(ncol=width,nrow=height,data=NA)
rownames(MMa)<-rowname
rownames(TTa)<-rowname
rownames(OOa)<-rowname
rownames(DDa)<-rowname
MMc<-matrix(ncol=width,nrow=height,data=NA)
TTc<-matrix(ncol=width,nrow=height,data=NA)
OOc<-matrix(ncol=width,nrow=height,data=NA)
DDc<-matrix(ncol=width,nrow=height,data=NA)
rownames(MMc)<-rowname
rownames(TTc)<-rowname
rownames(OOc)<-rowname
rownames(DDc)<-rowname
for(i in 3height-2)){
for(j in 3width-2)){
MMb[i,j]<-MM[i,j];
TTb[i,j]<-TT[i,j];
OOb[i,j]<-OO[i,j];
DDb[i,j]<-DD[i,j];
RRb[i,j]<-RR[i,j];
}
}
for(i in c(1,height)){
for(j in c(1:width)){
RRa[i,j]<-RR[i,j];
MMa[i,j]<-MM[i,j];
TTa[i,j]<-TT[i,j];
OOa[i,j]<-OO[i,j];
DDa[i,j]<-DD[i,j];
}
}
for(i in c(2,(height-1))){#2eme couronne
for(j in c(2width-1))){
RRc[i,j]<-RR[i,j];
MMc[i,j]<-MM[i,j];
TTc[i,j]<-TT[i,j];
OOc[i,j]<-OO[i,j];
DDc[i,j]<-DD[i,j];
}
}
RRRa<-RRa[1:puits]
RRRb<-RRb[1:puits]
RRRc<-RRc[1:puits]
MMMa<-MMa[1:puits]
MMMb<-MMb[1:puits]
MMMc<-MMc[1:puits]
TTTa<-TTa[1:puits]
TTTb<-TTb[1:puits]
TTTc<-TTc[1:puits]
OOOa<-OOa[1:puits]
OOOb<-OOb[1:puits]
OOOc<-OOc[1:puits]
DDDa<-DDa[1:puits]
DDDb<-DDb[1:puits]
DDDc<-DDc[1:puits]
###########################################
pdf(file=paste(sub(".txt",".pdf",fichier),sep=""),paper="a4r",width=10,height=13);
plot(C[,1],C[,2],type='l',ylim=c(0,M),xlab="Time (s)",ylab="OD");
for(i in 1:puits){
lines(C[,1],C[,i+1],col=i);
}
#lines(C[,1],C[,15*16+9+1],col=2,lwd=3);
par(mfcol=c(height,width));
par(oma=c(0,0,0,0));
par(mar=c(0,0,0,0));
for(i in 1:puits){
plot(C[,1],C[,i+1],type='l',main="",sub="",xlab="",ylab="",yaxt = "n",xaxt = "n",ylim=c(0,M));
#if(i==(15*16+9)){lines(C[,1],C[,i+1],col=2,lwd=2);}
}
par(mar=c(5, 4, 4, 2) + 0.1);
levelplot(t(MM),main="MGR")
levelplot(t(TT),main="TMax")
levelplot(t(OO),main="ODatMax")
levelplot(t(DD),main="MaxOD")
MMM<-MM[1:puits]
TTT<-TT[1:puits]
OOO<-OO[1:puits]
DDD<-DD[1:puits]
TOUT<-cbind(MMM,TTT,OOO,DDD)
colnames(TOUT)<-c("MGR","TMax","ODatMax","MaxOD")
pairs(TOUT)
par(mfrow=c(2,2))
plot(RRR,MMM,xlab="Distance to the center",ylab="MGR",col=0)
points(RRRa,MMMa,col=2,pch=3)
points(RRRc,MMMc,col=3,pch=3)
points(RRRb,MMMb,col='blue')
legend("topleft",legend=c("Centre","1e couronnes","2e couronnes"),col=c('blue','red','green'),pch=c(1,3,3),bty='n')
plot(RRR,TTT,xlab="Distance to the center",ylab="TMax",col=0)
points(RRRa,TTTa,col=2,pch=3)
points(RRRc,TTTc,col=3,pch=3)
points(RRRb,TTTb,col='blue')
legend("topleft",legend=c("Centre","1e couronnes","2e couronnes"),col=c('blue','red','green'),pch=c(1,3,3),bty='n')
plot(RRR,OOO,xlab="Distance to the center",ylab="ODatMax",col=0)
points(RRRa,OOOa,col=2,pch=3)
points(RRRc,OOOc,col=3,pch=3)
points(RRRb,OOOb,col='blue')
legend("topleft",legend=c("Centre","1e couronnes","2e couronnes"),col=c('blue','red','green'),pch=c(1,3,3),bty='n')
plot(RRR,DDD,xlab="Distance to the center",ylab="MaxOD",col=0)
points(RRRa,DDDa,col=2,pch=3)
points(RRRc,DDDc,col=3,pch=3)
points(RRRb,DDDb,col='blue')
legend("topleft",legend=c("Centre","1e couronnes","2e couronnes"),col=c('blue','red','green'),pch=c(1,3,3),bty='n')
levelplot(t(MMb),main="MGRb")
levelplot(t(TTb),main="TMaxb")
levelplot(t(OOb),main="ODatMaxb")
levelplot(t(DDb),main="MaxODb")
par(mfrow=c(2,2))
hist(MMM,xlab="MGR",main="")
CV<-sd(MMM,na.rm=TRUE)/mean(MMM,na.rm=TRUE)
legend("topleft",legend=paste("CV = ",signif(CV,2)*100," %",sep=""),bty='n')
hist(MMMb,xlab="MGRb",main="")
CVb<-sd(MMMb,na.rm=TRUE)/mean(MMMb,na.rm=TRUE)
legend("topleft",legend=paste("CV = ",signif(CVb,2)*100," %",sep=""),bty='n')
hist(MMMb[MMMb>0.00015],xlab="MGRb > 0.00015",main="")
CVbpos<-sd(MMMb[MMMb>0.00015],na.rm=TRUE)/mean(MMMb[MMMb>0.00015],na.rm=TRUE)
legend("topleft",legend=paste("CV = ",signif(CVbpos,2)*100," %",sep=""),bty='n')
dev.off()
}
path <- file.path("C:/...")
{
fichiers=list.files(path,".txt",full.names=TRUE)
A<-read.table(fichiers[1],header=TRUE)
Time<-as.integer(sub("s","",A[,1]))
puits=ncol(A)-1
if ( puits == 96 )
{
width = 12
height = 8
}
if ( puits == 384 )
{
width = 24
height = 16
}
C<-cbind(Time,A[,2puits+1)])
M<-max(C[,2puits+1)])
pdf(file="Test.pdf",paper="a4r",width=10,height=13);
plot(C[,1],C[,2],type='l',ylim=c(0,M),xlab="Time (s)",ylab="OD");
for(i in 1:puits){
lines(C[,1],C[,i+1],col=i);
}
par(mfcol=c(height,width));
par(oma=c(0,0,0,0));
par(mar=c(0,0,0,0));
for(i in 1:puits){
plot(C[,1],C[,i+1],type='l',main="",sub="",xlab="",ylab="",yaxt = "n",xaxt = "n",ylim=c(0,M));
}
dev.off()
#NoReg
count<-1;
ParamSmooth<-0.6
cut<-0.01
FLength<-length(fichiers)
MGR<-matrix(ncol=length(fichiers),nrow=puits);
TMax<-matrix(ncol=length(fichiers),nrow=puits);
ODMax<-matrix(ncol=length(fichiers),nrow=puits);
MaxOD<-matrix(ncol=length(fichiers),nrow=puits);
Index2<-matrix(ncol=length(fichiers),nrow=puits);
MGRcut<-array(dim=c(puits,length(fichiers)));
for(fichier in fichiers){
Tecan=read.table(file=fichier,header=TRUE);
Time<-as.integer(sub("s","",Tecan[,1]))
Donnees<-cbind(Time,Tecan[,2puits+1)])
# Tecan[,1]<-as.integer(sub("s","",Tecan[,1]))
# Donnees<-Tecan;
pdf(file=paste(sub(".txt","",fichier),"_MGR.pdf",sep=""),paper="a4r",width=10,height=13);
par(mfcol=c(height,width));
par(oma=c(0,0,0,0));
par(mar=c(0,0,0,0));
T<-matrix(ncol=1,nrow=length(Donnees[,1]));
Sel<-matrix(ncol=1,nrow=length(Donnees[,1]));
L<-matrix(ncol=1,nrow=length(Donnees[,1]));
PL<-matrix(ncol=1,nrow=length(Donnees[,1]));
for(col in 1:puits){
minOD<-min(Donnees[,col+1],na.rm=TRUE);
for (i in 1:1){
T[,i]<-Donnees[,col+1]-minOD+cut;
Sel[,i]<-is.finite(log(T[,i]));
L[Sel[,i],i]<-smooth.spline(Donnees[Sel[,i],1],log(T[Sel[,i],i]),spar=ParamSmooth)$y;
PL[Sel[,i],i]<-predict(smooth.spline(Donnees[Sel[,i],1],log(T[Sel[,i],i]),spar=ParamSmooth),Donnees[Sel[,i],1],deriv=1)$y;
}
Slope<-PL[Sel[,i],i]<-predict(smooth.spline(Donnees[Sel[,i],1],log(T[Sel[,i],i]),spar=ParamSmooth),Donnees[Sel[,i],1],deriv=1)$y;
SSlope<-c();
SSlope<-predict(smooth.spline(Donnees[Sel[,i],1],Slope,spar=ParamSmooth),Donnees[Sel[,i],1],deriv=1)$y;
SSlope1<-SSlope[1length(Slope)-1)]*SSlope[2:length(Slope)];
Sign<-SSlope1<=0;
MGR[col,count]<-max(Slope[Sign]);
#MGRcut[col,count]<-max(Slope[Sign]);
Index<-which(Sign);
if ( length(Index) != 0 )
{
plot(Donnees[Sel[,1],1],PL[Sel[,1],1],ylim=c(0,1E-3),type='l',main="",sub="",xlab="",ylab="",yaxt = "n",xaxt = "n");
Index2[col,count]<-Index[which.max(Slope[Index])]
TMax[col,count]<-Donnees[Index2[col,count],1];
ODMax[col,count]<-Donnees[Index2[col,count],col+1];
MaxOD[col,count]<-max(Donnees[,col+1],na.rm=TRUE);
#MaxOD[col,count]<-Donnees[nrow(A)-1,col+1];##Add ~ 50 000s after TMax
lines(Donnees[Sel[,1],1],Slope);
points(Donnees[Index2[col,count],1],MGR[col,count],col=2);
par(new=TRUE);
#plot(Donnees[Sel[,1],1],SSlope1[Sel[,1]],type='l',main="",sub="",xlab="",ylab="",yaxt = "n",xaxt = "n",col='grey');
abline(h=0,col='grey');
par(new=TRUE);
plot(Donnees[Sel[,i],1],Donnees[Sel[,i],col+1],ylim=c(0,1.5),type='l',main="",sub="",xlab="",ylab="",yaxt = "n",xaxt = "n",col='blue');
par(new=TRUE);
plot(Donnees[Sel[,1],1],L[Sel[,1],1],type='l',main="",sub="",xlab="",ylab="",yaxt = "n",xaxt = "n",col='green');
#legend("topleft",legend=c(well[col]),bty="n");
#legend("bottomright",legend=c(col),bty="n");
}
else
{
plot(Donnees[Sel[,1],1],PL[Sel[,1],1],ylim=c(0,1E-3),type='l',main="ERROR",sub="Error",xlab="",ylab="",yaxt = "n",xaxt = "n");
par(new=TRUE);
plot(Donnees[Sel[,i],1],Donnees[Sel[,i],col+1],ylim=c(0,1.5),type='l',main="",sub="",xlab="",ylab="",yaxt = "n",xaxt = "n",col='blue');
par(new=TRUE);
plot(Donnees[Sel[,1],1],L[Sel[,1],1],type='l',main="",sub="",xlab="",ylab="",yaxt = "n",xaxt = "n",col='grey');
}
}
dev.off();
DrawSummary(fichier,MGR[,count],TMax[,count],ODMax[,count],MaxOD[,count],C,M)
i<-1
rows<-rep(0,puits)
if ( puits == 384 )
{
for(t in 1:24)
for(j in 1:16 )
{
rows[i]<-paste(c("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P")[j],t,sep="")
i<-i+1
}
}
if ( puits == 96 )
{
for(t in 1:12)
for(j in 1:8 )
{
rows[i]<-paste(c("A","B","C","D","E","F","G","H")[j],t,sep="")
i<-i+1
}
}
tab<-matrix(ncol=4,nrow=length(MGR[,count]),dimnames=list(rows,c("MGR","TMax","ODMax","MaxOD")))
tab[,1]<-MGR[,count]
tab[,2]<-TMax[,count]
tab[,3]<-ODMax[,count]
tab[,4]<-MaxOD[,count]
write.table(tab,paste(sub(".txt","",fichier),"_outexcel.csv",sep=""),sep=";")
count<-count+1;
}
}
Partager