sciunit.models package

Submodules

sciunit.models.backends module

Base class for simulator backends for SciUnit models.

class sciunit.models.backends.Backend[source]

Bases: sciunit.base.SciUnit

Base class for simulator backends.

Should only be used with model classes derived from RunnableModel. Supports caching of simulation results. Backend classes should implement simulator-specific details of modifying, running, and reading results from the simulation.

__module__ = 'sciunit.models.backends'
_backend_run() → Any[source]

Run the model via the backend.

backend_run() → Any[source]

Check for cached results; then run the model if needed.

Returns:
Any: The result of running backend.
cache_to_results(cache: Any) → Any[source]

A method to convert cache to some hypothetical Results object.

Args:
cache (Any): An object returned from .get_memory_cache() or .get_disk_cache().
Returns:
Any (optional): Either an object with the results of the simulation, or None (e.g. if cache_to_results() simply injects the results into some global object).
clear_disk_cache() → None[source]

Removes the cache file from the disk if it exists.

f = None

The function that handles running the simulation

get_disk_cache(key: str = None) → Any[source]

Return result in disk cache for key ‘key’ or None if not found.

Args:
key (str, optional): keys that will be used to find cached data. Defaults to None.
Returns:
Any: The disk cache for key ‘key’ or None if not found.
get_memory_cache(key: str = None) → dict[source]

Return result in memory cache for key ‘key’ or None if not found.

Args:
key (str, optional): [description]. Defaults to None.
Returns:
dict: The memory cache for key ‘key’ or None if not found.
init_backend(*args, **kwargs) → None[source]

Initialize the backend.

init_cache() → None[source]

Initialize the cache.

init_disk_cache(location: Union[str, pathlib.Path, bool, None] = None) → None[source]

Initialize the on-disk version of the cache.

init_memory_cache() → None[source]

Initialize the in-memory version of the cache.

load_model() → None[source]

Load the model into memory.

name = None

Name of the backend

recorded_variables = None

Optional list of state variables for a backend to record.

results_to_cache(results: Any) → Any[source]

A method to convert the results from your model run into storable cache object (usually a simple dictionary or an array).

Args:
results (Any): An object returned from your ._backend_run().
Returns:
Any: The results in the format that’s good for storing in cache.
save_results(path: Union[str, pathlib.Path] = '.') → None[source]

Save results on disk.

Args:
path (Union[str, Path], optional): [description]. Defaults to ‘.’.
set_attrs(**attrs) → None[source]

Set model attributes on the backend.

set_disk_cache(results: Any, key: str = None) → None[source]

Store result in disk cache with key matching model state.

Args:
results (Any): [description] key (str, optional): [description]. Defaults to None.
set_memory_cache(results: Any, key: str = None) → None[source]

Store result in memory cache with key matching model state.

Args:
results (Any): [description] key (str, optional): [description]. Defaults to None.
set_run_params(**run_params) → None[source]

Set model attributes on the backend.

state_hide = ['memory_cache', '_results', 'stdout', 'exec_in_dir', 'model']
exception sciunit.models.backends.BackendException[source]

Bases: Exception

Generic backend exception class.

__module__ = 'sciunit.models.backends'
__weakref__

list of weak references to the object (if defined)

sciunit.models.backends.register_backends(vars: dict) → None[source]

Register backends for use with models.

Args:
vars (dict): a dictionary of variables obtained from e.g. locals(),
at least some of which are Backend classes, e.g. from imports.

sciunit.models.base module

Base class for SciUnit models.

class sciunit.models.base.Model(name=None, **params)[source]

Bases: sciunit.base.SciUnit

Abstract base class for sciunit models.

__getattr__(attr)[source]
__init__(name=None, **params)[source]

Instantiate model.

__module__ = 'sciunit.models.base'
__repr__()[source]

Returns a representation of the model.

__str__()[source]

Return the model name.

_backend = None

Optional model backend for executing some methods, e.g. simulations.

capabilities
check_params() → None[source]

Check model parameters to see if they are reasonable.

For example, this method could check self.params to see if a particular value was within an acceptable range. This should be implemented as needed by specific model classes.

curr_method(back: int = 0) → str[source]

Return the name of the current method (calling this one).

Args:
back (int, optional): [description]. Defaults to 0.
Returns:
str: The name of the current method that calls this one.
describe() → str[source]

Describe the model.

Returns:
str: The description of the model.
description = ''

A description of the model.

extra_capability_checks = None

Optional extra checks of capabilities on a per-instance basis.

failed_extra_capabilities

Check to see if instance passes its extra_capability_checks.

classmethod get_capabilities() → list[source]

List the model’s capabilities.

is_match(match: Union[str, Model]) → bool[source]

Return whether this model is the same as match.

Matches if the model is the same as or has the same name as match.

