Utils
pynapple.core.utils
Utility functions
convert_to_numpy_array
Convert any array like object to numpy ndarray.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
ArrayLike
|
|
required |
array_name |
str
|
Array name if RuntimeError is raised |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Numpy array object |
Raises:
Type | Description |
---|---|
RuntimeError
|
If input can't be converted to numpy array |
Source code in pynapple/core/utils.py
get_backend
is_array_like
Check if an object is array-like.
This function determines if an object has array-like properties.
An object is considered array-like if it has attributes typically associated with arrays
(such as .shape
, .dtype
, and .ndim
), supports indexing, and is iterable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
object
|
The object to check for array-like properties. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the object is array-like, False otherwise. |
Notes
This function uses a combination of checks for attributes (shape
, dtype
, ndim
),
indexability, and iterability to determine if the given object behaves like an array.
It is designed to be flexible and work with various types of array-like objects, including
but not limited to NumPy arrays and JAX arrays. However, it may not be full proof for all
possible array-like types or objects that mimic these properties without being suitable for
numerical operations.
Source code in pynapple/core/utils.py
cast_to_numpy
Convert an input array-like object to a NumPy array.
This function attempts to convert an input object to a NumPy array using np.asarray
.
If the input is not already a NumPy ndarray, it issues a warning indicating that a conversion
has taken place and shows the original type of the input. This function is useful for
ensuring compatibility with Numba operations in cases where the input might come from
various array-like sources (for instance, jax.numpy.Array).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
array_like
|
The input object to convert. This can be any object that |
required |
array_name |
str
|
The name of the variable that we are converting, printed in the warning message. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
A NumPy ndarray representation of the input |
Warnings
A warning is issued if the input values
is not already a NumPy ndarray, indicating
that a conversion has taken place and showing the original type of the input.
Source code in pynapple/core/utils.py
check_filename
Check if the filename is valid and return the path
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str or Path
|
The filename |
required |
Returns:
Type | Description |
---|---|
Path
|
The path to the file |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the filename is a directory or the parent does not exist |