Published
Edited
Aug 12, 2021
1 star
Insert cell
Insert cell
function range(x) {
// Similar to Python's range function.
let a = [];
for(var i = 0; i < x; i++) {
a.push(a);
}
return a;
}
Insert cell
randomSelection = (l, r, nSel) => {
// Random choice without replacement between l and r (includes l, excludes r).
// Used to compute random connections.
let e = new Object();
for(var i = l; i < r; i++) {
e[i] = i;
}
let selection = [];
for(var i = 0; i < nSel; i++) {
let el;
while(1) {
el = l + Math.ceil(Math.random() * (r - l - i));
if(e[el] != null){
break;
}
}
selection.push(e[el])
delete e[el];
}
return selection;
}
Insert cell
Insert cell
function* lif_system_a(nSteps, g, v_ext_v_thr) {
// Simulate a system of leaky integrate-and-fire neurons.
// This is slightly annoying. Having a higher level framework (e.g. Brian) would certainly be helpful.
// We use constants suggested by Brunel (2000), section 2.
// For simplicity, we simulate with a granularity of 1 ms.
const N = 1000;
const N_E = 0.8 * N;
const N_I = 0.2 * N;
const tau_E = 20;
const theta = 20;
const V_r = 10;
const D = 2; // Delay.
const rp = 3; // refractory period.
const C_ext = 1;
const J = 0.1; // mV, as in Figure 8.
// Proportion of active connections.
const eps = 0.1;
// Using a slightly different definition than Brunel (2000) because I'm
// assuming one big external synapse.
let v_thr = theta / (J * tau_E);
let v_ext = v_ext_v_thr * v_thr;
console.log(v_thr);
console.log(v_ext);
console.log(v_ext_v_thr);
// Initialize empty arrays for each neuron.
let spikeTimes = range(N).map((x) => new Array());
let totalSpikes = (new Array(Math.max(D, rp))).fill(0);

// Because the memory overhead of using js' default float64s is substantial, we use typed uint8 arrays
// which let us decrease the memory overhead by 8x. We could go down another 8x by packing
// 8 time steps to a byte, but let's not go nuts just yet.
// Here we encounter some of JS' limitations: there's no first class matrix object.
// Conceptually, we just want to take slices rasters[:, t-D], but we have to think
// about memory layout for this to be straightforward.
let rasters = range(nSteps).map(x => new Uint8Array(N));
let vs = new Uint8Array(N);
// Let's generate random connections.
let connE = [];
let connI = [];
for(var i = 0; i < N; i++) {
connE.push(randomSelection(0, N_E, eps * N_E));
connI.push(randomSelection(N_E, N, eps * N_I));
}
let exc, inh = 0.0;
for(let i = Math.max(D, rp); i < nSteps; i++) {
let newSpikeTimes = range(N).map((x) => new Array());
let sumSpikes = 0;
for(let j = 0; j < N; j++) {
// Random external drive. This looks a bit strange, but it means
// that v_thr = theta / (J C_ext tau)
let driveExt = stdlib.base.random.poisson(v_ext);
//let driveExt = v_ext * C_ext;
// Sum up contributions from all the synaptic currents, delayed.
// Note that the inhibitory inputs are scaled by a factor g.
// Note that we use a reduce function to implement the sum.
let drive = connE[j].map(x => rasters[i - D][x]).reduce((a, b) => a + b, 0) +
-g * connI[j].map(x => rasters[i - D][x]).reduce((a, b) => a + b, 0);
let RI = J * (
drive +
driveExt);
// equation 1.
let dv = - vs[j] / tau_E + RI;
vs[j] += dv;
// If there was a recent spike, override.
if(rasters[i - 1][j] == 1) {
if(rasters[i - D][j] == 0) {
vs[j] = V_r;
rasters[i][j] = 1;
}
}
if(vs[j] > theta) {
// A spike has been generated
spikeTimes[j].push(i);
newSpikeTimes[j].push(i);
rasters[i][j] = 1;
vs[j] = V_r;
sumSpikes += 1;
}
}
totalSpikes.push(sumSpikes / N);
yield [newSpikeTimes, totalSpikes];
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
x = d3.scaleLinear().domain([0, 8]).range([pad, selSize - pad])
Insert cell
y = d3.scaleLinear().domain([20, 0]).range([pad, selSize - pad])
Insert cell
selSize = 200;
Insert cell
pad = 30;
Insert cell
drag = {
function dragstarted() {
d3.select(this).attr("stroke-width", 4);
}

function dragged(event, d) {
d3.select(this).attr("cx", event.x).attr("cy", event.y);
let widget = d3.select("#selectionWidget").node();
let [a, b, oldT] = widget.value;
// Debounce.
if(((new Date()) - oldT) > 200) {
widget.value = [x.invert(event.x), y.invert(event.y), (new Date())];
widget.dispatchEvent(new CustomEvent('input', {bubbles: true}));
} else {
widget.value = [x.invert(event.x), y.invert(event.y), oldT];
}
}

function dragended() {
d3.select(this).attr("stroke-width", 2);
}

return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
Insert cell
height = 300;
Insert cell
Insert cell
Insert cell
maxT = 100;
Insert cell
stdlib = require("https://unpkg.com/@stdlib/stdlib@0.0.32/dist/stdlib-flat.min.js")
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more