Skip to content

Instantly share code, notes, and snippets.

View mstimberg's full-sized avatar
💾

Marcel Stimberg mstimberg

💾
View GitHub Profile
from brian2 import *
# import brian2cuda
set_device('cpp_standalone', directory='layers_openmp')
prefs.devices.cpp_standalone.openmp_threads = 4
group1 = NeuronGroup(100, 'dv/dt = 100*Hz : 1', threshold='v>1', reset='v=0')
group1.v = 'rand()'
group2 = NeuronGroup(100, '''dv/dt = -v/tau : 1
tau : second ''', threshold='v>1', reset='v=0')
group2.tau = 10*ms
@mstimberg
mstimberg / unpaywall_dois.py
Created January 9, 2023 09:35
Add openaccess links to Neuromatch Academy further reading lists (via unpaywall API)
import json
import os
import re
import requests
doi_link = r'(doi: \[.*\]\(https://doi.org/([a-zA-Z0-9/._\-()]+)\))'
unpaywall_api = 'https://api.unpaywall.org/v2/'
email = 'email@example.com' # ← EMAIL
def gather_doi_info(fname):
"""
@mstimberg
mstimberg / stimulus_change_standalone.py
Last active July 21, 2022 13:32
Changing a stimulus in an ongoing simulation in Brian's C++ standalone mode
from brian2 import *
set_device('cpp_standalone')
# Each "input" is one combination of rates across the 5 neurons
n_stimuli = 10
rates = TimedArray(np.random.rand(n_stimuli, 5)*100*Hz, dt=100*ms)
stimulus_length = 100*ms
max_repetitions = 5
input_spikes = NeuronGroup(5, '''
@mstimberg
mstimberg / IF.nml
Created November 23, 2020 17:16
Example for problem in NeuroML2 export
<?xml version="1.0" ?>
<Lems xmlns="http://www.neuroml.org/lems/0.7.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.neuroml.org/lems/0.7.3 https://raw.githubusercontent.com/LEMS/LEMS/development/Schemas/LEMS/LEMS_v0.7.3.xsd">
<Include file="LEMSUnitsConstants.xml"/>
<Include file="Simulation.xml"/>
<Include file="NeuroML2CoreTypes.xml"/>
<ComponentType extends="baseCell" name="neurongroup">
<Property dimension="voltage" name="v0"/>
<EventPort direction="out" name="spike"/>
<Exposure dimension="voltage" name="v"/>
<Dynamics>
@mstimberg
mstimberg / indexing_neurongroup.svg
Created September 14, 2020 14:56
Indexing in Brian 2 (with units / without units)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mstimberg
mstimberg / brian_openmp.ipynb
Created March 23, 2020 17:22
OpenMP code generation for synaptic propagation in Brian 2
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mstimberg
mstimberg / parallel_issue.py
Created March 19, 2020 14:00
Parallelization issues
from brian2 import *
# The following example is very contrived, but writing to a variable that is linked with repeating indices
# might come up in some contexts. It's clearly a cornercase, but one that we should handle.
set_device('cpp_standalone')
prefs.devices.cpp_standalone.openmp_threads = 8 # number of threads
group1 = NeuronGroup(2, 'x : integer')
group2 = NeuronGroup(1000, 'y : integer (linked)')
group2.y = linked_var(group1, 'x', index=np.arange(2).repeat(500))
obj = group2.run_regularly('y += 1')
@mstimberg
mstimberg / remaining_time_estimates.py
Last active March 12, 2020 15:26
Visualize the remaining time estimated with a simple algorithm that estimates only based on the total time elapsed so far.
import numpy as np
import matplotlib.pyplot as plt
scenarios = [('constant', [1., 2., 3., 4., 5., 6., 7., 8., 9., 10.], # elapsed time
[.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.]), # fraction of simulation completed
('varying', [1., 2., 3., 4., 5., 6., 7., 8., 9., 10.],
[.1, .175, .3, .375, .5, .575, .7, .775, .9, 1.]),
('slow start', [1., 2., 3., 4., 5., 6., 7., 8., 9., 10.],
[.05, 0.1, 0.15, 0.2, 0.25, 0.4, 0.55, 0.7, 0.85, 1.0]),
('slow end', [1., 2., 3., 4., 5., 6., 7., 8., 9., 10.],
@mstimberg
mstimberg / sensitivity_analysis.ipynb
Last active February 27, 2020 14:26
Local forward sensitivity analysis of a neural model with Brian 2
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.