Python operator.abs() Examples

The following are 30 code examples of operator.abs(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module operator , or try the search function .
Example #1
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def argmax(iterable, key=None, both=False):
    """
    >>> argmax([4,2,-5])
    0
    >>> argmax([4,2,-5], key=abs)
    2
    >>> argmax([4,2,-5], key=abs, both=True)
    (2, 5)
    """
    if key is not None:
        it = imap(key, iterable)
    else:
        it = iter(iterable)
    score, argmax = reduce(max, izip(it, count()))
    if both:
        return argmax, score
    return argmax 
Example #2
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def compose(*funcs):
    """Compose `funcs` to a single function.

    >>> compose(operator.abs, operator.add)(-2,-3)
    5
    >>> compose()('nada')
    'nada'
    >>> compose(sorted, set, partial(filter, None))(range(3)[::-1]*2)
    [1, 2]
    """
    # slightly optimized for most common cases and hence verbose
    if len(funcs) == 2: f0,f1=funcs; return lambda *a,**kw: f0(f1(*a,**kw))
    elif len(funcs) == 3: f0,f1,f2=funcs; return lambda *a,**kw: f0(f1(f2(*a,**kw)))
    elif len(funcs) == 0: return lambda x:x     # XXX single kwarg
    elif len(funcs) == 1: return funcs[0]
    else:
        def composed(*args,**kwargs):
            y = funcs[-1](*args,**kwargs)
            for f in funcs[:0:-1]: y = f(y)
            return y
        return composed 
Example #3
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def argmax(iterable, key=None, both=False):
    """
    >>> argmax([4,2,-5])
    0
    >>> argmax([4,2,-5], key=abs)
    2
    >>> argmax([4,2,-5], key=abs, both=True)
    (2, 5)
    """
    if key is not None:
        it = imap(key, iterable)
    else:
        it = iter(iterable)
    score, argmax = reduce(max, izip(it, count()))
    if both:
        return argmax, score
    return argmax 
Example #4
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def argmax(iterable, key=None, both=False):
    """
    >>> argmax([4,2,-5])
    0
    >>> argmax([4,2,-5], key=abs)
    2
    >>> argmax([4,2,-5], key=abs, both=True)
    (2, 5)
    """
    if key is not None:
        it = imap(key, iterable)
    else:
        it = iter(iterable)
    score, argmax = reduce(max, izip(it, count()))
    if both:
        return argmax, score
    return argmax 
Example #5
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def compose(*funcs):
    """Compose `funcs` to a single function.

    >>> compose(operator.abs, operator.add)(-2,-3)
    5
    >>> compose()('nada')
    'nada'
    >>> compose(sorted, set, partial(filter, None))(range(3)[::-1]*2)
    [1, 2]
    """
    # slightly optimized for most common cases and hence verbose
    if len(funcs) == 2: f0,f1=funcs; return lambda *a,**kw: f0(f1(*a,**kw))
    elif len(funcs) == 3: f0,f1,f2=funcs; return lambda *a,**kw: f0(f1(f2(*a,**kw)))
    elif len(funcs) == 0: return lambda x:x     # XXX single kwarg
    elif len(funcs) == 1: return funcs[0]
    else:
        def composed(*args,**kwargs):
            y = funcs[-1](*args,**kwargs)
            for f in funcs[:0:-1]: y = f(y)
            return y
        return composed 
Example #6
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def argmax(iterable, key=None, both=False):
    """
    >>> argmax([4,2,-5])
    0
    >>> argmax([4,2,-5], key=abs)
    2
    >>> argmax([4,2,-5], key=abs, both=True)
    (2, 5)
    """
    if key is not None:
        it = imap(key, iterable)
    else:
        it = iter(iterable)
    score, argmax = reduce(max, izip(it, count()))
    if both:
        return argmax, score
    return argmax 
Example #7
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def compose(*funcs):
    """Compose `funcs` to a single function.

    >>> compose(operator.abs, operator.add)(-2,-3)
    5
    >>> compose()('nada')
    'nada'
    >>> compose(sorted, set, partial(filter, None))(range(3)[::-1]*2)
    [1, 2]
    """
    # slightly optimized for most common cases and hence verbose
    if len(funcs) == 2: f0,f1=funcs; return lambda *a,**kw: f0(f1(*a,**kw))
    elif len(funcs) == 3: f0,f1,f2=funcs; return lambda *a,**kw: f0(f1(f2(*a,**kw)))
    elif len(funcs) == 0: return lambda x:x     # XXX single kwarg
    elif len(funcs) == 1: return funcs[0]
    else:
        def composed(*args,**kwargs):
            y = funcs[-1](*args,**kwargs)
            for f in funcs[:0:-1]: y = f(y)
            return y
        return composed 
Example #8
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def argmax(iterable, key=None, both=False):
    """
    >>> argmax([4,2,-5])
    0
    >>> argmax([4,2,-5], key=abs)
    2
    >>> argmax([4,2,-5], key=abs, both=True)
    (2, 5)
    """
    if key is not None:
        it = imap(key, iterable)
    else:
        it = iter(iterable)
    score, argmax = reduce(max, izip(it, count()))
    if both:
        return argmax, score
    return argmax 
Example #9
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def argmax(iterable, key=None, both=False):
    """
    >>> argmax([4,2,-5])
    0
    >>> argmax([4,2,-5], key=abs)
    2
    >>> argmax([4,2,-5], key=abs, both=True)
    (2, 5)
    """
    if key is not None:
        it = imap(key, iterable)
    else:
        it = iter(iterable)
    score, argmax = reduce(max, izip(it, count()))
    if both:
        return argmax, score
    return argmax 
Example #10
Source File: parser.py    From algorithm with Do What The F*ck You Want To Public License 6 votes vote down vote up
def assignment(self):
        varLst = []
        while self.isAssignment():
            varLst .append(self.match().value)
            self.match(ASSIGN)
        self.sentenceValue()
        sym0 = self.getSymbol(varLst[0])
        lastLevel=abs(self.level-sym0.level)
        lastAddr = sym0.addr
        self.genIns('STO',lastLevel,sym0.addr)
        for var in varLst[1:]:
            sym = self.getSymbol(var)
            if sym.type=='CONST':
                raise Exception('[Error]: Const "{}" can\'t be reassigned'.format(sym.name))
            self.genIns('LOD',lastLevel,lastAddr)
            lastLevel = abs(self.level-sym.level)
            lastAddr = sym.addr
            self.genIns('STO',lastLevel,sym.addr) 
Example #11
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def argmax(iterable, key=None, both=False):
    """
    >>> argmax([4,2,-5])
    0
    >>> argmax([4,2,-5], key=abs)
    2
    >>> argmax([4,2,-5], key=abs, both=True)
    (2, 5)
    """
    if key is not None:
        it = imap(key, iterable)
    else:
        it = iter(iterable)
    score, argmax = reduce(max, izip(it, count()))
    if both:
        return argmax, score
    return argmax 
Example #12
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def compose(*funcs):
    """Compose `funcs` to a single function.

    >>> compose(operator.abs, operator.add)(-2,-3)
    5
    >>> compose()('nada')
    'nada'
    >>> compose(sorted, set, partial(filter, None))(range(3)[::-1]*2)
    [1, 2]
    """
    # slightly optimized for most common cases and hence verbose
    if len(funcs) == 2: f0,f1=funcs; return lambda *a,**kw: f0(f1(*a,**kw))
    elif len(funcs) == 3: f0,f1,f2=funcs; return lambda *a,**kw: f0(f1(f2(*a,**kw)))
    elif len(funcs) == 0: return lambda x:x     # XXX single kwarg
    elif len(funcs) == 1: return funcs[0]
    else:
        def composed(*args,**kwargs):
            y = funcs[-1](*args,**kwargs)
            for f in funcs[:0:-1]: y = f(y)
            return y
        return composed 
Example #13
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def argmax(iterable, key=None, both=False):
    """
    >>> argmax([4,2,-5])
    0
    >>> argmax([4,2,-5], key=abs)
    2
    >>> argmax([4,2,-5], key=abs, both=True)
    (2, 5)
    """
    if key is not None:
        it = imap(key, iterable)
    else:
        it = iter(iterable)
    score, argmax = reduce(max, izip(it, count()))
    if both:
        return argmax, score
    return argmax 
Example #14
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def argmax(iterable, key=None, both=False):
    """
    >>> argmax([4,2,-5])
    0
    >>> argmax([4,2,-5], key=abs)
    2
    >>> argmax([4,2,-5], key=abs, both=True)
    (2, 5)
    """
    if key is not None:
        it = imap(key, iterable)
    else:
        it = iter(iterable)
    score, argmax = reduce(max, izip(it, count()))
    if both:
        return argmax, score
    return argmax 
Example #15
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def compose(*funcs):
    """Compose `funcs` to a single function.

    >>> compose(operator.abs, operator.add)(-2,-3)
    5
    >>> compose()('nada')
    'nada'
    >>> compose(sorted, set, partial(filter, None))(range(3)[::-1]*2)
    [1, 2]
    """
    # slightly optimized for most common cases and hence verbose
    if len(funcs) == 2: f0,f1=funcs; return lambda *a,**kw: f0(f1(*a,**kw))
    elif len(funcs) == 3: f0,f1,f2=funcs; return lambda *a,**kw: f0(f1(f2(*a,**kw)))
    elif len(funcs) == 0: return lambda x:x     # XXX single kwarg
    elif len(funcs) == 1: return funcs[0]
    else:
        def composed(*args,**kwargs):
            y = funcs[-1](*args,**kwargs)
            for f in funcs[:0:-1]: y = f(y)
            return y
        return composed 
Example #16
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def argmax(iterable, key=None, both=False):
    """
    >>> argmax([4,2,-5])
    0
    >>> argmax([4,2,-5], key=abs)
    2
    >>> argmax([4,2,-5], key=abs, both=True)
    (2, 5)
    """
    if key is not None:
        it = imap(key, iterable)
    else:
        it = iter(iterable)
    score, argmax = reduce(max, izip(it, count()))
    if both:
        return argmax, score
    return argmax 
Example #17
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def compose(*funcs):
    """Compose `funcs` to a single function.

    >>> compose(operator.abs, operator.add)(-2,-3)
    5
    >>> compose()('nada')
    'nada'
    >>> compose(sorted, set, partial(filter, None))(range(3)[::-1]*2)
    [1, 2]
    """
    # slightly optimized for most common cases and hence verbose
    if len(funcs) == 2: f0,f1=funcs; return lambda *a,**kw: f0(f1(*a,**kw))
    elif len(funcs) == 3: f0,f1,f2=funcs; return lambda *a,**kw: f0(f1(f2(*a,**kw)))
    elif len(funcs) == 0: return lambda x:x     # XXX single kwarg
    elif len(funcs) == 1: return funcs[0]
    else:
        def composed(*args,**kwargs):
            y = funcs[-1](*args,**kwargs)
            for f in funcs[:0:-1]: y = f(y)
            return y
        return composed 
Example #18
Source File: awmstools.py    From DeepLearning_Wavelet-LSTM with MIT License 6 votes vote down vote up
def compose(*funcs):
    """Compose `funcs` to a single function.

    >>> compose(operator.abs, operator.add)(-2,-3)
    5
    >>> compose()('nada')
    'nada'
    >>> compose(sorted, set, partial(filter, None))(range(3)[::-1]*2)
    [1, 2]
    """
    # slightly optimized for most common cases and hence verbose
    if len(funcs) == 2: f0,f1=funcs; return lambda *a,**kw: f0(f1(*a,**kw))
    elif len(funcs) == 3: f0,f1,f2=funcs; return lambda *a,**kw: f0(f1(f2(*a,**kw)))
    elif len(funcs) == 0: return lambda x:x     # XXX single kwarg
    elif len(funcs) == 1: return funcs[0]
    else:
        def composed(*args,**kwargs):
            y = funcs[-1](*args,**kwargs)
            for f in funcs[:0:-1]: y = f(y)
            return y
        return composed 
Example #19
Source File: core_test.py    From keras-lambda with MIT License 5 votes vote down vote up
def setUp(self):
    super(CoreUnaryOpsTest, self).setUp()

    self.ops = [
        ('abs', operator.abs, math_ops.abs, core.abs_function),
        ('neg', operator.neg, math_ops.negative, core.neg),
        # TODO(shoyer): add unary + to core TensorFlow
        ('pos', None, None, None),
        ('sign', None, math_ops.sign, core.sign),
        ('reciprocal', None, math_ops.reciprocal, core.reciprocal),
        ('square', None, math_ops.square, core.square),
        ('round', None, math_ops.round, core.round_function),
        ('sqrt', None, math_ops.sqrt, core.sqrt),
        ('rsqrt', None, math_ops.rsqrt, core.rsqrt),
        ('log', None, math_ops.log, core.log),
        ('exp', None, math_ops.exp, core.exp),
        ('log', None, math_ops.log, core.log),
        ('ceil', None, math_ops.ceil, core.ceil),
        ('floor', None, math_ops.floor, core.floor),
        ('cos', None, math_ops.cos, core.cos),
        ('sin', None, math_ops.sin, core.sin),
        ('tan', None, math_ops.tan, core.tan),
        ('acos', None, math_ops.acos, core.acos),
        ('asin', None, math_ops.asin, core.asin),
        ('atan', None, math_ops.atan, core.atan),
        ('lgamma', None, math_ops.lgamma, core.lgamma),
        ('digamma', None, math_ops.digamma, core.digamma),
        ('erf', None, math_ops.erf, core.erf),
        ('erfc', None, math_ops.erfc, core.erfc),
        ('lgamma', None, math_ops.lgamma, core.lgamma),
    ]
    total_size = np.prod([v.size for v in self.original_lt.axes.values()])
    self.test_lt = core.LabeledTensor(
        math_ops.cast(self.original_lt, dtypes.float32) / total_size,
        self.original_lt.axes) 
Example #20
Source File: parser.py    From algorithm with Do What The F*ck You Want To Public License 5 votes vote down vote up
def funcall(self):
        name = self.match().value
        sym = self.getSymbol(name)
        saved = self.curClosure
        self.curClosure = sym.closure
        n2= self.real_arg_list()
        self.curClosure = saved
        if sym.argNum!=n2:
            self.errorArg(sym.argNum,n2)
        self.genIns('CAL',abs(self.level-sym.level),sym.value)
        self.genIns('INT',1,n2)
        self.genIns('PUSH',0,0) 
Example #21
Source File: compare.py    From OpenWARP with Apache License 2.0 5 votes vote down vote up
def compute(filename1, filename2):
    """
    Compute the MAE between real numbers in two files.
    It drops all non number. It use 0 for NaN
    """

    x1 = []
    x2 = []

    if os.path.exists(filename1) and os.path.isfile(filename1):
        f = open(filename1, 'r')
        for line in f:
                for word in line.split():
                    if isfloat(word):
                        x1.append(0 if isnan(float(word)) else float(word))
        f.close()

    if os.path.exists(filename2) and os.path.isfile(filename2):
        f = open(filename2, 'r')
        for line in f:
                for word in line.split():
                    if isfloat(word):
                        x2.append(0 if isnan(float(word)) else float(word))
        f.close()

    m = min(len(x1), len(x2))
    score = sum(map(abs, map(sub, x1[:m], x2[:m])))
    print(len(x1))
    print(len(x2))
    if m > 0:
        score = score/m

    print("MAE between {} and {} is {}".format(filename1, filename2, score))

    #np.savetxt(filename1+ '_correct', x1)
    #np.savetxt(filename2+ '_correct', x2)

    return score 
Example #22
Source File: test_Signal.py    From myhdl with GNU Lesser General Public License v2.1 5 votes vote down vote up
def testAbs(self):
        self.unaryCheck(operator.abs) 
Example #23
Source File: compare.py    From OpenWARP with Apache License 2.0 5 votes vote down vote up
def compute(filename1, filename2):
    """
    Compute the MAE between real numbers in two files.
    It drops all non number. It use 0 for NaN
    """

    x1 = []
    x2 = []

    if os.path.exists(filename1) and os.path.isfile(filename1):
        f = open(filename1, 'r')
        for line in f:
                for word in line.split():
                    if isfloat(word):
                        x1.append(0 if isnan(float(word)) else float(word))
        f.close()

    if os.path.exists(filename2) and os.path.isfile(filename2):
        f = open(filename2, 'r')
        for line in f:
                for word in line.split():
                    if isfloat(word):
                        x2.append(0 if isnan(float(word)) else float(word))
        f.close()

    m = min(len(x1), len(x2))
    score = sum(map(abs, map(sub, x1[:m], x2[:m])))
    print(len(x1))
    print(len(x2))
    if m > 0:
        score = score/m

    print("MAE between {} and {} is {}".format(filename1, filename2, score))

    #np.savetxt(filename1+ '_correct', x1)
    #np.savetxt(filename2+ '_correct', x2)

    return score 
Example #24
Source File: test_util.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def __abs__(self):
    return NonStandardInteger(operator.abs(self.val)) 
Example #25
Source File: nodes.py    From hyper-engine with Apache License 2.0 5 votes vote down vote up
def __init__(self, start=0.0, end=1.0):
    super(UniformNode, self).__init__()
    self._shift = min(start, end)
    self._scale = abs(end - start)
    self._describe = 'uniform(%f, %f)' % (start, end) 
Example #26
Source File: nodes.py    From hyper-engine with Apache License 2.0 5 votes vote down vote up
def __abs__(self):  return _op1(self, operator.abs) 
Example #27
Source File: test_operator.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_abs(self):
        self.failUnlessRaises(TypeError, operator.abs)
        self.failUnlessRaises(TypeError, operator.abs, None)
        self.failUnless(operator.abs(-1) == 1)
        self.failUnless(operator.abs(1) == 1) 
Example #28
Source File: test_operator.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_abs(self):
        self.failUnlessRaises(TypeError, operator.abs)
        self.failUnlessRaises(TypeError, operator.abs, None)
        self.failUnless(operator.abs(-1) == 1)
        self.failUnless(operator.abs(1) == 1) 
Example #29
Source File: executor.py    From manticore with GNU Affero General Public License v3.0 5 votes vote down vote up
def f32_abs(self, store, stack):
        return self.f32_unary(store, stack, operator.abs, F32) 
Example #30
Source File: test_util.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __abs__(self):
    return NonStandardInteger(operator.abs(self.val))