Args:
match (Union[str, ‘Model’]): [description]
Returns:
bool: Whether this model is the same as match.
name = None

The name of the model. Defaults to the class name.

params = None

The parameters to the model (a dictionary). These distinguish one model of a class from another.

run_args = None

These are the run-time arguments for the model. Execution of run() should make use of these arguments.

state_hide = ['results', 'temp_dir', '_temp_dir', 'stdout']

sciunit.models.examples module

Example SciUnit model classes.

class sciunit.models.examples.CacheByInstancePersistentUniformModel(a, b, name=None)[source]

Bases: sciunit.models.examples.PersistentUniformModel

TODO

__module__ = 'sciunit.models.examples'
produce_number(**kwargs)
class sciunit.models.examples.CacheByValuePersistentUniformModel(a, b, name=None)[source]

Bases: sciunit.models.examples.PersistentUniformModel

TODO

__module__ = 'sciunit.models.examples'
produce_number(**kwargs)
class sciunit.models.examples.ConstModel(constant: Union[int, float], name: str = None)[source]

Bases: sciunit.models.base.Model, sciunit.capabilities.ProducesNumber

A model that always produces a constant number as output.

__init__(constant: Union[int, float], name: str = None)[source]

Instantiate model.

__module__ = 'sciunit.models.examples'
produce_number() → Union[int, float][source]

Produce a number.

class sciunit.models.examples.PersistentUniformModel(a, b, name=None)[source]

Bases: sciunit.models.examples.UniformModel

TODO

__module__ = 'sciunit.models.examples'
produce_number() → float[source]

Produece a number between a and b.

Returns:
float: The number between a and b.
run() → None[source]
class sciunit.models.examples.RepeatedRandomNumberModel(name=None, **params)[source]

Bases: sciunit.models.base.Model, sciunit.capabilities.ProducesNumber

An example model to demonstrate ProducesNumber with cypy.lazy.

__module__ = 'sciunit.models.examples'
produce_number()[source]

Each call to this method will produce the same random number as was returned in the first call, ensuring reproducibility and eliminating computational overhead.

Returns:
float: A random number produced.
class sciunit.models.examples.SharedModel(name=None, **params)[source]

Bases: sciunit.models.base.Model

A model that, each time it is instantiated with the same parameters, will return the same instance at the same locaiton in memory. Attributes should not be set post-instantiation unless the goal is to set those attributes on all models of this class.

__module__ = 'sciunit.models.examples'
static __new__(cls, *args, **kwargs)

Override used by sciunit.utils.intern to cache instances of this class.

static __static_new__(cls, *args, **kwargs)

Override used by sciunit.utils.intern to cache instances of this class.

_intern__pool = {}
class sciunit.models.examples.UniformModel(a, b, name=None)[source]

Bases: sciunit.models.base.Model, sciunit.capabilities.ProducesNumber

A model that always produces a random uniformly distributed number. in [a,b] as output.

__init__(a, b, name=None)[source]

Instantiate model.

__module__ = 'sciunit.models.examples'
produce_number() → float[source]

Produece a number between a and b.

Returns:
float: The number between a and b.
class sciunit.models.examples.UniqueRandomNumberModel(name=None, **params)[source]

Bases: sciunit.models.base.Model, sciunit.capabilities.ProducesNumber

An example model to ProducesNumber.

__module__ = 'sciunit.models.examples'
produce_number() → float[source]

Each call to this method will produce a different random number.

Returns:
float: A random number produced.

sciunit.models.runnable module

Runnable model.

class sciunit.models.runnable.RunnableModel(name, backend=None, attrs=None)[source]

Bases: sciunit.models.base.Model, sciunit.capabilities.Runnable

A model which can be run to produce simulation results.

__del__() → None[source]
__init__(name, backend=None, attrs=None)[source]

Instantiate model.

__module__ = 'sciunit.models.runnable'
check_run_params() → None[source]

Check if run parameters are reasonable for this model class.

Raise a sciunit.BadParameterValueError if any of them are not.

get_backend()[source]

Return the simulation backend.

reset_default_run_params() → None[source]
reset_run_params() → None[source]
run(**run_params) → None[source]

Run the simulation (or lookup the results in the cache).

set_attrs(**attrs) → None[source]

Set model attributes, e.g. input resistance of a cell.

set_backend(backend: Union[str, tuple, list, None])[source]

Set the simulation backend.

Args:
backend (Union[str, tuple, list, None]): One or more name(s) of backend(s).
The name of backend should be registered before using.
Raises:
TypeError: Backend must be string, tuple, list, or None Exception: The backend was not found.
set_default_run_params(**params) → None[source]

Set default parameters for all runs.

Note these are parameters of the simulation itself, not the model.

set_run_params(**run_params) → None[source]

Set run-time parameters, e.g. the somatic current to inject.

state
use_default_run_params() → None[source]

Module contents

SciUnit models.