Python numpy.geterr() 使用实例

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

def setUp(self):
        # Base data definition.
        x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
        y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
        a10 = 10.
        m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
        m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
        xm = masked_array(x, mask=m1)
        ym = masked_array(y, mask=m2)
        z = np.array([-.5, 0., .5, .8])
        zm = masked_array(z, mask=[0, 1, 0, 0])
        xf = np.where(m1, 1e+20, x)
        xm.set_fill_value(1e+20)
        self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 2

def setUp(self):
        # Base data definition.
        x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
        y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
        a10 = 10.
        m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
        m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
        xm = masked_array(x, mask=m1)
        ym = masked_array(y, mask=m2)
        z = np.array([-.5, 0., .5, .8])
        zm = masked_array(z, mask=[0, 1, 0, 0])
        xf = np.where(m1, 1e+20, x)
        xm.set_fill_value(1e+20)
        self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 3

def with_error_settings(**new_settings):
    """
    TODO.

    Arguments:
      **new_settings: TODO

    Returns:
    """
    @decorator.decorator
    def dec(f, *args, **kwargs):
        old_settings = np.geterr()

        np.seterr(**new_settings)
        ret = f(*args, **kwargs)

        np.seterr(**old_settings)

        return ret

    return dec 

Example 4

def setUp(self):
        # Base data definition.
        x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
        y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
        a10 = 10.
        m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
        m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
        xm = masked_array(x, mask=m1)
        ym = masked_array(y, mask=m2)
        z = np.array([-.5, 0., .5, .8])
        zm = masked_array(z, mask=[0, 1, 0, 0])
        xf = np.where(m1, 1e+20, x)
        xm.set_fill_value(1e+20)
        self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 5

def setUp(self):
        # Base data definition.
        x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
        y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
        a10 = 10.
        m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
        m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
        xm = masked_array(x, mask=m1)
        ym = masked_array(y, mask=m2)
        z = np.array([-.5, 0., .5, .8])
        zm = masked_array(z, mask=[0, 1, 0, 0])
        xf = np.where(m1, 1e+20, x)
        xm.set_fill_value(1e+20)
        self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 6

def setUp(self):
        # Base data definition.
        x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
        y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
        a10 = 10.
        m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
        m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
        xm = masked_array(x, mask=m1)
        ym = masked_array(y, mask=m2)
        z = np.array([-.5, 0., .5, .8])
        zm = masked_array(z, mask=[0, 1, 0, 0])
        xf = np.where(m1, 1e+20, x)
        xm.set_fill_value(1e+20)
        self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 7

def setUp(self):
        # Base data definition.
        x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
        y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
        a10 = 10.
        m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
        m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
        xm = masked_array(x, mask=m1)
        ym = masked_array(y, mask=m2)
        z = np.array([-.5, 0., .5, .8])
        zm = masked_array(z, mask=[0, 1, 0, 0])
        xf = np.where(m1, 1e+20, x)
        xm.set_fill_value(1e+20)
        self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 8

def transform(value, left_scale, right_scale, scale=0):
    if left_scale is None or right_scale is None:
        raise Exception('Left or Right scales cannot be None.')
    if scale not in [0, 1]:
        raise Exception('Scale must be 0 or 1.')
    invalid_err = np.geterr().get('invalid')
    invalid_err = np.geterr().get('invalid')
    np.seterr(invalid='ignore')    
    if scale == 0:
        range_ = np.absolute(right_scale - left_scale)
        translated_value = np.abs(value - left_scale)    
        ret_val =  (translated_value / range_)
    else:
        if left_scale <= 0.0:
            raise Exception()
        ls = np.log10(left_scale)    
        rs = np.log10(right_scale)
        range_ = rs - ls
        translated_value = np.log10(value) - ls
        ret_val =  (translated_value / range_)
    np.seterr(invalid=invalid_err)
    return ret_val 

Example 9

def setUp(self):
        # Base data definition.
        x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
        y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
        a10 = 10.
        m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
        m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
        xm = masked_array(x, mask=m1)
        ym = masked_array(y, mask=m2)
        z = np.array([-.5, 0., .5, .8])
        zm = masked_array(z, mask=[0, 1, 0, 0])
        xf = np.where(m1, 1e+20, x)
        xm.set_fill_value(1e+20)
        self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 10

def test_default(self):
        err = np.geterr()
        self.assertEqual(err, dict(
            divide='warn',
            invalid='warn',
            over='warn',
            under='ignore',
        )) 

Example 11

def test_set(self):
        with np.errstate():
            err = np.seterr()
            old = np.seterr(divide='print')
            self.assertTrue(err == old)
            new = np.seterr()
            self.assertTrue(new['divide'] == 'print')
            np.seterr(over='raise')
            self.assertTrue(np.geterr()['over'] == 'raise')
            self.assertTrue(new['divide'] == 'print')
            np.seterr(**old)
            self.assertTrue(np.geterr() == old) 

Example 12

def setUp(self):
        # Base data definition.
        self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6),
                  array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 13

def __enter__(self):
        self.err = numpy.geterr()
        numpy.seterr(**self.kw) 

Example 14

