-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinDataOnMatrix.py
More file actions
executable file
·150 lines (122 loc) · 3.25 KB
/
binDataOnMatrix.py
File metadata and controls
executable file
·150 lines (122 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
from sys import *
from albertcommon import *
from getopt import getopt
from numpy import *
def printUsageAndExit(programName):
print >> stderr,programName,"[options] filename"
print >> stderr,"Options:"
print >> stderr,"--min set the min of the bins [default: detect the min in data]"
print >> stderr,"--max set the max of the bins [default: detect the max in data]"
print >> stderr,"--nbins set the number of bins [default: 11]"
print >> stderr,"--bins [x1,x2,x3...,xN], or range(x,y) or linspace(s,e,nbins) some other python code. directly set the bin lower bounds"
print >> stderr,"--cols colRange. operate on these range only [default: 2-_1]"
print >> stderr,"--ceiling f. set the ceiling above the max by 1+f [default: f=0.1]"
print >> stderr,"--keycol col [default: 1]"
explainColumns(stderr)
exit(1)
def StrListToFloatListIgnoreErrors(S):
L=[]
for s in S:
try:
L.append(float(s))
except:
pass
return L
def getProbVector(V):
sumV=sum(V)
p=[]
for v in V:
p.append(float(v)/sumV)
return p
if __name__=='__main__':
programName=argv[0]
opts,args=getopt(argv[1:],'',['min=','max=','nbins=','bins=','cols=','ceiling=','keycol='])
minBins=None
maxBins=None
nbins=11
cols="2-_1"
ceiling=0.1
bins=None
headerRow=1
startRow=2
fs="\t"
keycol="1"
for o,v in opts:
if o=='--min':
minBins=float(v)
elif o=='--max':
maxBins=float(v)
elif o=='--nbins':
nbins=int(v)
elif o=='--bins':
bins=eval(v)
elif o=='--cols':
cols=v
elif o=='--ceiling':
ceiling=float(v)
elif o=='--keycol':
keycol=v
try:
filename,=args
except:
printUsageAndExit(programName)
header,prestarts=getHeader(filename,headerRow,startRow,fs)
cols=getCol0ListFromCol1ListStringAdv(header,cols)
keycol=getCol0ListFromCol1ListStringAdv(header,keycol)[0]
#if bins not defined and ( minBins or maxBins not defined). first pass:
if bins==None and ( minBins==None or maxBins==None ):
minValue=None
maxValue=None
fil=open(filename)
lino=0
for lin in fil:
lino+=1
if lino<startRow:
continue
lin=lin.rstrip("\r\n")
fields=lin.split(fs)
values=StrListToFloatListIgnoreErrors(getSubvector(fields,cols))
if minValue==None:
try:
minValue=min(values)
except:
pass
else:
minValue=min(minValue,min(values))
if maxValue==None:
try:
maxValue=max(values)
except:
pass
else:
maxValue=max(maxValue,max(values))
fil.close()
if minBins==None:
minBins=minValue
if maxBins==None:
maxBins=maxValue*(1+ceiling)
if bins==None:
bins=linspace(minBins,maxBins,nbins)
print >> stdout,fs.join(["key"]+toStrList(bins))
lino=0
fil=open(filename)
for lin in fil:
lino+=1
if lino<startRow:
continue
lin=lin.rstrip("\r\n")
fields=lin.split(fs)
values=StrListToFloatListIgnoreErrors(getSubvector(fields,cols))
#print >> stderr,"values:",values
if len(values)==0:
print >> stdout,fs.join([fields[keycol]]+(['NA']*len(bins)))
else:
bincounts=[0]*len(bins)
digitized=digitize(values,bins)
#print >> stderr,"degit",digitized
for digix in digitized:
bincounts[digix-1]+=1
binp=getProbVector(bincounts)
print >> stdout,fs.join([fields[keycol]]+toStrList(binp))
fil.close()