pulse2percept.utils.base
PrettyPrint,
Frozen,
Parametrized,
Data,
bijective26_name,
cached,
gamma,
unique
Functions
Bijective base-26 numeration |
|
|
Cached property decorator |
|
Freezes a class Raise an error when trying to set an undeclared name, or when calling from a method other than |
|
Returns the impulse response of |
|
Whether |
Classes
|
N-dimensional data container |
|
"Frozen" classes (and subclasses) do not allow for new class attributes to be set outside the constructor. |
|
Abstract base class for objects with user-settable parameters |
An abstract class that provides a way to prettyprint all class attributes, inspired by scikit-learn. |
Exceptions
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_paramsmethod 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()
Exception.add_note(note) – add a note to the exception
- name
attribute name
- obj
object
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- pulse2percept.utils.base.has_own_attr(obj, name)[source]
Whether
objhas an attributename, without running its getterhasattrinvokes a property or other descriptor just to answer the question, which for adeprecated_aliasmeans a spurious deprecation warning every time an attribute is merely probed. Looking the name up on the type instead settles the same question without ever reading the instance’s value.Note that this does not consult
__getattr__, so it answers “does the object own this attribute”, not the broader “can this attribute be read”.Added in version 0.10.0.
- pulse2percept.utils.base.freeze_class(set, normalize=None)[source]
Freezes a class Raise an error when trying to set an undeclared name, or when calling from a method other than
Frozen.__init__or the__init__method of a class derived from Frozen- Parameters:
set (callable) – The
__setattr__to delegate to once the assignment is allowed.normalize (callable, optional) –
normalize(self, name, value), called before the assignment whenvaluecarries a physical unit, and returning the value to store. See_normalize_param.This is a parameter rather than a wrapper around
set_attron purpose: both this function andis_built()locate their caller by stack depth, and inserting another__setattr__frame between the caller and this one would move it.
- 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]
Abstract base class for objects with user-settable parameters
Provides the following functionality:
Pretty-print class attributes (via
_pprint_paramsandPrettyPrint)User-settable parameters must be listed in
get_default_paramsNew class attributes can only be added in the constructor (enforced via
FrozenandFreezeError)Value-based equality and deep copying that understand NumPy arrays
Parameters can declare the physical unit they are stored in (via
get_param_units), so that a unitful value assigned to one is converted before it is stored
Added in version 0.10.0.
- get_param_units()[source]
Return a dict of the units that parameters are stored in
Maps a parameter name to the
Unitthat the implementation assumes it is expressed in. AQuantityassigned to such a parameter is checked against that unit and rescaled to it, so thatFadingTemporal(tau=100) FadingTemporal(tau=100 * ms) FadingTemporal(tau=0.1 * s)
all store the same float. Bare numbers keep their documented meaning and are passed through untouched.
Parameters absent from this dict take plain numbers: they are either dimensionless (
thresh_percept) or empirical fit parameters whose dimension the implementation does not actually commit to. Declaring a unit is a statement about what the equations assume, so a parameter should only appear here when that is documented or unambiguous.This dict is not restricted to the names in
get_default_params: it describes every physical attribute this object normalizes. A constructor argument assigned straight toself–DefaultSizeModeltakesrhothat way – belongs here too, and is converted like any other.Subclasses extend rather than replace it:
def get_param_units(self): return {**super().get_param_units(), 'dt': ms, 'tau': ms}
Added in version 0.10.0.
- class pulse2percept.utils.base.Data(data, axes=None, metadata=None)[source]
N-dimensional data container
Added in version 0.6.
- pulse2percept.utils.base.gamma(n, tau, tsample, tol=0.01)[source]
Returns the impulse response of
ncascaded leaky integratorsThis function calculates the impulse response of
ncascaded 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
tolof 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_activeflag 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'