def test_default(self):
        err = np.geterr()
        self.assertEqual(err, dict(
            divide='warn',
            invalid='warn',
            over='warn',
            under='ignore',
        )) 

Example 15

def test_set(self):
        with np.errstate():
            err = np.seterr()
            old = np.seterr(divide='print')
            self.assertTrue(err == old)
            new = np.seterr()
            self.assertTrue(new['divide'] == 'print')
            np.seterr(over='raise')
            self.assertTrue(np.geterr()['over'] == 'raise')
            self.assertTrue(new['divide'] == 'print')
            np.seterr(**old)
            self.assertTrue(np.geterr() == old) 

Example 16

def setUp(self):
        # Base data definition.
        self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6),
                  array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 17

def index(self):
        if self._instantiated_index is None:
            if self._index_class is None:
                raise RuntimeError("You should not instantiate Dataset.")
            self._instantiated_index = self._index_class(
                self, dataset_type=self.dataset_type)
            # Now we do things that we need an instantiated index for
            # ...first off, we create our field_info now.
            oldsettings = np.geterr()
            np.seterr(all='ignore')
            self.create_field_info()
            np.seterr(**oldsettings)
        return self._instantiated_index 

Example 18

def __enter__(self):
        self.err = numpy.geterr()
        numpy.seterr(**self.kw) 

Example 19

def test_default(self):
        err = np.geterr()
        self.assertEqual(err, dict(
            divide='warn',
            invalid='warn',
            over='warn',
            under='ignore',
        )) 

Example 20

def test_set(self):
        with np.errstate():
            err = np.seterr()
            old = np.seterr(divide='print')
            self.assertTrue(err == old)
            new = np.seterr()
            self.assertTrue(new['divide'] == 'print')
            np.seterr(over='raise')
            self.assertTrue(np.geterr()['over'] == 'raise')
            self.assertTrue(new['divide'] == 'print')
            np.seterr(**old)
            self.assertTrue(np.geterr() == old) 

Example 21

def setUp(self):
        # Base data definition.
        self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6),
                  array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 22

def test_default(self):
        err = np.geterr()
        self.assertEqual(err, dict(
            divide='warn',
            invalid='warn',
            over='warn',
            under='ignore',
        )) 

Example 23

def test_set(self):
        with np.errstate():
            err = np.seterr()
            old = np.seterr(divide='print')
            self.assertTrue(err == old)
            new = np.seterr()
            self.assertTrue(new['divide'] == 'print')
            np.seterr(over='raise')
            self.assertTrue(np.geterr()['over'] == 'raise')
            self.assertTrue(new['divide'] == 'print')
            np.seterr(**old)
            self.assertTrue(np.geterr() == old) 

Example 24

def setUp(self):
        # Base data definition.
        self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6),
                  array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 25

def test_default(self):
        err = np.geterr()
        self.assertEqual(err, dict(
            divide='warn',
            invalid='warn',
            over='warn',
            under='ignore',
        )) 

Example 26

def test_set(self):
        with np.errstate():
            err = np.seterr()
            old = np.seterr(divide='print')
            self.assertTrue(err == old)
            new = np.seterr()
            self.assertTrue(new['divide'] == 'print')
            np.seterr(over='raise')
            self.assertTrue(np.geterr()['over'] == 'raise')
            self.assertTrue(new['divide'] == 'print')
            np.seterr(**old)
            self.assertTrue(np.geterr() == old) 

Example 27

def setUp(self):
        # Base data definition.
        self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6),
                  array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 28

def test_default(self):
        err = np.geterr()
        self.assertEqual(err, dict(
            divide='warn',
            invalid='warn',
            over='warn',
            under='ignore',
        )) 

Example 29

def test_set(self):
        with np.errstate():
            err = np.seterr()
            old = np.seterr(divide='print')
            self.assertTrue(err == old)
            new = np.seterr()
            self.assertTrue(new['divide'] == 'print')
            np.seterr(over='raise')
            self.assertTrue(np.geterr()['over'] == 'raise')
            self.assertTrue(new['divide'] == 'print')
            np.seterr(**old)
            self.assertTrue(np.geterr() == old) 

Example 30

def _sample(self):
        super()._sample()

        self._fft_values[:] = numpy.abs(numpy.fft.rfft(self._window_function * self.buffer))

        if self._output == 'fft':
            pass
        elif self._output == 'psd':
            # Reminder for future-self:
            # Our Input signal is clamped between -1.0 and +1.0 yet if we convert the values above ^ into dB
            # we get values way beyond 0dB which makes no sense. Turns out we have to normalize the resulting
            # vector.
            # Thank you my hero: https://dsp.stackexchange.com/a/32080
            # TODO: This can be pre-calculated!
            self._fft_values[:] = numpy.power(self._fft_values * 2.0, 2) \
                / numpy.power(self._window_function_sum * self._reference_value, 2)
        else:
            raise PulsevizException('This should not happen.')

        if self._scaling == 'lin':
            pass
        elif self._scaling == 'log':
            numpy.seterr(divide='ignore')
            self._fft_values[:] = 10.0 * numpy.log10(self._fft_values)
            numpy.seterr(all='raise')  # TODO: Use result of numpy.geterr instead?
        else:
            raise PulsevizException('This should not happen.')

        if self._scaling == 'log':
            self._fft_values[:] += self._fft_weights
        else:
            self._fft_values[:] *= numpy.power(10, self._fft_weights / 20)  # TODO: Test this. 

