_lib._numpy_compat

Functions copypasted from newer versions of numpy.

Module Contents

Classes

Functions

_assert_warns(warning_class, func, *args, **kw)

r Fail unless the given callable throws the specified warning.

This definition is copypasted from numpy 1.9.0.dev. The version in earlier numpy returns None.

warning_class : class
The class defining the warning that func is expected to throw.
func : callable
The callable to test.
*args : Arguments
Arguments passed to func.
**kwargs : Kwargs
Keyword arguments passed to func.

The value returned by func.

_maybe_view_as_subclass(original_array, new_array)
_broadcast_to(array, shape, subok, readonly)
broadcast_to(array, shape, subok=False)
unique(ar, return_index=False, return_inverse=False, return_counts=False)

Find the unique elements of an array.

Returns the sorted unique elements of an array. There are three optional outputs in addition to the unique elements: the indices of the input array that give the unique values, the indices of the unique array that reconstruct the input array, and the number of times each unique value comes up in the input array.

ar : array_like
Input array. This will be flattened if it is not already 1-D.
return_index : bool, optional
If True, also return the indices of ar that result in the unique array.
return_inverse : bool, optional
If True, also return the indices of the unique array that can be used to reconstruct ar.
return_counts : bool, optional

If True, also return the number of times each unique value comes up in ar.

New in version 1.9.0.

unique : ndarray
The sorted unique values.
unique_indices : ndarray, optional
The indices of the first occurrences of the unique values in the (flattened) original array. Only provided if return_index is True.
unique_inverse : ndarray, optional
The indices to reconstruct the (flattened) original array from the unique array. Only provided if return_inverse is True.
unique_counts : ndarray, optional

The number of times each of the unique values comes up in the original array. Only provided if return_counts is True.

New in version 1.9.0.

Taken over from numpy 1.12.0-dev (c8408bf9c). Omitted examples, see numpy documentation for those.

polyvalfromroots(x, r, tensor=True)

r Evaluate a polynomial specified by its roots at points x.

This function is copypasted from numpy 1.12.0.dev.

If r is of length N, this function returns the value

The parameter x is converted to an array only if it is a tuple or a list, otherwise it is treated as a scalar. In either case, either x or its elements must support multiplication and addition both with themselves and with the elements of r.

If r is a 1-D array, then p(x) will have the same shape as x. If r is multidimensional, then the shape of the result depends on the value of tensor. If tensor is ``True` the shape will be r.shape[1:] + x.shape; that is, each polynomial is evaluated at every value of x. If tensor is False, the shape will be r.shape[1:]; that is, each polynomial is evaluated only for the corresponding broadcast value of x. Note that scalars have shape (,).

x : array_like, compatible object
If x is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, x or its elements must support addition and multiplication with with themselves and with the elements of r.
r : array_like
Array of roots. If r is multidimensional the first index is the root index, while the remaining indices enumerate multiple polynomials. For instance, in the two dimensional case the roots of each polynomial may be thought of as stored in the columns of r.
tensor : boolean, optional
If True, the shape of the roots array is extended with ones on the right, one for each dimension of x. Scalars have dimension 0 for this action. The result is that every column of coefficients in r is evaluated for every element of x. If False, x is broadcast over the columns of r for the evaluation. This keyword is useful when r is multidimensional. The default value is True.
values : ndarray, compatible object
The shape of the returned array is described above.

polyroots, polyfromroots, polyval

>>> from numpy.polynomial.polynomial import polyvalfromroots
>>> polyvalfromroots(1, [1,2,3])
0.0
>>> a = np.arange(4).reshape(2,2)
>>> a
array([[0, 1],
       [2, 3]])
>>> polyvalfromroots(a, [-1, 0, 1])
array([[ -0.,   0.],
       [  6.,  24.]])
>>> r = np.arange(-2, 2).reshape(2,2) # multidimensional coefficients
>>> r # each column of r defines one polynomial
array([[-2, -1],
       [ 0,  1]])
>>> b = [-2, 1]
>>> polyvalfromroots(b, r, tensor=True)
array([[-0.,  3.],
       [ 3., 0.]])
>>> polyvalfromroots(b, r, tensor=False)
array([-0.,  0.])
class suppress_warnings(forwarding_rule="always")

Context manager and decorator doing much the same as warnings.catch_warnings.

However, it also provides a filter mechanism to work around http://bugs.python.org/issue4180.

This bug causes Python before 3.4 to not reliably show warnings again after they have been ignored once (even within catch_warnings). It means that no “ignore” filter can be used easily, since following tests might need to see the warning. Additionally it allows easier specificity for testing warnings and can be nested.

forwarding_rule : str, optional
One of “always”, “once”, “module”, or “location”. Analogous to the usual warnings module filter mode, it is useful to reduce noise mostly on the outmost level. Unsuppressed and unrecorded warnings will be forwarded based on this rule. Defaults to “always”. “location” is equivalent to the warnings “default”, match by exact location the warning warning originated from.

Filters added inside the context manager will be discarded again when leaving it. Upon entering all filters defined outside a context will be applied automatically.

When a recording filter is added, matching warnings are stored in the log attribute as well as in the list returned by record.

If filters are added and the module keyword is given, the warning registry of this module will additionally be cleared when applying it, entering the context, or exiting it. This could cause warnings to appear a second time after leaving the context if they were configured to be printed once (default) and were already printed before the context was entered.

Nesting this context manager will work as expected when the forwarding rule is “always” (default). Unfiltered and unrecorded warnings will be passed out and be matched by the outer level. On the outmost level they will be printed (or caught by another warnings context). The forwarding rule argument can modify this behaviour.

Like catch_warnings this context manager is not threadsafe.

>>> with suppress_warnings() as sup:
...     sup.filter(DeprecationWarning, "Some text")
...     sup.filter(module=np.ma.core)
...     log = sup.record(FutureWarning, "Does this occur?")
...     command_giving_warnings()
...     # The FutureWarning was given once, the filtered warnings were
...     # ignored. All other warnings abide outside settings (may be
...     # printed/error)
...     assert_(len(log) == 1)
...     assert_(len(sup.log) == 1)  # also stored in log attribute

Or as a decorator:

>>> sup = suppress_warnings()
>>> sup.filter(module=np.ma.core)  # module must match exact
>>> @sup
>>> def some_function():
...     # do something which causes a warning in np.ma.core
...     pass
__init__(forwarding_rule="always")
_clear_registries()
_filter(category=Warning, message="", module=None, record=False)
filter(category=Warning, message="", module=None)

Add a new suppressing filter or apply it if the state is entered.

category : class, optional
Warning class to filter
message : string, optional
Regular expression matching the warning message.
module : module, optional
Module to filter for. Note that the module (and its file) must match exactly and cannot be a submodule. This may make it unreliable for external modules.

When added within a context, filters are only added inside the context and will be forgotten when the context is exited.

record(category=Warning, message="", module=None)

Append a new recording filter or apply it if the state is entered.

All warnings matching will be appended to the log attribute.

category : class, optional
Warning class to filter
message : string, optional
Regular expression matching the warning message.
module : module, optional
Module to filter for. Note that the module (and its file) must match exactly and cannot be a submodule. This may make it unreliable for external modules.
log : list
A list which will be filled with all matched warnings.

When added within a context, filters are only added inside the context and will be forgotten when the context is exited.

__enter__()
__exit__(*exc_info)
_showwarning(message, category, filename, lineno, *args, **kwargs)
__call__(func)

Function decorator to apply certain suppressions to a whole function.