-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
1 addition
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"cells":[{"cell_type":"markdown","metadata":{},"source":["# NetPyNE Tutorial 9: Evolutionary Algorithm for Parameters Optimization"]},{"cell_type":"markdown","metadata":{},"source":["## Preliminaries\n","\n","If you are going to run this notebook locally using Jupyter Notebook, start from following instructions https://github.com/suny-downstate-medical-center/netpyne/blob/development/netpyne/tutorials/README.md.\n","\n","If you are using Open Source Brain or EBRAINS, everything is already set up.\n","\n","On any other online platform (e.g. on Google Collab) you might need to run the following commmands to install NEURON and NetPyNE using **pip**:\n","```\n","!pip install neuron\n","!pip install netpyne\n","```\n","\n"]},{"cell_type":"markdown","metadata":{},"source":["Code in this tutorial provides the set up of batch simulation itself, while the network paramters and default configuration of individual simulations are provided in separate files in `batch_evol_tut` folder. We will switch to this folder to make these files accessible from this notebook. We also need to install `inspyred`, which implements the core of evolutionary algorithm."]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":99,"status":"ok","timestamp":1621615838144,"user":{"displayName":"Rammohan Shukla","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgfcDr2KHdiGwdZp-DCPyR3RvG0VfxRFktQ-0JlgQ=s64","userId":"13669639583442617912"},"user_tz":240},"id":"xAt-SfNS8rQt","outputId":"8f15b253-0cdf-4a4d-add1-1e5666938673"},"outputs":[],"source":["!pip install inspyred\n","import os\n","os.chdir(os.getcwd()+'/batch_evol_tut')"]},{"cell_type":"markdown","metadata":{},"source":["\n","\n","Two examples are provided: 'simple' and 'complex'.\n","In 'simple', 3 parameters are optimized to match target firing rates in 2 populations.\n","In 'complex', 6 parameters are optimized to match target firing rates in 6 populations.\n","\n","First we import all necessary modules, and then describe parameters space to explore."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["# %matplotlib inline\n","from netpyne import specs\n","from netpyne.batch import Batch\n","\n","networkType = 'simple' # 'simple' or 'complex'\n","\n","if networkType == 'simple':\n"," ## simple net\n"," params = specs.ODict()\n"," params['prob'] = [0.01, 0.5]\n"," params['weight'] = [0.001, 0.1]\n"," params['delay'] = [1, 20]\n","\n"," pops = {}\n"," pops['S'] = {'target': 5, 'width': 2, 'min': 2}\n"," pops['M'] = {'target': 15, 'width': 2, 'min': 0.2}\n","\n","elif networkType == 'complex':\n"," # complex net\n"," params = specs.ODict()\n"," params['probEall'] = [0.05, 0.2] # 0.1\n"," params['weightEall'] = [0.0025, 0.0075] #5.0\n"," params['probIE'] = [0.2, 0.6] #0.4\n"," params['weightIE'] = [0.0005, 0.002]\n"," params['probLengthConst'] = [100,200]\n"," params['stimWeight'] = [0.05, 0.2]\n","\n"," pops = {}\n"," pops['E2'] = {'target': 5, 'width': 2, 'min': 1}\n"," pops['I2'] = {'target': 10, 'width': 5, 'min': 2}\n"," pops['E4'] = {'target': 30, 'width': 10, 'min': 1}\n"," pops['I4'] = {'target': 10, 'width': 3, 'min': 2}\n"," pops['E5'] = {'target': 40, 'width': 4, 'min': 1}\n"," pops['I5'] = {'target': 25, 'width': 5, 'min': 2}"]},{"cell_type":"markdown","metadata":{},"source":["Provide fitness function and it's arguments:"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["fitnessFuncArgs = {}\n","fitnessFuncArgs['pops'] = pops\n","fitnessFuncArgs['maxFitness'] = 1000\n","\n","def fitnessFunc(simData, **kwargs):\n"," import numpy as np\n"," pops = kwargs['pops']\n"," maxFitness = kwargs['maxFitness']\n"," popFitness = [None for i in pops.items()]\n"," popFitness = [min(np.exp( abs(v['target'] - simData['popRates'][k]) / v['width']), maxFitness)\n"," if simData[\"popRates\"][k]>v['min'] else maxFitness for k,v in pops.items()]\n"," fitness = np.mean(popFitness)\n"," popInfo = '; '.join(['%s rate=%.1f fit=%1.f'%(p,r,f) for p,r,f in zip(list(simData['popRates'].keys()), list(simData['popRates'].values()), popFitness)])\n"," print(' '+popInfo)\n"," return fitness"]},{"cell_type":"markdown","metadata":{},"source":["Here we create `Batch` object with paramaters to tune, and set output folder, optimization method ('evol'), fitness function and all the configurations from above."]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000},"executionInfo":{"elapsed":131343,"status":"error","timestamp":1621615972335,"user":{"displayName":"Rammohan Shukla","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgfcDr2KHdiGwdZp-DCPyR3RvG0VfxRFktQ-0JlgQ=s64","userId":"13669639583442617912"},"user_tz":240},"id":"f0P--qg5YUT6","outputId":"e63a68dd-05ae-4c36-f46a-eaf7b6550e02"},"outputs":[],"source":["batch = Batch(params=params)\n","\n","# Set output folder, grid method (all param combinations), and run configuration\n","batch.batchLabel = 'simple'\n","batch.saveFolder = './' + batch.batchLabel\n","batch.method = 'evol'\n","batch.runCfg = {\n","\t'type': 'mpi_bulletin',#'hpc_slurm',\n","\t'script': 'init.py',\n","\t# options required only for hpc\n","\t'mpiCommand': 'mpirun',\n","\t'nodes': 1,\n","\t'coresPerNode': 2,\n","\t'allocation': 'default',\n","\t'email': '[email protected]',\n","\t'reservation': None,\n","\t'folder': '/home/salvadord/evol'\n","\t#'custom': 'export LD_LIBRARY_PATH=\"$HOME/.openmpi/lib\"' # only for conda users\n","}\n","batch.evolCfg = {\n","\t'evolAlgorithm': 'custom',\n","\t'fitnessFunc': fitnessFunc, # fitness expression (should read simData)\n","\t'fitnessFuncArgs': fitnessFuncArgs,\n","\t'pop_size': 6,\n","\t'num_elites': 1, # keep this number of parents for next generation if they are fitter than children\n","\t'mutation_rate': 0.4,\n","\t'crossover': 0.5,\n","\t'maximize': False, # maximize fitness function?\n","\t'max_generations': 4,\n","\t'time_sleep': 5, # wait this time before checking again if sim is completed (for each generation)\n","\t'maxiter_wait': 40, # max number of times to check if sim is completed (for each generation)\n","\t'defaultFitness': 1000 # set fitness value in case simulation time is over\n","}"]},{"cell_type":"markdown","metadata":{},"source":["Now we can run this code. When completed, you can inspect folder `batch_evol_tut/{networkType}`, where `{networkType}` is either `simple` or `complex`, to find the output of each generation and the summary for all, including the optimal values of requested parameters (short summary will also appear in the output of the cell). \n","\n","Note that the algorithm is stochastic, so you will not get the same results if re-run it again."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"aij9ZSq0UnLd"},"outputs":[],"source":["batch.run()"]}],"metadata":{"colab":{"collapsed_sections":[],"name":"netpyne_batch_evol.ipynb","provenance":[{"file_id":"1Vywfic5grokY-kOE4nQLtCFiw9diDGgR","timestamp":1621558581181},{"file_id":"1P9Y-rLqpKTP_cJZmWQY8qYsUMfNnju4N","timestamp":1621556006673},{"file_id":"1xcqB5I_iBlz3TNopuNERCJ1StlvZZJw5","timestamp":1621531137101},{"file_id":"19y6MLKhDAdBxLUZm2sHOuQx-5bqSODs-","timestamp":1621524871397}]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.9.16"}},"nbformat":4,"nbformat_minor":0} | ||
{"cells":[{"cell_type":"markdown","metadata":{},"source":["# NetPyNE Tutorial 9: Evolutionary Algorithm for Parameters Optimization"]},{"cell_type":"markdown","metadata":{},"source":["## Preliminaries\n","\n","If you are going to run this notebook locally using Jupyter Notebook, start from following instructions https://github.com/suny-downstate-medical-center/netpyne/blob/development/netpyne/tutorials/README.md.\n","\n","If you are using Open Source Brain or EBRAINS, everything is already set up.\n","\n","On any other online platform (e.g. on Google Collab) you might need to run the following commmands to install NEURON and NetPyNE using **pip**:\n","```\n","!pip install neuron\n","!pip install netpyne\n","```\n","\n"]},{"cell_type":"markdown","metadata":{},"source":["Code in this tutorial provides the set up of batch simulation itself, while the network paramters and default configuration of individual simulations are provided in separate files in `tut_batch_evol` folder. We will switch to this folder to make these files accessible from this notebook. We also need to install `inspyred`, which implements the core of evolutionary algorithm."]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":99,"status":"ok","timestamp":1621615838144,"user":{"displayName":"Rammohan Shukla","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgfcDr2KHdiGwdZp-DCPyR3RvG0VfxRFktQ-0JlgQ=s64","userId":"13669639583442617912"},"user_tz":240},"id":"xAt-SfNS8rQt","outputId":"8f15b253-0cdf-4a4d-add1-1e5666938673"},"outputs":[],"source":["!pip install inspyred\n","import os\n","os.chdir(os.getcwd()+'/tut_batch_evol')"]},{"cell_type":"markdown","metadata":{},"source":["\n","\n","Two examples are provided: 'simple' and 'complex'.\n","In 'simple', 3 parameters are optimized to match target firing rates in 2 populations.\n","In 'complex', 6 parameters are optimized to match target firing rates in 6 populations.\n","\n","First we import all necessary modules, and then describe parameters space to explore."]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["# %matplotlib inline\n","from netpyne import specs\n","from netpyne.batch import Batch\n","\n","networkType = 'simple' # 'simple' or 'complex'\n","\n","if networkType == 'simple':\n"," ## simple net\n"," params = specs.ODict()\n"," params['prob'] = [0.01, 0.5]\n"," params['weight'] = [0.001, 0.1]\n"," params['delay'] = [1, 20]\n","\n"," pops = {}\n"," pops['S'] = {'target': 5, 'width': 2, 'min': 2}\n"," pops['M'] = {'target': 15, 'width': 2, 'min': 0.2}\n","\n","elif networkType == 'complex':\n"," # complex net\n"," params = specs.ODict()\n"," params['probEall'] = [0.05, 0.2] # 0.1\n"," params['weightEall'] = [0.0025, 0.0075] #5.0\n"," params['probIE'] = [0.2, 0.6] #0.4\n"," params['weightIE'] = [0.0005, 0.002]\n"," params['probLengthConst'] = [100,200]\n"," params['stimWeight'] = [0.05, 0.2]\n","\n"," pops = {}\n"," pops['E2'] = {'target': 5, 'width': 2, 'min': 1}\n"," pops['I2'] = {'target': 10, 'width': 5, 'min': 2}\n"," pops['E4'] = {'target': 30, 'width': 10, 'min': 1}\n"," pops['I4'] = {'target': 10, 'width': 3, 'min': 2}\n"," pops['E5'] = {'target': 40, 'width': 4, 'min': 1}\n"," pops['I5'] = {'target': 25, 'width': 5, 'min': 2}"]},{"cell_type":"markdown","metadata":{},"source":["Provide fitness function and it's arguments:"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["fitnessFuncArgs = {}\n","fitnessFuncArgs['pops'] = pops\n","fitnessFuncArgs['maxFitness'] = 1000\n","\n","def fitnessFunc(simData, **kwargs):\n"," import numpy as np\n"," pops = kwargs['pops']\n"," maxFitness = kwargs['maxFitness']\n"," popFitness = [None for i in pops.items()]\n"," popFitness = [min(np.exp( abs(v['target'] - simData['popRates'][k]) / v['width']), maxFitness)\n"," if simData[\"popRates\"][k]>v['min'] else maxFitness for k,v in pops.items()]\n"," fitness = np.mean(popFitness)\n"," popInfo = '; '.join(['%s rate=%.1f fit=%1.f'%(p,r,f) for p,r,f in zip(list(simData['popRates'].keys()), list(simData['popRates'].values()), popFitness)])\n"," print(' '+popInfo)\n"," return fitness"]},{"cell_type":"markdown","metadata":{},"source":["Here we create `Batch` object with paramaters to tune, and set output folder, optimization method ('evol'), fitness function and all the configurations from above."]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000},"executionInfo":{"elapsed":131343,"status":"error","timestamp":1621615972335,"user":{"displayName":"Rammohan Shukla","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GgfcDr2KHdiGwdZp-DCPyR3RvG0VfxRFktQ-0JlgQ=s64","userId":"13669639583442617912"},"user_tz":240},"id":"f0P--qg5YUT6","outputId":"e63a68dd-05ae-4c36-f46a-eaf7b6550e02"},"outputs":[],"source":["batch = Batch(params=params)\n","\n","# Set output folder, grid method (all param combinations), and run configuration\n","batch.batchLabel = 'simple'\n","batch.saveFolder = './' + batch.batchLabel\n","batch.method = 'evol'\n","batch.runCfg = {\n","\t'type': 'mpi_bulletin',#'hpc_slurm',\n","\t'script': 'init.py',\n","\t# options required only for hpc\n","\t'mpiCommand': 'mpirun',\n","\t'nodes': 1,\n","\t'coresPerNode': 2,\n","\t'allocation': 'default',\n","\t'email': '[email protected]',\n","\t'reservation': None,\n","\t'folder': '/home/salvadord/evol'\n","\t#'custom': 'export LD_LIBRARY_PATH=\"$HOME/.openmpi/lib\"' # only for conda users\n","}\n","batch.evolCfg = {\n","\t'evolAlgorithm': 'custom',\n","\t'fitnessFunc': fitnessFunc, # fitness expression (should read simData)\n","\t'fitnessFuncArgs': fitnessFuncArgs,\n","\t'pop_size': 6,\n","\t'num_elites': 1, # keep this number of parents for next generation if they are fitter than children\n","\t'mutation_rate': 0.4,\n","\t'crossover': 0.5,\n","\t'maximize': False, # maximize fitness function?\n","\t'max_generations': 4,\n","\t'time_sleep': 5, # wait this time before checking again if sim is completed (for each generation)\n","\t'maxiter_wait': 40, # max number of times to check if sim is completed (for each generation)\n","\t'defaultFitness': 1000 # set fitness value in case simulation time is over\n","}"]},{"cell_type":"markdown","metadata":{},"source":["Now we can run this code. When completed, you can inspect folder `tut_batch_evol/{networkType}`, where `{networkType}` is either `simple` or `complex`, to find the output of each generation and the summary for all, including the optimal values of requested parameters (short summary will also appear in the output of the cell). \n","\n","Note that the algorithm is stochastic, so you will not get the same results if re-run it again."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"aij9ZSq0UnLd"},"outputs":[],"source":["batch.run()"]}],"metadata":{"colab":{"collapsed_sections":[],"name":"netpyne_batch_evol.ipynb","provenance":[{"file_id":"1Vywfic5grokY-kOE4nQLtCFiw9diDGgR","timestamp":1621558581181},{"file_id":"1P9Y-rLqpKTP_cJZmWQY8qYsUMfNnju4N","timestamp":1621556006673},{"file_id":"1xcqB5I_iBlz3TNopuNERCJ1StlvZZJw5","timestamp":1621531137101},{"file_id":"19y6MLKhDAdBxLUZm2sHOuQx-5bqSODs-","timestamp":1621524871397}]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.9.16"}},"nbformat":4,"nbformat_minor":0} |