annotate tools/mytools/cdf-old-not-used/cdf.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 python wrapper for cdf plot
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 '''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 import os,sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 filename = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 output = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 c1, c2 = sys.argv[3].strip().split(',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 label1,label2 = sys.argv[5].strip().split(',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 title = sys.argv[6].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 log = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 rf = open('tmp.r','w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 rf.write("options(warn=-1)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 rf.write("x <- read.table('"+filename+"')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 rf.write("mycdf <- function(x1,x2) {\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 rf.write(" sx1 <- sort(x1)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 rf.write(" sy1 <- c(1:length(sx1))/length(sx1)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 rf.write(" sx2 <- sort(x2)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 rf.write(" sy2 <- c(1:length(sx2))/length(sx2)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 rf.write(" mi <- min(c(x1,x2))\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 rf.write(" ma <- max(c(x1,x2))\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 rf.write(" sx1 <- c(mi,sx1,ma)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 rf.write(" sx2 <- c(mi,sx2,ma)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 rf.write(" sy1 <- c(0,sy1,1)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 rf.write(" sy2 <- c(0,sy2,1)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 rf.write(" pv <- ks.test(x1,x2)$p.value\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 rf.write(" list(sx1,sy1,sx2,sy2,pv)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 rf.write("}\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 rf.write("list1 <- x[,"+c1+"]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 rf.write("list2 <- x[,"+c2+"]\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 if log == 'log':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 rf.write("list1 <- log2(1+list1) \n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 rf.write("list2 <- log2(1+list2) \n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 #rf.write("list1[list1 == -Inf] <- min(list1[list1 != -Inf])\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 rf.write("res <- mycdf(list1,list2)\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 rf.write("pdf('"+output+"')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 rf.write("plot(res[[1]],res[[2]],type='l',lty=1,lwd=2,col='red',main=paste('"+title+"',format(res[[5]], digit=2,scientific = T),sep=' , p='),xlab='value',ylab='cumulative frequency')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 rf.write("lines(res[[3]],res[[4]],type='l',lty=2,lwd=2,col='blue')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 rf.write("label1 <- paste('"+label1+"','( n1=',length(list1),')')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 rf.write("label2 <- paste('"+label2+"','( n2=',length(list2),')')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 rf.write("legend('bottomright',c(label1,label2),col=c('red','blue'), lty=1:2, lwd=2, bty='n')\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 rf.write("boxplot(list1,list2,names=c(label1,label2))\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 rf.write("dev.off()\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 rf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 os.system("R --vanilla < tmp.r")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 os.system('rm tmp.r')