brainstate.mixin module#

Mixin classes and utility types for brainstate.

This module provides various mixin classes and custom type definitions that enhance the functionality of brainstate components. It includes parameter description mixins, alignment interfaces, and custom type definitions for expressing complex type requirements.

The brainstate.mixin module provides mixin classes and utility types that enhance the functionality of BrainState components through multiple inheritance. It includes parameter description systems and advanced type definitions for expressing complex type requirements.

Key Features#

  • Parameter Descriptors: Reusable parameter templates for object instantiation

  • Type System: Advanced union and intersection type utilities (JointTypes, OneOfTypes)

  • Deferred Instantiation: Templates for creating multiple objects with shared configurations

  • Utility Functions: Helper functions for hashability checks and marking unimplemented methods

Quick Start#

Using parameter descriptors for reusable configurations:

import brainstate

class NeuronModel(brainstate.mixin.ParamDesc):
    def __init__(self, size, tau=10.0, threshold=1.0):
        self.size = size
        self.tau = tau
        self.threshold = threshold

# Create a reusable template
neuron_template = NeuronModel.desc(size=100, tau=20.0)

# Create multiple instances with different thresholds
excitatory = neuron_template(threshold=1.0)
inhibitory = neuron_template(threshold=0.5)

Using advanced type system:

from typing import Protocol

# Define protocols/interfaces
class Trainable(Protocol):
    def train(self): ...

class Evaluable(Protocol):
    def evaluate(self): ...

# Require both interfaces (intersection type)
TrainableEvaluableModel = brainstate.mixin.JointTypes[Trainable, Evaluable]

# Allow either type (union type)
NumericType = brainstate.mixin.OneOfTypes[int, float]

Base Mixin Classes#

Core mixin classes providing foundational functionality.

Mixin

Base Mixin object for behavioral extensions.

Parameter Description System#

Classes and utilities for creating reusable parameter templates and deferred object instantiation.

ParamDesc

Mixin for describing initialization parameters.

ParamDescriber

Parameter descriptor for deferred object instantiation.

HashableDict

A dictionary that can be hashed by converting non-hashable values to strings.

NoSubclassMeta

Metaclass that prevents a class from being subclassed.

Advanced Type System#

Utilities for creating complex type annotations and requirements.

Type Combinators#

Classes and functions for creating union and intersection types.

JointTypes

Helper class to enable subscript syntax for JointTypes.

OneOfTypes

Helper class to enable subscript syntax for OneOfTypes.

_JointGenericAlias

Generic alias for JointTypes (intersection types).

_OneOfGenericAlias

Generic alias for OneOfTypes (union types).

Utility Functions and Decorators#

Helper functions and decorators for enhanced functionality.

hashable

Check if an object is hashable.

not_implemented

Decorator to mark a function as not implemented.