Skip to content

Commit

Permalink
Merge pull request #20 from OpenSourceBrain/test_pynn3
Browse files Browse the repository at this point in the history
Add export of new PyNN to NeuroML
  • Loading branch information
pgleeson authored Apr 2, 2024
2 parents 6150525 + 9324c5d commit 592a7df
Show file tree
Hide file tree
Showing 9 changed files with 59,392 additions and 40 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ NEST_SLI/*.gdf
/PyNN/tests.log
/PyNN/data/2024*
/p0/*
/NeuroML2/*.dat
/PyNN/LEMS_Sim_Microcircuit*.xml
/NeuroML2/*.spikes
/NeuroML2/Microcircuit*.gv
8 changes: 3 additions & 5 deletions NeuroML2/.test.validate.omt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Script for running automated tests on OSB using Travis-CI, see https://github.com/OpenSourceBrain/osb-model-validation
# Still in development, subject to change without notice!!

# This test will validate all of the NeuroML 2 files in the current directory using: jnml -validate *.nml
target: "*.nml"
# Script for running automated tests on OSB, see https://github.com/OpenSourceBrain/osb-model-validation

target: "*.nml"
engine: jNeuroML_validate
277 changes: 277 additions & 0 deletions NeuroML2/LEMS_Sim_Microcircuit_0_2pcnt.xml

Large diffs are not rendered by default.

58,938 changes: 58,938 additions & 0 deletions NeuroML2/Microcircuit_0_2pcnt.net.nml

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions NeuroML2/regenerate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -ex

cd ../PyNN
python test_neuroml.py

cp *.nml LEMS*ml ../NeuroML2
66 changes: 61 additions & 5 deletions PyNN/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from helpers import boxplot
from helpers import compute_DC
from pyNN.random import RandomDistribution
from pyNN.space import RandomStructure, Cuboid
import math


class Network:
Expand Down Expand Up @@ -52,7 +54,7 @@ def __init__(self, sim_dict, net_dict, stim_dict=None):
self.data_path = sim_dict['data_path']


def setup_pyNN(self):
def setup_pyNN(self, extra_setup_params):
""" Reset and configure the simulator.
Where the simulator is NEST,
Expand All @@ -73,7 +75,8 @@ def setup_pyNN(self):
self.sim.setup(timestep=self.sim_resolution,
threads=self.sim_dict['local_num_threads'],
grng_seed=grng_seed,
rng_seeds=rng_seeds)
rng_seeds=rng_seeds,
**extra_setup_params)
if self.sim.rank() == 0:
print('Master seed: %i ' % master_seed)
print('Number of total processes: %i' % N_tp)
Expand Down Expand Up @@ -158,12 +161,65 @@ def create_populations(self):
[self.net_dict['neuron_params']['V0_mean'],
self.net_dict['neuron_params']['V0_sd']],
) # todo: specify rng
layer_structures = {}

x_dim_scaled = self.net_dict['x_dimension'] * math.sqrt(self.N_scaling)
z_dim_scaled = self.net_dict['z_dimension'] * math.sqrt(self.N_scaling)

default_cell_radius = 10 # for visualisation
default_input_radius = 5 # for visualisation
layer_thicknesses = self.net_dict["layer_thicknesses"]


for i, pop in enumerate(self.net_dict['populations']):
layer = pop[:-1]

y_offset = 0
if layer == 'L6': y_offset = layer_thicknesses['L6']/2
elif layer == 'L5': y_offset = layer_thicknesses['L6']+layer_thicknesses['L5']/2
elif layer == 'L4': y_offset = layer_thicknesses['L6']+layer_thicknesses['L5']+layer_thicknesses['L4']/2
elif layer == 'L23': y_offset = layer_thicknesses['L6']+layer_thicknesses['L5']+layer_thicknesses['L4']+layer_thicknesses['L23']/2
else:
raise Exception("Problem with %s"%layer)

layer_volume = Cuboid(x_dim_scaled,layer_thicknesses[layer],z_dim_scaled)
layer_structures[layer] = RandomStructure(layer_volume, origin=(0,y_offset,0))

parameters['i_offset'] = self.DC_amp_e[i] * 0.001 # pA --> nA
population = self.sim.Population(int(self.nr_neurons[i]),
neuron_model(**parameters),
structure=layer_structures[layer],
label=pop)
population.initialize(v=v_init)
# Store whether population is inhibitory or excitatory
population.annotate(type=pop[-1:])

population.annotate(radius=default_cell_radius)
population.annotate(structure=str(layer_structures[layer]))


try:
import opencortex.utils.color as occ
print('Adding color for %s'%pop)
if 'L23' in pop:
if 'E' in pop: color = occ.L23_PRINCIPAL_CELL
if 'I' in pop: color = occ.L23_INTERNEURON
if 'L4' in pop:
if 'E' in pop: color = occ.L4_PRINCIPAL_CELL
if 'I' in pop: color = occ.L4_INTERNEURON
if 'L5' in pop:
if 'E' in pop: color = occ.L5_PRINCIPAL_CELL
if 'I' in pop: color = occ.L5_INTERNEURON
if 'L6' in pop:
if 'E' in pop: color = occ.L6_PRINCIPAL_CELL
if 'I' in pop: color = occ.L6_INTERNEURON

population.annotate(color=color)
except Exception as e:
print(e)
# Don't worry about it, it's just metadata
pass

self.pops.append(population)

def create_devices(self):
Expand Down Expand Up @@ -230,7 +286,7 @@ def create_poisson(self):
for i, target_pop in enumerate(self.pops):
poisson = self.sim.Population(target_pop.size,
self.sim.SpikeSourcePoisson(rate=rate_ext[i]),
label="Input to {}".format(target_pop.label))
label="Input_to_{}".format(target_pop.label))
self.poisson.append(poisson)

def create_dc_generator(self):
Expand Down Expand Up @@ -369,7 +425,7 @@ def connect_dc_generator(self):
if self.stim_dict['dc_input']:
self.dc[i].inject_into(target_pop)

def setup(self):
def setup(self, extra_setup_params = {}):
"""
Execute subfunctions of the network.
Expand All @@ -378,7 +434,7 @@ def setup(self):
each other and with devices and input nodes.
"""
self.setup_pyNN()
self.setup_pyNN(extra_setup_params)
self.create_populations()
self.create_devices()
self.create_thalamic_input()
Expand Down
23 changes: 23 additions & 0 deletions PyNN/network_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,26 @@ def get_std_PSP_matrix(PSP_rel, number_of_pop):


net_dict.update(updated_dict)

total_cortical_thickness = 1500.0
N_full = net_dict['N_full']
N_E_total = N_full[0]+N_full[2]+N_full[4]+N_full[6]

dimensions_3D = {
'x_dimension': 1000,
'z_dimension': 1000,
#thalamus_offset = -300

'total_cortical_thickness': total_cortical_thickness,

# Have the thicknesses proportional to the numbers of E cells in each layer
'layer_thicknesses': {
'L23': total_cortical_thickness*N_full[0]/N_E_total,
'L4' : total_cortical_thickness*N_full[2]/N_E_total,
'L5' : total_cortical_thickness*N_full[4]/N_E_total,
'L6' : total_cortical_thickness*N_full[6]/N_E_total,
'thalamus' : 100
}
}

net_dict.update(dimensions_3D)
74 changes: 44 additions & 30 deletions PyNN/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
PyNN microcircuit example
---------------------------
Example file to run the microcircuit.
Based on original PyNEST version by Hendrik Rothe, Hannah Bos, Sacha van Albada; May 2016
Adapted for PyNN by Andrew Davison, December 2017
Test file for the microcircuit.
"""

Expand All @@ -18,29 +15,46 @@
from stimulus_params import stim_dict
import os


# Initialize the network and pass parameters to it.
tic = time.time()

net_dict['N_scaling'] = 0.1
net_dict['to_record'] = ['spikes', 'v']
sim_dict['data_path'] = os.path.join(os.getcwd(), 'results')


net = network.Network(sim_dict, net_dict, stim_dict)
toc = time.time() - tic
print("Time to initialize the network: %.2f s" % toc)
# Connect all nodes.
tic = time.time()
net.setup()
toc = time.time() - tic
print("Time to create the connections: %.2f s" % toc)
# Simulate.
tic = time.time()
net.simulate()
toc = time.time() - tic
print("Time to simulate: %.2f s" % toc)
tic = time.time()
net.write_data()
toc = time.time() - tic
print("Time to write data: %.2f s" % toc)
def setup(simulator='nest', N_scaling=0.1):
# Initialize the network and pass parameters to it.
tic = time.time()

net_dict['N_scaling'] = N_scaling

net_dict['to_record'] = ['spikes', 'v']
sim_dict['data_path'] = os.path.join(os.getcwd(), 'results')

sim_dict['simulator'] = simulator

net = network.Network(sim_dict, net_dict, stim_dict)
toc = time.time() - tic
print("Time to initialize the network: %.2f s" % toc)

# Connect all nodes.
tic = time.time()
extra_setup_params = {}
if simulator=='neuroml':
extra_setup_params['reference']='Microcircuit_%spcnt'%(str(N_scaling*100).replace('.','_'))

net.setup(extra_setup_params)
toc = time.time() - tic
print("Time to create the connections: %.2f s" % toc)
return net, net_dict, sim_dict


def run(net):

# Simulate.
tic = time.time()
net.simulate()
toc = time.time() - tic
print("Time to simulate: %.2f s" % toc)
tic = time.time()
net.write_data()
toc = time.time() - tic
print("Time to write data: %.2f s" % toc)


if __name__ == "__main__":
net, net_dict, sim_dict = setup(N_scaling=0.1)
run(net)
35 changes: 35 additions & 0 deletions PyNN/test_neuroml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
"""
PyNN microcircuit example
---------------------------
Export to NeuroML of the microcircuit.
"""

import time
import numpy as np
import network
from network_params import net_dict
from sim_params import sim_dict
from stimulus_params import stim_dict
import os

from test import setup

def export(net):

# Export.
tic = time.time()
print("Exporting...")
net.simulate()
net.sim.end()
toc = time.time() - tic
print("Time to export: %.2f s" % toc)



if __name__ == "__main__":
net, net_dict, sim_dict = setup(simulator='neuroml',
N_scaling=0.002)
export(net)

0 comments on commit 592a7df

Please sign in to comment.