-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombineWinsIntoFeatureVec.py
60 lines (54 loc) · 2.07 KB
/
combineWinsIntoFeatureVec.py
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
import sys, os
statFilePrefix = sys.argv[1]
summaryStatDir = "/".join(statFilePrefix.split("/")[:-1])
statH = {}
for fileName in os.listdir(summaryStatDir):
if fileName.startswith(statFilePrefix.split("/")[-1]):
winIndex = int(fileName.split("_")[1].split(".")[0])
statFile = open(summaryStatDir + "/" + fileName)
header = True
assert not statH.has_key(winIndex)
statH[winIndex] = {}
for line in statFile.xreadlines():
line = line.strip().split("\t")
if header:
header = False
statNames = line
else:
for i in range(len(line)):
stat = float(line[i])
if not statH[winIndex].has_key(statNames[i]):
statH[winIndex][statNames[i]] = []
statH[winIndex][statNames[i]].append(stat)
statFile.close()
winIndices = sorted(statH)
lenH = {}
for winIndex in winIndices:
for statName in statH[winIndex].keys():
lenH[len(statH[winIndex][statName])] = winIndex
if len(lenH) != 1:
sys.exit("Not all windows have the same number of simulations (%s). AAAAAAAAAAAAARRRRRRRRRRRGGGGGGHHHHHHHHHHH!!!" %(lenH))
header = []
for j in range(len(statNames)):
for k in range(len(statH)):
header.append(statNames[j] + "_win%s" %(k))
print "\t".join(header)
for i in range(lenH.keys()[0]):#ith simulation
outline = []
for j in range(len(statNames)):#jth statistic
statVec = []
for k in range(len(statH)):#kth window
statVec.append(statH[k][statNames[j]][i])
minVal = min(statVec)
if minVal < 0:
statVec = [x-minVal for x in statVec]
normStatVec = []
#statMean = sum(statVec)/float(len(statVec))
statSum = float(sum(statVec))
if statSum == 0:
normStatVec = [1.0/len(statVec)]*len(statVec)
else:
for k in range(len(statVec)):
normStatVec.append(statVec[k]/statSum)
outline.extend(normStatVec)
print "\t".join([str(x) for x in outline])