]> git.treefish.org Git - phys/latscripts.git/blob - meanstep.py
...
[phys/latscripts.git] / meanstep.py
1 #!/usr/bin/python
2
3 """ meanstep.py
4  ...
5 """
6
7 import sys
8 import os
9
10 results = {}
11
12 if len(sys.argv) != 4 and len(sys.argv) != 5:
13     print "usage: ./meanstep.py <datadir> <matching col 1>,...,<matching col n> <mean col 1>,...,<mean col n> [<outdir>]"
14     exit(0)
15
16 matchcols = sys.argv[2].split(",")
17 meancols = sys.argv[3].split(",")
18
19 if len(sys.argv) == 5:
20     outdir = sys.argv[4]
21     writetofile = True
22 else:
23     writetofile = False
24
25 for root, dirs, filenames in os.walk(sys.argv[1]):
26     for f in filenames:
27         if f[-4:] != ".dat":
28             continue
29
30         if not f[:-4] in results:
31             results[f[:-4]] = {}
32             print "Found new fileid: " + f[:-4]
33
34         with open(root + "/" + f) as inf:
35             for line in inf:
36                 if line[0:1] == "#":
37                     continue
38
39                 linecols = line.split()
40                 matchkey = ""
41                 for imatchcol in range(0,len(matchcols)):
42                     matchkey = matchkey + linecols[int(matchcols[imatchcol])-1]
43                 
44                 if not matchkey in results[f[:-4]]:
45                     results[f[:-4]][matchkey] = linecols + [1]
46                 else:
47                     results[f[:-4]][matchkey][ len(results[f[:-4]][matchkey]) - 1 ] = \
48                         results[f[:-4]][matchkey][ len(results[f[:-4]][matchkey]) - 1 ] + 1 
49          
50                     for imeancol in range(0,len(meancols)):
51                         results[f[:-4]][matchkey][int(meancols[imeancol])-1] = \
52                             float(results[f[:-4]][matchkey][int(meancols[imeancol])-1]) + \
53                             float(linecols[int(meancols[imeancol])-1])
54
55 for fileid in results:
56     print "\n[" + fileid + "]"
57
58     if writetofile:
59         outfile = open(outdir + "/" + fileid + "_meanstep.dat", "w+")
60
61     for matchkey in results[fileid]:
62         for imeancol in range(0,len(meancols)):
63             results[fileid][matchkey][int(meancols[imeancol])-1] = \
64                 float(results[fileid][matchkey][int(meancols[imeancol])-1]) / \
65                 results[fileid][matchkey][ len(results[f[:-4]][matchkey]) - 1 ]
66         
67         for icol in range(0,len(results[fileid][matchkey])-1):
68             sys.stdout.write(str(results[fileid][matchkey][icol]))
69             if writetofile:
70                 outfile.write(str(results[fileid][matchkey][icol]));
71
72             if icol != len(results[fileid][matchkey])-2:
73                 sys.stdout.write("\t")
74                 if writetofile:
75                     outfile.write("\t")
76                                   
77         print ""
78         if writetofile:
79             outfile.write("\n")
80
81     if writetofile:
82         outfile.close()