]> Creatis software - clitk.git/commitdiff
merging
authorPierre Gueth <gueth@linux6.dg.creatis.insa-lyon.fr>
Fri, 20 May 2011 13:53:38 +0000 (15:53 +0200)
committerSimon Rit <simon.rit@creatis.insa-lyon.fr>
Fri, 5 Jul 2013 07:17:29 +0000 (09:17 +0200)
cluster_tools/mergeStatFile.py [new file with mode: 0755]

diff --git a/cluster_tools/mergeStatFile.py b/cluster_tools/mergeStatFile.py
new file mode 100755 (executable)
index 0000000..eaa78cb
--- /dev/null
@@ -0,0 +1,91 @@
+#!/usr/bin/env python2
+# coding: utf-8
+
+import sys
+import re
+import datetime
+
+linere = re.compile(r'''^#\s+([a-zA-Z]+)\s+=\s(.*)$''')
+mergedlines = ['NumberOfRun', 'NumberOfEvents', 'NumberOfTracks', 'NumberOfSteps', 'NumberOfGeometricalSteps', 'NumberOfPhysicalSteps', 'ElapsedTime', 'StartDate', 'EndDate']
+
+assert(len(sys.argv)==7)
+assert(sys.argv[1]=="-i")
+assert(sys.argv[3]=="-j")
+assert(sys.argv[5]=="-o")
+
+def total_seconds(deltat):
+    try:
+        return float(deltat.total_seconds())
+    except AttributeError: # total_seconds defined in 2.7
+        total = 0.
+        total += deltat.seconds
+        total += deltat.microseconds*1e-6
+        total += deltat.days*3600.*24.
+        return total
+
+def parse_stat_file(filename):
+    keys = {}
+    for line in open(filename,"r").readlines():
+        match = linere.match(line)
+        assert(match is not None)
+        groups = match.groups()
+        if groups[0] not in mergedlines:
+            continue
+        keys[groups[0]]=groups[1]
+    return keys
+
+def merge_keys(ikeys,jkeys):
+    mindate = None
+    maxdate = None
+    keys = {}
+    for line in mergedlines:
+        value = None
+
+        if value is None:
+            try:
+                ivalue = int(ikeys[line])
+                jvalue = int(jkeys[line])
+                value = ivalue + jvalue
+                value = str(value)
+            except ValueError:
+                pass
+
+        if value is None:
+            try:
+                ivalue = float(ikeys[line])
+                jvalue = float(jkeys[line])
+                value = ivalue + jvalue
+                value = str(value)
+            except ValueError:
+                pass
+
+        if value is None:
+                ivalue = datetime.datetime.strptime(ikeys[line],"%a %b %d %H:%M:%S %Y")
+                jvalue = datetime.datetime.strptime(jkeys[line],"%a %b %d %H:%M:%S %Y")
+                if line=="StartDate":
+                    value = min(ivalue,jvalue)
+                    mindate = value
+                if line=="EndDate":
+                    value = max(ivalue,jvalue)
+                    maxdate = value
+                value = value.strftime("%a %b %d %H:%M:%S %Y")
+
+        assert(value is not None)
+        keys[line] = value
+    if mindate is not None and maxdate is not None:
+        speedup = float(keys["ElapsedTime"])/total_seconds(maxdate-mindate)
+        keys["Speedup"] = str(speedup)
+    return keys
+
+def format_keys(keys):
+    output = "\n".join("# %s = %s" % (line,keys[line]) for line in mergedlines)
+    if "Speedup" in keys:
+        output += "\n# Speedup = %s" % keys["Speedup"]
+    return output
+
+ikeys = parse_stat_file(sys.argv[2])
+jkeys = parse_stat_file(sys.argv[4])
+keys  = merge_keys(ikeys,jkeys)
+output = format_keys(keys)
+open(sys.argv[6],"w").write(output)
+