From 6a98ad57b514c9019fe69e3c12fb9c1d88800ef1 Mon Sep 17 00:00:00 2001 From: jchen6727 Date: Mon, 13 May 2024 18:49:07 -0500 Subject: [PATCH] Batch (#818) * batch tools subpackage * comm update * new search methods * fixed import issues * updates comm, search * search.py * wrapper tools search * update to batchtools * codebase moved to netpyne batchtools https://github.com/jchen6727/netpyne/tree/batch * update to comm.py, runners.py * update to search (kwargs) * config update (should delete two keys @ end?) * replace pubtk with batchtk for compatibility with pip * update the submit (zsh->sh) * update batchtools, submits * fixed socket functionality for submits.py * batchtools documentation * minor updates- search --- netpyne/batchtools/__init__.py | 14 + netpyne/batchtools/comm.py | 48 ++ netpyne/batchtools/docs/batchtools.ipynb | 314 +++++++++++++ netpyne/batchtools/docs/batchtools.rst | 206 +++++++++ netpyne/batchtools/examples/CA3/cfg.py | 45 ++ .../batchtools/examples/CA3/grid_search.py | 39 ++ netpyne/batchtools/examples/CA3/init.py | 32 ++ netpyne/batchtools/examples/CA3/mod/CA1ih.mod | 64 +++ .../batchtools/examples/CA3/mod/CA1ika.mod | 85 ++++ .../batchtools/examples/CA3/mod/CA1ikdr.mod | 60 +++ .../batchtools/examples/CA3/mod/CA1ina.mod | 89 ++++ .../examples/CA3/mod/MyExp2SynBB.mod | 67 +++ .../examples/CA3/mod/MyExp2SynNMDABB.mod | 108 +++++ .../batchtools/examples/CA3/mod/aux_fun.inc | 43 ++ .../batchtools/examples/CA3/mod/caolmw.mod | 47 ++ .../batchtools/examples/CA3/mod/icaolmw.mod | 51 +++ .../batchtools/examples/CA3/mod/iholmw.mod | 60 +++ .../batchtools/examples/CA3/mod/kcaolmw.mod | 52 +++ .../batchtools/examples/CA3/mod/kdrbwb.mod | 76 ++++ .../batchtools/examples/CA3/mod/nafbwb.mod | 81 ++++ netpyne/batchtools/examples/CA3/netParams.py | 321 +++++++++++++ .../batchtools/examples/CA3/optuna_search.py | 38 ++ netpyne/batchtools/header.py | 0 netpyne/batchtools/runners.py | 137 ++++++ netpyne/batchtools/search.py | 430 ++++++++++++++++++ netpyne/batchtools/submits.py | 167 +++++++ 26 files changed, 2674 insertions(+) create mode 100644 netpyne/batchtools/__init__.py create mode 100644 netpyne/batchtools/comm.py create mode 100644 netpyne/batchtools/docs/batchtools.ipynb create mode 100644 netpyne/batchtools/docs/batchtools.rst create mode 100644 netpyne/batchtools/examples/CA3/cfg.py create mode 100644 netpyne/batchtools/examples/CA3/grid_search.py create mode 100644 netpyne/batchtools/examples/CA3/init.py create mode 100644 netpyne/batchtools/examples/CA3/mod/CA1ih.mod create mode 100644 netpyne/batchtools/examples/CA3/mod/CA1ika.mod create mode 100644 netpyne/batchtools/examples/CA3/mod/CA1ikdr.mod create mode 100644 netpyne/batchtools/examples/CA3/mod/CA1ina.mod create mode 100644 netpyne/batchtools/examples/CA3/mod/MyExp2SynBB.mod create mode 100644 netpyne/batchtools/examples/CA3/mod/MyExp2SynNMDABB.mod create mode 100644 netpyne/batchtools/examples/CA3/mod/aux_fun.inc create mode 100644 netpyne/batchtools/examples/CA3/mod/caolmw.mod create mode 100644 netpyne/batchtools/examples/CA3/mod/icaolmw.mod create mode 100644 netpyne/batchtools/examples/CA3/mod/iholmw.mod create mode 100644 netpyne/batchtools/examples/CA3/mod/kcaolmw.mod create mode 100644 netpyne/batchtools/examples/CA3/mod/kdrbwb.mod create mode 100644 netpyne/batchtools/examples/CA3/mod/nafbwb.mod create mode 100644 netpyne/batchtools/examples/CA3/netParams.py create mode 100644 netpyne/batchtools/examples/CA3/optuna_search.py create mode 100644 netpyne/batchtools/header.py create mode 100644 netpyne/batchtools/runners.py create mode 100644 netpyne/batchtools/search.py create mode 100644 netpyne/batchtools/submits.py diff --git a/netpyne/batchtools/__init__.py b/netpyne/batchtools/__init__.py new file mode 100644 index 000000000..c380458d2 --- /dev/null +++ b/netpyne/batchtools/__init__.py @@ -0,0 +1,14 @@ +from netpyne.batchtools.runners import NetpyneRunner +from batchtk.runtk import dispatchers +from batchtk import runtk + +specs = NetpyneRunner() + +from netpyne.batchtools.comm import Comm + +comm = Comm() + + +#from ray import tune as space.comm +#list and lb ub + diff --git a/netpyne/batchtools/comm.py b/netpyne/batchtools/comm.py new file mode 100644 index 000000000..1a8a12763 --- /dev/null +++ b/netpyne/batchtools/comm.py @@ -0,0 +1,48 @@ +from netpyne.batchtools import specs +from batchtk.runtk.runners import get_class +from batchtk import runtk +from neuron import h +import warnings +HOST = 0 # for the purposes of send and receive with mpi. + +class Comm(object): + def __init__(self, runner = specs): + self.runner = runner + h.nrnmpi_init() + self.pc = h.ParallelContext() + self.rank = self.pc.id() + self.connected = False + + def initialize(self): + if self.is_host(): + try: + self.runner.connect() + self.connected = True + except Exception as e: + print("Failed to connect to the Dispatch Server, failover to Local mode. See: {}".format(e)) + self.runner._set_inheritance('file') #TODO or could change the inheritance of the runner ... + self.runner.env[runtk.MSGOUT] = "{}/{}.out".format(self.runner.cfg.saveFolder, self.runner.cfg.simLabel) + + def set_runner(self, runner_type): + self.runner = get_class(runner_type)() + def is_host(self): + return self.rank == HOST + def send(self, data): + if self.is_host(): + if self.connected: + self.runner.send(data) + else: + self.runner.write(data) + + def recv(self): #TODO to be tested, broadcast to all workers? + if self.is_host() and self.connected: + data = self.runner.recv() + else: + data = None + #data = self.is_host() and self.runner.recv() + #probably don't put a blocking statement in a boolean evaluation... + self.pc.barrier() + return self.pc.py_broadcast(data, HOST) + + def close(self): + self.runner.close() diff --git a/netpyne/batchtools/docs/batchtools.ipynb b/netpyne/batchtools/docs/batchtools.ipynb new file mode 100644 index 000000000..22fe3489f --- /dev/null +++ b/netpyne/batchtools/docs/batchtools.ipynb @@ -0,0 +1,314 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "source": [ + "Jupyter Tutorial: The NetPyNE batchtools subpackage\n", + "How to use the `specs` and `comm` to communicate with the `batchtools` `dispatcher`\n" + ], + "metadata": { + "collapsed": false + }, + "id": "89ec6ca2392a9a0d" + }, + { + "cell_type": "markdown", + "source": [ + "For each individual `sim`, communication with the `batchtools` `dispatcher` occurs through the `specs` and `comm` objects" + ], + "metadata": { + "collapsed": false + }, + "id": "be50f40d8e61a944" + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "from netpyne.batchtools import specs, comm" + ], + "metadata": { + "collapsed": false + }, + "id": "6f321aedb7faf945", + "execution_count": null + }, + { + "cell_type": "markdown", + "source": [ + "the `specs` object is an instantiation of a custom class extending the `batchtk` `Runner` ..." + ], + "metadata": { + "collapsed": false + }, + "id": "5f2f08f0b5e582c3" + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "help(type(specs))" + ], + "metadata": { + "collapsed": false + }, + "id": "29fa261236494bc3", + "execution_count": null + }, + { + "cell_type": "markdown", + "source": [ + "From this `specs` object, we can similarly call `specs.NetParams` and `specs.SimConfig` to create the NetPyNE objects..." + ], + "metadata": { + "collapsed": false + }, + "id": "64ead24451bbad4a" + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "help(specs.NetParams)\n", + "help(specs.SimConfig)" + ], + "metadata": { + "collapsed": false + }, + "id": "43d263d080800019", + "execution_count": null + }, + { + "cell_type": "markdown", + "source": [ + "The `batchtools` job submission tool uses `environmental variables` to pass values to our `config` object created by `specs.SimConfig`, these `environmental variables` are captured during the `specs` `object creation` which occurs during the batchtools `import` (from the `batchtools` `__init__.py`:\n", + "```\n", + "from netpyne.batchtools.runners import NetpyneRunner\n", + "specs = NetpyneRunner()\n", + "```" + ], + "metadata": { + "collapsed": false + }, + "id": "710cc6084bd7af02" + }, + { + "cell_type": "markdown", + "source": [ + "Let's `export` some `environmental variables` to pass values to our `config` object. When this is handled by the `batchtools` `subpackage`, this occurs automatically..." + ], + "metadata": { + "collapsed": false + }, + "id": "52704684f5e80f3c" + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "%env STRRUNTK0 =foo.bar=baz\n", + "%env FLOATRUNTK1 =float_val=7.7\n", + "from netpyne.batchtools import NetpyneRunner\n", + "specs = NetpyneRunner()" + ], + "metadata": { + "collapsed": false + }, + "id": "50de117ff7f43aa6", + "execution_count": null + }, + { + "cell_type": "markdown", + "source": [ + "One way of retrieving these values is by calling `specs.get_mappings()`" + ], + "metadata": { + "collapsed": false + }, + "id": "fac14e517044b980" + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "print(specs.get_mappings())" + ], + "metadata": { + "collapsed": false + }, + "id": "257fad390f4abce", + "execution_count": null + }, + { + "cell_type": "markdown", + "source": [ + "Now, let's create our `config` object using the `specs.SimConfig()` constructor\n", + "This `config` object will hold a `dictionary` such that the initial values `foo['bar']` = `not_baz` and a `float_val` = `3.3`" + ], + "metadata": { + "collapsed": false + }, + "id": "92d41061bb828744" + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "cfg = specs.SimConfig()\n", + "cfg.foo = {'bar': 'not_baz', 'qux': 'quux'}\n", + "cfg.float_val = 3.3\n", + "print(\"cfg.foo['bar'] = {}\".format(cfg.foo['bar']))\n", + "print(\"cfg.float_val = {}\".format(cfg.float_val))" + ], + "metadata": { + "collapsed": false + }, + "id": "ca121d6ab30c3e7b", + "execution_count": null + }, + { + "cell_type": "markdown", + "source": [ + "Finally, calling the `cfg.update_cfg()` method will overwrite the original values with our environment values, (`baz` and `7.7`)...\n", + "\n", + "in NetPyNE, this was originally handled with the:\n", + "```\n", + "try:\n", + " from __main__ import cfg\n", + "except:\n", + " from cfg import cfg\n", + "```\n", + "API idiom in the `netParams.py` file...\n", + " \n", + "as well as the \n", + "```\n", + "cfg, netParams = sim.readCmdLineArgs(simConfigDefault='src/cfg.py', netParamsDefault='src/netParams.py')\n", + "```\n", + "API idiom in the `init.py` file...\n", + "\n", + "using the `batchtools` subpackage, we can treat the `cfg` as an object and pass it between scripts via `import` statements...\n", + "in `netParams.py`...\n", + "```\n", + "from cfg import cfg\n", + "cfg.update()\n", + "```\n", + "in `init.py`...\n", + "```\n", + "from netParams import cfg, netParams\n", + "sim.createSimulateAnalyze(simConfig=cfg, netParams=netParams)\n", + "```" + ], + "metadata": { + "collapsed": false + }, + "id": "6ea43f729d0685d4" + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "print(\"prior to cfg.update()\")\n", + "print(\"cfg.foo['bar'] = {}\".format(cfg.foo['bar']))\n", + "print(\"cfg.float_val = {}\".format(cfg.float_val))\n", + "print()\n", + "cfg.update() # call update_cfg to update values in the cfg object with values assigned by batch\n", + "print(\"after the cfg.update()\")\n", + "print(\"cfg.foo['bar'] = {}\".format(cfg.foo['bar']))\n", + "print(\"cfg.float_val = {}\".format(cfg.float_val))" + ], + "metadata": { + "collapsed": false + }, + "id": "a9426b6e6594961", + "execution_count": null + }, + { + "cell_type": "markdown", + "source": [ + "Finally, the `comm object` is used to report to the monitoring `dispatcher object`\n", + "the means of communication is dependent on which `dispatcher object` is instantiated, and communicated through environmental variables\n", + "in this case, since there is no `dispatcher object` the `comm` methods will simply perform `pass operations`" + ], + "metadata": { + "collapsed": false + }, + "id": "65bbb0ef2c76295a" + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "comm.initialize() # initializes comm object, establishing channel to communicate with the host dispatcher object" + ], + "metadata": { + "collapsed": false + }, + "id": "e9141d91d6e02aa3", + "execution_count": null + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "print(comm.is_host()) # returns a boolean IF the calling process is the 0th ranked parallelcontext, similar to sim.pc.rank == 0" + ], + "metadata": { + "collapsed": false + }, + "id": "5ed6a524bd8a3e0b", + "execution_count": null + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "comm.send('message') # sends 'message' to the `dispatcher object`" + ], + "metadata": { + "collapsed": false + }, + "id": "1966edbf32649352", + "execution_count": null + }, + { + "cell_type": "code", + "outputs": [], + "source": [ + "comm.close() #finalizes communication, closes any resources used to communicate with the `dispatcher object`" + ], + "metadata": { + "collapsed": false + }, + "id": "34f021af4127363c" + }, + { + "cell_type": "markdown", + "source": [], + "metadata": { + "collapsed": false + }, + "id": "648746fff96b8a72" + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/netpyne/batchtools/docs/batchtools.rst b/netpyne/batchtools/docs/batchtools.rst new file mode 100644 index 000000000..38bf6e7c9 --- /dev/null +++ b/netpyne/batchtools/docs/batchtools.rst @@ -0,0 +1,206 @@ +Running a Batch Job +=================== + +The NetPyNE batchtools subpackage provides a method of automating job submission and reporting:: + + + batch<-->\ /---> configuration_0 >---\ + \ / specs---\ + \<--->dispatcher_0 sim_0 + \ \ comm ---/ + \ \---< results_0 <---/ + \ + \ /---> configuration_1 >---\ + \ / specs---\ + \<--->dispatcher_1 sim_1 + \ \ comm ---/ + \ \---< results_1 <---/ + \ + \ + ... + + +1. Retrieving batch configuration values through the ``specs`` object +----- +Each simulation is able to retrieve relevant configurations through the ``specs`` object, and communicate with +the dispatcher through the ``comm`` object. + +importing the relevant objects:: + + from netpyne.batchtools import specs, comm + cfg = specs.SimConfig() # create a SimConfig object + netParams = specs.NetParams() # create a netParams object + +``netpyne.batchtools.specs`` behaves similarly to ``netpyne.sim.specs`` except in the following cases: + +* ``netpyne.batchtools.specs`` automatically captures relevant configuration mappings created by the ``dispatcher`` upon initialization + + * these mappings can be retrieved via ``specs.get_mappings()`` + +* the SimConfig object created by ``netpyne.batch.specs.SimConfig()`` will update itself with relevant configuration mappings through the ``update()`` method:: + + from netpyne.batchtools import specs # import the custom batch specs + cfg = specs.SimConfig() # create a SimConfig object + cfg.update() # update the cfg object with any relevant mappings for this particular batch job + +The ``update`` method will update the ``SimConfig`` object with the configuration mappings captured in ``specs`` (see: ``specs.get_mappings()``) + +This replaces the previous idiom for updating the SimConfig object with mappings from the batched job submission:: + + try: + from __main__ import cfg # import SimConfig object with params from parent module + except: + from cfg import cfg # if no simConfig in parent module, import directly from tut8_cfg module + + + +2. Communicating results to the ``dispatcher`` with the ``comm`` object +----- + +Prior batched simulations relied on ``.pkl`` files to communicate data. The ``netpyne.batch`` subpackage uses a specific ``comm`` object to send custom data back +The ``comm`` object determines the method of communication based on the batch job submission type. + +In terms of the simulation, the following functions are available to the user: + +* **comm.initialize()**: establishes a connection with the batch ``dispatcher`` for sending data + +* **comm.send()**: sends ```` to the batch ``dispatcher`` + +* **comm.close()**: closes and cleans up the connection with the batch ``dispatcher`` + +3. Specifying a batch job +----- +Batch job handling is implemented with methods from ``netpyne.batchtools.search`` + +**search**:: + + def search(job_type: str, # the submission engine to run a single simulation (e.g. 'sge', 'sh') + comm_type: str, # the method of communication between host dispatcher and the simulation (e.g. 'socket', 'filesystem') + run_config: Dict, # batch configuration, (keyword: string pairs to customize the submit template) + params: Dict, # search space (dictionary of parameter keys: tune search spaces) + algorithm: Optional[str] = "variant_generator", # search algorithm to use, see SEARCH_ALG_IMPORT for available options + label: Optional[str] = 'search', # label for the search + output_path: Optional[str] = '../batch', # directory for storing generated files + checkpoint_path: Optional[str] = '../ray', # directory for storing checkpoint files + max_concurrent: Optional[int] = 1, # number of concurrent trials to run at one time + batch: Optional[bool] = True, # whether concurrent trials should run synchronously or asynchronously + num_samples: Optional[int] = 1, # number of trials to run + metric: Optional[str] = "loss", # metric to optimize (this should match some key: value pair in the returned data + mode: Optional[str] = "min", # either 'min' or 'max' (whether to minimize or maximize the metric + algorithm_config: Optional[dict] = None, # additional configuration for the search algorithm + ) -> tune.ResultGrid: # results of the search + +The basic search implemented with the ``search`` function uses ``ray.tune`` as the search algorithm backend, returning a ``tune.ResultGrid`` which can be used to evaluate the search space and results. It takes the following parameters; + +* **job_type**: either "``sge``" or "``sh``", specifying how the job should be submitted, "``sge``" will submit batch jobs through the Sun Grid Engine. "``sh``" will submit bach jobs through the shell on a local machine +* **comm_type**: either "``socket``" or "``filesystem``", specifying how the job should communicate with the dispatcher +* **run_config**: a dictionary of keyword: string pairs to customize the submit template, the expected keyword: string pairs are dependent on the job_type:: + + ======= + sge + ======= + queue: the queue to submit the job to (#$ -q {queue}) + cores: the number of cores to request for the job (#$ -pe smp {cores}) + vmem: the amount of memory to request for the job (#$ -l h_vmem={vmem}) + realtime: the amount of time to request for the job (#$ -l h_rt={realtime}) + command: the command to run for the job + + example: + run_config = { + 'queue': 'cpu.q', # request job to be run on the 'cpu.q' queue + 'cores': 8, # request 8 cores for the job + 'vmem': '8G', # request 8GB of memory for the job + 'realtime': '24:00:00', # set timeout of the job to 24 hours + 'command': 'mpiexec -n $NSLOTS -hosts $(hostname) nrniv -python -mpi init.py' + } # set the command to be run to 'mpiexec -n $NSLOTS -hosts $(hostname) nrniv -python -mpi init.py' + + ======= + sh + ======= + command: the command to run for the job + + example: + run_config = { + 'command': 'mpiexec -n 8 nrniv -python -mpi init.py' + } # set the command to be run + +* **params**: a dictionary of config values to perform the search over. The keys of the dictionary should match the keys of the config object to be updated. Lists or numpy generators >2 values will force a grid search over the values; otherwise, a list of two values will create a uniform distribution sample space. + + **usage 1**: updating a constant value specified in the ``SimConfig`` object :: + + # take a config object with the following parameter ``foo`` + cfg = specs.SimConfig() + cfg.foo = 0 + cfg.update() + + # specify a search space for ``foo`` such that a simulation will run with: + # cfg.foo = 0 + # cfg.foo = 1 + # cfg.foo = 2 + # ... + # cfg.foo = 9 + + # using: + params = { + 'foo': range(10) + } + + **usage 2**: updating a nested object in the ``SimConfig`` object:: + + # to update a nested object, the package uses the `.` operator to specify reflection into the object. + # take a config object with the following parameter object ``foo`` + cfg = specs.SimConfig() + cfg.foo = {'bar': 0, 'baz': 0} + cfg.update() + + # specify a search space for ``foo['bar']`` with `foo.bar` such that a simulation will run: + # cfg.foo['bar'] = 0 + # cfg.foo['bar'] = 1 + # cfg.foo['bar'] = 2 + # ... + # cfg.foo['bar'] = 9 + + # using: + params = { + 'foo.bar': range(10) + } + + # this reflection works with nested objects as well... + # i.e. + # cfg.foo = {'bar': {'baz': 0}} + # params = {'foo.bar.baz': range(10)} + +* **algorithm** : the search algorithm (supported within ``ray.tune``) + + **Supported algorithms**:: + + * "variant_generator": grid and random based search of the parameter space (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + * "random": grid and random based search of the parameter space (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + * "axe": optimization algorithm (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + * "bayesopt": optimization algorithm (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + * "hyperopt": optimization algorithm (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + * "bohb": optimization algorithm (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + * "nevergrad": optimization algorithm (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + * "optuna": optimization algorithm (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + * "hebo": optimization algorithm (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + * "sigopt": optimization algorithm (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + * "zoopt": optimization algorithm (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + +* **label**: a label for the search, used for output file naming + +* **output_path**: the directory for storing generated files, can be a relative or absolute path + +* **checkpoint_path**: the directory for storing checkpoint files in case the search needs to be restored, can be a relative or absolute path + +* **max_concurrent**: the number of concurrent trials to run at one time, it is recommended to keep in mind the resource usage of each trial to avoid overscheduling + +* **batch**: whether concurrent trials should run synchronously or asynchronously + +* **num_samples**: the number of trials to run, for any grid search, each value in the grid will be sampled ``num_samples`` times. + +* **metric**: the metric to optimize (this should match some key: value pair in the returned data) + +* **mode**: either 'min' or 'max' (whether to minimize or maximize the metric) + +* **algorithm_config**: additional configuration for the search algorithm (see: https://docs.ray.io/en/latest/tune/api/suggestion.html) + diff --git a/netpyne/batchtools/examples/CA3/cfg.py b/netpyne/batchtools/examples/CA3/cfg.py new file mode 100644 index 000000000..6041126a3 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/cfg.py @@ -0,0 +1,45 @@ +from netpyne.batchtools import specs + +### config ### + +cfg = specs.SimConfig() + +cfg.duration = 1000 +cfg.dt = 0.1 +cfg.hparams = {'v_init': -65.0} +cfg.verbose = False +cfg.recordTraces = {} # don't save this +cfg.recordStim = False +cfg.recordStep = 0.1 # Step size in ms to save data (eg. V traces, LFP, etc) +cfg.filename = '00' # Set file output name +cfg.savePickle = False # Save params, network and sim output to pickle file +cfg.saveDat = False +cfg.saveJson = True +cfg.printRunTime = 0.1 +cfg.recordLFP = None # don't save this + +cfg.analysis['plotRaster'] = {'saveFig': True} # raster ok +cfg.analysis['plotTraces'] = { } # don't save this +cfg.analysis['plotLFPTimeSeries'] = { } # don't save this + +cfg.cache_efficient = True # better with MPI? +""" remove all of the unecessary data """ +cfg.saveCellSecs = False +cfg.saveCellConns = False + +cfg.nmda={#NMDA search space + "PYR->BC" : 1.38e-3, + "PYR->OLM": 0.7e-3, + "PYR->PYR": 0.004e-3, +} +cfg.ampa={#AMPA search space + "PYR->BC" : 0.36e-3, + "PYR->OLM": 0.36e-3, + "PYR->PYR": 0.02e-3, +} + +cfg.gaba = {#GABA search space + "BC->BC" : 4.5e-3, + "BC->PYR" : 0.72e-3, + "OLM->PYR": 72e-3, +} diff --git a/netpyne/batchtools/examples/CA3/grid_search.py b/netpyne/batchtools/examples/CA3/grid_search.py new file mode 100644 index 000000000..fdebf651b --- /dev/null +++ b/netpyne/batchtools/examples/CA3/grid_search.py @@ -0,0 +1,39 @@ +from netpyne.batchtools.search import search +import numpy + +params = {'nmda.PYR->BC' : numpy.linspace(1e-3, 1.8e-3, 3), + #'nmda.PYR->OLM': numpy.linspace(0.4e-3, 1.0e-3, 3), + #'nmda.PYR->PYR': numpy.linspace(0.001e-3, 0.007e-3, 3), + 'ampa.PYR->BC' : numpy.linspace(0.2e-3, 0.5e-3, 3), + #'ampa.PYR->OLM': numpy.linspace(0.2e-3, 0.5e-3, 3), + #'ampa.PYR->PYR': numpy.linspace(0.01e-3, 0.03e-3, 3), + #'gaba.BC->BC' : numpy.linspace(1e-3, 7e-3, 3), + 'gaba.BC->PYR' : numpy.linspace(0.4e-3, 1.0e-3, 3), + #'gaba.OLM->PYR': numpy.linspace(40e-3, 100e-3, 3), + } + +# use batch_shell_config if running directly on the machine +shell_config = {'command': 'mpiexec -np 4 nrniv -python -mpi init.py',} + +# use batch_sge_config if running on a +sge_config = { + 'queue': 'cpu.q', + 'cores': 5, + 'vmem': '4G', + 'realtime': '00:30:00', + 'command': 'mpiexec -n $NSLOTS -hosts $(hostname) nrniv -python -mpi init.py'} + +run_config = shell_config + +search(job_type = 'sh', + comm_type = 'socket', + label = 'search', + params = params, + output_path = '../grid_batch', + checkpoint_path = '../ray', + run_config = run_config, + num_samples = 1, + metric = 'loss', + mode = 'min', + algorithm = "variant_generator", + max_concurrent = 9) diff --git a/netpyne/batchtools/examples/CA3/init.py b/netpyne/batchtools/examples/CA3/init.py new file mode 100644 index 000000000..f1921bab3 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/init.py @@ -0,0 +1,32 @@ +from netpyne.batchtools import specs +from netpyne.batchtools import comm +from netpyne import sim +from netParams import netParams, cfg +import json + +comm.initialize() + +sim.createSimulate(netParams=netParams, simConfig=cfg) +print('completed simulation...') +#comm.pc.barrier() +#sim.gatherData() +if comm.is_host(): + netParams.save("{}/{}_params.json".format(cfg.saveFolder, cfg.simLabel)) + print('transmitting data...') + inputs = specs.get_mappings() + #print(json.dumps({**inputs})) + results = sim.analysis.popAvgRates(show=False) + + +#out_json = json.dumps({**inputs, **rates}) + + results['PYR_loss'] = (results['PYR'] - 3.33875)**2 + results['BC_loss'] = (results['BC'] - 19.725 )**2 + results['OLM_loss'] = (results['OLM'] - 3.470 )**2 + results['loss'] = (results['PYR_loss'] + results['BC_loss'] + results['OLM_loss']) / 3 + out_json = json.dumps({**inputs, **results}) + + print(out_json) +#TODO put all of this in a single function. + comm.send(out_json) + comm.close() diff --git a/netpyne/batchtools/examples/CA3/mod/CA1ih.mod b/netpyne/batchtools/examples/CA3/mod/CA1ih.mod new file mode 100644 index 000000000..93d435e30 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/CA1ih.mod @@ -0,0 +1,64 @@ +: $Id: CA1ih.mod,v 1.4 2010/12/13 21:35:47 samn Exp $ +TITLE Ih CA3 + +UNITS { + (mA) = (milliamp) + (mV) = (millivolt) +} + +NEURON { + SUFFIX hcurrent + NONSPECIFIC_CURRENT ih + RANGE g, e, v50, htau, hinf + RANGE gfactor +} + +PARAMETER { + celsius (degC) + g= 0.0001 (mho/cm2) + e= -30 (mV) + v50=-82 (mV) + gfactor = 1 +} + +STATE { + h +} + +ASSIGNED { + ih (mA/cm2) + hinf + htau (ms) + v (mV) +} + +PROCEDURE iassign () { ih=g*h*(v-e)*gfactor } + +BREAKPOINT { + SOLVE states METHOD cnexp + iassign() +} + +DERIVATIVE states { + rates(v) + h'= (hinf- h)/ htau +} + +INITIAL { + rates(v) + h = hinf + iassign() +} + +PROCEDURE rates(v (mV)) { + UNITSOFF + : HCN1 + :hinf = 1/(1+exp(0.151*(v-v50))) + :htau = exp((0.033*(v+75)))/(0.011*(1+exp(0.083*(v+75)))) + + : HCN2 + hinf = 1/(1+exp((v-v50)/10.5)) + htau = (1/(exp(-14.59-0.086*v)+exp(-1.87+0.0701*v))) + UNITSON +} + diff --git a/netpyne/batchtools/examples/CA3/mod/CA1ika.mod b/netpyne/batchtools/examples/CA3/mod/CA1ika.mod new file mode 100644 index 000000000..9e4fe6922 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/CA1ika.mod @@ -0,0 +1,85 @@ +: $Id: CA1ika.mod,v 1.2 2010/12/01 05:06:07 samn Exp $ +TITLE Ika CA1 + +UNITS { + (mA) = (milliamp) + (mV) = (millivolt) +} + +NEURON { + SUFFIX kacurrent + NONSPECIFIC_CURRENT ika, ikad + RANGE g, gd, e, ninf, ntau, ndinf, ndtau, linf, ltau +} + +PARAMETER { + celsius (degC) + g= 0.048 (mho/cm2) + gd= 0 (mho/cm2) + e= -90 (mV) +} + +STATE { + n + nd : distal + l +} + +ASSIGNED { + v (mV) + ika (mA/cm2) + ikad (mA/cm2) + ninf + ntau (ms) + ndinf + ndtau (ms) + linf + ltau (ms) +} + +PROCEDURE iassign () { + ika=g*n*l*(v-e) + ikad=gd*nd*l*(v-e) +} + +BREAKPOINT { + SOLVE states METHOD cnexp + iassign() +} + +DERIVATIVE states { + rates(v) + n'= (ninf- n)/ ntau + l'= (linf- l)/ ltau + nd'= (ndinf-nd)/ndtau +} + +INITIAL { + rates(v) + n = ninf + l = linf + iassign() +} + +PROCEDURE rates(v (mV)) { + LOCAL a, b + UNITSOFF + a = exp(-0.038*(1.5+1/(1+exp(v+40)/5))*(v-11)) + b = exp(-0.038*(0.825+1/(1+exp(v+40)/5))*(v-11)) + ntau=4*b/(1+a) + if (ntau<0.1) {ntau=0.1} + ninf=1/(1+a) + + a=exp(-0.038*(1.8+1/(1+exp(v+40)/5))*(v+1)) + b=exp(-0.038*(0.7+1/(1+exp(v+40)/5))*(v+1)) + ndtau=2*b/(1+a) + if (ndtau<0.1) {ndtau=0.1} + ndinf=1/(1+a) + + a = exp(0.11*(v+56)) + ltau=0.26*(v+50) + if (ltau<2) {ltau=2} + linf=1/(1+a) + UNITSON +} + diff --git a/netpyne/batchtools/examples/CA3/mod/CA1ikdr.mod b/netpyne/batchtools/examples/CA3/mod/CA1ikdr.mod new file mode 100644 index 000000000..4c5236362 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/CA1ikdr.mod @@ -0,0 +1,60 @@ +: $Id: CA1ikdr.mod,v 1.2 2010/12/01 05:10:52 samn Exp $ +TITLE IKDR CA1 + +UNITS { + (mA) = (milliamp) + (mV) = (millivolt) +} + +NEURON { + SUFFIX kdrcurrent + NONSPECIFIC_CURRENT ik + RANGE g, e, ninf, ntau +} + +PARAMETER { + celsius (degC) + g = 0.010 (mho/cm2) + e = -90 (mV) +} + +STATE { + n +} + +ASSIGNED { + v (mV) + ik (mA/cm2) + ninf + ntau (ms) +} + +PROCEDURE iassign () { ik=g*n*(v-e) } + +BREAKPOINT { + SOLVE states METHOD cnexp + iassign() +} + +DERIVATIVE states { + rates(v) + n'= (ninf- n)/ ntau +} + +INITIAL { + rates(v) + n = ninf + iassign() +} + +PROCEDURE rates(v (mV)) { + LOCAL a, b + UNITSOFF + a = exp(-0.11*(v-13)) + b = exp(-0.08*(v-13)) + ntau=50*b/(1+a) + if (ntau<2) {ntau=2} + ninf=1/(1+a) + UNITSON +} + diff --git a/netpyne/batchtools/examples/CA3/mod/CA1ina.mod b/netpyne/batchtools/examples/CA3/mod/CA1ina.mod new file mode 100644 index 000000000..d33ab9739 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/CA1ina.mod @@ -0,0 +1,89 @@ +: $Id: CA1ina.mod,v 1.4 2010/11/30 19:50:00 samn Exp $ +TITLE INa CA1 + +UNITS { + (mA) = (milliamp) + (mV) = (millivolt) +} + +NEURON { + SUFFIX nacurrent + NONSPECIFIC_CURRENT ina + RANGE g, e, vi, ki + RANGE minf,hinf,iinf,mtau,htau,itau : testing +} + +PARAMETER { + : v (mV) + celsius (degC) + g = 0.032 (mho/cm2) + e = 55 (mV) + vi = -60 (mV) + ki = 0.8 +} + +STATE { + m + h + I : i +} + +ASSIGNED { + i (mA/cm2) + ina (mA/cm2) + minf + mtau (ms) + hinf + htau (ms) + iinf + itau (ms) + v (mV) : testing +} + +: PROCEDURE iassign () { ina=g*m*m*m*h*i*(v-e) } +PROCEDURE iassign () { i=g*m*m*m*h*I*(v-e) ina=i} + +BREAKPOINT { + SOLVE states METHOD cnexp + iassign() +} + +DERIVATIVE states { + rates(v) + m' = (minf - m) / mtau + h' = (hinf - h) / htau + : i' = (iinf - i) / itau + I' = (iinf - I) / itau +} + +INITIAL { + rates(v) + h = hinf + m = minf + : i = iinf + I = iinf + iassign() : testing +} + + +PROCEDURE rates(v (mV)) { + LOCAL a, b + UNITSOFF + a = 0.4*(v+30)/(1-exp(-(v+30)/7.2)) + b = 0.124*(v+30)/(exp((v+30)/7.2)-1) + mtau=0.5/(a+b) + if (mtau<0.02) {mtau=0.02} + minf=a/(a+b) + a = 0.03*(v+45)/(1-exp(-(v+45)/1.5)) + b = 0.01*(v+45)/(exp((v+45)/1.5)-1) + htau=0.5/(a+b) + if (htau<0.5) {htau=0.5} + hinf=1/(1+exp((v+50)/4)) + a = exp(0.45*(v+66)) + b = exp(0.09*(v+66)) + itau=3000*b/(1+a) + if (itau<10) {itau=10} + iinf=(1+ki*exp((v-vi)/2))/(1+exp((v-vi)/2)) + UNITSON +} + diff --git a/netpyne/batchtools/examples/CA3/mod/MyExp2SynBB.mod b/netpyne/batchtools/examples/CA3/mod/MyExp2SynBB.mod new file mode 100644 index 000000000..9a68baef1 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/MyExp2SynBB.mod @@ -0,0 +1,67 @@ +: $Id: MyExp2SynBB.mod,v 1.4 2010/12/13 21:27:51 samn Exp $ +NEURON { +: THREADSAFE + POINT_PROCESS MyExp2SynBB + RANGE tau1, tau2, e, i, g, Vwt, gmax + NONSPECIFIC_CURRENT i +} + +UNITS { + (nA) = (nanoamp) + (mV) = (millivolt) + (uS) = (microsiemens) +} + +PARAMETER { + tau1=.1 (ms) <1e-9,1e9> + tau2 = 10 (ms) <1e-9,1e9> + e=0 (mV) + gmax = 1e9 (uS) + Vwt = 0 : weight for inputs coming in from vector +} + +ASSIGNED { + v (mV) + i (nA) + g (uS) + factor + etime (ms) +} + +STATE { + A (uS) + B (uS) +} + +INITIAL { + LOCAL tp + + Vwt = 0 : testing + + if (tau1/tau2 > .9999) { + tau1 = .9999*tau2 + } + A = 0 + B = 0 + tp = (tau1*tau2)/(tau2 - tau1) * log(tau2/tau1) + factor = -exp(-tp/tau1) + exp(-tp/tau2) + factor = 1/factor +} + +BREAKPOINT { + SOLVE state METHOD cnexp + g = B - A + if (g>gmax) {g=gmax}: saturation + i = g*(v - e) +} + +DERIVATIVE state { + A' = -A/tau1 + B' = -B/tau2 +} + +NET_RECEIVE(w (uS)) {LOCAL ww + ww=w + A = A + ww*factor + B = B + ww*factor +} diff --git a/netpyne/batchtools/examples/CA3/mod/MyExp2SynNMDABB.mod b/netpyne/batchtools/examples/CA3/mod/MyExp2SynNMDABB.mod new file mode 100644 index 000000000..01291643a --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/MyExp2SynNMDABB.mod @@ -0,0 +1,108 @@ +: $Id: MyExp2SynNMDABB.mod,v 1.4 2010/12/13 21:28:02 samn Exp $ +NEURON { +: THREADSAFE + POINT_PROCESS MyExp2SynNMDABB + RANGE tau1, tau2, e, i, iNMDA, s, sNMDA, r, tau1NMDA, tau2NMDA, Vwt, smax, sNMDAmax + NONSPECIFIC_CURRENT i, iNMDA +} + +UNITS { + (nA) = (nanoamp) + (mV) = (millivolt) + (uS) = (microsiemens) +} + +PARAMETER { + tau1 = 0.1 (ms) <1e-9,1e9> + tau2 = 10 (ms) <1e-9,1e9> + tau1NMDA = 15 (ms) + tau2NMDA = 150 (ms) + e = 0 (mV) + mg = 1 + r = 1 + smax = 1e9 (1) + sNMDAmax = 1e9 (1) + + Vwt = 0 : weight for inputs coming in from vector +} + +ASSIGNED { + v (mV) + i (nA) + iNMDA (nA) + s (1) + sNMDA (1) + mgblock (1) + factor (1) + factor2 (1) + + etime (ms) +} + +STATE { + A (1) + B (1) + A2 (1) + B2 (1) +} + +INITIAL { + + LOCAL tp + + Vwt = 0 : testing + + if (tau1/tau2 > .9999) { + tau1 = .9999*tau2 + } + A = 0 + B = 0 + tp = (tau1*tau2)/(tau2 - tau1) * log(tau2/tau1) + factor = -exp(-tp/tau1) + exp(-tp/tau2) + factor = 1/factor + + if (tau1NMDA/tau2NMDA > .9999) { + tau1NMDA = .9999*tau2NMDA + } + A2 = 0 + B2 = 0 + tp = (tau1NMDA*tau2NMDA)/(tau2NMDA - tau1NMDA) * log(tau2NMDA/tau1NMDA) + factor2 = -exp(-tp/tau1NMDA) + exp(-tp/tau2NMDA) + factor2 = 1/factor2 +} + +BREAKPOINT { + SOLVE state METHOD cnexp + : Jahr Stevens 1990 J. Neurosci + mgblock = 1.0 / (1.0 + 0.28 * exp(-0.062(/mV) * v) ) + s = B - A + sNMDA = B2 - A2 + if (s >smax) {s =smax }: saturation + if (sNMDA>sNMDAmax) {sNMDA=sNMDAmax}: saturation + i = s * (v - e) + iNMDA = sNMDA * (v - e) * mgblock +} + +DERIVATIVE state { + A' = -A/tau1 + B' = -B/tau2 + A2' = -A2/tau1NMDA + B2' = -B2/tau2NMDA +} + +NET_RECEIVE(w (uS)) {LOCAL ww + ww=w + :printf("NMDA Spike: %g\n", t) + if(r>=0){ : if r>=0, g = AMPA + NMDA*r + A = A + factor *ww + B = B + factor *ww + A2 = A2 + factor2*ww*r + B2 = B2 + factor2*ww*r + }else{ + if(r>-1000){ : if r>-1, g = NMDA*r + A2 = A2 - factor2*ww*r + B2 = B2 - factor2*ww*r + } + : if r<0 and r<>-1, g = 0 + } +} diff --git a/netpyne/batchtools/examples/CA3/mod/aux_fun.inc b/netpyne/batchtools/examples/CA3/mod/aux_fun.inc new file mode 100644 index 000000000..ccb579afb --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/aux_fun.inc @@ -0,0 +1,43 @@ +: $Id: aux_fun.inc,v 1.1 2009/11/04 01:24:52 samn Exp $ +COMMENT + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +// +// NOTICE OF COPYRIGHT AND OWNERSHIP OF SOFTWARE +// +// Copyright 2007, The University Of Pennsylvania +// School of Engineering & Applied Science. +// All rights reserved. +// For research use only; commercial use prohibited. +// Distribution without permission of Maciej T. Lazarewicz not permitted. +// mlazarew@seas.upenn.edu +// +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +ENDCOMMENT + + + +:------------------------------------------------------------------- +FUNCTION fun1(v(mV),V0(mV),A(/ms),B(mV))(/ms) { + + fun1 = A*exp((v-V0)/B) +} + +FUNCTION fun2(v(mV),V0(mV),A(/ms),B(mV))(/ms) { + + fun2 = A/(exp((v-V0)/B)+1) +} + +FUNCTION fun3(v(mV),V0(mV),A(/ms),B(mV))(/ms) { + + if(fabs((v-V0)/B)<1e-6) { + :if(v==V0) { + fun3 = A*B/1(mV) * (1- 0.5 * (v-V0)/B) + } else { + fun3 = A/1(mV)*(v-V0)/(exp((v-V0)/B)-1) + } +} + +FUNCTION min(x,y) { if (x<=y){ min = x }else{ min = y } } +FUNCTION max(x,y) { if (x>=y){ max = x }else{ max = y } } diff --git a/netpyne/batchtools/examples/CA3/mod/caolmw.mod b/netpyne/batchtools/examples/CA3/mod/caolmw.mod new file mode 100644 index 000000000..3ea21a7ef --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/caolmw.mod @@ -0,0 +1,47 @@ +: $Id: caolmw.mod,v 1.2 2010/11/30 16:40:09 samn Exp $ +COMMENT + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +// +// NOTICE OF COPYRIGHT AND OWNERSHIP OF SOFTWARE +// +// Copyright 2007, The University Of Pennsylvania +// School of Engineering & Applied Science. +// All rights reserved. +// For research use only; commercial use prohibited. +// Distribution without permission of Maciej T. Lazarewicz not permitted. +// mlazarew@seas.upenn.edu +// +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +ENDCOMMENT + +UNITS { + (mollar) = (1/liter) + (M) = (mollar) + (mM) = (millimollar) + (mA) = (milliamp) + (mV) = (millivolt) + (mS) = (millisiemens) +} + +NEURON { + SUFFIX Caolmw + USEION ca READ ica, cai WRITE cai + RANGE alpha, tau +} + +PARAMETER { + alpha = 0.002 (cm2-M/mA-ms) + tau = 80 (ms) +} + +ASSIGNED { ica (mA/cm2) } + +INITIAL { cai = 0 } + +STATE { cai (mM) } + +BREAKPOINT { SOLVE states METHOD cnexp } + +DERIVATIVE states { cai' = -(1000) * alpha * ica - cai/tau } diff --git a/netpyne/batchtools/examples/CA3/mod/icaolmw.mod b/netpyne/batchtools/examples/CA3/mod/icaolmw.mod new file mode 100644 index 000000000..51112d099 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/icaolmw.mod @@ -0,0 +1,51 @@ +: $Id: icaolmw.mod,v 1.2 2010/11/30 16:44:13 samn Exp $ +COMMENT + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +// +// NOTICE OF COPYRIGHT AND OWNERSHIP OF SOFTWARE +// +// Copyright 2007, The University Of Pennsylvania +// School of Engineering & Applied Science. +// All rights reserved. +// For research use only; commercial use prohibited. +// Distribution without permission of Maciej T. Lazarewicz not permitted. +// mlazarew@seas.upenn.edu +// +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +ENDCOMMENT + +UNITS { + (mA) = (milliamp) + (mV) = (millivolt) + (mS) = (millisiemens) +} + +NEURON { + SUFFIX ICaolmw + USEION ca WRITE ica + RANGE gca,eca +} + +PARAMETER { + gca = 1 (mS/cm2) + eca = 120 (mV) +} + +ASSIGNED { + ica (mA/cm2) + v (mV) +} + +PROCEDURE iassign () { ica = (1e-3) * gca * mcainf(v)^2 * (v-eca) } + +INITIAL { + iassign() +} + +BREAKPOINT { iassign() } + +FUNCTION mcainf(v(mV)) { mcainf = fun2(v, -20, 1, -9)*1(ms) } + +INCLUDE "aux_fun.inc" diff --git a/netpyne/batchtools/examples/CA3/mod/iholmw.mod b/netpyne/batchtools/examples/CA3/mod/iholmw.mod new file mode 100644 index 000000000..ccd919202 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/iholmw.mod @@ -0,0 +1,60 @@ +: $Id: iholmw.mod,v 1.2 2010/11/30 16:34:22 samn Exp $ +COMMENT + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +// +// NOTICE OF COPYRIGHT AND OWNERSHIP OF SOFTWARE +// +// Copyright 2007, The University Of Pennsylvania +// School of Engineering & Applied Science. +// All rights reserved. +// For research use only; commercial use prohibited. +// Distribution without permission of Maciej T. Lazarewicz not permitted. +// mlazarew@seas.upenn.edu +// +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +ENDCOMMENT + +UNITS { + (mA) = (milliamp) + (mV) = (millivolt) + (mS) = (millisiemens) +} + +NEURON { + SUFFIX Iholmw + NONSPECIFIC_CURRENT i + RANGE gh,eh +} + +PARAMETER { + gh = 0.15 (mS/cm2) + eh = -40 (mV) +} + +ASSIGNED { + v (mV) + i (mA/cm2) +} + +STATE { q } + +PROCEDURE iassign () { i = (1e-3) * gh * q * (v-eh) } + +INITIAL { + q = qinf(v) + iassign() +} + +BREAKPOINT { + SOLVE states METHOD cnexp + iassign() +} + +DERIVATIVE states { q' = (qinf(v)-q)/qtau(v) } + +FUNCTION qinf(v(mV)) { qinf = fun2(v, -80, 1, 10)*1(ms) } +FUNCTION qtau(v(mV))(ms) { qtau = 200(ms)/(exp((v+70(mV))/20(mV))+exp(-(v+70(mV))/20(mV))) + 5(ms) } + +INCLUDE "aux_fun.inc" diff --git a/netpyne/batchtools/examples/CA3/mod/kcaolmw.mod b/netpyne/batchtools/examples/CA3/mod/kcaolmw.mod new file mode 100644 index 000000000..b2368787e --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/kcaolmw.mod @@ -0,0 +1,52 @@ +: $Id: kcaolmw.mod,v 1.2 2010/11/30 16:47:18 samn Exp $ +COMMENT + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +// +// NOTICE OF COPYRIGHT AND OWNERSHIP OF SOFTWARE +// +// Copyright 2007, The University Of Pennsylvania +// School of Engineering & Applied Science. +// All rights reserved. +// For research use only; commercial use prohibited. +// Distribution without permission of Maciej T. Lazarewicz not permitted. +// mlazarew@seas.upenn.edu +// +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +ENDCOMMENT + +UNITS { + (mA) = (milliamp) + (mV) = (millivolt) + (mS) = (millisiemens) + (mollar) = (1/liter) + (mM) = (millimollar) +} + +NEURON { + SUFFIX KCaolmw + USEION k WRITE ik + USEION ca READ cai + RANGE gkca,ek,kd +} + +PARAMETER { + gkca = 10 (mS/cm2) + ek = -90 (mV) + kd = 30 (mM) +} + +ASSIGNED { + cai (mM) + v (mV) + ik (mA/cm2) +} + +PROCEDURE iassign () { ik = (1e-3) * gkca * cai/(cai+kd) * (v-ek) } + +INITIAL { + iassign() +} + +BREAKPOINT { iassign() } diff --git a/netpyne/batchtools/examples/CA3/mod/kdrbwb.mod b/netpyne/batchtools/examples/CA3/mod/kdrbwb.mod new file mode 100644 index 000000000..fc52ae534 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/kdrbwb.mod @@ -0,0 +1,76 @@ +: $Id: kdrbwb.mod,v 1.4 2010/12/13 21:35:26 samn Exp $ +COMMENT + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +// +// NOTICE OF COPYRIGHT AND OWNERSHIP OF SOFTWARE +// +// Copyright 2007, The University Of Pennsylvania +// School of Engineering & Applied Science. +// All rights reserved. +// For research use only; commercial use prohibited. +// Distribution without permission of Maciej T. Lazarewicz not permitted. +// mlazarew@seas.upenn.edu +// +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +ENDCOMMENT + +UNITS { + (mA) = (milliamp) + (mV) = (millivolt) + (mS) = (millisiemens) +} + +NEURON { + SUFFIX Kdrbwb + USEION k WRITE ik + RANGE phin,gkdr,ek + RANGE taon,ninf +} + +PARAMETER { + gkdr = 9 (mS/cm2) + ek = -90 (mV) + phin = 5 +} + +ASSIGNED { + v (mV) + ik (mA/cm2) + celsius (degC) + ninf (1) + taon (ms) +} + +STATE { n } + +PROCEDURE iassign () { ik = (1e-3) * gkdr * n^4 * (v-ek) } + +INITIAL { + rates(v) + n = ninf + iassign() +} + +BREAKPOINT { + SOLVE states METHOD cnexp + iassign() +} + +DERIVATIVE states { + rates(v) + n' = (ninf-n)/taon +} + +PROCEDURE rates(v(mV)) { LOCAL an, bn, q10 + q10 = phin:^((celsius-27.0(degC))/10.0(degC)) + + an = fun3(v, -34, -0.01, -10) + bn = fun1(v, -44, 0.125, -80) + + ninf = an/(an+bn) + taon = 1./((an+bn)*q10) +} + +INCLUDE "aux_fun.inc" diff --git a/netpyne/batchtools/examples/CA3/mod/nafbwb.mod b/netpyne/batchtools/examples/CA3/mod/nafbwb.mod new file mode 100644 index 000000000..37281dc94 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/mod/nafbwb.mod @@ -0,0 +1,81 @@ +: $Id: nafbwb.mod,v 1.4 2010/12/13 21:35:08 samn Exp $ +COMMENT + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +// +// NOTICE OF COPYRIGHT AND OWNERSHIP OF SOFTWARE +// +// Copyright 2007, The University Of Pennsylvania +// School of Engineering & Applied Science. +// All rights reserved. +// For research use only; commercial use prohibited. +// Distribution without permission of Maciej T. Lazarewicz not permitted. +// mlazarew@seas.upenn.edu +// +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +ENDCOMMENT + +UNITS { + (mA) = (milliamp) + (mV) = (millivolt) + (mS) = (millisiemens) +} + +NEURON { + SUFFIX Nafbwb + USEION na WRITE ina + RANGE phih + RANGE gna, ena, taoh : testing +} + +PARAMETER { + gna = 35 (mS/cm2) + ena = 55 (mV) + phih = 5 +} + +ASSIGNED { + v (mV) + ina (mA/cm2) + minf (1) + hinf (1) + taoh (ms) + celsius (degC) +} + +STATE { h } + +PROCEDURE iassign () { ina = (1e-3) * gna * minf^3 * h * (v-ena) } + +INITIAL { + rates(v) + h = hinf + iassign() +} + +BREAKPOINT { + SOLVE states METHOD cnexp + iassign() +} + +DERIVATIVE states { + rates(v) + h' = (hinf-h)/taoh +} + +PROCEDURE rates(v(mV)) { LOCAL am, bm, ah, bh, q10 + + q10 = phih:^((celsius-27.0(degC))/10.0(degC)) + + am = fun3(v, -35, -0.1, -10) + bm = fun1(v, -60, 4, -18) + minf = am/(am+bm) + + ah = fun1(v, -58, 0.07, -20) + bh = fun2(v, -28, 1, -10) + hinf = ah/(ah+bh) + taoh = 1./((ah+bh)*q10) +} + +INCLUDE "aux_fun.inc" diff --git a/netpyne/batchtools/examples/CA3/netParams.py b/netpyne/batchtools/examples/CA3/netParams.py new file mode 100644 index 000000000..156e9ff20 --- /dev/null +++ b/netpyne/batchtools/examples/CA3/netParams.py @@ -0,0 +1,321 @@ +from netpyne.batchtools import specs +from cfg import cfg + +cfg.update_cfg() +### params ### +# Network parameters +netParams = specs.NetParams() # object of class NetParams to store the network parameters +netParams.defaultThreshold = 0.0 +netParams.defineCellShapes = True # sets 3d geometry aligned along the y-axis + + +############################################################################### +## Cell types +############################################################################### +# Basket cell +BasketCell = {'secs':{}} +BasketCell['secs']['soma'] = {'geom': {}, 'mechs': {}} +BasketCell['secs']['soma']['geom'] = {'diam': 100, 'L': 31.831, 'nseg': 1, 'cm': 1} +BasketCell['secs']['soma']['mechs'] = {'pas': {'g': 0.1e-3, 'e': -65}, 'Nafbwb': {}, 'Kdrbwb': {}} +netParams.cellParams['BasketCell'] = BasketCell + + +# OLM cell +OlmCell = {'secs':{}} +OlmCell['secs']['soma'] = {'geom': {}, 'mechs': {}} +OlmCell['secs']['soma']['geom'] = {'diam': 100, 'L': 31.831, 'nseg': 1, 'cm': 1} +OlmCell['secs']['soma']['mechs'] = { + 'pas': {'g': 0.1e-3, 'e': -65}, + 'Nafbwb': {}, + 'Kdrbwb': {}, + 'Iholmw': {}, + 'Caolmw': {}, + 'ICaolmw': {}, + 'KCaolmw': {}} +netParams.cellParams['OlmCell'] = OlmCell + + +# Pyramidal cell +PyrCell = {'secs':{}} +PyrCell['secs']['soma'] = {'geom': {}, 'mechs': {}} +PyrCell['secs']['soma']['geom'] = {'diam': 20, 'L': 20, 'cm': 1, 'Ra': 150} +PyrCell['secs']['soma']['mechs'] = { + 'pas': {'g': 0.0000357, 'e': -70}, + 'nacurrent': {}, + 'kacurrent': {}, + 'kdrcurrent': {}, + 'hcurrent': {}} +PyrCell['secs']['Bdend'] = {'geom': {}, 'mechs': {}} +PyrCell['secs']['Bdend']['geom'] = {'diam': 2, 'L': 200, 'cm': 1, 'Ra': 150} +PyrCell['secs']['Bdend']['topol'] = {'parentSec': 'soma', 'parentX': 0, 'childX': 0} +PyrCell['secs']['Bdend']['mechs'] = { + 'pas': {'g': 0.0000357, 'e': -70}, + 'nacurrent': {'ki': 1}, + 'kacurrent': {}, + 'kdrcurrent': {}, + 'hcurrent': {}} +PyrCell['secs']['Adend1'] = {'geom': {}, 'mechs': {}} +PyrCell['secs']['Adend1']['geom'] = {'diam': 2, 'L': 150, 'cm': 1, 'Ra': 150} +PyrCell['secs']['Adend1']['topol'] = {'parentSec': 'soma', 'parentX': 1.0, 'childX': 0} # here there is a change: connected to end soma(1) instead of soma(0.5) +PyrCell['secs']['Adend1']['mechs'] = { + 'pas': {'g': 0.0000357, 'e': -70}, + 'nacurrent': {'ki': 0.5}, + 'kacurrent': {'g': 0.072}, + 'kdrcurrent': {}, + 'hcurrent': {'v50': -82, 'g': 0.0002}} +PyrCell['secs']['Adend2'] = {'geom': {}, 'mechs': {}} +PyrCell['secs']['Adend2']['geom'] = {'diam': 2, 'L': 150, 'cm': 1, 'Ra': 150} +PyrCell['secs']['Adend2']['topol'] = {'parentSec': 'Adend1', 'parentX': 1, 'childX': 0} +PyrCell['secs']['Adend2']['mechs'] = { + 'pas': {'g': 0.0000357, 'e': -70}, + 'nacurrent': {'ki': 0.5}, + 'kacurrent': {'g': 0, 'gd': 0.120}, + 'kdrcurrent': {}, + 'hcurrent': {'v50': -90, 'g': 0.0004}} +PyrCell['secs']['Adend3'] = {'geom': {}, 'mechs': {}} +PyrCell['secs']['Adend3']['geom'] = {'diam': 2, 'L': 150, 'cm': 2, 'Ra': 150} +PyrCell['secs']['Adend3']['topol'] = {'parentSec': 'Adend2', 'parentX': 1, 'childX': 0} +PyrCell['secs']['Adend3']['mechs'] = { + 'pas': {'g': 0.0000714, 'e': -70}, + 'nacurrent': {'ki': 0.5}, + 'kacurrent': {'g': 0, 'gd': 0.200}, + 'kdrcurrent': {}, + 'hcurrent': {'v50': -90, 'g': 0.0007}} +netParams.cellParams['PyrCell'] = PyrCell + + +############################################################################### +## Synaptic mechs +############################################################################### + +netParams.synMechParams['AMPAf'] = {'mod': 'MyExp2SynBB', 'tau1': 0.05, 'tau2': 5.3, 'e': 0} +netParams.synMechParams['NMDA'] = {'mod': 'MyExp2SynNMDABB', 'tau1': 0.05, 'tau2': 5.3, 'tau1NMDA': 15, 'tau2NMDA': 150, 'r': 1, 'e': 0} +netParams.synMechParams['GABAf'] = {'mod': 'MyExp2SynBB', 'tau1': 0.07, 'tau2': 9.1, 'e': -80} +netParams.synMechParams['GABAs'] = {'mod': 'MyExp2SynBB', 'tau1': 0.2, 'tau2': 20, 'e': -80} +netParams.synMechParams['GABAss'] = {'mod': 'MyExp2SynBB', 'tau1': 20, 'tau2': 40, 'e': -80} + + +############################################################################### +## Populations +############################################################################### +netParams.popParams['PYR'] = {'cellType': 'PyrCell', 'numCells': 800} +netParams.popParams['BC'] = {'cellType': 'BasketCell', 'numCells': 200} +netParams.popParams['OLM'] = {'cellType': 'OlmCell', 'numCells': 200} + + +############################################################################### +# Current-clamp to cells +############################################################################### +netParams.stimSourceParams['IClamp_PYR'] = {'type': 'IClamp', 'del': 2*0.1, 'dur': 1e9, 'amp': 50e-3} +netParams.stimSourceParams['IClamp_OLM'] = {'type': 'IClamp', 'del': 2*0.1, 'dur': 1e9, 'amp': -25e-3} + +netParams.stimTargetParams['IClamp_PYR->PYR'] = { + 'source': 'IClamp_PYR', + 'sec': 'soma', + 'loc': 0.5, + 'conds': {'pop': 'PYR'}} + +netParams.stimTargetParams['IClamp_OLM->OLM'] = { + 'source': 'IClamp_OLM', + 'sec': 'soma', + 'loc': 0.5, + 'conds': {'pop': 'OLM'}} + + +############################################################################### +# Setting connections +############################################################################### + +# PYR -> X, NMDA +netParams.connParams['PYR->BC_NMDA'] = {'preConds': {'pop': 'PYR'}, 'postConds': {'pop': 'BC'}, + 'convergence': 100, + 'weight': cfg.nmda['PYR->BC'], + 'delay': 2, + 'sec': 'soma', + 'loc': 0.5, + 'synMech': 'NMDA'} + +netParams.connParams['PYR->OLM_NMDA'] = {'preConds': {'pop': 'PYR'}, 'postConds': {'pop': 'OLM'}, + 'convergence': 10, + 'weight': cfg.nmda['PYR->OLM'], + 'delay': 2, + 'sec': 'soma', + 'loc': 0.5, + 'synMech': 'NMDA'} + +netParams.connParams['PYR->PYR_NMDA'] = {'preConds': {'pop': 'PYR'}, 'postConds': {'pop': 'PYR'}, + 'convergence': 25, + 'weight': cfg.nmda['PYR->PYR'], + 'delay': 2, + 'sec': 'Bdend', + 'loc': 1.0, + 'synMech': 'NMDA'} + +# PYR -> X, AMPA +netParams.connParams['PYR->BC_AMPA'] = {'preConds': {'pop': 'PYR'}, 'postConds': {'pop': 'BC'}, + 'convergence': 100, + 'weight': cfg.ampa['PYR->BC'], + 'delay': 2, + 'sec': 'soma', + 'loc': 0.5, + 'synMech': 'AMPAf'} + +netParams.connParams['PYR->OLM_AMPA'] = {'preConds': {'pop': 'PYR'}, 'postConds': {'pop': 'OLM'}, + 'convergence': 10, + 'weight': cfg.ampa['PYR->OLM'], + 'delay': 2, + 'sec': 'soma', + 'loc': 0.5, + 'synMech': 'AMPAf'} + +netParams.connParams['PYR->PYR_AMPA'] = {'preConds': {'pop': 'PYR'}, 'postConds': {'pop': 'PYR'}, + 'convergence': 25, + 'weight': cfg.ampa['PYR->PYR'], + 'delay': 2, + 'sec': 'Bdend', + 'loc': 1.0, + 'synMech': 'AMPAf'} + +# BC -> X, GABA +netParams.connParams['BC->BC_GABA'] = {'preConds': {'pop': 'BC'}, 'postConds': {'pop': 'BC'}, + 'convergence': 60, + 'weight': cfg.gaba['BC->BC'], + 'delay': 2, + 'sec': 'soma', + 'loc': 0.5, + 'synMech': 'GABAf'} + +netParams.connParams['BC->PYR_GABA'] = {'preConds': {'pop': 'BC'}, 'postConds': {'pop': 'PYR'}, + 'convergence': 50, + 'weight': cfg.gaba['BC->PYR'], + 'delay': 2, + 'sec': 'soma', + 'loc': 0.5, + 'synMech': 'GABAf'} + + +# OLM -> PYR, GABA +netParams.connParams['OLM->PYR_GABA'] = {'preConds': {'pop': 'OLM'}, 'postConds': {'pop': 'PYR'}, + 'convergence': 20, + 'weight': cfg.gaba['OLM->PYR'], + 'delay': 2, + 'sec': 'Adend2', + 'loc': 0.5, + 'synMech': 'GABAs'} + + +############################################################################### +# Setting NetStims +############################################################################### +# to PYR +netParams.stimSourceParams['NetStim_PYR_SOMA_AMPA'] = {'type': 'NetStim', 'interval': 1, 'number': 1000*cfg.duration, 'start': 0, 'noise': 1} +netParams.stimTargetParams['NetStim_PYR_SOMA_AMPA->PYR'] = { + 'source': 'NetStim_PYR_SOMA_AMPA', + 'conds': {'pop': 'PYR'}, + 'sec': 'soma', + 'loc': 0.5, + 'weight': 4*0.05e-3, # different from published value + 'delay': 2*0.1, + 'synMech': 'AMPAf'} + +netParams.stimSourceParams['NetStim_PYR_ADEND3_AMPA'] = {'type': 'NetStim', 'interval': 1, 'number': 1000*cfg.duration, 'start': 0, 'noise': 1} +netParams.stimTargetParams['NetStim_PYR_ADEND3_AMPA->PYR'] = { + 'source': 'NetStim_PYR_ADEND3_AMPA', + 'conds': {'pop': 'PYR'}, + 'sec': 'Adend3', + 'loc': 0.5, + 'weight': 4*0.05e-3, # different from published value + 'delay': 2*0.1, + 'synMech': 'AMPAf'} + +netParams.stimSourceParams['NetStim_PYR_SOMA_GABA'] = {'type': 'NetStim', 'interval': 1, 'number': 1000*cfg.duration, 'start': 0, 'noise': 1} +netParams.stimTargetParams['NetStim_PYR_SOMA_GABA->PYR'] = { + 'source': 'NetStim_PYR_SOMA_GABA', + 'conds': {'pop': 'PYR'}, + 'sec': 'soma', + 'loc': 0.5, + 'weight': 0.012e-3, + 'delay': 2*0.1, + 'synMech': 'GABAf'} + +netParams.stimSourceParams['NetStim_PYR_ADEND3_GABA'] = {'type': 'NetStim', 'interval': 1, 'number': 1000*cfg.duration, 'start': 0, 'noise': 1} +netParams.stimTargetParams['NetStim_PYR_ADEND3_GABA->PYR'] = { + 'source': 'NetStim_PYR_ADEND3_GABA', + 'conds': {'pop': 'PYR'}, + 'sec': 'Adend3', + 'loc': 0.5, + 'weight': 0.012e-3, + 'delay': 2*0.1, + 'synMech': 'GABAf'} + +netParams.stimSourceParams['NetStim_PYR_ADEND3_NMDA'] = {'type': 'NetStim', 'interval': 100, 'number': int((1000/100.0)*cfg.duration), 'start': 0, 'noise': 1} +netParams.stimTargetParams['NetStim_PYR_ADEND3_NMDA->PYR'] = { + 'source': 'NetStim_PYR_ADEND3_NMDA', + 'conds': {'pop': 'PYR'}, + 'sec': 'Adend3', + 'loc': 0.5, + 'weight': 6.5e-3, + 'delay': 2*0.1, + 'synMech': 'NMDA'} + +# to BC +netParams.stimSourceParams['NetStim_BC_SOMA_AMPA'] = {'type': 'NetStim', 'interval': 1, 'number': 1000*cfg.duration, 'start': 0, 'noise': 1} +netParams.stimTargetParams['NetStim_BC_SOMA_AMPA->BC'] = { + 'source': 'NetStim_BC_SOMA_AMPA', + 'conds': {'pop': 'BC'}, + 'sec': 'soma', + 'loc': 0.5, + 'weight': 0.02e-3, + 'delay': 2*0.1, + 'synMech': 'AMPAf'} + +netParams.stimSourceParams['NetStim_BC_SOMA_GABA'] = {'type': 'NetStim', 'interval': 1, 'number': 1000*cfg.duration, 'start': 0, 'noise': 1} +netParams.stimTargetParams['NetStim_BC_SOMA_GABA->BC'] = { + 'source': 'NetStim_BC_SOMA_GABA', + 'conds': {'pop': 'BC'}, + 'sec': 'soma', + 'loc': 0.5, + 'weight': 0.2e-3, + 'delay': 2*0.1, + 'synMech': 'GABAf'} + +# to OLM +netParams.stimSourceParams['NetStim_OLM_SOMA_AMPA'] = {'type': 'NetStim', 'interval': 1, 'number': 1000*cfg.duration, 'start': 0, 'noise': 1} +netParams.stimTargetParams['NetStim_OLM_SOMA_AMPA->OLM'] = { + 'source': 'NetStim_OLM_SOMA_AMPA', + 'conds': {'pop': 'OLM'}, + 'sec': 'soma', + 'loc': 0.5, + 'weight': 0.0625e-3, + 'delay': 2*0.1, + 'synMech': 'AMPAf'} + +netParams.stimSourceParams['NetStim_OLM_SOMA_GABA'] = {'type': 'NetStim', 'interval': 1, 'number': 1000*cfg.duration, 'start': 0, 'noise': 1} +netParams.stimTargetParams['NetStim_OLM_SOMA_GABA->OLM'] = { + 'source': 'NetStim_OLM_SOMA_GABA', + 'conds': {'pop': 'OLM'}, + 'sec': 'soma', + 'loc': 0.5, + 'weight': 0.2e-3, + 'delay': 2*0.1, + 'synMech': 'GABAf'} + +# Medial Septal inputs to BC and OLM cells +netParams.stimSourceParams['Septal'] = {'type': 'NetStim', 'interval': 150, 'number': int((1000/150)*cfg.duration), 'start': 0, 'noise': 0} +netParams.stimTargetParams['Septal->BC'] = { + 'source': 'Septal', + 'conds': {'pop': 'BC'}, + 'sec': 'soma', + 'loc': 0.5, + 'weight': 1.6e-3, + 'delay': 2*0.1, + 'synMech': 'GABAss'} + +netParams.stimTargetParams['Septal->OLM'] = { + 'source': 'Septal', + 'conds': {'pop': 'OLM'}, + 'sec': 'soma', + 'loc': 0.5, + 'weight': 1.6e-3, + 'delay': 2*0.1, + 'synMech': 'GABAss'} diff --git a/netpyne/batchtools/examples/CA3/optuna_search.py b/netpyne/batchtools/examples/CA3/optuna_search.py new file mode 100644 index 000000000..30cf45f6f --- /dev/null +++ b/netpyne/batchtools/examples/CA3/optuna_search.py @@ -0,0 +1,38 @@ +from netpyne.batchtools.search import search + +params = {'nmda.PYR->BC' : [1e-3, 1.8e-3], + #'nmda.PYR->OLM': [0.4e-3, 1.0e-3], + #'nmda.PYR->PYR': [0.001e-3, 0.007e-3], + 'ampa.PYR->BC' : [0.2e-3, 0.5e-3], + #'ampa.PYR->OLM': [0.2e-3, 0.5e-3], + #'ampa.PYR->PYR': [0.01e-3, 0.03e-3], + #'gaba.BC->BC' : [1e-3, 7e-3], + 'gaba.BC->PYR' : [0.4e-3, 1.0e-3], + #'gaba.OLM->PYR': [40e-3, 100e-3], + } + +# use batch_shell_config if running directly on the machine +shell_config = {'command': 'mpiexec -np 4 nrniv -python -mpi init.py',} + +# use batch_sge_config if running on a +sge_config = { + 'queue': 'cpu.q', + 'cores': 5, + 'vmem': '4G', + 'realtime': '00:30:00', + 'command': 'mpiexec -n $NSLOTS -hosts $(hostname) nrniv -python -mpi init.py'} + +run_config = shell_config + +search(job_type = 'sh', + comm_type = 'socket', + label = 'search', + params = params, + output_path = '../optuna_batch', + checkpoint_path = '../ray', + run_config = run_config, + num_samples = 27, + metric = 'loss', + mode = 'min', + algorithm = 'optuna', + max_concurrent = 3) diff --git a/netpyne/batchtools/header.py b/netpyne/batchtools/header.py new file mode 100644 index 000000000..e69de29bb diff --git a/netpyne/batchtools/runners.py b/netpyne/batchtools/runners.py new file mode 100644 index 000000000..687ef3645 --- /dev/null +++ b/netpyne/batchtools/runners.py @@ -0,0 +1,137 @@ +from batchtk.runtk.utils import convert, set_map, create_script +from batchtk import runtk +from batchtk.runtk.runners import Runner, get_class +import os + +class NetpyneRunner(Runner): + """ + runner for netpyne + see class runner + mappings <- + """ + def __new__(cls, inherit=None, **kwargs): + _super = get_class(inherit) + + def __init__(self, netParams=None, cfg=None, **kwargs): + """ + NetpyneRunner constructor + + Parameters + ---------- + self - NetpyneRunner instance + netParams - optional netParams instance (defaults to None, created with method: get_NetParams) + cfg - optional SimConfig instance (defaults to None, created with method: get_SimConfig) + N.B. requires cfg with the update_cfg method. see in get_SimConfig: + self.cfg = type("Runner_SimConfig", (specs.SimConfig,), + {'__mappings__': self.mappings, + 'update_cfg': update_cfg})() + kwargs - Unused + """ + _super.__init__(self, **kwargs) + self.netParams = netParams + self.cfg = cfg + + def _set_inheritance(self, inherit): + """ + Method for changing inheritance of NetpyneRunner + see runtk.RUNNERS + Parameters + ---------- + self + inherit + """ + if inherit in runtk.RUNNERS: + cls = type(self) + cls.__bases__ = (runtk.RUNNERS[inherit],) + else: + raise KeyError("inheritance {} not found in runtk.RUNNERS (please check runtk.RUNNERS for valid strings...".format(inherit)) + + + def get_NetParams(self): + """ + Creates / Returns a NetParams instance + Parameters + ---------- + self + + Returns + ------- + NetParams instance + + """ + if self.netParams: + return self.netParams + else: + from netpyne import specs + self.netParams = specs.NetParams() + return self.netParams + + def update_cfg(self): #intended to take `cfg` instance as self + """ + Updates the SimConfig instance with mappings to the runner, called from a SimConfig instance + + Parameters + ---------- + self - specs (NetpyneRunner) SimConfig instance + + Returns + ------- + None (updates SimConfig instance in place) + """ + for assign_path, value in self.__mappings__.items(): + try: + set_map(self, assign_path, value) + except Exception as e: + raise Exception("failed on mapping: cfg.{} with value: {}\n{}".format(assign_path, value, e)) + + def get_SimConfig(self): + """ + Creates / Returns a SimConfig instance + Parameters + ---------- + self - NetpyneRunner instance + + Returns + ------- + SimConfig instance + """ + if self.cfg: + return self.cfg + else: + from netpyne import specs + self.cfg = type("Runner_SimConfig", (specs.SimConfig,), + {'__mappings__': self.mappings, + 'update_cfg': update_cfg, + 'update': update_cfg})() + return self.cfg + + def set_SimConfig(self): + """ + updates the SimConfig instance with mappings to the runner, called from a Runner instance + + Parameters + ---------- + self + """ + # assumes values are only in 'cfg' + for assign_path, value in self.mappings.items(): + try: + set_map(self, "cfg.{}".format(assign_path), value) + except Exception as e: + raise Exception("failed on mapping: cfg.{} with value: {}\n{}".format(assign_path, value, e)) + + def set_mappings(self, filter=''): + # arbitrary filter, can work with 'cfg' or 'netParams' + for assign_path, value in self.mappings.items(): + if filter in assign_path: + set_map(self, assign_path, value) + + return type("NetpyneRunner{}".format(str(_super.__name__)), (_super,), + {'__init__': __init__, + '_set_inheritance': _set_inheritance, + 'get_NetParams': get_NetParams, + 'NetParams': get_NetParams, + 'SimConfig': get_SimConfig, + 'get_SimConfig': get_SimConfig, + 'set_SimConfig': set_SimConfig, + 'set_mappings': set_mappings})(**kwargs) # need to override __init__ or else will call parent diff --git a/netpyne/batchtools/search.py b/netpyne/batchtools/search.py new file mode 100644 index 000000000..724e4ee6e --- /dev/null +++ b/netpyne/batchtools/search.py @@ -0,0 +1,430 @@ +import ray +import pandas +import os +from ray import tune, train +from ray.air import session, RunConfig +from ray.tune.search.basic_variant import BasicVariantGenerator +from ray.tune.search import create_searcher, ConcurrencyLimiter, SEARCH_ALG_IMPORT +from netpyne.batchtools import runtk +from collections import namedtuple +from batchtk.raytk.search import ray_trial, LABEL_POINTER +from batchtk.utils import get_path +import numpy +from typing import Any, Callable, Dict, List, Optional, Tuple, Union + + + +choice = tune.choice +grid = tune.grid_search +uniform = tune.uniform + +def ray_optuna_search(dispatcher_constructor: Callable, # constructor for the dispatcher (e.g. INETDispatcher) + submit_constructor: Callable, # constructor for the submit (e.g. SHubmitSOCK) + run_config: Dict, # batch configuration, (keyword: string pairs to customize the submit template) + params: Dict, # search space (dictionary of parameter keys: tune search spaces) + label: Optional[str] = 'optuna_search', # label for the search + output_path: Optional[str] = '../batch', # directory for storing generated files + checkpoint_path: Optional[str] = '../ray', # directory for storing checkpoint files + max_concurrent: Optional[int] = 1, # number of concurrent trials to run at one time + batch: Optional[bool] = True, # whether concurrent trials should run synchronously or asynchronously + num_samples: Optional[int] = 1, # number of trials to run + metric: Optional[str] = "loss", # metric to optimize (this should match some key: value pair in the returned data + mode: Optional[str] = "min", # either 'min' or 'max' (whether to minimize or maximize the metric + optuna_config: Optional[dict] = None, # additional configuration for the optuna search algorithm + ) -> namedtuple('Study', ['algo', 'results']): + """ + ray_optuna_search(...) + + Parameters + ---------- + dispatcher_constructor:Callable, # constructor for the dispatcher (e.g. INETDispatcher) + submit_constructor:Callable, # constructor for the submit (e.g. SHubmitSOCK) + run_config:Dict, # batch configuration, (keyword: string pairs to customize the submit template) + params:Dict, # search space (dictionary of parameter keys: tune search spaces) + label:Optional[str] = 'optuna_search', # label for the search + output_path:Optional[str] = '../batch', # directory for storing generated files + checkpoint_path:Optional[str] = '../ray', # directory for storing checkpoint files + max_concurrent:Optional[int] = 1, # number of concurrent trials to run at one time + batch:Optional[bool] = True, # whether concurrent trials should run synchronously or asynchronously + num_samples:Optional[int] = 1, # number of trials to run + metric:Optional[str] = "loss", # metric to optimize (this should match some key: value pair in the returned data + mode:Optional[str] = "min", # either 'min' or 'max' (whether to minimize or maximize the metric + optuna_config:Optional[dict] = None, # additional configuration for the optuna search algorithm (incl. sampler, seed, etc.) + + Creates + ------- +