<filename>python/ikit/math.py
import numpy as np

def endpoint_error(a, b):
    return np.average(np.sqrt(np.sum(np.power(a - b, 2), axis=2)))

def spatial_endpoint_error(a, b):
    return np.sqrt(np.sum(np.power(a - b, 2), axis=2))

def normalize_equally(*args):
    ret = []
    min = np.inf
    max = -np.inf
    for arg in args:
        min = np.minimum(min, np.min(arg))
        max = np.maximum(max, np.max(arg))

    r = max - min
    for arg in args:
        if r > 0:  ret.append((arg.copy() - min)/r)
        else: ret.append(arg.copy())

    if len(ret) == 1: return ret[0]
    return ret

def int_from_string(x):
    import re

    match = re.compile('([0-9\.]+)[mM]').match(x)
    if match: return int(float(match.group(1))*1000000)

    match = re.compile('([0-9\.]+)[kK]').match(x)
    if match: return int(float(match.group(1))*1000)

    return int(x)

def format_big_number(x):
    if x>1000:
        x/=1000
        if x>1000:
            x/=1000
            if x>1000:
                x/=1000
                return '%dG' % x
            else:
                return '%dM' % x
        else:
            return '%dk' % x
    else:
        return x

<filename>python/ikit/math.py
import numpy as np

def endpoint_error(a, b):
    return np.average(np.sqrt(np.sum(np.power(a - b, 2), axis=2)))

def spatial_endpoint_error(a, b):
    return np.sqrt(np.sum(np.power(a - b, 2), axis=2))

def normalize_equally(*args):
    ret = []
    min = np.inf
    max = -np.inf
    for arg in args:
        min = np.minimum(min, np.min(arg))
        max = np.maximum(max, np.max(arg))

    r = max - min
    for arg in args:
        if r > 0:  ret.append((arg.copy() - min)/r)
        else: ret.append(arg.copy())

    if len(ret) == 1: return ret[0]
    return ret

def int_from_string(x):
    import re

    match = re.compile('([0-9\.]+)[mM]').match(x)
    if match: return int(float(match.group(1))*1000000)

    match = re.compile('([0-9\.]+)[kK]').match(x)
    if match: return int(float(match.group(1))*1000)

    return int(x)

def format_big_number(x):
    if x>1000:
        x/=1000
        if x>1000:
            x/=1000
            if x>1000:
                x/=1000
                return '%dG' % x
            else:
                return '%dM' % x
        else:
            return '%dk' % x
    else:
        return x
