Docstring Reference¶
This is the auto-generated docstring reference for the ct-neat-python package.
The ctneat.ctrnn Continuous-Time Recurrent Neural Network Module¶
This module implements Continuous-Time Recurrent Neural Networks (CTRNNs).
- class ctneat.ctrnn.CTRNN(inputs: List[int], outputs: List[int], node_evals: Dict[int, CTRNNNodeEval], custom_advance: Callable | None = None)[source]¶
Sets up the ctrnn network itself.
- __init__(inputs: List[int], outputs: List[int], node_evals: Dict[int, CTRNNNodeEval], custom_advance: Callable | None = None)[source]¶
Initialize the CTRNN with the given input and output nodes, and node evaluations. :param inputs: The input node IDs. :param outputs: The output node IDs. :param node_evals: A dictionary mapping node IDs to their evaluations (CTRNNNodeEval objects). :param custom_advance: An optional custom advance function.
- advance(inputs: List[float], advance_time: float, time_step: float | None = None)[source]¶
Advance the simulation by the given amount of time, assuming that inputs are constant at the given values during the simulated time. :param inputs: The input values to the network. :param advance_time: The amount of time to advance the simulation. :param time_step: The time step to use for the simulation.
- Returns:
The output values of the network after the simulation.
- static create(genome, config, time_constant)[source]¶
Receives a genome and returns its phenotype (a CTRNN). :param genome: The genome to create the CTRNN from. :param config: The configuration object. :param time_constant: The time constant to use for all nodes.
- class ctneat.ctrnn.CTRNNNodeEval(time_constant: float, activation: Callable, aggregation: Callable, bias: float, response: float, links: List[Tuple[int, float]])[source]¶
- __init__(time_constant: float, activation: Callable, aggregation: Callable, bias: float, response: float, links: List[Tuple[int, float]])[source]¶
Initialize a CTRNN node evaluation. :param time_constant: The time constant of the node. :param activation: The activation function of the node. :param aggregation: The aggregation function of the node. :param bias: The bias term of the node. :param response: The response term of the node. :param links: The links from other nodes (incoming connections).
The ctneat.ctrnn.ctrnn_visualize CTRNN Visualization Module¶
This module provides functions to visualize the dynamics and trajectories of CTRNNs.
This file contains functions used to visualize the CTRNN model.
- ctneat.ctrnn.ctrnn_visualize.draw_ctrnn_dynamics(states: ndarray, normalize: bool = False, uniform_time: bool = True, times: ndarray | list | None = None, iznn: bool | None = False, save: bool = False, show: bool = True, dir_name: str | None = 'ctneat_outputs', file_name: str | None = None) None[source]¶
This function draws the dynamics of the CTRNN over time.
- Parameters:
states – A 2D numpy array where each row corresponds to the state of the network at a given time step, and each column corresponds to a specific node’s state.
normalize – Whether to normalize the states before plotting. If True, each node’s state is scaled to [0, 1].
uniform_time – Whether the time steps are uniform. If True, the function generates a uniform time array.
times – If uniform_time is False, a list or numpy array of time points corresponding to each row in states. If uniform_time is True, this parameter is ignored.
iznn – Whether the network is an Izhikevich spiking neural network (IZNN). If True, the function gives correct labels.
save – Whether to save the plot as a file. If False, the plot is shown interactively.
show – Whether to display the plot interactively. If False, the plot is only saved to a file if ‘save’ is True.
dir_name – Optional directory name to save the output file. If None, saves in the current directory.
file_name – Optional file name to save the output file. If None, defaults to ‘ctrnn_dynamics’.
- Returns:
None
- ctneat.ctrnn.ctrnn_visualize.draw_ctrnn_net(node_list: list, node_inputs: dict, iznn: bool | None = False, dir_name: str | None = 'ctneat_outputs', file_name: str | None = None) None[source]¶
This function draws the CTRNN network structure.
- Parameters:
node_list – A list of node IDs in the network.
node_inputs – A dictionary where keys are node IDs and values are lists of input connections for each node. (I.e. each list contains tuples of (input_node_id, weight) for the node with the corresponding ID)
iznn – Whether the network is an Izhikevich spiking neural network (IZNN). If True, the function gives correct labels.
dir_name – Optional directory name to save the output file. If None, saves in the current directory.
file_name – Optional file name to save the output file. If None, defaults to ‘ctrnn_network’.
- Returns:
None
- ctneat.ctrnn.ctrnn_visualize.draw_ctrnn_trajectory(states: ndarray, n_components: int = 2, iznn: bool | None = False, save: bool = False, show: bool = True, dir_name: str | None = 'ctneat_outputs', file_name: str | None = None) None[source]¶
This function draws a trajectory of the CTRNN’s state space. If there are more than ‘n_components’ nodes, the PCA is used to reduce the dimensionality to ‘n_components’.
- Parameters:
states – A 2D numpy array where each row corresponds to the state of the network at a given time step,
state. (and each column corresponds to a specific node's)
n_components – The number of components to reduce to (default is 2, max is 3). Note that if n_components is equal to 1 and the number of nodes is also 1, this function is equivalent to draw_ctrnn_dynamics.
iznn – Whether the network is an Izhikevich spiking neural network (IZNN). If True, the function gives correct labels.
save – Whether to save the plot as a file. If False, the plot is shown interactively.
show – Whether to display the plot interactively. If False, the plot is only saved to a file if ‘save’ is True.
dir_name – Optional directory name to save the output file. If None, saves in the current directory.
file_name – Optional file name to save the output file. If None, defaults to ‘ctrnn_face_portrait’.
- Returns:
None
- Raises:
ValueError – If n_components is not 1, 2, or 3.
The ctneat.iznn Spiking Neuron Module¶
This module implements the Izhikevich neuron model and network.
- class ctneat.iznn.IZNN(neurons: Dict[int, IZNeuron], inputs: List[int], outputs: List[int], event_driven: bool = False)[source]¶
Basic iznn network object.
- __init__(neurons: Dict[int, IZNeuron], inputs: List[int], outputs: List[int], event_driven: bool = False)[source]¶
Initializes the IZNN with the given neurons, inputs, and outputs.
- advance(dt: float, method: str | None = 'LSODA', events: bool = False, ret: List[str] | str | None = None) List[float] | List[List[float]][source]¶
Advances the simulation by the given time step in milliseconds.
- Parameters:
dt_msec (float) – The time step in milliseconds.
method (str) –
The integration method to use. If None, uses manually written RK4, otherwise defaults to SciPy’s LSODA. If specified, uses SciPy’s solve_ivp with the given method. Valid methods are listed in the SciPy documentation for solve_ivp (https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html). And here is a summary of available methods:
’RK45’ (Default): An adaptive Runge-Kutta method of order 5(4). It’s a great general-purpose choice and a good starting point.
’RK23’: A lower-order adaptive Runge-Kutta method. Faster but less accurate than RK45.
’DOP853’: A high-order (8th) adaptive Runge-Kutta method for when you need very high precision.
- ’LSODA’: This is a particularly important one for spiking neurons. It’s a solver that automatically
switches between methods for non-stiff and stiff problems. A “stiff” ODE is one where some parts of the solution change very rapidly while others change slowly (like the membrane potential during a spike!). LSODA is often very efficient and stable for these kinds of systems.
’BDF’ and ‘Radau’: Other excellent methods for stiff problems.
events (bool) – Whether to use event detection for spikes. Only applicable if ‘method’ is specified.
ret (list(str) or str or None) –
Specifies what to return. If a list of strings, returns a list of lists, where each inner list corresponds to the requested attribute for all output neurons. If a single string, returns a list corresponding to the requested attribute for all output neurons. If None, returns a list of firing states for all output neurons. Valid strings are:
’fired’ - returns the firing states (1.0 if fired, 0.0 otherwise) ‘voltages’ - returns the membrane potentials (in millivolts) ‘recovery’ - returns the recovery variables ‘all’ - returns a list of lists: [fired states, voltages, recovery variables]
- Returns:
A list or a list of lists as specified by the ‘ret’ parameter.
- Raises:
ValueError – If an invalid integration method is specified.
- advance_event_driven(dt_msec: float, method: str = 'LSODA', ret: List[str] | str | None = None) List[float] | List[List[float]][source]¶
Advances the simulation by at most dt_msec using a true event-driven approach.
The simulation advances to the time of the earliest spike event in the network, or by the full dt_msec if no spikes occur in that interval. This ensures that spike timing is captured with high precision.
- Parameters:
dt_msec (float) – The maximum time step to advance in milliseconds.
method (str) –
The integration method to use. If None, uses manually written RK4, otherwise defaults to SciPy’s LSODA. If specified, uses SciPy’s solve_ivp with the given method. Valid methods are listed in the SciPy documentation for solve_ivp (https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html). And here is a summary of available methods:
’RK45’ (Default): An adaptive Runge-Kutta method of order 5(4). It’s a great general-purpose choice and a good starting point.
’RK23’: A lower-order adaptive Runge-Kutta method. Faster but less accurate than RK45.
’DOP853’: A high-order (8th) adaptive Runge-Kutta method for when you need very high precision.
’LSODA’: This is a particularly important one for spiking neurons. It’s a solver that automatically switches between methods for non-stiff and stiff problems. A “stiff” ODE is one where some parts of the solution change very rapidly while others change slowly (like the membrane potential during a spike!). LSODA is often very efficient and stable for these kinds of systems.
’BDF’ and ‘Radau’: Other excellent methods for stiff problems.
ret (list(str) or str or None) –
Specifies what to return. If a list of strings, returns a list of lists, where each inner list corresponds to the requested attribute for all output neurons. If a single string, returns a list corresponding to the requested attribute for all output neurons. If None, returns a list of firing states for all output neurons. Valid strings are:
’fired’ - returns the firing states (1.0 if fired, 0.0 otherwise) ‘voltages’ - returns the membrane potentials (in millivolts) ‘recovery’ - returns the recovery variables ‘all’ - returns a list of lists: [fired states, voltages, recovery variables]
- Returns:
A list or a list of lists as specified by the ‘ret’ parameter, representing the state of the output neurons after the time step.
- Raises:
ValueError – If an invalid integration method is specified.
- advance_simple(dt_msec, method: str | None = 'LSODA', events: bool = False, ret: List[str] | str | None = None) List[float] | List[List[float]][source]¶
Advances the simulation by the given time step in milliseconds.
- Parameters:
dt_msec (float) – The time step in milliseconds.
method (str) –
The integration method to use. If None, uses manually written RK4, otherwise defaults to SciPy’s LSODA. If specified, uses SciPy’s solve_ivp with the given method. Valid methods are listed in the SciPy documentation for solve_ivp (https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html). And here is a summary of available methods:
’RK45’ (Default): An adaptive Runge-Kutta method of order 5(4). It’s a great general-purpose choice and a good starting point.
’RK23’: A lower-order adaptive Runge-Kutta method. Faster but less accurate than RK45.
’DOP853’: A high-order (8th) adaptive Runge-Kutta method for when you need very high precision.
’LSODA’: This is a particularly important one for spiking neurons. It’s a solver that automatically switches between methods for non-stiff and stiff problems. A “stiff” ODE is one where some parts of the solution change very rapidly while others change slowly (like the membrane potential during a spike!). LSODA is often very efficient and stable for these kinds of systems.
’BDF’ and ‘Radau’: Other excellent methods for stiff problems.
events (bool) – Whether to use event detection for spikes. Only applicable if ‘method’ is specified.
ret (list(str) or str or None) –
Specifies what to return. If a list of strings, returns a list of lists, where each inner list corresponds to the requested attribute for all output neurons. If a single string, returns a list corresponding to the requested attribute for all output neurons. If None, returns a list of firing states for all output neurons. Valid strings are:
’fired’ - returns the firing states (1.0 if fired, 0.0 otherwise) ‘voltages’ - returns the membrane potentials (in millivolts) ‘recovery’ - returns the recovery variables ‘all’ - returns a list of lists: [fired states, voltages, recovery variables]
- Returns:
A list or a list of lists as specified by the ‘ret’ parameter.
- Raises:
ValueError – If an invalid integration method is specified.
- static create(genome, config)[source]¶
Receives a genome and returns its phenotype (a neural network).
- get_time_step_msec()[source]¶
Returns a safe time step in milliseconds for the current network configuration. This is a placeholder implementation and should be replaced with a proper calculation.
- set_inputs(inputs: List[float])[source]¶
Assigns input voltages.
- Parameters:
inputs (list) – A list of input voltages. (in millivolts, where each voltage corresponds to an input neuron)
- class ctneat.iznn.IZNeuron(bias: float, a: float, b: float, c: float, d: float, inputs: List[Tuple[int, float]])[source]¶
Sets up and simulates the iznn nodes (neurons).
- __init__(bias: float, a: float, b: float, c: float, d: float, inputs: List[Tuple[int, float]])[source]¶
a, b, c, d are the parameters of the Izhikevich model.
- Parameters:
bias (float) – The bias of the neuron.
a (float) – The time-scale of the recovery variable.
b (float) – The sensitivity of the recovery variable.
c (float) – The after-spike reset value of the membrane potential.
d (float) – The after-spike reset value of the recovery variable.
inputs (list(tuple(int, float))) – A list of (input key, weight) pairs for incoming connections.
- advance(dt_msec: float)[source]¶
This is a default advance method which is simply a wrapper to the advance_scipy method.
- Parameters:
dt_msec (float) – The time step in milliseconds.
- advance_rk4(dt_msec: float)[source]¶
Advances simulation time using 4th-Order Runge-Kutta.
- if v >= 30 then
v <- c, u <- u + d
- else
v’ = 0.04 * v^2 + 5v + 140 - u + I u’ = a * (b * v - u)
- Parameters:
dt_msec (float) – The time step in milliseconds.
- advance_scipy(dt_msec: float, method: str = 'LSODA')[source]¶
Advances simulation time using a solver from SciPy.
- Parameters:
dt_msec (float) – The time step in milliseconds.
method (str) – The integration method to use (e.g., ‘RK45’, ‘LSODA’). Other options are listed in the SciPy documentation for solve_ivp (https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html).
The ctneat.iznn.dynamic_attractors Dynamic Attractors Module¶
This module provides functions to automatically find and analyze dynamic attractors in spiking neural networks.