Computational Neuroscience with Python
Model the brain from a single cell up, in code: membrane circuits, integrate-and-fire and Hodgkin-Huxley neurons, synapses, networks, plasticity, neural coding and decoding, and the dynamics of decisions, all from scratch with numpy.
10 projects, 250 hands-on levels, run in your browser.
Syllabus
- The Neuron as a Circuit: A neuron's membrane is a tiny electrical circuit: a capacitor (the lipid bilayer) in parallel with resistors (ion channels). Start from charge, capacitance, and Ohm's law, build the exponential dynamics that follow, then integrate them numerically to simulate a passive membrane responding to injected current. This is the foundation every neuron model is built on.
- The Integrate-and-Fire Neuron: Add one rule to the passive membrane and it becomes a spiking neuron: when the voltage reaches a threshold, fire a spike and reset. The leaky integrate-and-fire (LIF) neuron is the workhorse of computational neuroscience. Build it, measure its firing-rate response to current (the F-I curve), add a refractory period, and quantify spike-train irregularity. Units here are mV, ms, nA, and megaohms, so R times I is a voltage in mV.
- The Hodgkin-Huxley Neuron: Where the action potential comes from. Hodgkin and Huxley won a Nobel Prize for describing the spike as the dance of voltage-gated sodium and potassium channels. Build their model from the gating-rate functions up: the m, h, and n gates, the channel conductances and currents, and the four coupled equations that, integrated together, produce a real action potential out of pure biophysics.
- Synapses: Neurons talk through synapses. A presynaptic spike opens channels in the postsynaptic cell, letting current flow toward a reversal potential: excitatory synapses depolarize, inhibitory ones hold the cell down. Build conductance-based synapses from the current law up, give them realistic rise-and-decay kinetics, watch excitation and inhibition combine, see inputs sum to drive a spike, and add short-term plasticity that makes synapses depress and facilitate.
- Networks of Neurons: A single neuron does little; the brain's power is in connections. Represent a network as a weight matrix, drive each neuron with the weighted sum of its inputs, and let firing-rate dynamics evolve to a fixed point. Then balance excitation against inhibition, check a network for stability, and measure population activity. This is the linear-algebra heart of network neuroscience.
- Plasticity and Learning: Synapses change with experience, and that change is how the brain learns. Start with Hebb's rule (neurons that fire together wire together), see why it is unstable and how Oja's rule and normalization tame it, then build spike-timing-dependent plasticity where the order of pre and post spikes decides potentiation or depression. Finish with homeostasis, and an Oja learner that discovers the principal direction of its inputs.
- Neural Coding: How do spikes represent the world? Build the firing-rate code and smooth spike trains into rates, then tuning curves that make a neuron respond most to its preferred stimulus. Model the irregular, Poisson-like firing of real cortex, quantify its variability with the Fano factor, and finish by encoding a stimulus in the activity of a whole population.
- Decoding and Information: If neurons encode the world, can we read it back out? Decode a stimulus from population activity by center of mass, winner-take-all, and the population vector, then do it optimally with maximum likelihood. Measure how discriminable two responses are with d-prime, quantify information with entropy and mutual information, and train a linear decoder to predict a stimulus from spike counts.
- Dynamics and Models: Step back from single channels to the dynamics of neural systems. Reduce the spike to a two-variable FitzHugh-Nagumo model and read it in the phase plane, model interacting populations with Wilson-Cowan equations that can oscillate, store memories as fixed points in a Hopfield network, and model a decision as evidence accumulating to a bound. This is the systems-level view of the brain as a dynamical system.
- Capstone: A Spiking Circuit: The payoff: put the whole track together into a working spiking circuit. Vectorize the integrate-and-fire neuron to run a whole population at once, couple the neurons with conductance-based synapses through a weight matrix, drive them with tuned input, run the circuit forward, and read the stimulus back out of the spike counts. By the end you have built, from scratch, a population of spiking neurons that represents and reports what it sees.
Key concepts
- Action potential: The spike: a fast, all-or-none ~100 mV swing produced by voltage-gated sodium and potassium channels, described by the Hodgkin-Huxley model.
- Attractor network: A recurrent network whose dynamics flow toward stored stable states. A Hopfield network stores patterns as energy minima and cleans up corrupted inputs.
- Capacitance: The membrane's ability to store charge, Q = C * V . A smaller cell has smaller capacitance and is more excitable, since the same charge moves its voltage f…
- Coefficient of variation: The standard deviation of the inter-spike intervals divided by their mean. Near 0 for a clock-like neuron, near 1 for Poisson-irregular firing.
- d-prime: A signal-detection measure of how separable two responses are: the difference of their means in units of noise standard deviation. Larger d-prime means easier…
- Drift-diffusion model: A model of decision-making as noisy evidence accumulating over time until it reaches a bound. It captures both the choice made and the time it takes.
- Driving force: How far the membrane is from a channel's reversal potential, V - E . The current through a channel is its conductance times this driving force, and it vani…
- Entropy: The average uncertainty of a distribution, -sum p log2 p in bits. Largest when all outcomes are equally likely.
- EPSP: Excitatory postsynaptic potential: the transient depolarization an excitatory synapse produces. Many EPSPs can sum in time and space to reach threshold.
- Euler method: The simplest numerical integration: step a differential equation forward by value + dt * rate . The engine behind every simulation in this track.
- Excitation-inhibition balance: The regime where large excitatory and inhibitory inputs nearly cancel, leaving a small, fluctuating net drive. It produces the irregular firing seen in cortex.
- F-I curve: The frequency-current curve: a neuron's firing rate as a function of input current. It has a rheobase (the smallest current that makes it fire) and a gain…
- Fano factor: The variance of the spike count divided by its mean, a measure of count variability across trials. It equals 1 for a Poisson process.
- Firing rate: Spikes per second (Hz), the most common neural code. Estimated by counting spikes in a window or smoothing the spike train with a kernel.
- FitzHugh-Nagumo model: A two-variable reduction of the action potential, a fast voltage-like variable with a cubic nonlinearity and a slow recovery variable. Simple enough to study i…
- Fixed point: A state where the dynamics stop changing. Attractor networks store memories as stable fixed points; decision models flow toward bounds.
- Gating variable: A number between 0 and 1 giving the fraction of a channel's gates that are open. It moves toward a voltage-dependent steady state x_inf with time constant…
- Hebbian learning: Neurons that fire together wire together: a synapse strengthens in proportion to correlated pre- and post-synaptic activity, dw = eta * pre * post . Powerful b…
- Hodgkin-Huxley model: The Nobel-winning biophysical model of the action potential, four coupled equations for the voltage and the m, h, and n gating variables of the sodium and pota…
- Hopfield network: An attractor memory built from the outer product of stored binary patterns. It recalls a full pattern from a noisy fragment by descending an energy function.
- Integrate-and-fire: The workhorse spiking model: a leaky membrane that integrates its input until it reaches a threshold, then fires a spike and resets. Simple enough to analyze,…
- Inter-spike interval: The time gap between consecutive spikes. Its distribution and variability characterize how regularly a neuron fires.
- Leak conductance: The always-open channels that pull the membrane toward its leak reversal potential, modeled as I = g * (V - E) . They set the resting potential together with t…
- Maximum-likelihood decoding: The optimal decoder: pick the stimulus that makes the observed spike counts most probable under a Poisson noise model.
- Membrane potential: The voltage across a neuron's membrane, set by the separation of charge, typically near -65 mV at rest. Every model in this track tracks how it changes ove…
- Mutual information: How much knowing the neural response reduces uncertainty about the stimulus, in bits. Zero when response and stimulus are independent.
- Nullcline: A curve in the phase plane where one variable stops changing. Where two nullclines cross is a fixed point of the system.
- Oja's rule: A stabilized Hebbian rule that keeps the weight vector at unit length and makes a neuron learn the principal component of its inputs.
- Phase plane: The plane of a two-variable system's state. Trajectories, nullclines, and fixed points in it reveal whether the system rests, spikes, or oscillates.
- Poisson process: A model of irregular firing where each tiny time bin spikes independently with probability set by the rate. Its spike count has a Fano factor near 1 and expone…
- Population code: Representing a stimulus in the joint activity of many tuned neurons. The stimulus is decoded from the bump of activity by center of mass, population vector, or…
- Population vector: A decoder that sums each neuron's preferred direction weighted by its firing rate, then reads off the angle. A simple, robust way to decode direction.
- Refractory period: A brief time after a spike during which a neuron cannot fire again. It caps the maximum firing rate at 1000 / t_ref Hz.
- Resting potential: The steady membrane voltage with no input, where all the channel currents balance, near -65 mV in these models.
- Reversal potential: The voltage at which a channel passes no net current. Above it the current flows one way, below it the other. Excitatory synapses have a high reversal, inhibit…
- Short-term plasticity: Fast, reversible changes in synaptic strength with use: depression spends resources with each spike, facilitation raises release. Modeled with the Tsodyks-Mark…
- Spike raster: A record of which neurons spiked at which times, stored as a binary matrix of neurons by time. The raw output of a spiking-network simulation.
- STDP: Spike-timing-dependent plasticity: a synapse potentiates when the presynaptic spike comes shortly before the postsynaptic one, and depresses when the order is…
- Summation: How a neuron adds its inputs: temporal summation combines inputs close in time, spatial summation combines inputs from different synapses arriving together.
- Synapse: The connection between neurons: a presynaptic spike opens a conductance in the postsynaptic cell, pulling its voltage toward the synaptic reversal potential.
- Synaptic plasticity: Lasting change in synaptic strength that underlies learning. Includes Hebbian learning, Oja's rule, and spike-timing-dependent plasticity.
- Threshold: The voltage a neuron must reach to fire a spike, around -50 mV . In integrate-and-fire models, crossing it triggers a spike and a reset.
- Time constant: tau = R * C , how fast the membrane responds. After one time constant a passive membrane has moved about 63% of the way to its steady state.
- Tuning curve: A neuron's firing rate as a function of the stimulus, often a Gaussian or cosine bump peaked at its preferred stimulus.
- Wilson-Cowan model: Coupled equations for the activity of an excitatory and an inhibitory population. Their feedback can settle to a steady state or break into oscillations.