diff --git a/bmtk/simulator/bionet/__init__.py b/bmtk/simulator/bionet/__init__.py index 0e24a44fb..e8b606b88 100644 --- a/bmtk/simulator/bionet/__init__.py +++ b/bmtk/simulator/bionet/__init__.py @@ -26,3 +26,33 @@ from bmtk.simulator.bionet.bionetwork import BioNetwork from bmtk.simulator.bionet.biosimulator import BioSimulator from bmtk.simulator.bionet.nrn import reset + +import sys +import argparse +import copy + + +class ArgumentParser(argparse.ArgumentParser): + """A Helper class for using argparse when calling script through nrniv, eg + $ nrniv -python run_bionet.py + + or + $ mpirun -np 2 nrniv -mpi -python run_bionet.py + + + """ + def parse_known_args(self, args=None, namespace=None): + if args is None: + args = copy.copy(sys.argv) + args = ArgumentParser.parse_nrniv_arg(args)[1:] + + return super().parse_known_args(args, namespace) + + @staticmethod + def parse_nrniv_arg(sys_argv): + if sys_argv[0].endswith('nrniv') or sys_argv[0].endswith('nrniv.exe'): + for i, cmd_opt in enumerate(sys_argv): + if cmd_opt == '-python': + return sys_argv[i+1:] + else: + return sys_argv \ No newline at end of file diff --git a/examples/README.md b/examples/README.md index fde538312..52c7abdb4 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,59 +1,115 @@ # Examples -This is the examples directory for the Brain Modeling Toolkit (bmtk) software package. Here you will find examples of -how to build, simulate, and plot simple brain network models of a variety of different levels-of-resolution using -bmtk. These examples are toy models for demonstration of how to use bmtk capable of running on a laptop/desktop -machine (for some examples of scientifically developed models that run on bmtk please see the following link: -https://alleninstitute.github.io/bmtk/examples.html). +The following directory contains examples of running various parts of the Brain Modeling Toolkit (BMTK), including all the necessacary files to build a network, simulate it with one or more types of stimulation, and record and plot results. Each directory highlights a different type of model or a different features available in BMTK. +The majority of the examples are small toy examples that can be readily ran on most laptops or desktops. The minimum they requires BMTK, but depending on the module may have additional requirements. -### BioNet (biophysically detailed) models -Each ```bio_*/``` directory uses the BioNet simulator to run morphologically detailed network simulations using the NEURON -simulation tool. +## Directory Structure + +Most of the examples have a prefix based on which simulation engine is being used to run the model. + +- **bio_\*/** - Examples of biophysically detailed network models that run using [BioNet](https://alleninstitute.github.io/bmtk/bionet.html). +- **point_\*/** - Examples of point-neuron network models that run using [PointNet](https://alleninstitute.github.io/bmtk/pointnet.html) +- **pop_\*/** - Examples of populations based rates models that run using [PopNet](https://alleninstitute.github.io/bmtk/popnet.html) +- **pop_\*/** - Examples that uses [FilterNet](https://alleninstitute.github.io/bmtk/filternet.html) to convert visual or auditory stimuli into spike-models based on spatio-temporal statistics. + +Other important directories + +- **bio_components/** - External files shared by BioNet models and simulations (Morphologies, parameters, etc.). +- **point_components/** - External files shared by PointNet models and simulations +- **pop_components/** - External files shared by PopNet models and simulations + +## Running Simulations + +### BioNet + +**(Prerequisite) Compiling NEURON mechanisms** + +The components for the BioNet examples are located in ../examples/bio_components. If the NEURON mechanisms have not already been compiled, the following should compile the NEURON mechanisms and place them in another folder in /mechanisms. + +```bash +$ cd ../bio_components/mechanisms +$ nrnivmodl modfiles +$ cd - +``` +Failure to compile the mechanisms results in an error such as: +``` +**Warning**: NEURON mechanisms not found in ./../bio_components/mechanisms. + [...] + ValueError: argument not a density mechanism name +``` +--- + + +To run a full simulation of the network on a single using the default simulation configuration, run the following on a command line: + +```bash +$ python run_bionet.py +``` + +or to use a different SONATA configuration file (eg `.json`) + +```bash +$ python run_bionet.py .json +``` + +If using a machine or cluster with mutiple cores you may use `mpirun` (if available on machine) to run the simulation in `N` cores with the following command: + +```bash +$ mpirun -np nrniv -mpi -python run_bionet.py .json +``` + +When simulation has completed it will create an *output* folder with logs, simulated spike trains, and any other recorded variables as set in the `` + + +### PointNet + +Executing a simulation inside the model directory can be done in the command line using either -```bio_components/``` contains external parameter and model files which are shared by most of the BioNet -examples (note: this location can be changed in the each example's ```config.circuit.json``` file). This also includes -a ```bio_components/mechanism/``` directory which require extra compilation for the Allen Institute models. To run -the BioNet examples one will have to run the following commands to compile the extra neuronal mechanisms: ```bash -$ cd examples/bio_components/mechanisms -$ nrnivmodl modfiles +$ python run_pointnet.py ``` -For more information on using BioNet see the following: https://alleninstitute.github.io/bmtk/bionet.html +Which will run the default simulation configurations. Or to run a different simulation setup you can specify a specific `.json` file path: -### PointNet (point-neuron) models +```bash +$ python run_pointnet.py .json +``` -```point_*/``` directories contain examples that use the PointNet simulator to run point-neuron type models, including -Allen Institute's Generalized Integrate-and-Fire (GLIF) models. Using these examples will require installing the -NEST simulator. +If using a machine or cluster with mutiple cores you may use `mpirun` (if available on machine) to run the simulation in `N` cores with the following command: -```point_components/``` directory contains model files that are shared by many of the different PointNet examples. This -can be changed in each examples' ```config.circuit.json``` files. +```bash +$ mpirun -np python run_pointnet.py .json +``` -For more information on using PointNet see the following: https://alleninstitute.github.io/bmtk/pointnet.html +When simulation has completed it will create an *output* folder with logs, simulated spike trains, and any other recorded variables as set in the `` +### PopNet -### PopNet (population firing rates) models +Executing a simulation inside the model directory can be done in the command line using either -```pop_*/```directories contain examples that use the PopNet simulator to run population level firing-rate model -simulations. This requires installing the DiPDE simulator. +```bash +$ python run_popnet.py +``` -```pop_components/``` directory contains model files that are shared by many of the different PopNet examples. This -can be changed in each examples' ```config.circuit.json``` files. +Which will run the default simulation configurations. Or to run a different simulation setup you can specify a specific `.json` file path: -For more information on using PopNet see the following: https://alleninstitute.github.io/bmtk/popnet.html +```bash +$ python run_popnet.py .json +``` +If using a machine or cluster with mutiple cores you may use `mpirun` (if available on machine) to run the simulation in `N` cores with the following command: -### FilterNet (LNP) models +```bash +$ mpirun -np python run_popnet.py .json +``` -```filter_*/``` directories contain examples that use the PopNet simulator to run filter models simulations to convert -visual stimuli into spike-trains. +When simulation has completed it will create an *output* folder with logs, simulated spike trains, and any other recorded variables as set in the `` -```filter_components/``` directory contains model files that are shared by many of the different FilterNet examples. This -can be changed in each examples' ```config.circuit.json``` files. -For more information on using PopNet see the following: https://alleninstitute.github.io/bmtk/filternet.html +## Updating Simulation Parameters +BMTK uses the *config.simulation_\*.json* files to determine simulation parameters like run-time, time delta, stimulus, recorded varaibles, and a number of other important factors. These are simple json files that can be edited with most text editors. +Please see our [BMTK User Guide](https://alleninstitute.github.io/bmtk/user_guide.html) or the [SONATA Developers Guide](https://github.com/AllenInstitute/sonata/blob/master/docs/SONATA_DEVELOPER_GUIDE.md) for a description of the configuration format and a list of features and attributes available in BMTK. \ No newline at end of file diff --git a/examples/bio_14cells/README.md b/examples/bio_14cells/README.md index 55f4a1bdc..96bb2238d 100644 --- a/examples/bio_14cells/README.md +++ b/examples/bio_14cells/README.md @@ -1,50 +1,13 @@ -# 14 Cell network +# 14 Cell Biophysical Network Model -A small network of 14 cells - 10 multi-compartment biophysically detailed cells and 4 point integrate-and-fire cells - -called V1. Recieves input from two networks of virtual cells (spike-trains) - LGN and TW. +A small network of 14 cells; 10 multi-compartment biophysically detailed cells and 4 point integrate-and-fire cells, called V1. Recieves input from two networks of virtual cells (spike-trains) - LGN and TW. -Uses the BioNet simulator (requires NEURON) +**Requirements** +- BMTK +- NEURON 8.0+ -## Compiling NEURON mechanisms -The components for the BioNet examples are located in /examples/bio_components. If the NEURON mechanisms have not already been compiled, the following should compile the NEURON mechanisms and place them in another folder in /mechanisms. -```bash -$ cd ../bio_components/mechanisms -$ nrnivmodl modfiles -$ cd - -``` -Failure to compile the mechanisms results in an error such as: -``` -**Warning**: NEURON mechanisms not found in ./../bio_components/mechanisms. - [...] - ValueError: argument not a density mechanism name -``` - - -## Running: -To run a simulation, install bmtk and run the following: -``` -$ python run_bionet.py config.simulation.json -``` -If successful, will create a *output* directory containing log, V1 spike trains and recorded cell variables. - -## The Network: -The network files have already been built and stored as SONATA files in the *network/* directory. The bmtk Builder -script used to create the network files is *build_network.py*. To adjust the parameters and/or topology of the network -change this file and run: -``` -$ python build_network.py -``` -This will overwrite the existing files in the network directory. Note that there is some randomness in how the network -is built, so expect (slightly) different simulation results everytime the network is rebuilt. - -## Simulation Parameters -Parameters to run the simulation, including run-time, inputs, recorded variables, and networks are stored in config.json -and can modified with a text editor. - -## Plotting results -``` -$ python plot_output.py -``` +## Running the Simulation +[See Instructions for running BioNet Simulation](../README.md#bionet) diff --git a/examples/bio_14cells/run_bionet.py b/examples/bio_14cells/run_bionet.py index c2e8fd834..4b2bbc143 100644 --- a/examples/bio_14cells/run_bionet.py +++ b/examples/bio_14cells/run_bionet.py @@ -1,6 +1,3 @@ -"""Simulates an example network of 450 cell receiving two kinds of exernal input as defined in the configuration file""" -import os -import sys from bmtk.simulator import bionet @@ -15,18 +12,9 @@ def run(config_path): if __name__ == '__main__': - # Find the appropriate config.json file - config_path = None - if __file__ != sys.argv[-1]: - config_path = sys.argv[-1] - if not os.path.exists(config_path): - raise AttributeError('configuration file {} does not exist.'.format(config_path)) - else: - for cfg_path in ['config.json', 'config.simulation.json']: - if os.path.exists(cfg_path): - config_path = cfg_path - break - else: - raise AttributeError('Could not find configuration json file.') + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) - run(config_path) diff --git a/examples/bio_1cell/README.md b/examples/bio_1cell/README.md index d8950756d..6aac3788c 100644 --- a/examples/bio_1cell/README.md +++ b/examples/bio_1cell/README.md @@ -1,56 +1,24 @@ -# 1 Cell network - -A single excitatory cell that is stimulated by current clamps or virtual synapses - -Uses the BioNet simulator (requires NEURON) - -## Compiling NEURON mechanisms -The components for the BioNet examples are located in /examples/bio_components. If the NEURON mechanisms have not already been compiled, the following should compile the NEURON mechanisms and place them in another folder in /mechanisms. - -```bash -$ cd ../bio_components/mechanisms -$ nrnivmodl modfiles -$ cd - -``` -Failure to compile the mechanisms results in an error such as: -``` -**Warning**: NEURON mechanisms not found in ./../bio_components/mechanisms. - [...] - ValueError: argument not a density mechanism name -``` - - -## Running: -To run a simulation using a current clamp, install bmtk and run the following: -```bash -$ python run_bionet.py config.simulation_iclamp.json -``` -If successful, will create a *output_iclamp* directory containing log, V1 spike trains and recorded cell variables. - -You can also use the "virt" network which will use a virtual cell to stimulate the biophysical cell with synaptic -stimulation. - -```bash -$ python run_bionet.py config.simulation_syns.json -``` - -## The Network: -The network files have already been built and stored as SONATA files in the *network/* directory. The bmtk Builder -script used to create the network files is *build_network.py*. To adjust the parameters and/or topology of the network -change this file and run: -``` -$ python build_network.py -``` -This will overwrite the existing files in the network directory. Note that there is some randomness in how the network -is built, so expect (slightly) different simulation results everytime the network is rebuilt. - -## Simulation Parameters -Parameters to run the simulation, including run-time, inputs, recorded variables, and networks are stored in config.json -and can modified with a text editor. - -## Plotting results -``` -$ python plot_output.py -``` +# 1 Cell Biophysical Network Model +An Example of a biophysical network containing a single biophysical morphologically detailed cell. The cell recieves stimuli either from a current clamp at the soma (using *config.simulation_iclamp.json*) or with synaptic stimuli using virtual cells with predefined spike-trains (*config.simulation_syns.json*). + + +## Requirements +- BMTK +- NEURON 8.0+ + + +## Important Files + +- **inputs/** - folder contains pre-generated spike-train files use as stimuli for *config.simulation_iclamp.json* +- **network/** - Folder containing network model. +- **config.simulation_iclamp.json** - Configuration file with instructions for running network driven by current clamp stimuli attached at the soma. +- **config.simulation_syns.json** - Configuration file with instructions for running network driven by virtual cells that synaptical stimulate our network. +- **build_network.py** - script to rebuild the "network". WARNING: By default will override the **network/** folder. +- **run_bionet.py** - Main script for running BioNet simulation of the network. + + +## Running the Simulation + +[See Instructions for running BioNet Simulation](../README.md#bionet) diff --git a/examples/bio_1cell/output_iclamp/membrane_potential.h5 b/examples/bio_1cell/output_iclamp/membrane_potential.h5 index e47cbfd2f..19bae7115 100644 Binary files a/examples/bio_1cell/output_iclamp/membrane_potential.h5 and b/examples/bio_1cell/output_iclamp/membrane_potential.h5 differ diff --git a/examples/bio_1cell/output_iclamp/spikes.csv b/examples/bio_1cell/output_iclamp/spikes.csv index 6bcc015e6..a6d2513c2 100644 --- a/examples/bio_1cell/output_iclamp/spikes.csv +++ b/examples/bio_1cell/output_iclamp/spikes.csv @@ -1,21 +1,21 @@ timestamps population node_ids 579.9000000001204 biocell 0 -2230.499999999449 biocell 0 -2119.499999999045 biocell 0 -2008.2999999988208 biocell 0 -1896.9999999989225 biocell 0 -1785.5999999990238 biocell 0 -1674.0999999991252 biocell 0 -1562.5999999992266 biocell 0 -1451.2999999993278 biocell 0 -1340.4999999994286 biocell 0 -1230.6999999995285 biocell 0 -1123.0999999996263 biocell 0 -1019.3999999997206 biocell 0 -922.0999999998093 biocell 0 -833.9999999998893 biocell 0 +2229.8999999994467 biocell 0 +2118.899999999043 biocell 0 +2007.7999999988217 biocell 0 +1896.6999999989227 biocell 0 +1785.299999999024 biocell 0 +1673.8999999991254 biocell 0 +1562.3999999992268 biocell 0 +1451.099999999328 biocell 0 +1340.2999999994288 biocell 0 +1230.5999999995286 biocell 0 +1122.9999999996264 biocell 0 +1019.2999999997209 biocell 0 +921.9999999998092 biocell 0 +833.8999999998894 biocell 0 757.1999999999591 biocell 0 691.400000000019 biocell 0 633.7000000000714 biocell 0 -2341.4999999998527 biocell 0 -2452.300000000256 biocell 0 +2340.79999999985 biocell 0 +2451.7000000002536 biocell 0 diff --git a/examples/bio_1cell/output_iclamp/spikes.h5 b/examples/bio_1cell/output_iclamp/spikes.h5 index f43b56f87..130887058 100644 Binary files a/examples/bio_1cell/output_iclamp/spikes.h5 and b/examples/bio_1cell/output_iclamp/spikes.h5 differ diff --git a/examples/bio_1cell/run_bionet.py b/examples/bio_1cell/run_bionet.py index fbaebdea1..b992aacb5 100644 --- a/examples/bio_1cell/run_bionet.py +++ b/examples/bio_1cell/run_bionet.py @@ -1,6 +1,3 @@ -"""Simulates an example network of 450 cell receiving two kinds of exernal input as defined in the configuration file""" -import os -import sys from bmtk.simulator import bionet @@ -15,10 +12,8 @@ def run(config_path): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - config_path = sys.argv[-1] - run(config_path) - else: - # run('config.simulation_syns.json') - run('config.simulation_iclamp.json') + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation_iclamp.json', help='Path to the SONATA configuration file') + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/bio_300cells_nosurround/README.md b/examples/bio_300cells_nosurround/README.md index 1e4134bb2..a65383a09 100644 --- a/examples/bio_300cells_nosurround/README.md +++ b/examples/bio_300cells_nosurround/README.md @@ -1,4 +1,4 @@ -# 300 cells network +# 300 cell Biophysical Network Model This is a small example network of a 300 cell simulation based on the 45,000 mouse layer 4 network described in Arkhipov et. al. 2018. Of the cells 300 are biophysically detailed multicompartment cells downloaded from the diff --git a/examples/bio_300cells_nosurround/run_bionet.py b/examples/bio_300cells_nosurround/run_bionet.py index 73aff102f..d35bc3ed5 100644 --- a/examples/bio_300cells_nosurround/run_bionet.py +++ b/examples/bio_300cells_nosurround/run_bionet.py @@ -1,5 +1,3 @@ -"""Simulates an example network of 450 cell receiving two kinds of exernal input as defined in the configuration file""" -import sys, os from bmtk.simulator import bionet @@ -15,18 +13,8 @@ def run(config_file): if __name__ == '__main__': - # Find the appropriate config.json file - config_path = None - if __file__ != sys.argv[-1]: - config_path = sys.argv[-1] - if not os.path.exists(config_path): - raise AttributeError('configuration file {} does not exist.'.format(config_path)) - else: - for cfg_path in ['config.json', 'config.simulation.json']: - if os.path.exists(cfg_path): - config_path = cfg_path - break - else: - raise AttributeError('Could not find configuration json file.') + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation_iclamp.json', help='Path to the SONATA configuration file') - run(config_path) + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/bio_450cells/README.md b/examples/bio_450cells/README.md index 8be70b779..9d4fcc6c3 100644 --- a/examples/bio_450cells/README.md +++ b/examples/bio_450cells/README.md @@ -1,4 +1,4 @@ -# 450 cells network +# 450 Cell Biophysical Network Model This is a small example network of a 450 cell simulation based on the 45,000 mouse layer 4 network described in Arkhipov et. al. 2018. Of the cells 180 are biophysically detailed multicompartment cells downloaded from the diff --git a/examples/bio_450cells/run_bionet.py b/examples/bio_450cells/run_bionet.py index 73aff102f..acc823a98 100644 --- a/examples/bio_450cells/run_bionet.py +++ b/examples/bio_450cells/run_bionet.py @@ -1,5 +1,3 @@ -"""Simulates an example network of 450 cell receiving two kinds of exernal input as defined in the configuration file""" -import sys, os from bmtk.simulator import bionet @@ -15,18 +13,8 @@ def run(config_file): if __name__ == '__main__': - # Find the appropriate config.json file - config_path = None - if __file__ != sys.argv[-1]: - config_path = sys.argv[-1] - if not os.path.exists(config_path): - raise AttributeError('configuration file {} does not exist.'.format(config_path)) - else: - for cfg_path in ['config.json', 'config.simulation.json']: - if os.path.exists(cfg_path): - config_path = cfg_path - break - else: - raise AttributeError('Could not find configuration json file.') + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', help='Path to the SONATA configuration file') - run(config_path) + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/bio_450cells_compact/README.md b/examples/bio_450cells_compact/README.md index 6445eb9ab..df395f5ca 100644 --- a/examples/bio_450cells_compact/README.md +++ b/examples/bio_450cells_compact/README.md @@ -1,4 +1,4 @@ -# 450 cells network +# 450 Cell Biophysical Network Model This is a small example network of a 450 cell simulation based on the 45,000 mouse layer 4 network described in Arkhipov et. al. 2018. Of the cells 180 are biophysically detailed multicompartment cells downloaded from the diff --git a/examples/bio_450cells_compact/run_bionet.py b/examples/bio_450cells_compact/run_bionet.py index 19967bfe2..d00b70ac5 100644 --- a/examples/bio_450cells_compact/run_bionet.py +++ b/examples/bio_450cells_compact/run_bionet.py @@ -16,18 +16,8 @@ def run(config_file): if __name__ == '__main__': - # Find the appropriate config.json file - config_path = None - if __file__ != sys.argv[-1]: - config_path = sys.argv[-1] - if not os.path.exists(config_path): - raise AttributeError('configuration file {} does not exist.'.format(config_path)) - else: - for cfg_path in ['config.json', 'config.simulation.json']: - if os.path.exists(cfg_path): - config_path = cfg_path - break - else: - raise AttributeError('Could not find configuration json file.') - - run(config_path) + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) \ No newline at end of file diff --git a/examples/bio_450cells_replay/README.md b/examples/bio_450cells_replay/README.md new file mode 100644 index 000000000..e6e606383 --- /dev/null +++ b/examples/bio_450cells_replay/README.md @@ -0,0 +1 @@ +# 450 Cell Biophysical Network Model with Disconnected Replay diff --git a/examples/bio_450cells_replay/run_bionet.py b/examples/bio_450cells_replay/run_bionet.py index a907f7d73..28a172c0a 100644 --- a/examples/bio_450cells_replay/run_bionet.py +++ b/examples/bio_450cells_replay/run_bionet.py @@ -1,6 +1,3 @@ -"""Simulates an example network of 450 cell receiving two kinds of exernal input as defined in the configuration file""" -import sys, os - from bmtk.simulator import bionet @@ -16,9 +13,8 @@ def run(config_file): if __name__ == '__main__': - # run('config.recurrent.json') - # run('config.feedforward.json') - run('config.all_cells.json') - # run('config.w_extern.json') - # run('config.biophys_cells.json') - # run('config.pv_cells.json') + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.all_cells.json', help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/bio_advanced_stimuli/README.md b/examples/bio_advanced_stimuli/README.md index d726900e4..73e792d78 100644 --- a/examples/bio_advanced_stimuli/README.md +++ b/examples/bio_advanced_stimuli/README.md @@ -1,30 +1,7 @@ # BioNet input simulation -A simple non-connected network of 5 neuron (visual cortex mouse models taken from the Allen Cell-Types -database) network with various inputs including current clamp, external synaptic inputs, and extracellular -electrode stimulation. +A simple non-connected network of 5 neuron (visual cortex mouse models taken from the Allen Cell-Types database) network with various inputs including current clamp, external synaptic inputs, and extracellular electrode stimulation. -## Building the network -Network files are stored in network/bio_nodes.h5 and network/bio_node_types.csv. In the same directory you'll -also find network files representing virtual input (virt*). You can rebuild the network by running - -```bash -$ python build_network.py -``` -## Compiling NEURON mechanisms -The components for the BioNet examples are located in /examples/bio_components. If the NEURON mechanisms have not already been compiled, the following should compile the NEURON mechanisms and place them in another folder in /mechanisms. - -```bash -$ cd ../bio_components/mechanisms -$ nrnivmodl modfiles -$ cd - -``` -Failure to compile the mechanisms results in an error such as: -``` -**Warning**: NEURON mechanisms not found in ./../bio_components/mechanisms. - [...] - ValueError: argument not a density mechanism name -``` ## Simulations ### Current clamp Stimulates the network using a series of current clamps at all the biophysically detailed cell models diff --git a/examples/bio_advanced_stimuli/run_bionet.py b/examples/bio_advanced_stimuli/run_bionet.py index 46223dbf8..b571b4308 100644 --- a/examples/bio_advanced_stimuli/run_bionet.py +++ b/examples/bio_advanced_stimuli/run_bionet.py @@ -1,6 +1,3 @@ -"""Simulates an example network of 450 cell receiving two kinds of exernal input as defined in the configuration file""" -import sys - from bmtk.simulator import bionet from bmtk.analyzer.compartment import plot_traces @@ -14,18 +11,20 @@ def run(config_file): sim.run() plot_traces(config_file=config_file, report_name='membrane_potential', population='bio') + bionet.nrn.quit_execution() if __name__ == '__main__': - if __file__ != sys.argv[-1]: - run(sys.argv[-1]) - else: - # Make sure to run only one at a time - run('config.simulation_iclamp.json') # Current clamp stimulation - # run('config.simulation_iclamp.aslist.json') - # run('config.simulation_iclamp.csv.json') - # run('config.simulation_iclamp.nwb.json') - - # run('config.simulation_xstim.json') # Extracellular electrode stimulation - # run('config.simulation_spikes.json') # Synaptic stimulation with external virtual cells - # run('config.simulation_spont_activity.json') # Spontaneous synaptic activity + # default_config = 'config.simulation_iclamp.json' + # default_config = 'config.simulation_iclamp.aslist.json' + # default_config = 'config.simulation_iclamp.csv.json' + # default_config = 'config.simulation_iclamp.nwb.json' + default_config = 'config.simulation_xstim.json' + # default_config = 'config.simulation_spikes.json' + # default_config = 'config.simulation_spont_activity.json' + + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default=default_config, help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/bio_all_active_sweep/run_bionet.py b/examples/bio_all_active_sweep/run_bionet.py index 2b44e3f18..c2acd9681 100644 --- a/examples/bio_all_active_sweep/run_bionet.py +++ b/examples/bio_all_active_sweep/run_bionet.py @@ -1,9 +1,6 @@ -import sys - from bmtk.simulator import bionet from bmtk.simulator.bionet.io_tools import io from bmtk.simulator.bionet.default_setters.cell_models import set_params_allactive -# from bmtk.simulator.bionet.pyfunction_cache import add_cell_processor from bmtk.simulator.bionet import model_processing from bmtk.analyzer.compartment import plot_traces @@ -15,7 +12,6 @@ def aibs_allactive_fullaxon(hobj, cell, dynamics_params): # The main difference is that in the original the axon is cut and replaced by a # stub. Here we leave the full axon intact io.log_info('Initializing Cell Model Params') - # fix_axon_allactive(hobj) set_params_allactive(hobj, dynamics_params) return hobj @@ -33,11 +29,8 @@ def run(config_path): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - config_path = sys.argv[-1] - run(config_path) - else: - # run('config.simulation_syns.json') - # run('config.simulation.491766131_stubaxon.sweep35.json') - run('config.simulation.491766131_fullaxon.sweep35.json') + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.491766131_fullaxon.sweep35.json', help='Path to the SONATA configuration file') + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/bio_comsol/run_bionet.py b/examples/bio_comsol/run_bionet.py index d924f0354..2ffc5f640 100644 --- a/examples/bio_comsol/run_bionet.py +++ b/examples/bio_comsol/run_bionet.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -import sys from bmtk.simulator import bionet def run(config_file): @@ -13,9 +11,12 @@ def run(config_file): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - run(sys.argv[-1]) - else: - run('config.comsol_stat.json') - # run('config.comsol_stat2.json') - # run('config.comsol_tdep.json') + default_config = 'config.comsol_stat.json' + default_config = 'config.comsol_stat2.json' + default_config = 'config.comsol_tdep.json' + + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default=default_config, help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/bio_intfire_ring/run_bionet.py b/examples/bio_intfire_ring/run_bionet.py index 1bb885db7..3de3cbe34 100644 --- a/examples/bio_intfire_ring/run_bionet.py +++ b/examples/bio_intfire_ring/run_bionet.py @@ -1,4 +1,3 @@ -import sys from bmtk.simulator import bionet @@ -13,7 +12,8 @@ def run(config_file): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - run(sys.argv[-1]) - else: - run('config.simulation.json') + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/bio_neuropixels/run_bmtk.py b/examples/bio_neuropixels/run_bmtk.py index bbe4e4787..fcb279191 100644 --- a/examples/bio_neuropixels/run_bmtk.py +++ b/examples/bio_neuropixels/run_bmtk.py @@ -1,9 +1,6 @@ -"""Simulates an example network of 450 cell receiving two kinds of exernal input as defined in the configuration file""" -import sys, os from bmtk.simulator import bionet - def run(config_file): conf = bionet.Config.from_json(config_file, validate=True) conf.build_env() @@ -16,11 +13,12 @@ def run(config_file): if __name__ == '__main__': - # Find the appropriate config.json file - if __file__ != sys.argv[-1]: - config_path = sys.argv[-1] - run(config_path) - else: - # run('config.simulation.sample.json') - # run('config.simulation.units_map.json') - run('config.simulation.multi_session.json') + # default_config = 'config.simulation.sample.json' + # default_config = 'config.simulation.units_map.json' + default_config = 'config.simulation.multi_session.json' + + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default=default_config, help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/bio_on_off/run_bionet.py b/examples/bio_on_off/run_bionet.py index 1bb885db7..3de3cbe34 100644 --- a/examples/bio_on_off/run_bionet.py +++ b/examples/bio_on_off/run_bionet.py @@ -1,4 +1,3 @@ -import sys from bmtk.simulator import bionet @@ -13,7 +12,8 @@ def run(config_file): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - run(sys.argv[-1]) - else: - run('config.simulation.json') + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/bio_simulated_annealing/run_bionet.py b/examples/bio_simulated_annealing/run_bionet.py index 0c1e3ff84..5473692a3 100644 --- a/examples/bio_simulated_annealing/run_bionet.py +++ b/examples/bio_simulated_annealing/run_bionet.py @@ -144,7 +144,8 @@ def run_iteration(config_file): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - run_iteration(sys.argv[-1]) - else: - run_iteration('config.simulation.json') + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run_iteration(args.config_path) diff --git a/examples/bio_stp_models/run_bionet.py b/examples/bio_stp_models/run_bionet.py index 9067d1fe2..eac3291f3 100644 --- a/examples/bio_stp_models/run_bionet.py +++ b/examples/bio_stp_models/run_bionet.py @@ -1,5 +1,3 @@ -"""Simulates an example network of 450 cell receiving two kinds of exernal input as defined in the configuration file""" -import sys from bmtk.simulator import bionet @@ -15,7 +13,8 @@ def run(config_file): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - run(sys.argv[-1]) - else: - run('config.simulation.json') + parser = bionet.ArgumentParser(description='Run BioNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/filter_graitings/run_filternet.py b/examples/filter_graitings/run_filternet.py index 8846a8e4d..126ccf77e 100644 --- a/examples/filter_graitings/run_filternet.py +++ b/examples/filter_graitings/run_filternet.py @@ -1,4 +1,4 @@ -import sys +import argparse from bmtk.simulator import filternet @@ -12,7 +12,9 @@ def run(config_file): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - run(sys.argv[-1]) - else: - run('config.simulation.json') + parser = argparse.ArgumentParser(description='Run FilterNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/filter_graitings_preset/run_filternet.py b/examples/filter_graitings_preset/run_filternet.py index 8846a8e4d..835deefc1 100644 --- a/examples/filter_graitings_preset/run_filternet.py +++ b/examples/filter_graitings_preset/run_filternet.py @@ -1,4 +1,5 @@ -import sys +import argparse + from bmtk.simulator import filternet @@ -12,7 +13,9 @@ def run(config_file): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - run(sys.argv[-1]) - else: - run('config.simulation.json') + parser = argparse.ArgumentParser(description='Run FilterNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) \ No newline at end of file diff --git a/examples/filter_looming/run_filternet.py b/examples/filter_looming/run_filternet.py index 8846a8e4d..126ccf77e 100644 --- a/examples/filter_looming/run_filternet.py +++ b/examples/filter_looming/run_filternet.py @@ -1,4 +1,4 @@ -import sys +import argparse from bmtk.simulator import filternet @@ -12,7 +12,9 @@ def run(config_file): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - run(sys.argv[-1]) - else: - run('config.simulation.json') + parser = argparse.ArgumentParser(description='Run FilterNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/filter_movie/config.simulation.json b/examples/filter_movie/config.simulation.json new file mode 100644 index 000000000..6a0682446 --- /dev/null +++ b/examples/filter_movie/config.simulation.json @@ -0,0 +1,43 @@ +{ + "manifest": { + "$BASE_DIR": ".", + "$OUTPUT_DIR": "$BASE_DIR/output", + "$INPUT_DIR": "$BASE_DIR/movie_files" + }, + + "run": { + "tstop": 2050.0, + "dt": 0.1 + }, + + "target_simulator": "LGNModel", + + "conditions": { + "jitter_lower": 1.0, + "jitter_upper": 1.0 + }, + + "inputs": { + "movie_input": { + "input_type": "movie", + "module": "movie", + "data_file": "$INPUT_DIR/test_scene.npy", + "frame_rate": 1000.0, + "evaluation_options": { + "t_min": 3.0, + "t_max": 4.0 + } + } + }, + + "output": { + "output_dir": "$OUTPUT_DIR", + "log_file": "log.txt", + "rates_csv": "rates.csv", + "spikes_csv": "spikes.csv", + "spikes_h5": "spikes.h5", + "overwrite_output_dir": true + }, + + "network": "config.circuit.json" +} diff --git a/examples/filter_movie/run_filternet.py b/examples/filter_movie/run_filternet.py index 8846a8e4d..126ccf77e 100644 --- a/examples/filter_movie/run_filternet.py +++ b/examples/filter_movie/run_filternet.py @@ -1,4 +1,4 @@ -import sys +import argparse from bmtk.simulator import filternet @@ -12,7 +12,9 @@ def run(config_file): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - run(sys.argv[-1]) - else: - run('config.simulation.json') + parser = argparse.ArgumentParser(description='Run FilterNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/point_120cells/run_pointnet.py b/examples/point_120cells/run_pointnet.py index 16eaa7ae0..441d44a62 100644 --- a/examples/point_120cells/run_pointnet.py +++ b/examples/point_120cells/run_pointnet.py @@ -1,5 +1,4 @@ -import os, sys - +import argparse from bmtk.simulator import pointnet @@ -13,6 +12,13 @@ def run(config_file): if __name__ == '__main__': - # Find the appropriate config.json file - run('config.simulation.json') - # run('config.simulation_perturbations.json') + default_config = 'config.simulation.json' + # default_config = 'config.simulation_perturbations.json' + + parser = argparse.ArgumentParser(description='Run PointNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default=default_config, + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) + diff --git a/examples/point_120cells_nestml/run_pointnet.py b/examples/point_120cells_nestml/run_pointnet.py index 16eaa7ae0..1c3dad0c5 100644 --- a/examples/point_120cells_nestml/run_pointnet.py +++ b/examples/point_120cells_nestml/run_pointnet.py @@ -1,5 +1,4 @@ -import os, sys - +import argparse from bmtk.simulator import pointnet @@ -13,6 +12,9 @@ def run(config_file): if __name__ == '__main__': - # Find the appropriate config.json file - run('config.simulation.json') - # run('config.simulation_perturbations.json') + parser = argparse.ArgumentParser(description='Run PointNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/point_450cells/run_pointnet.py b/examples/point_450cells/run_pointnet.py index 2d51cfdc6..c7e2c34a5 100644 --- a/examples/point_450cells/run_pointnet.py +++ b/examples/point_450cells/run_pointnet.py @@ -1,7 +1,8 @@ +import argparse from bmtk.simulator import pointnet -def main(config_file): +def run(config_file): configure = pointnet.Config.from_json(config_file) configure.build_env() @@ -11,4 +12,9 @@ def main(config_file): if __name__ == '__main__': - main('config.simulation.json') + parser = argparse.ArgumentParser(description='Run PointNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/point_450glifs/run_pointnet.py b/examples/point_450glifs/run_pointnet.py index 5cb72c043..c38b52ae0 100644 --- a/examples/point_450glifs/run_pointnet.py +++ b/examples/point_450glifs/run_pointnet.py @@ -12,13 +12,9 @@ def run(config_file): if __name__ == '__main__': - parser = argparse.ArgumentParser(description='Run point network simulation.') - parser.add_argument('config_file', - type=str, - nargs='?', - default='config.simulation.json', - help='Path to the configuration file' - ) - args = parser.parse_args() + parser = argparse.ArgumentParser(description='Run PointNet network simulation.') + parser.add_argument('config_file', type=str, nargs='?', default='config.simulation.json', + help='Path to the configuration file') + args = parser.parse_known_args() run(args.config_file) diff --git a/examples/point_iclamp/build_network.py b/examples/point_iclamp/build_network.py new file mode 100644 index 000000000..5453e95bf --- /dev/null +++ b/examples/point_iclamp/build_network.py @@ -0,0 +1,106 @@ +import numpy as np + +from bmtk.builder import NetworkBuilder +from bmtk.builder.auxi.node_params import positions_columinar + + +lif_models = { + 'LIF_exc': { + 'N': 10, + 'ei': 'e', + 'pop_name': 'LIF_exc', + 'model_type': 'point_neuron', + 'model_template': 'nest:iaf_psc_delta', + 'dynamics_params': 'iaf_psc_delta_exc.json' + }, + # 'LIF_inh': { + # 'N': 40, + # 'ei': 'i', + # 'pop_name': 'LIF_inh', + # 'model_type': 'point_neuron', + # 'model_template': 'nest:iaf_psc_delta', + # 'dynamics_params': 'iaf_psc_delta_inh.json' + # } +} + +input_network_model = { + 'input_network': { + 'N': 100, + 'ei': 'e', + 'pop_name': 'input_network', + 'model_type': 'virtual' + } +} + + +def random_connections(source, target, p=0.1): + sid = source['node_id'] # Get source id + tid = target['node_id'] # Get target id + + # Avoid self-connections. + if sid == tid: + return None + + return np.random.binomial(1, p) # nsyns + + +def build_cortex_network(): + cortex = NetworkBuilder('cortex') + for model in lif_models: + params = lif_models[model].copy() + positions = positions_columinar(N=lif_models[model]['N'], center=[0, 10.0, 0], max_radius=50.0, height=200.0) + cortex.add_nodes( + x=positions[:, 0], + y=positions[:, 1], + z=positions[:, 2], + **params + ) + + # cortex.add_edges( + # source={'ei': 'e'}, + # connection_rule=random_connections, + # connection_params={'p': 0.1}, + # syn_weight=2.0, + # delay=1.5, + # dynamics_params='ExcToInh.json', + # model_template='static_synapse' + # ) + + # cortex.add_edges( + # source={'ei': 'i'}, + # connection_rule=random_connections, + # connection_params={'p': 0.1}, + # syn_weight=-1.5, + # delay=1.5, + # dynamics_params='InhToExc.json', + # model_template='static_synapse' + # ) + + cortex.build() + cortex.save(output_dir='network') + + return cortex + + +def build_thalamus_network(cortex): + thalamus = NetworkBuilder('thalamus') + thalamus.add_nodes(**input_network_model['input_network']) + + thalamus.add_edges( + target=cortex.nodes(), + connection_rule=random_connections, + connection_params={'p': 0.1}, + syn_weight=4.2, + delay=1.5, + dynamics_params='ExcToExc.json', + model_template='static_synapse' + ) + thalamus.build() + thalamus.save(output_dir='network') + + return thalamus + + +if __name__ == '__main__': + cortex_net = build_cortex_network() + # thalamus_net = build_thalamus_network(cortex_net) diff --git a/examples/point_iclamp/run_pointnet.py b/examples/point_iclamp/run_pointnet.py index aac2143b8..5e9e66ccb 100644 --- a/examples/point_iclamp/run_pointnet.py +++ b/examples/point_iclamp/run_pointnet.py @@ -1,5 +1,4 @@ -import sys - +import argparse from bmtk.simulator import pointnet from bmtk.analyzer.compartment import plot_traces @@ -14,11 +13,16 @@ def run(config_file): plot_traces(config_file=config_file, report_name='membrane_potential', population='cortex') + if __name__ == '__main__': - if __file__ != sys.argv[-1]: - run(sys.argv[-1]) - else: - # run('config.simulation_iclamp.json') - # run('config.simulation_iclamp.aslist.json') - # run('config.simulation_iclamp.csv.json') - run('config.simulation_iclamp.nwb.json') + default_config = 'config.simulation_iclamp.json' + # default_config = 'config.simulation_iclamp.aslist.json' + # default_config = 'config.simulation_iclamp.csv.json' + # default_config = 'config.simulation_iclamp.nwb.json' + + parser = argparse.ArgumentParser(description='Run PointNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/point_neuropixels/run_pointnet.py b/examples/point_neuropixels/run_pointnet.py index 0cfd0dd3c..ba3279dba 100644 --- a/examples/point_neuropixels/run_pointnet.py +++ b/examples/point_neuropixels/run_pointnet.py @@ -1,5 +1,4 @@ -import os, sys - +import argparse from bmtk.simulator import pointnet @@ -13,7 +12,13 @@ def run(config_file): if __name__ == '__main__': - # Find the appropriate config.json file - # run('config.simulation.sample.json') - # run('config.simulation.units_map.json') - run('config.simulation.multi_sessions.json') + # default_config = 'config.simulation.sample.json' + # default_config = 'config.simulation.units_map.json' + default_config = 'config.simulation.multi_sessions.json' + + parser = argparse.ArgumentParser(description='Run PointNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) \ No newline at end of file diff --git a/examples/point_stdp/run_pointnet.py b/examples/point_stdp/run_pointnet.py index b2b81e0ff..0b0f9ae1d 100644 --- a/examples/point_stdp/run_pointnet.py +++ b/examples/point_stdp/run_pointnet.py @@ -1,5 +1,4 @@ -import os, sys - +import argparse from bmtk.simulator import pointnet @@ -13,7 +12,9 @@ def run(config_file): if __name__ == '__main__': - if os.path.basename(__file__) != sys.argv[-1]: - run(sys.argv[-1]) - else: - run('config.simulation_iclamp.json') \ No newline at end of file + parser = argparse.ArgumentParser(description='Run PointNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) \ No newline at end of file diff --git a/examples/pop_2pops/run_popnet.py b/examples/pop_2pops/run_popnet.py index 3d6706528..3a94f0fe7 100644 --- a/examples/pop_2pops/run_popnet.py +++ b/examples/pop_2pops/run_popnet.py @@ -1,6 +1,4 @@ -import sys -import os - +import argparse from bmtk.simulator import popnet from bmtk.analyzer.firing_rates import plot_rates_popnet @@ -9,7 +7,7 @@ def plot_rates(cells_path='network/internal_node_types.csv', rates_path='output/ plot_rates_popnet(cells_path, rates_path, model_keys='pop_name') -def main(config_file): +def run(config_file): # initialize and run the simulation configure = popnet.config.from_json(config_file) configure.build_env() @@ -23,7 +21,9 @@ def main(config_file): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - main(sys.argv[-1]) - else: - main('config.simulation.json') + parser = argparse.ArgumentParser(description='Run PopNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) diff --git a/examples/pop_7pops_converted/run_popnet.py b/examples/pop_7pops_converted/run_popnet.py index 976aeac99..f62dfdb49 100644 --- a/examples/pop_7pops_converted/run_popnet.py +++ b/examples/pop_7pops_converted/run_popnet.py @@ -1,8 +1,8 @@ -import sys +import argparse from bmtk.simulator import popnet -def main(config_file): +def run(config_file): configure = popnet.config.from_json(config_file) configure.build_env() @@ -12,7 +12,10 @@ def main(config_file): if __name__ == '__main__': - if __file__ != sys.argv[-1]: - main(sys.argv[-1]) - else: - main('config.simulation.json') + parser = argparse.ArgumentParser(description='Run PopNet network simulation.') + parser.add_argument('config_path', type=str, nargs='?', default='config.simulation.json', + help='Path to the SONATA configuration file') + + args, _ = parser.parse_known_args() + run(args.config_path) + diff --git a/examples/pop_ssn_123/run_popnet.py b/examples/pop_ssn_123/run_popnet.py index 3699f2b86..6ebdd2457 100644 --- a/examples/pop_ssn_123/run_popnet.py +++ b/examples/pop_ssn_123/run_popnet.py @@ -39,9 +39,10 @@ def run(configuration_path): sim.run() if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.add_argument('config', nargs='?', default='config.simulation.json', help='SONATA configuration json file.') + parser = argparse.ArgumentParser(description='Run PopNet network simulation.') + parser.add_argument('config_path', nargs='?', default='config.simulation.json', + help='Path to SONATA configuration json file.') args = parser.parse_args() - run(args.config) + run(args.config_path) - plot_rates(args.config, label_column='pop_name') + plot_rates(args.config_path, label_column='pop_name') diff --git a/tests/simulator/bionet/test_argparser.py b/tests/simulator/bionet/test_argparser.py new file mode 100644 index 000000000..6d6744f31 --- /dev/null +++ b/tests/simulator/bionet/test_argparser.py @@ -0,0 +1,25 @@ +import sys +import pytest + +from bmtk.simulator.bionet import ArgumentParser + + +@pytest.mark.parametrize('args,parsed_output', [ + ('run_bionet.py', 'config.default.json'), + ('run_bionet.py config.new.json', 'config.new.json'), + ('nrniv -python run_bionet.py', 'config.default.json'), + ('nrniv -python run_bionet.py config.new.json', 'config.new.json'), + ('nrniv.exe -python run_bionet.py', 'config.default.json'), + ('nrniv.exe -python run_bionet.py config.new.json', 'config.new.json'), + ('nrniv.exe -python run_bionet.py --invalid', 'config.default.json'), + ('nrniv.exe -python run_bionet.py config.new.json --invalid', 'config.new.json'), +]) +def test_parse_args(monkeypatch, args, parsed_output): + monkeypatch.setattr(sys, 'argv', args.split()) + parser = ArgumentParser() + parser.add_argument('--opt', type=float, nargs=1, default='1.0') + parser.add_argument('config_path', type=str, nargs='?', default='config.default.json') + + args, _ = parser.parse_known_args() + assert(args.opt == 1.0) + assert(args.config_path == parsed_output)