Neuroblox API
This page documents the API functions available to users of Neuroblox. This page is focused on utilities like plotting, system generation and querying, and . For documentation of the various Blox, see the Blox documentation page.
Model Creation and Querying
At the highest level, a Neuroblox model consists of a weighted directed graph (represented as a MetaDiGraph) whose nodes are Blox representing neurons and populations of neurons, and whose edges are connectors dictating how the dynamics of the Blox affect each other. The weights of the edges represent the strengths of synaptic connections. The model graph is used to generate a system of ordinary differential equations that can be solved using Julia's differential equations solvers.
Graphs and Systems
The following functions are used in the construction of graphs.
NeurobloxBase.create_adjacency_edges! — Function
create_adjacency_edges!(g::MetaDiGraph, adj_matrix::Matrix{T};
kwargs...)Given an adjacency matrix, populate the graph g with the edges whose weights are stored in the adjacency matrix. Any additional keyword arguments in kwargs... will be forwarded to add_connection!
The following functions are used in the construction of systems from graphs or lists of Blox.
Missing docstring for system_from_graph. Check Documenter's build log for details.
Missing docstring for system_from_parts. Check Documenter's build log for details.
The following functions are used to query graphs.
Missing docstring for connectors_from_graph. Check Documenter's build log for details.
Missing docstring for generate_discrete_callbacks. Check Documenter's build log for details.
Blox
Blox are the basic components of neural circuits. For documentation of the kinds of blox available, please see the Blox documentation page. Internally these are represented as ModelingToolkit systems.
The following functions are used to query blox.
NeurobloxBase.get_exci_neurons — Function
get_exci_neurons(n::Union{GraphSystem, AbstractBlox})Returns all excitatory neurons within a blox or graph.
- If
n <: Union{GraphSystem, AbstractComposite}, returns all excitatory neurons within the graph or composite blox. - If
n::AbstractExciNeuron, returns a single-element vector[n].
NeurobloxBase.get_inh_neurons — Function
get_inh_neurons(n::Union{GraphSystem, AbstractBlox})Returns all inhibitory neurons within a blox or graph.
- If
n <: Union{GraphSystem, AbstractComposite}, returns all excitatory neurons within the graph or composite blox. - If
n::AbstractExciNeuron, returns a single-element vector[n].
Missing docstring for get_input_equations. Check Documenter's build log for details.
Base.nameof — Function
nameof(blox)Returns the name of blox without the namespace prefix.
See also namespaced_nameof and full_namespaced_nameof.
NeurobloxBase.namespaced_nameof — Function
namespaced_nameof(blox)Returns the name of the blox, prefaced by its entire inner namespace (all levels of the namespace EXCEPT for the highest level). Namespaces represent the parent composite blox or graph that blox belongs to. E.g. the name cort1.wta2.neuron4 comes from a neuron neuron4 which belongs to wta2 (possibly a WinnerTakeAll blox) which in turn is part of cort1 (possibly a Cortical blox).
See also nameof and full_namespaced_nameof.
NeurobloxBase.full_namespaced_nameof — Function
full_namespaced_nameof(blox)Return the name of the blox, prefaced by its entire inner namespace (all levels of the namespace INCLUDING for the highest level). Namespaces represent the parent composite blox or graph that blox belongs to. E.g. the name mdl.cort1.wta2.neuron4 comes from a neuron neuron4 which belongs to wta2 (possibly a WinnerTakeAll blox), which in turn is part of cort1 (possibly a Cortical blox), which finally is part of a circuit mdl.
See also nameof and namespaced_nameof.
Connectors
Connectors connect between Blox. They are characterized by connection equations dictating how the state of one Blox affects the other, affects that are triggered when the source spikes, a weight representing the strength of the synaptic connection, and a learning rule dictating how that weight changes when the source fires.
When constructing the graph, the desired connections are represented as edges and added using add_edge!, but the Connectors are not instantiated until the full system is created using system_from_graph.
The following are used to query properties of connectors.
Missing docstring for discrete_callbacks. Check Documenter's build log for details.
The following functions must be defined every time the user wants to define a new kind of connection between two types of Blox.
Missing docstring for connection_equations. Check Documenter's build log for details.
Missing docstring for connection_spike_affects. Check Documenter's build log for details.
Missing docstring for connection_learning_rule. Check Documenter's build log for details.
Missing docstring for connection_callbacks. Check Documenter's build log for details.
Connection rules are used to define the structure of connections between composite blox (i.e. ones consisting of multiple neurons). They are passed in as a connection_rule keyword argument to add_edge!, which can be :hypergeometric, :density, or :weightmatrix. Internally, these keyword arguments correspond to the following functions.
Missing docstring for hypergeometric_connections. Check Documenter's build log for details.
Missing docstring for density_connections. Check Documenter's build log for details.
Missing docstring for weight_matrix_connections. Check Documenter's build log for details.
Missing docstring for indegree_constrained_connections. Check Documenter's build log for details.
Plotting
This section documents helpers for generating plots from solutions to Neuroblox simulations. The backend for generating plots for Neuroblox is Makie. In order to call these functions, one must have a Makie backend installed, such as CairoMakie or GLMakie.
NeurobloxBase.meanfield — Function
meanfield(blox::Union{AbstractComposite, Vector{AbstractNeuron}}, sol::AbstractSolution)Plot the mean-field voltage (in mV) as a function of time (in ms) for a blox.
Arguments:
- blox : a composite blox, e.g. a brain region or microcircuit, containing multiple neurons or a vector of neurons.
- sol : a solution object after running a simulation (i.e. the output of
solve)
Note: this function requires Makie to be loaded.
See also meanfield!, meanfield_timeseries.
NeurobloxBase.meanfield! — Function
meanfield!(ax::Axis, blox::Union{AbstractComposite, Vector{AbstractNeuron}}, sol)Update an existing plot Axis to show the mean-field voltage (in mV) as a function of time (in ms) for a blox.
Arguments:
- ax : an
Axisobject attached to aFigurefromMakie. - blox : a composite blox, e.g. a brain region or microcircuit, containing multiple neurons or a vector of neurons.
- sol : a solution object after running a simulation (i.e. the output of
solve)
Note: this function requires Makie to be loaded.
See also meanfield, meanfield_timeseries.
NeurobloxBase.rasterplot — Function
rasterplot(blox::Union{AbstractComposite, Vector{AbstractNeuron}}, sol::AbstractSolution; threshold = nothing, kwargs...)Create a scatterplot of spikes, where the x-axis is time (in ms) and the y-axis represents neurons organized in separate rows.
Arguments:
- blox : a composite blox, e.g. a brain region or microcircuit, containing multiple neurons or a vector of neurons.
- sol : a solution object after running a simulation (i.e. the output of
solve)
Keyword arguments:
- threshold : [mV] Spiking threshold. Internally used in
detect_spikesto calculate all spiking events. Note that neurons likeNeurobloxPharma.HHNeuronExciandNeurobloxPharma.HHNeuronInhibdo not inherently contain a threshold and so require thisthresholdargument to be passed in order to determine spiking events. - kwargs... : All other keyword arguments are passed to Makie to control figure properties.
Note: this function requires Makie to be loaded.
See also rasterplot!, detect_spikes.
NeurobloxBase.rasterplot! — Function
rasterplot!(ax::Axis, blox::Union{AbstractComposite, Vector{AbstractNeuron}}, sol::AbstractSolution; threshold = nothing, kwargs...)Update an existing plot Axis to show a scatterplot of spikes, where the x-axis is time (in ms) and the y-axis represents neurons organized in separate rows.
Arguments:
- ax : an
Axisobject attached to aFigurefromMakie. - blox : a composite blox, e.g. a brain region or microcircuit, containing multiple neurons or a vector of neurons.
- sol : a solution object after running a simulation (i.e. the output of
solve)
Keyword arguments:
- threshold : [mV] Spiking threshold. Internally used in
detect_spikesto calculate all spiking events. Note that neurons likeNeurobloxPharma.HHNeuronExciandNeurobloxPharma.HHNeuronInhibdo not inherently contain a threshold and so require thisthresholdargument to be passed in order to determine spiking events. - kwargs... : All other keyword arguments are passed to Makie to control figure properties.
Note: this function requires Makie to be loaded.
See also rasterplot, detect_spikes.
NeurobloxBase.stackplot — Function
stackplot(blox::Union{AbstractComposite, Vector{AbstractNeuron}}, sol::AbstractSolution)Plot the voltage timeseries of all neurons contained in blox, stacked on top of each other. The x-axis is time (in ms) and on the y-axis voltage timeseries are plotted on separate rows for each neuron in blox.
Arguments:
- blox : a composite blox, e.g. a brain region or microcircuit, containing multiple neurons or a vector of neurons.
- sol : a solution object after running a simulation (i.e. the output of
solve)
Note: this function requires Makie to be loaded.
See also stackplot!.
NeurobloxBase.stackplot! — Function
stackplot!(ax::Axis, blox::Union{AbstractComposite, Vector{AbstractNeuron}}, sol::AbstractSolution)Update an existing plot Axis to show the voltage timeseries of all neurons contained in blox, stacked on top of each other. The x-axis is time (in ms) and on the y-axis voltage timeseries are plotted on separate rows for each neuron in blox.
Arguments:
- ax : an
Axisobject attached to aFigurefromMakie. - blox : a composite blox, e.g. a brain region or microcircuit, containing multiple neurons or a vector of neurons.
- sol : a solution object after running a simulation (i.e. the output of
solve)
Note: this function requires Makie to be loaded.
See also stackplot.
NeurobloxBase.frplot — Function
frplot(blox::Union{AbstractComposite, Vector{AbstractNeuron}}, sol::AbstractSolution; win_size = 10, overlap = 0, transient = 0, threshold = nothing, kwargs...)Plot the firing rate of blox as a function of time (in s).
Arguments:
- blox : a composite blox, e.g. a brain region or microcircuit, containing multiple neurons or a vector of neurons. If blox contains more than one neurons, then the average firing rate across all neurons is plotted.
- sol : a solution object after running a simulation (i.e. the output of
solve)
Keyword arguments:
- win_size : [ms] Sliding window size.
- overlap : in range [0,1]. Overlap between two consecutive sliding windows.
- transient : [ms] Transient period in the beginning of the timeseries that is ignored during firing rate calculation.
- threshold : [mV] Spiking threshold.
- kwargs... : All other keyword arguments are passed to Makie to control figure properties.
win_size, overlap, transient and threshold are internally used in firing_rate to calculate the firing rate timeseries.
Note: this function requires Makie to be loaded.
See also frplot!, firing_rate.
NeurobloxBase.frplot! — Function
frplot!(ax::Axis, blox::Union{AbstractComposite, Vector{AbstractNeuron}}, sol::AbstractSolution; win_size = 10, overlap = 0, transient = 0, threshold = nothing, kwargs...)Update an existing plot Axis to show the firing rate of blox as a function of time (in s).
Arguments:
- ax : an
Axisobject attached to aFigurefromMakie. - blox : a composite blox, e.g. a brain region or microcircuit, containing multiple neurons or a vector of neurons. If blox contains more than one neurons, then the average firing rate across all neurons is plotted.
- sol : a solution object after running a simulation (i.e. the output of
solve)
Keyword arguments:
- win_size : [ms] Sliding window size.
- overlap : in range [0,1]. Overlap between two consecutive sliding windows.
- transient : [ms] Transient period in the beginning of the timeseries that is ignored during firing rate calculation.
- threshold : [mV] Spiking threshold.
- kwargs... : All other keyword arguments are passed to Makie to control figure properties.
win_size, overlap, transient and threshold are internally used in firing_rate to calculate the firing rate timeseries.
Note: this function requires Makie to be loaded.
See also frplot, firing_rate.
NeurobloxBase.powerspectrumplot — Function
powerspectrumplot(blox::Union{AbstractComposite, Vector{Union{AbstractNeuron, AbstractNeuralMass}}, sol::AbstractSolution; sampling_rate=nothing, method=periodogram, window=nothing, kwargs...)Plot the power spectrum (intensity as a function of frequency) of neurons and neural mass objects contained in blox.
Arguments:
- blox : a composite blox, e.g. a brain region or microcircuit, or a vector containing multiple neurons and/or neural masses.
- sol : a solution object after running a simulation (i.e. the output of
solve)
Keyword arguments:
- sampling_rate : [Hz] Sampling rate.
- method : Method to calculate the periodogram. Options are [
periodogram,welch_pgram]. See theDSP documentationfor more information. - window : An optional window function to be applied to the original signal before computing the Fourier transform. Options are [
nothing,hamming,hanning]. See theDSP documentationfor more information. - kwargs... : All other keyword arguments are passed to Makie to control figure properties.
Note: this function requires Makie to be loaded.
See also powerspectrumplot!, powerspectrum.
NeurobloxBase.powerspectrumplot! — Function
powerspectrumplot!(ax::Axis, blox::Union{AbstractComposite, Vector{Union{AbstractNeuron, AbstractNeuralMass}}, sol::AbstractSolution; sampling_rate=nothing, method=periodogram, window=nothing, kwargs...)Update an existing plot Axis to show the power spectrum (intensity as a function of frequency) of neurons and neural mass objects contained in blox.
Arguments:
- ax : an
Axisobject attached to aFigurefromMakie. - blox : a composite blox, e.g. a brain region or microcircuit, or a vector containing multiple neurons and/or neural masses.
- sol : a solution object after running a simulation (i.e. the output of
solve)
Keyword arguments:
- sampling_rate : [Hz] Sampling rate.
- method : Method to calculate the periodogram. Options are [
periodogram,welch_pgram]. See theDSP documentationfor more information. - window : An optional window function to be applied to the original signal before computing the Fourier transform. Options are [
nothing,hamming,hanning]. See theDSP documentationfor more information. - kwargs... : All other keyword arguments are passed to Makie to control figure properties.
Note: this function requires Makie to be loaded.
See also powerspectrumplot, powerspectrum.
Additionally there are several helpers for extracting useful information from solutions to simulations, such as the timing of spikes and the firing rate. Several of these are called by the plotting functions.
NeurobloxBase.detect_spikes — Function
detect_spikes(blox::AbstractNeuron, sol::AbstractSolution;
threshold=nothing,
tolerance=1e-3,
ts=nothing,
scheduler=:serial)Find the spike timepoints of blox according to simulation solution sol. Return a SparseVector that is equal to 1 at the timepoints of each spike.
Keyword arguments: - threshold : [mV] Spiking threshold. Note that neurons like NeurobloxPharma.HHNeuronExci and NeurobloxPharma.HHNeuronInhib do not inherently contain a threshold and so require this threshold argument to be passed in order to determine spiking events. - tolerance : [mV] The tolerance around the threshold value in which a maximum counts as a spike. - ts : [ms] Timepoints in simulation solution sol where spikes are detected. It can be a range, a vector or an individual timepoint. If no values are passed (and ts=nothing) then the entire range of the solution sol is used.
NeurobloxBase.firing_rate — Function
firing_rate(blox::Union{AbstractComposite, Vector{AbstractNeuron}}, sol::AbstractSolution;
transient=0, win_size=last(sol.t) - transient,
overlap=0, threshold=nothing,
scheduler=:serial, kwargs...)Calculates the firing rate of blox. If blox is a composite blox or a vector containing multiple elements, then the average firing rate across all neurons in blox is returned.
Keyword arguments:
- win_size : [ms] Sliding window size.
- overlap : in range [0,1]. Overlap between two consecutive sliding windows.
- transient : [ms] Transient period in the beginning of the timeseries that is ignored during firing rate calculation.
- threshold : [mV] Spiking threshold.
See also frplot.
NeurobloxBase.inter_spike_intervals — Function
inter_spike_intervals(blox::AbstractNeuron, sol; threshold, ts)Return the time intervals between subsequent spikes in the solution of a single neuron.
inter_spike_intervals(blox::AbstractNeuron, sol; threshold, ts)Return the time intervals between subsequent spikes in the solution of a Blox. Outputs a matrix whose rows are the interspike intervals for a single neuron.
NeurobloxBase.flat_inter_spike_intervals — Function
flat_inter_spike_intervals(blox::AbstractNeuron, sol; threshold, ts)Return the time intervals between subsequent spikes in the solution of a Blox. Concatenates the lists of interspike intervals of all vectors into a single vector.
NeurobloxBase.powerspectrum — Function
powerspectrum(cb::Union{AbstractComposite, Vector{AbstractNeuron}}, sol::AbstractSolution; sampling_rate=nothing, method=periodogram, window=nothing)Calculate the power spectrum of the voltage timeseries for all neurons and/or neural masses contained in blox.
See powerspectrumplot for more information on the keyword arguments.
NeurobloxBase.voltage_timeseries — Function
voltage_timeseries(cb::Union{AbstractComposite, Vector{Union{AbstractNeuron, AbstractNeuralMass}}, sol::AbstractSolution; ts)Return the voltage timeseries of all neurons and/or neural masses contained in blox. The output is a Matrix where each row is a separate neuron or neural mass and each column is a timepoint.
Keyword arguments:
- ts : [ms] Timepoints at which the voltage values are evaluated in simulation solution
sol. It can be a range, a vector or an individual timepoint. If no values are passed (andts=nothing) then the entire range of the solutionsolis used.
NeurobloxBase.meanfield_timeseries — Function
meanfield_timeseries(cb::Union{AbstractComposite, Vector{AbstractNeuron}, sol, state; ts)Return the average timeseries of state variable state over all neurons contained in cb.
Keyword arguments:
- ts : [ms] Timepoints at which the timeseries is evaluated in simulation solution
sol. It can be a range, a vector or an individual timepoint. If no values are passed (andts=nothing) then the entire range of the solutionsolis used.
NeurobloxBase.state_timeseries — Function
state_timeseries(blox::AbstractBlox, sol::SciMLBase.AbstractSolution, state::String; ts = nothing)Return the timeseries of the state variable state from blox.
Keyword arguments:
- ts : [ms] Timepoints at which the timeseries is evaluated in simulation solution
sol. It can be a range, a vector or an individual timepoint. If no values are passed (andts=nothing) then the entire range of the solutionsolis used.
state_timeseries(cb::Union{AbstractComposite, Vector{<:AbstractBlox}},
sol::SciMLBase.AbstractSolution, state::String; ts = nothing)Return the timeseries of the state variable state for each blox contained in cb. The resulting collection of timeseries are stacked in a Matrix where each row is a separate blox in cb.
Keyword arguments:
- ts : [ms] Timepoints at which the timeseries is evaluated in simulation solution
sol. It can be a range, a vector or an individual timepoint. If no values are passed (andts=nothing) then the entire range of the solutionsolis used.
Reinforcement Learning
The following section documents the infrastructure for performing reinforcement learning on neural systems. The neural system acts as the agent, while the environment is a series of sensory stimuli presented to the model. The agent's action is the classification of the stimuli, and the choice follows some policy. Learning occurs as the connection weights of the system are updated according to some learning rule after each choice made by the agent.
NeurobloxPharma.ClassificationEnvironment — Type
ClassificationEnvironment(stim::ImageStimulus, N_trials; name, namespace, t_stimulus, t_pause)Create an environment for reinforcement learning. A set of images is presented to the agent to be classified. This struct stores the correct class for each image, and the current trial of the experiment.
Arguments:
- stim: The ImageStimulus, created from a set of images
- N_trials: Number of trials. The agent performs one classification each trial.
- t_stimulus: The length of time the stimulus is on (ms)
- t_pause: The length of time the stimulus is off (ms)
The following policies are implemented in Neuroblox. Policies are represented by the AbstractActionSelection type. Policies are added as nodes to the graph, with the set of actions represented by incoming connections.
NeurobloxPharma.GreedyPolicy — Type
GreedyPolicy(; name, t_decision, namespace, competitor_states = Symbol[])A policy that makes a choice by picking the state with the highest value among competitor_states which represent each available choice. t_decision is the time of the decision.
Missing docstring for action_selection_from_graph. Check Documenter's build log for details.
The following learning rules are implemented in Neuroblox:
NeurobloxPharma.HebbianPlasticity — Type
HebbianPlasticity(; K, W_lim,
state_pre = nothing,
state_post = nothing,
t_pre = nothing,
t_post = nothing)Hebbian plasticity rule. The connection weight is updated according to :
\[ w_{j+1} = w_j + \text{feedback} × K x_\text{pre} x_\text{post} (W_\text{lim} - w)\]
where feedback is a binary indicator of the correctness of the model's action, and x indicates the activity of the pre- and post-synaptic neuron states state_pre and state_post at timepoints t_pre and t_post respectively.
Arguments: - K : the learning rate of the connection - Wlim : the maximum weight for the connection - statepre : state of the presynaptic neuron that is used in the plasticity rule (by default this is state V in neurons). - statepost : statepre : state of the postsynaptic neuron that is used in the plasticity rule (by default this is state V in neurons). - tpre : timepoint at which `statepreis evaluated to be used in the plasticity rule. - t_post : t_pre : timepoint at whichstate_post` is evaluated to be used in the plasticity rule.
See also HebbianModulationPlasticity.
NeurobloxPharma.HebbianModulationPlasticity — Type
HebbianModulationPlasticity(; K, decay, α, θₘ,
state_pre = nothing,
state_post = nothing,
t_pre = nothing,
t_post = nothing,
t_mod = nothing,
modulator = nothing)Hebbian plasticity rule, modulated by the dopamine reward prediction error. The weight update is largest when the reward prediction error is far from the modulation threshold θₘ.
\[ ϵ = \text{feedback} - (\text{DA}_b - \text{DA}) w_{j+1} = w_j + \max(\times K x_\text{pre} x_\text{post} ϵ(ϵ + θₘ) dσ(α(ϵ + θₘ)) - \text{decay} × w, -w)\]
where feedback is a binary indicator of the correctness of the model's action, DA_b is the baseline dopamine level, DA is the modulator's dopamine release, dσ is the derivative of the logistic function, and x indicates the activity of the pre- and post-synaptic neuron states state_pre and state_post at timepoints t_pre and t_post respectively. The decay prevents the weights from diverging.
Arguments: - K: the learning rate of the connection - decay: Decay of the weight update - α: the selectivity of the derivative of the logistic function - θₘ: the modulation threshold for the reward prediction error - statepre : state of the presynaptic neuron that is used in the plasticity rule (by default this is state V in neurons). - statepost : statepre : state of the postsynaptic neuron that is used in the plasticity rule (by default this is state V in neurons). - tpre : timepoint at which state_pre is evaluated to be used in the plasticity rule. - tpost : tpre : timepoint at which state_post is evaluated to be used in the plasticity rule.
See also HebbianPlasticity.
NeurobloxBase.weight_gradient — Function
weight_gradient(lr::AbstractLearningRule, sol, w, feedback)Calculate the way that the weight w should change based on the solution of the reinforcement learning experiment.
The following functions are used to run reinforcement learning experiments.
NeurobloxBase.run_warmup — Function
run_warmup(agent::AbstractAgent, env::AbstractEnvironment, t_warmup; kwargs...)Run the initial solve of the RL experiment for t_warmup.
NeurobloxBase.run_trial! — Function
run_trial!(agent::AbstractAgent, env::AbstractEnvironment, weights, u0; kwargs...)Run a single trial of a RL experiment. Update the connection weights according to the learning rules.
NeurobloxBase.run_experiment! — Function
run_experiment!(agent::AbstractAgent, env::AbstractEnvironment; verbose=false, t_warmup=0, kwargs...)Perform a full RL experiment with agent in the environment env. Will run until the maximum number of trials in env is reached.