Example 31

def inverse_transform(value, left_scale, right_scale, scale=0):
    if left_scale is None or right_scale is None:
        raise Exception('Left or Right scales cannot be None.')
    if scale not in [0, 1]:
        raise Exception('Scale must be 0 or 1.')
    invalid_err = np.geterr().get('invalid')
    np.seterr(invalid='ignore')    
    if scale == 0:
        range_ = np.absolute(right_scale - left_scale)
        translated_value = value * range_
        if (left_scale > right_scale):
            ret_val = left_scale - translated_value
        else:    
            ret_val = left_scale + translated_value
    else:
        ls = np.log10(left_scale)    
        rs = np.log10(right_scale)
        range_ = rs - ls
        translated_value = value * range_
        translated_value = np.round(translated_value, 3)
        translated_value = translated_value + ls
        ret_val = np.power(10, translated_value)
    np.seterr(invalid=invalid_err)
    return ret_val  
 
    

###############################################################################
############################################################################### 

Example 32

def test_default(self):
        err = np.geterr()
        self.assertEqual(err, dict(
            divide='warn',
            invalid='warn',
            over='warn',
            under='ignore',
        )) 

Example 33

def test_set(self):
        with np.errstate():
            err = np.seterr()
            old = np.seterr(divide='print')
            self.assertTrue(err == old)
            new = np.seterr()
            self.assertTrue(new['divide'] == 'print')
            np.seterr(over='raise')
            self.assertTrue(np.geterr()['over'] == 'raise')
            self.assertTrue(new['divide'] == 'print')
            np.seterr(**old)
            self.assertTrue(np.geterr() == old) 

Example 34

def setUp(self):
        # Base data definition.
        self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6),
                  array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),)
        self.err_status = np.geterr()
        np.seterr(divide='ignore', invalid='ignore') 

Example 35

def geterr():
    """
    Get the current way of handling floating-point errors.

    Returns
    -------
    res : dict
        A dictionary with keys "divide", "over", "under", and "invalid",
        whose values are from the strings "ignore", "print", "log", "warn",
        "raise", and "call". The keys represent possible floating-point
        exceptions, and the values define how these exceptions are handled.

    See Also
    --------
    geterrcall, seterr, seterrcall

    Notes
    -----
    For complete documentation of the types of floating-point exceptions and
    treatment options, see `seterr`.

    Examples
    --------
    >>> np.geterr()
    {'over': 'warn', 'divide': 'warn', 'invalid': 'warn',
    'under': 'ignore'}
    >>> np.arange(3.) / np.arange(3.)
    array([ NaN,   1.,   1.])

    >>> oldsettings = np.seterr(all='warn', over='raise')
    >>> np.geterr()
    {'over': 'raise', 'divide': 'warn', 'invalid': 'warn', 'under': 'warn'}
    >>> np.arange(3.) / np.arange(3.)
    __main__:1: RuntimeWarning: invalid value encountered in divide
    array([ NaN,   1.,   1.])

    """
    maskvalue = umath.geterrobj()[1]
    mask = 7
    res = {}
    val = (maskvalue >> SHIFT_DIVIDEBYZERO) & mask
    res['divide'] = _errdict_rev[val]
    val = (maskvalue >> SHIFT_OVERFLOW) & mask
    res['over'] = _errdict_rev[val]
    val = (maskvalue >> SHIFT_UNDERFLOW) & mask
    res['under'] = _errdict_rev[val]
    val = (maskvalue >> SHIFT_INVALID) & mask
    res['invalid'] = _errdict_rev[val]
    return res 

Example 36

def geterrcall():
    """
    Return the current callback function used on floating-point errors.

    When the error handling for a floating-point error (one of "divide",
    "over", "under", or "invalid") is set to 'call' or 'log', the function
    that is called or the log instance that is written to is returned by
    `geterrcall`. This function or log instance has been set with
    `seterrcall`.

    Returns
    -------
    errobj : callable, log instance or None
        The current error handler. If no handler was set through `seterrcall`,
        ``None`` is returned.

    See Also
    --------
    seterrcall, seterr, geterr

    Notes
    -----
    For complete documentation of the types of floating-point exceptions and
    treatment options, see `seterr`.

    Examples
    --------
    >>> np.geterrcall()  # we did not yet set a handler, returns None

    >>> oldsettings = np.seterr(all='call')
    >>> def err_handler(type, flag):
    ...     print("Floating point error (%s), with flag %s" % (type, flag))
    >>> oldhandler = np.seterrcall(err_handler)
    >>> np.array([1, 2, 3]) / 0.0
    Floating point error (divide by zero), with flag 1
    array([ Inf,  Inf,  Inf])

    >>> cur_handler = np.geterrcall()
    >>> cur_handler is err_handler
    True

    """
    return umath.geterrobj()[2] 
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注