changeset 355:430c9e92cd23

Added common directory
author Joseph Turian <turian@iro.umontreal.ca>
date Thu, 19 Jun 2008 16:12:29 -0400
parents d580b3a369a4
children 18702ceb2096
files common/__init__.py common/file.py common/floateq.py common/memory.py common/misc.py common/time.py misc.py
diffstat 7 files changed, 151 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/__init__.py	Thu Jun 19 16:12:29 2008 -0400
@@ -0,0 +1,1 @@
+from * import *
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/file.py	Thu Jun 19 16:12:29 2008 -0400
@@ -0,0 +1,12 @@
+import gzip, bz2
+
+def myopen(filename, mode="r", bufsize=-1):
+    """
+    open(), detecting .gz and .bz2 file suffixes
+    """
+    if filename[-3:] == ".gz":
+        return gzip.open(filename, mode, bufsize)
+    elif filename[-4:] == ".bz2":
+        return bz2.open(filename, mode, bufsize)
+    else:
+        return open(filename, mode, bufsize)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/floateq.py	Thu Jun 19 16:12:29 2008 -0400
@@ -0,0 +1,49 @@
+#
+# Determine if floating point numbers are very close
+###########
+
+import math
+
+DEFAULT_SANITY_CHECK_EPSILON = 1e-6
+
+def floateq(a, b, epsilon=DEFAULT_SANITY_CHECK_EPSILON):
+    """
+    Compare two floats, with some epsilon tolerance.
+    """
+    return absolute_relative_error(a, b) < epsilon
+
+def absolute_relative_error(a, b, epsilon=DEFAULT_SANITY_CHECK_EPSILON):
+    return abs(a - b) / (abs(a) + abs(b) + epsilon)
+
+def double_epsilon_multiplicative_eq(a, b, epsilon=DEFAULT_SANITY_CHECK_EPSILON):
+    """
+    Determine if doubles are equal to within a multiplicative factor of
+    L{epsilon}.
+    @note: This function should be preferred over
+    L{double_epsilon_additive_eq}, unless the values to be compared may
+    have differing signs.
+    @precondition: sign(a) == sign(b)
+    @rtype: bool
+    """
+    if a == b: return True
+    if a == 0 and b == 0: return True
+    assert a != 0
+    assert b != 0
+    assert sign(a) == sign(b)
+    if a > b: d = a / b
+    else: d = b / a
+    assert d >= 1
+    return True if d <= 1 + SANITY_CHECK_EPSILON else False
+
+def double_epsilon_additive_eq(a, b):
+    """
+    Determine if doubles are equal to within an additive factor of
+    L{SANITY_CHECK_EPSILON}.
+    @note: Prefer L{double_epsilon_multiplicative_eq} to this function
+    unless the values to be compared may have differing signs.
+    """
+    if a == b: return True
+    if a == 0 and b == 0: return True
+    assert sign(a) != sign(b)   # Should use SANITY_CHECK_EPSILON
+    d = math.fabs(a - b)
+    return d <= SANITY_CHECK_EPSILON
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/memory.py	Thu Jun 19 16:12:29 2008 -0400
@@ -0,0 +1,52 @@
+"""
+Determine memory usage of a program::
+    m0 = memory()
+    ...
+    m1 = memory(m0)
+@note: From U{http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286222/index_txt}
+@warning: Not portable.
+"""
+
+import os
+
+_proc_status = '/proc/%d/status' % os.getpid()
+
+_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
+          'KB': 1024.0, 'MB': 1024.0*1024.0}
+
+def _VmB(VmKey):
+    '''Private.
+    '''
+    global _proc_status, _scale
+     # get pseudo file  /proc/<pid>/status
+    try:
+        t = open(_proc_status)
+        v = t.read()
+        t.close()
+    except:
+        return 0.0  # non-Linux?
+     # get VmKey line e.g. 'VmRSS:  9999  kB\n ...'
+    i = v.index(VmKey)
+    v = v[i:].split(None, 3)  # whitespace
+    if len(v) < 3:
+        return 0.0  # invalid format?
+     # convert Vm value to bytes
+    return float(v[1]) * _scale[v[2]]
+
+
+def memory(since=0.0):
+    '''Return memory usage in bytes.
+    '''
+    return _VmB('VmSize:') - since
+
+
+def resident(since=0.0):
+    '''Return resident memory usage in bytes.
+    '''
+    return _VmB('VmRSS:') - since
+
+
+def stacksize(since=0.0):
+    '''Return stack size in bytes.
+    '''
+    return _VmB('VmStk:') - since
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/misc.py	Thu Jun 19 16:12:29 2008 -0400
@@ -0,0 +1,25 @@
+def sign(i, assertions=True):
+    """
+    + or - 1
+    @precondition: i != 0
+    """
+    if assertions:
+        assert i != 0
+    else:
+        if i == 0: return 0
+
+    return +1 if i > 0 else -1
+
+def percent_string(a, b):
+    assert a <= b
+    assert a >= 0
+    assert b > 0
+    return "%s of %s (%.2f%%)" % (a, b, 100.*a/b)
+
+def unique_elements_list_intersection(list1,list2):
+    """
+    Return the unique elements that are in both list1 and list2
+    (repeated elements in listi will not be duplicated in the result).
+    This should run in O(n1+n2) where n1=|list1|, n2=|list2|.
+    """
+    return list(set.intersection(set(list1),set(list2)))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/time.py	Thu Jun 19 16:12:29 2008 -0400
@@ -0,0 +1,12 @@
+
+import time
+#http://www.daniweb.com/code/snippet368.html
+def print_timing(func):
+    def wrapper(*arg):
+        t1 = time.time()
+        res = func(*arg)
+        t2 = time.time()
+        print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0)
+        return res
+    return wrapper
+
--- a/misc.py	Thu Jun 19 12:35:41 2008 -0400
+++ b/misc.py	Thu Jun 19 16:12:29 2008 -0400
@@ -1,18 +0,0 @@
-
-def unique_elements_list_intersection(list1,list2):
-    """
-    Return the unique elements that are in both list1 and list2
-    (repeated elements in listi will not be duplicated in the result).
-    This should run in O(n1+n2) where n1=|list1|, n2=|list2|.
-    """
-    return list(set.intersection(set(list1),set(list2)))
-import time
-#http://www.daniweb.com/code/snippet368.html
-def print_timing(func):
-    def wrapper(*arg):
-        t1 = time.time()
-        res = func(*arg)
-        t2 = time.time()
-        print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0)
-        return res
-    return wrapper