pulse2percept.utils.base

PrettyPrint, Frozen, Parametrized, Data, bijective26_name, cached, gamma, unique

Functions

bijective26_name(i)

Bijective base-26 numeration

cached(f)

Cached property decorator

freeze_class(set[, normalize])

Freezes a class

gamma(n, tau, tsample[, tol])

Returns the impulse response of n cascaded leaky integrators

has_own_attr(obj, name)

Return whether obj owns name without invoking its getter.

Classes

Data(data[, axes, metadata])

N-dimensional data container

Frozen()

"Frozen" classes (and subclasses) do not allow for new class attributes to be set outside the constructor.

Parametrized(**params)

Base class for objects with user-settable parameters.

PrettyPrint()

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

Exceptions

FreezeError

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.

class pulse2percept.utils.base.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 _pprint_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 _pprint_params(self):
...         return {'a': self.a, 'b': self.b}
>>> MyClass(1, 2)
MyClass(a=1, b=2)
exception pulse2percept.utils.base.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.

add_note(object, /)

Exception.add_note(note) – add a note to the exception

name

attribute name

obj

object

with_traceback(object, /)

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

pulse2percept.utils.base.has_own_attr(obj, name)[source]

Return whether obj owns name without invoking its getter.

Unlike hasattr, this avoids triggering properties such as deprecated aliases.

pulse2percept.utils.base.freeze_class(set, normalize=None)[source]

Freezes a class

Raise an error when trying to set an undeclared name from anywhere but the object’s own constructor.

Parameters:
  • set (callable) – The __setattr__ to delegate to once the assignment is allowed.

  • normalize (callable, optional) – normalize(self, name, value), called before the assignment when value carries a physical unit, and returning the value to store. See _normalize_param, which dispatches to the object’s own _normalize_param_value.

class pulse2percept.utils.base.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.

class pulse2percept.utils.base.Parametrized(**params)[source]

Base class for objects with user-settable parameters.

Parameters are declared by get_default_params(); subclasses may declare their storage units with get_param_units(). Frozen prevents adding new attributes after construction.

Added in version 0.10.0.

abstractmethod get_default_params()[source]

Return a dict of user-settable parameters

get_param_units()[source]

Return storage units for physical parameters.

Subclasses should extend this mapping. Parameters not listed here take plain dimensionless values.

Added in version 0.10.0.

set_params(**params)[source]

Set the parameters of this object

class pulse2percept.utils.base.Data(data, axes=None, metadata=None)[source]

N-dimensional data container

Added in version 0.6.

Parameters:
  • data (np.ndarray) – An N-dimensional NumPy array containing the data to store

  • axes (dict or tuple, optional) – For each dimension in data, specify axis name and labels.

  • metadata (dict, optional) – A dictionary that can store arbitrary metadata

pulse2percept.utils.base.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.base.cached(f)[source]

Cached property decorator

Decorator can be added to the property of a class to maintain a cache. This is useful when computing the property is computationall expensive. The property will only be computed on first call, and subsequent calls will refer to the cached result.

Important

When making use of a cached property, the class should also maintain a _cache_active flag set to True or False.

Added in version 0.7.

pulse2percept.utils.base.bijective26_name(i)[source]

Bijective base-26 numeration

Creates the “alphabetic number” for a given integer i following bijective base-26 numeration: A-Z, AA-AZ, BA-BZ, … ZA-ZZ, AAA-AAZ, ABA-ABZ, …

Parameters:

i (int) – Regular number to be translated into an alphabetic number

Returns:

name – Alphabetic number

Return type:

string

Examples

>>> bijective26_name(0)
'A'
>>> bijective26_name(26)
'AA'