pulse2percept.utils

This module provides a number of utility functions.

base Utility functions for pulse2percept
convolution convolution
deprecation deprecation
parallel
class pulse2percept.utils.deprecated(alt_func=None, deprecated_version=None, removed_version=None)[source]

Decorator to mark deprecated functions and classes with a warning.

Parameters:
  • alt_func (str) – If given, tell user what function to use instead.
  • deprecated_version (float or str) – The package version in which the function/class was first marked as deprecated.
  • removed_version (float or str) – The package version in which the deprecated function/class will be removed.
pulse2percept.utils.find_files_like(datapath, pattern)[source]

Finds files in a folder whose name matches a pattern

This function looks for files in folder datapath that match a regular expression pattern.

Parameters:
  • datapath (str) – Path to search
  • pattern (str) – A valid regular expression pattern

Examples

# Find all ‘.npz’ files in parent dir >>> files = find_files_like(‘..’, r’.*.npz$’)

exception pulse2percept.utils.FreezeError[source]

Exception class used to raise when trying to add attributes to Frozen

Classes of type Frozen do not allow for new attributes to be set outside the constructor.

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class pulse2percept.utils.Frozen[source]

“Frozen” classes (and subclasses) do not allow for new class attributes to be set outside the constructor. On attempting to add a new attribute, the class will raise a FreezeError.

pulse2percept.utils.gamma(n, tau, tsample, tol=0.01)[source]

Returns the impulse response of n cascaded leaky integrators

This function calculates the impulse response of n cascaded leaky integrators with constant of proportionality 1/tau: y = (t/theta).^(n-1).*exp(-t/theta)/(theta*factorial(n-1))

Parameters:
  • n (int) – Number of cascaded leaky integrators
  • tau (float) – Decay constant of leaky integration (seconds). Equivalent to the inverse of the constant of proportionality.
  • tsample (float) – Sampling time step (seconds).
  • tol (float) – Cut the kernel to size by ignoring function values smaller than a fraction tol of the peak value.
pulse2percept.utils.parfor(func, in_list, out_shape=None, n_jobs=-1, engine='joblib', scheduler='threading', func_args=[], func_kwargs={})[source]

Parallel for loop for NumPy arrays

Parameters:
  • func (callable) – The function to apply to each item in the array. Must have the form: func(arr, idx, *args, *kwargs) where arr is an ndarray and idx is an index into that array (a tuple). The Return of func needs to be one item (e.g. float, int) per input item.
  • in_list (list) – All legitimate inputs to the function to operate over.
  • out_shape (int or tuple of ints, optional) – If set, output will be reshaped accordingly. The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
  • n_jobs (integer, optional, default: 1) – The number of jobs to perform in parallel. -1 to use all cpus
  • engine (str, optional, default: 'joblib') – {‘dask’, ‘joblib’, ‘serial’} The last one is useful for debugging – runs the code without any parallelization.
  • scheduler (str, optional, default: 'threading') – Which scheduler to use (irrelevant for ‘serial’ engine): - ‘threading’: a scheduler backed by a thread pool - ‘multiprocessing’: a scheduler backed by a process pool
  • *func_args (list, optional) – Positional arguments to func
  • **func_kwargs (dict, optional) – Keyword arguments to func
Returns:

ndarray – NumPy array of identical shape to arr

Note

Equivalent to pyAFQ version (blob e20eaa0 from June 3, 2016): https://github.com/arokem/pyAFQ/blob/master/AFQ/utils/parallel.py

class pulse2percept.utils.PrettyPrint[source]

An abstract class that provides a way to prettyprint all class attributes, inspired by scikit-learn.

Classes deriving from PrettyPrint are required to implement a get_params method that returns a dictionary containing all the attributes to prettyprint.

Examples

>>> from pulse2percept.utils import PrettyPrint
>>> class MyClass(PrettyPrint):
...     def __init__(self, a, b):
...         self.a = a
...         self.b = b
...
...     def get_params(self):
...         return {'a': self.a, 'b': self.b}
>>> MyClass(1, 2)
MyClass(a=1, b=2)
get_params()[source]

Return a dictionary of class attributes