skneuromsi.core.modelabc module

Implementation of a metaclass to create renameable parameters in functions and methods.

This module provides classes and utilities for creating renameable parameters in functions and methods, as well as configuring the run method of SKNMSIMethodABC subclasses.

class skneuromsi.core.modelabc.ParameterAliasTemplate(target: str, template: Template, doc_pattern: str = None)[source]

Bases: object

Represents the rule for constructing aliases for a parameter.

Parameters:
  • target (str) – The name of the parameter to replace.

  • template (string.Template) – The template used to generate aliases for the target.

  • doc_pattern (re.Pattern, optional) – Regex pattern to replace all occurrences of the target with the alias in the documentation. If not provided, a default pattern is used.

target: str
template: Template
doc_pattern: str = None
property template_variables: frozenset

Variables del template.

render(context) str[source]

Crea un nuevo alias basado en el contexto provisto.

class skneuromsi.core.modelabc.SKNMSIRunConfig(*, _input: tuple, _output: tuple, _model_name: str, _model_type: str, _output_mode: str)[source]

Bases: object

Configuration class for using aliases in the run method and creating the result object.

Parameters:
  • _input (tuple) – Input configuration for alias templates.

  • _output (tuple) – Output configuration for alias templates.

  • _model_name (str) – Name of the model.

  • _model_type (str) – Type of the model.

  • _output_mode (str) – Output mode of the model.

classmethod from_method_class(method_class)[source]

Create a new configuration instance based on a subclass of SKNMSIMethodABC.

The class must implement the ‘_run_input’ variable, which contains the configuration of the alias templates.

Parameters:

method_class (type) – Subclass of SKNMSIMethodABC.

Returns:

Configuration instance based on the method class.

Return type:

SKNMSIRunConfig

property input_targets

Set of all configurable parameters of the run method.

property output_targets

Set of all configurable parameters of run method output.

property template_variables

All alias-template variables defined in all targets.

make_run_input_alias_map(context)[source]

Create a bidirectional dictionary that maps aliases to targets.

The context is the configuration provided by the user through the init parameters.

Parameters:

context (dict) – Context for rendering the aliases.

Returns:

Bidirectional dictionary mapping aliases to targets.

Return type:

frozenbidict

make_run_output_alias_map(context)[source]

Create a bidirectional dictionary that maps output aliases to targets.

Parameters:

context (dict) – Context for rendering the aliases.

Returns:

Bidirectional dictionary mapping output aliases to targets.

Return type:

frozenbidict

validate_init_and_run(method_class)[source]

Validate that the __init__ and run methods have the appropriate parameters.

The method takes as argument a class inherited from SKNMSIMethodABC.

Parameters:

method_class (type) – Subclass of SKNMSIMethodABC.

Raises:

TypeError – If __init__ or run() do not have the required parameters.

wrap_run(model_instance, run_template_context)[source]

Return a wrapper for the run method.

This method is used for each instance of a class object SKNMSIMethodABC.

The resulting method accepts as parameters the resulting aliases from the configuration and maps them to the “targets”.

Parameters:
  • model_instance (SKNMSIMethodABC) – Instance of the model class.

  • run_template_context (dict) – Context for rendering the aliases.

Returns:

Wrapped run method.

Return type:

callable

wrap_init(init_method)[source]

Wrap the __init__ method of an SKNMSIMethodABC subclass.

Parameters:

init_method (callable) – Original __init__ method.

Returns:

Wrapped __init__ method.

Return type:

callable

get_model_state(instance)[source]

Get the state of a model instance.

Parameters:

instance (SKNMSIMethodABC) – Model instance.

Returns:

State of the model instance.

Return type:

dict

set_model_state(instance, state)[source]

Set the state of a model instance.

Parameters:
  • instance (SKNMSIMethodABC) – Model instance.

  • state (dict) – State to set on the model instance.

class skneuromsi.core.modelabc.SKNMSIMethodABC[source]

Bases: object

Abstract class that allows to configure method names dynamically.

This class serves as the base class for all models in skneuromsi. It provides dynamic configuration of method names and parameters using aliases.

calculate_causes(**kwargs)[source]

Calculate the causes based on the model output.

This method should be overridden by subclasses to provide the specific implementation for calculating causes.

Parameters:

**kwargs – Keyword arguments representing the model output.

Returns:

The calculated causes.

Return type:

Any