def __init__(self): self.model_interpreter = NetPyNEModelInterpreter() # Geppetto model of a created network self.geppetto_model = None self.netParams = specs.NetParams() self.simConfig = specs.SimConfig() self.run_config = model.RunConfig() self.experiments = experiments model.register(metadata) synchronization.startSynchronization(self.__dict__) logging.debug("Initializing the original model") jupyter_geppetto.context = {'netpyne_geppetto': self} # Set running experiments without any subprocess to ERRROR experiments.get_experiments() running_exps = experiments.get_by_states([ model.ExperimentState.PENDING, model.ExperimentState.SIMULATING, model.ExperimentState.INSTANTIATING ]) if not simulations.local.is_running(): [experiments.set_to_error(e) for e in running_exps]
def test_getGeppettoModelSimpleNetwork(self): # object of class NetParams to store the network parameters netParams = specs.NetParams() # object of class SimConfig to store the simulation configuration simConfig = specs.SimConfig() # add dict with params for this pop netParams.popParams['PYR'] = { 'cellModel': 'HH', 'cellType': 'PYR', 'numCells': 20} cellRule = {'conds': {'cellModel': 'HH', 'cellType': 'PYR'}, 'secs': {}} # cell rule dict # soma params dict cellRule['secs']['soma'] = {'geom': {}, 'mechs': {}} cellRule['secs']['soma']['geom'] = { 'diam': 18.8, 'L': 18.8, 'Ra': 123.0} # soma geometry cellRule['secs']['soma']['mechs']['hh'] = { 'gnabar': 0.12, 'gkbar': 0.036, 'gl': 0.003, 'el': -70} # soma hh mechanism cellRule['secs']['soma']['vinit'] = -71 # add dict to list of cell params netParams.cellParams['PYR'] = cellRule netParams.synMechParams['AMPA'] = { 'mod': 'Exp2Syn', 'tau1': 0.1, 'tau2': 1.0, 'e': 0} netParams.stimSourceParams['bkg'] = { 'type': 'NetStim', 'rate': 10, 'noise': 0.5, 'start': 1} netParams.stimTargetParams['bkg->PYR1'] = {'source': 'bkg', 'conds': { 'pop': 'PYR'}, 'weight': 0.1, 'delay': 'uniform(1,5)'} netParams.connParams['PYR->PYR'] = { 'preConds': {'pop': 'PYR'}, 'postConds': {'pop': 'PYR'}, 'weight': 0.002, # weight of each connection # delay min=0.2, mean=13.0, var = 1.4 'delay': '0.2+normal(13.0,1.4)', 'threshold': 10, # threshold 'convergence': 'uniform(1,15)'} # convergence (num presyn targeting postsyn) is uniformly distributed between 1 and 15 self.getGeppettoModel(netParams, simConfig)
def exportNeuroML(self, modelParams): try: with redirect_stdout(sys.__stdout__): sim.exportNeuroML2(modelParams['fileName'], specs.SimConfig()) return utils.getJSONReply() except: return utils.getJSONError( "Error while exporting the NetPyNE model", sys.exc_info())
def deleteModel(self, modelParams): try: with redirect_stdout(sys.__stdout__): self.netParams = specs.NetParams() self.simConfig = specs.SimConfig() sim.initialize(specs.NetParams(), specs.SimConfig()) self.geppetto_model = None except Exception: message = "Error while exporting the NetPyNE model" logging.exception(message) return utils.getJSONError(message, sys.exc_info()) try: sim.clearAll() except: logging.exception("Failed to clear simulation") return utils.getJSONReply()
def __init__(self): self.model_interpreter = NetPyNEModelInterpreter() self.netParams = specs.NetParams() self.simConfig = specs.SimConfig() synchronization.startSynchronization(self.__dict__) logging.debug("Initializing the original model") jupyter_geppetto.context = {'netpyne_geppetto': self}
def deleteModel(self, modelParams): try: with redirect_stdout(sys.__stdout__): self.netParams = specs.NetParams() self.simConfig = specs.SimConfig() sim.initialize(specs.NetParams(), specs.SimConfig()) self.geppetto_model = None except: return utils.getJSONError( "Error while exporting the NetPyNE model", sys.exc_info()) try: # This function fails is some keys don't exists # sim.clearAll() # TODO: as part of #264 we should remove the method and use clearAll intstead self.clearSim() except: pass return utils.getJSONReply()
def run_batch(experiment): # Map params to netpyne format params = specs.ODict() grouped_params = [] for param in experiment["params"]: params[param["mapsTo"]] = param["values"] if param["inGroup"]: grouped_params.append(param["mapsTo"]) with open("netParams.json", "r") as f: net_params = json.load(f) net_params = specs.NetParams(net_params["net"]["params"]) with open("simConfig.json", "r") as f: sim_config = json.load(f) sim_config = specs.SimConfig(sim_config["simConfig"]) batch = Batch( cfg=sim_config, netParams=net_params, params=params, groupedParams=grouped_params, seed=experiment.get("seed", None), ) # Label will be subfolder of saveFolder batch.batchLabel = experiment.get("name", "batch_template_run") # Have to overwrite the saveFolder, default goes to root folder which is not always allowed by OS batch.saveFolder = os.getcwd() # For now, we only support grid|list batch.method = experiment.get("method", "grid") # for now, we only support mpi_direct or bulletinboard # * mpi_direct can be started by running batch.py # * mpi_bulletin requires to run "mpiexec -n 4 nrniv -mpi batch.py", otherwise runs in single core run_cfg = experiment.get("runCfg", None) cores = run_cfg.get("cores", None) cores = int(cores) if cores else None if run_cfg: batch.runCfg = { "type": run_cfg.get("type", "mpi_bulletin"), "script": run_cfg.get("script", "run.py"), "skip": run_cfg.get("skip", True), "cores": cores, } batch.run() return batch
def deleteModel(self, modelParams): try: with redirect_stdout(sys.__stdout__): self.netParams = specs.NetParams() self.simConfig = specs.SimConfig() self.netParams.todict() self.netParams.todict() if self.doIhaveInstOrSimData()['haveInstance']: sim.clearAll() self.geppetto_model = None return utils.getJSONReply() except: return utils.getJSONError( "Error while exporting the NetPyNE model", sys.exc_info())
def main(): args = init() net_params = specs.NetParams() sim_cfg = specs.SimConfig() logging.info("Opening cells") with open(args.cells, "r") as f: cells = json.load(f) logging.debug(cells) logging.info("Init cells") #cell_config.init_cells(net_params, cells) # Load single cell (For debug purposes) cell_config.load_cell(label=cells[0]["label"], cell_name=cells[0]["cell_name"], load_biophysic=True, net_params=net_params) logging.info("Init network parameters") net_config.init_net_params(net_params, cells) logging.info("Init simulation config") sim_config.init_sim_config(sim_cfg) logging.info("Analysing") # These commands can be used to initialize the network simulation before analysis and simulations sim.initialize(simConfig=sim_config, netParams=net_params) sim.net.createPops() sim.net.createCells() sim.net.connectCells() logging.debug("Cells") logging.debug(sim._gatherAllCellTags()[0]) logging.debug(net_params.cellParams.keys()) logging.debug(net_params.popParams.keys()) logging.debug(net_params.synMechParams.keys()) logging.debug("Number of synapse mechanisms: {}".format( len(net_params.synMechParams.keys()))) # TODO: Create synMechs, populations, connections # Simulate and create analysis analysis.analyse(net_params, sim_cfg)
def importNeuroML(self, modelParams): try: with redirect_stdout(sys.__stdout__): sim.initialize() sim.importNeuroML2(modelParams['neuroMLFolder'], simConfig=specs.SimConfig(), simulate=False, analyze=False) self.geppetto_model = self.model_interpreter.getGeppettoModel( sim) return json.loads( GeppettoModelSerializer.serialize(self.geppetto_model)) except: return utils.getJSONError( "Error while exporting the NetPyNE model", sys.exc_info())
def exportModel(self, args): try: with redirect_stdout(sys.__stdout__): if not args['netCells']: sim.initialize(netParams=self.netParams, simConfig=self.simConfig) sim.cfg.filename = args['fileName'] include = [ el for el in specs.SimConfig().saveDataInclude if el in args.keys() and args[el] ] if args['netCells']: include += ['netPops'] sim.cfg.saveJson = True sim.saveData(include) sim.cfg.saveJson = False return utils.getJSONReply() except: return utils.getJSONError( "Error while exporting the NetPyNE model", sys.exc_info())
def exportModel(self, args): try: with redirect_stdout(sys.__stdout__): if not args['netCells']: sim.initialize(netParams=self.netParams, simConfig=self.simConfig) sim.cfg.filename = args['fileName'] include = [ el for el in specs.SimConfig().saveDataInclude if el in args.keys() and args[el] ] if args['netCells']: include += ['netPops'] sim.cfg.saveJson = True sim.saveData(include) sim.cfg.saveJson = False with open(f"{sim.cfg.filename}_data.json") as json_file: data = json.load(json_file) return data except Exception: message = "Error while exporting the NetPyNE model" logging.exception(message) return utils.getJSONError(message, sys.exc_info())
def exportHLS(self, args): def convert2bool(string): return string.replace('true', 'True').replace('false', 'False').replace( 'null', 'False') def header(title, spacer='-'): return '\n# ' + title.upper() + ' ' + spacer * (77 - len(title)) + '\n' try: params = ['popParams', 'cellParams', 'synMechParams'] params += ['connParams', 'stimSourceParams', 'stimTargetParams'] fname = args['fileName'] if not fname: # default option fname = 'output.py' if not fname[-3:] == '.py': fname = f"{fname}.py" # TODO: use methods offered by netpyne to create this script! with open(fname, 'w') as script: script.write("from netpyne import specs, sim\n") script.write(header("documentation")) script.write( "Script generated with NetPyNE-UI. Please visit:\n") script.write( " - https://www.netpyne.org\n - https://github.com/MetaCell/NetPyNE-UI\n\n" ) script.write(header("script", spacer="=")) script.write("netParams = specs.NetParams()\n") script.write("simConfig = specs.SimConfig()\n") script.write(header("single value attributes")) for attr, value in list(self.netParams.__dict__.items()): if attr not in params: if value != getattr(specs.NetParams(), attr): script.write("netParams." + attr + " = ") script.write( convert2bool(json.dumps(value, indent=4)) + "\n") script.write(header("network attributes")) for param in params: for key, value in list( getattr(self.netParams, param).items()): script.write("netParams." + param + "[" + key + "] = ") script.write( convert2bool(json.dumps(value, indent=4)) + "\n") script.write(header("network configuration")) for attr, value in list(self.simConfig.__dict__.items()): if value != getattr(specs.SimConfig(), attr): script.write("simConfig." + attr + " = ") script.write( convert2bool(json.dumps(value, indent=4)) + "\n") script.write(header("create simulate analyze network")) script.write( "# sim.createSimulateAnalyze(netParams=netParams, simConfig=simConfig)\n" ) script.write(header("end script", spacer="=")) with open(fname) as f: file_b64 = base64.b64encode(bytes(f.read(), 'utf-8')).decode() export_info = {"fileContent": file_b64, "fileName": fname} return export_info except Exception: message = "Error while exporting NetPyNE model to python" logging.exception(message) return utils.getJSONError(message, sys.exc_info())
def loadModel(self, args): """ Imports a model stored as file in json format. :param args: :return: """ def remove(dictionary): # remove reserved keys such as __dict__, __Method__, etc # they appear when we do sim.loadAll(json_file) if isinstance(dictionary, dict): for key, value in list(dictionary.items()): if key.startswith('__'): dictionary.pop(key) else: remove(value) if not any([ args[option] for option in ['loadNetParams', 'loadSimCfg', 'loadSimData', 'loadNet'] ]): return utils.getJSONError( "Error while loading data", 'You have to select at least one option') try: owd = os.getcwd() compileModMechFiles(args['compileMod'], args['modFolder']) except: return utils.getJSONError("Error while importing/compiling mods", sys.exc_info()) finally: os.chdir(owd) try: with redirect_stdout(sys.__stdout__): sim.initialize() wake_up_geppetto = False if all([ args[option] for option in ['loadNetParams', 'loadSimCfg', 'loadSimData', 'loadNet'] ]): wake_up_geppetto = True if self.doIhaveInstOrSimData()['haveInstance']: sim.clearAll() sim.initialize() sim.loadAll(args['jsonModelFolder']) self.netParams = sim.net.params self.simConfig = sim.cfg remove(self.netParams.todict()) remove(self.simConfig.todict()) else: if args['loadNet']: wake_up_geppetto = True if self.doIhaveInstOrSimData()['haveInstance']: sim.clearAll() sim.initialize() sim.loadNet(args['jsonModelFolder']) if args['loadSimData']: # TODO (https://github.com/Neurosim-lab/netpyne/issues/360) wake_up_geppetto = True if not self.doIhaveInstOrSimData()['haveInstance']: sim.create(specs.NetParams(), specs.SimConfig()) sim.net.defineCellShapes() sim.gatherData(gatherLFP=False) sim.loadSimData(args['jsonModelFolder']) if args['loadSimCfg']: sim.loadSimCfg(args['jsonModelFolder']) self.simConfig = sim.cfg remove(self.simConfig.todict()) if args['loadNetParams']: if self.doIhaveInstOrSimData()['haveInstance']: sim.clearAll() sim.loadNetParams(args['jsonModelFolder']) self.netParams = sim.net.params remove(self.netParams.todict()) if wake_up_geppetto: if len(sim.net.cells) > 0: section = list(sim.net.cells[0].secs.keys())[0] if 'pt3d' not in list( sim.net.cells[0].secs[section].geom.keys()): sim.net.defineCellShapes() sim.gatherData() sim.loadSimData(args['jsonModelFolder']) sim.gatherData() self.geppetto_model = self.model_interpreter.getGeppettoModel( sim) return json.loads( GeppettoModelSerializer.serialize(self.geppetto_model)) else: return utils.getJSONReply() except: return utils.getJSONError("Error while loading the NetPyNE model", sys.exc_info())
from netpyne import sim from netpyne import specs nml2_file_name = 'SimpleNet.net.nml' simConfig = specs.SimConfig() # dictionary to store simConfig # Simulation parameters simConfig.duration = 10000 # Duration of the simulation, in ms simConfig.dt = 0.025 # Internal integration timestep to use simConfig.verbose = True simConfig.recordCells = ['all'] # which cells to record from simConfig.recordTraces = {'Vsoma':{'sec':'soma','loc':0.5,'var':'v'}} simConfig.filename = 'SimpleNet' # Set file output name simConfig.saveDat = True # save traces simConfig.plotRaster = True # Whether or not to plot a raster simConfig.plotCells = ['all'] # plot recorded traces for this list of cells sim.importNeuroML2SimulateAnalyze(nml2_file_name,simConfig)
def plot_batch_ind_stats(batchLabel, batchdatadir=batchdatadir, include=['allCells', 'eachPop'], statDataIn={}, timeRange=None, graphType='boxplot', stats=['rate', 'isicv'], bins=50, popColors=[], histlogy=False, histlogx=False, histmin=0.0, density=False, includeRate0=False, legendLabels=None, normfit=False, histShading=True, xlim=None, dpi=100, figSize=(6, 8), fontSize=12, saveData=None, showFig=True, save=True, outputdir='batch_figs'): """Plots individual connectivity plots for each parameter combination.""" from netpyne import specs if type(batchLabel) == str: params, data = batch_utils.load_batch(batchLabel, batchdatadir=batchdatadir) elif type(batchLabel) == tuple: batchLabel, params, data = batchLabel else: raise Exception() simLabels = data.keys() for simLabel in simLabels: print('Plotting sim: ' + simLabel) datum = data[simLabel] cfg = specs.SimConfig(datum['simConfig']) cfg.createNEURONObj = False sim.initialize() # create network object and set cfg and net params sim.loadAll('', data=datum, instantiate=False) sim.setSimCfg(cfg) try: print('Cells created: ' + str(len(sim.net.allCells))) except: print('Alternate sim loading...') sim.net.createPops() sim.net.createCells() sim.setupRecording() sim.gatherData() sim.allSimData = datum['simData'] if save: saveFig = batchdatadir + '/' + batchLabel + '/' + 'statFig_' + simLabel else: saveFig = None sim.analysis.plotSpikeStats(include=include, statDataIn=statDataIn, timeRange=timeRange, graphType=graphType, stats=stats, bins=bins, popColors=popColors, histlogy=histlogy, histlogx=histlogx, histmin=histmin, density=density, includeRate0=includeRate0, legendLabels=legendLabels, normfit=normfit, histShading=histShading, xlim=xlim, dpi=dpi, figSize=figSize, fontSize=fontSize, saveData=saveData, saveFig=saveFig, showFig=showFig)
def generate_and_run(simulation, simulator, network=None, return_results=False, base_dir=None, target_dir=None, num_processors=1): """ Generates the network in the specified simulator and runs, if appropriate """ if network == None: network = load_network_json(simulation.network) print_v("Generating network %s and running in simulator: %s..." % (network.id, simulator)) if simulator == 'NEURON': _generate_neuron_files_from_neuroml(network, dir_for_mod_files=target_dir) from neuromllite.NeuronHandler import NeuronHandler nrn_handler = NeuronHandler() for c in network.cells: if c.neuroml2_source_file: src_dir = os.path.dirname( os.path.abspath(c.neuroml2_source_file)) nrn_handler.executeHoc('load_file("%s/%s.hoc")' % (src_dir, c.id)) generate_network(network, nrn_handler, generate_network, base_dir) if return_results: raise NotImplementedError( "Reloading results not supported in Neuron yet...") elif simulator.lower() == 'sonata': # Will not "run" obviously... from neuromllite.SonataHandler import SonataHandler sonata_handler = SonataHandler() generate_network(network, sonata_handler, always_include_props=True, base_dir=base_dir) print_v("Done with Sonata...") elif simulator.lower().startswith('graph'): # Will not "run" obviously... from neuromllite.GraphVizHandler import GraphVizHandler, engines try: if simulator[-1].isalpha(): engine = engines[simulator[-1]] level = int(simulator[5:-1]) else: engine = 'dot' level = int(simulator[5:]) except Exception as e: print_v("Error parsing: %s: %s" % (simulator, e)) print_v( "Graphs of the network structure can be generated at many levels of detail (1-6, required) and laid out using GraphViz engines (d - dot (default); c - circo; n - neato; f - fdp), so use: -graph3c, -graph2, -graph4f etc." ) return handler = GraphVizHandler(level, engine=engine, nl_network=network) generate_network(network, handler, always_include_props=True, base_dir=base_dir) print_v("Done with GraphViz...") elif simulator.lower().startswith('matrix'): # Will not "run" obviously... from neuromllite.MatrixHandler import MatrixHandler try: level = int(simulator[6:]) except: print_v("Error parsing: %s" % simulator) print_v( "Matrices of the network structure can be generated at many levels of detail (1-n, required), so use: -matrix1, -matrix2, etc." ) return handler = MatrixHandler(level, nl_network=network) generate_network(network, handler, always_include_props=True, base_dir=base_dir) print_v("Done with MatrixHandler...") elif simulator.startswith('PyNN'): #_generate_neuron_files_from_neuroml(network) simulator_name = simulator.split('_')[1].lower() from neuromllite.PyNNHandler import PyNNHandler pynn_handler = PyNNHandler(simulator_name, simulation.dt, network.id) syn_cell_params = {} for proj in network.projections: synapse = network.get_child(proj.synapse, 'synapses') post_pop = network.get_child(proj.postsynaptic, 'populations') if not post_pop.component in syn_cell_params: syn_cell_params[post_pop.component] = {} for p in synapse.parameters: post = '' if synapse.pynn_receptor_type == "excitatory": post = '_E' elif synapse.pynn_receptor_type == "inhibitory": post = '_I' syn_cell_params[post_pop.component][ '%s%s' % (p, post)] = synapse.parameters[p] cells = {} for c in network.cells: if c.pynn_cell: cell_params = {} if c.parameters: for p in c.parameters: cell_params[p] = evaluate(c.parameters[p], network.parameters) dont_set_here = [ 'tau_syn_E', 'e_rev_E', 'tau_syn_I', 'e_rev_I' ] for d in dont_set_here: if d in c.parameters: raise Exception( 'Synaptic parameters like %s should be set ' + 'in individual synapses, not in the list of parameters associated with the cell' % d) if c.id in syn_cell_params: cell_params.update(syn_cell_params[c.id]) print_v("Creating cell with params: %s" % cell_params) exec('cells["%s"] = pynn_handler.sim.%s(**cell_params)' % (c.id, c.pynn_cell)) if c.pynn_cell != 'SpikeSourcePoisson': exec( "cells['%s'].default_initial_values['v'] = cells['%s'].parameter_space['v_rest'].base_value" % (c.id, c.id)) pynn_handler.set_cells(cells) receptor_types = {} for s in network.synapses: if s.pynn_receptor_type: receptor_types[s.id] = s.pynn_receptor_type pynn_handler.set_receptor_types(receptor_types) for input_source in network.input_sources: if input_source.pynn_input: pynn_handler.add_input_source(input_source) generate_network(network, pynn_handler, always_include_props=True, base_dir=base_dir) for pid in pynn_handler.populations: pop = pynn_handler.populations[pid] if 'all' in simulation.recordTraces or pop.label in simulation.recordTraces: if pop.can_record('v'): pop.record('v') pynn_handler.sim.run(simulation.duration) pynn_handler.sim.end() traces = {} events = {} if not 'NeuroML' in simulator: from neo.io import PyNNTextIO for pid in pynn_handler.populations: pop = pynn_handler.populations[pid] if 'all' in simulation.recordTraces or pop.label in simulation.recordTraces: filename = "%s.%s.v.dat" % (simulation.id, pop.label) all_columns = [] print_v("Writing data for %s to %s" % (pop.label, filename)) for i in range(len(pop)): if pop.can_record('v'): ref = '%s[%i]' % (pop.label, i) traces[ref] = [] data = pop.get_data('v', gather=False) for segment in data.segments: vm = segment.analogsignals[0].transpose()[i] if len(all_columns) == 0: tt = np.array([ t * simulation.dt / 1000. for t in range(len(vm)) ]) all_columns.append(tt) vm_si = [float(v / 1000.) for v in vm] traces[ref] = vm_si all_columns.append(vm_si) times_vm = np.array(all_columns).transpose() np.savetxt(filename, times_vm, delimiter='\t', fmt='%s') if return_results: _print_result_info(traces, events) return traces, events elif simulator == 'NetPyNE': if target_dir == None: target_dir = './' _generate_neuron_files_from_neuroml(network, dir_for_mod_files=target_dir) from netpyne import specs from netpyne import sim # Note NetPyNE from this branch is required: https://github.com/Neurosim-lab/netpyne/tree/neuroml_updates from netpyne.conversion.neuromlFormat import NetPyNEBuilder import pprint pp = pprint.PrettyPrinter(depth=6) netParams = specs.NetParams() simConfig = specs.SimConfig() netpyne_handler = NetPyNEBuilder(netParams, simConfig=simConfig, verbose=True) generate_network(network, netpyne_handler, base_dir=base_dir) netpyne_handler.finalise() simConfig = specs.SimConfig() simConfig.tstop = simulation.duration simConfig.duration = simulation.duration simConfig.dt = simulation.dt simConfig.seed = simulation.seed simConfig.recordStep = simulation.dt simConfig.recordCells = ['all'] simConfig.recordTraces = {} for pop in netpyne_handler.popParams.values(): if 'all' in simulation.recordTraces or pop.id in simulation.recordTraces: for i in pop['cellsList']: id = pop['pop'] index = i['cellLabel'] simConfig.recordTraces['v_%s_%s' % (id, index)] = { 'sec': 'soma', 'loc': 0.5, 'var': 'v', 'conds': { 'pop': id, 'cellLabel': index } } simConfig.saveDat = True print_v("NetPyNE netParams: ") pp.pprint(netParams.todict()) #print_v("NetPyNE simConfig: ") #pp.pprint(simConfig.todict()) sim.initialize( netParams, simConfig) # create network object and set cfg and net params sim.net.createPops() cells = sim.net.createCells( ) # instantiate network cells based on defined populations for proj_id in netpyne_handler.projection_infos.keys(): projName, prePop, postPop, synapse, ptype = netpyne_handler.projection_infos[ proj_id] print_v("Creating connections for %s (%s): %s->%s via %s" % (projName, ptype, prePop, postPop, synapse)) preComp = netpyne_handler.pop_ids_vs_components[prePop] for conn in netpyne_handler.connections[projName]: pre_id, pre_seg, pre_fract, post_id, post_seg, post_fract, delay, weight = conn #connParam = {'delay':delay,'weight':weight,'synsPerConn':1, 'sec':post_seg, 'loc':post_fract, 'threshold':threshold} connParam = { 'delay': delay, 'weight': weight, 'synsPerConn': 1, 'sec': post_seg, 'loc': post_fract } if ptype == 'electricalProjection': if weight != 1: raise Exception( 'Cannot yet support inputs where weight !=1!') connParam = { 'synsPerConn': 1, 'sec': post_seg, 'loc': post_fract, 'gapJunction': True, 'weight': weight } else: connParam = { 'delay': delay, 'weight': weight, 'synsPerConn': 1, 'sec': post_seg, 'loc': post_fract } #'threshold': threshold} connParam['synMech'] = synapse if post_id in sim.net.gid2lid: # check if postsyn is in this node's list of gids sim.net._addCellConn(connParam, pre_id, post_id) stims = sim.net.addStims( ) # add external stimulation to cells (IClamps etc) simData = sim.setupRecording( ) # setup variables to record for each cell (spikes, V traces, etc) sim.runSim() # run parallel Neuron simulation sim.gatherData() # gather spiking data and cell info from each node sim.saveData( ) # save params, cell info and sim output to file (pickle,mat,txt,etc) if return_results: raise NotImplementedError( "Reloading results not supported in NetPyNE yet...") elif simulator == 'jNeuroML' or simulator == 'jNeuroML_NEURON' or simulator == 'jNeuroML_NetPyNE': from pyneuroml.lems import generate_lems_file_for_neuroml from pyneuroml import pynml lems_file_name = 'LEMS_%s.xml' % simulation.id nml_file_name, nml_doc = generate_neuroml2_from_network( network, base_dir=base_dir, target_dir=target_dir) included_files = ['PyNN.xml'] for c in network.cells: if c.lems_source_file: included_files.append(c.lems_source_file) ''' if network.cells: for c in network.cells: included_files.append(c.neuroml2_source_file) ''' if network.synapses: for s in network.synapses: if s.lems_source_file: included_files.append(s.lems_source_file) print_v("Generating LEMS file prior to running in %s" % simulator) pops_plot_save = [] pops_spike_save = [] gen_plots_for_quantities = {} gen_saves_for_quantities = {} for p in network.populations: if simulation.recordTraces and ('all' in simulation.recordTraces or p.id in simulation.recordTraces): pops_plot_save.append(p.id) if simulation.recordSpikes and ('all' in simulation.recordSpikes or p.id in simulation.recordSpikes): pops_spike_save.append(p.id) if simulation.recordRates and ('all' in simulation.recordRates or p.id in simulation.recordRates): size = evaluate(p.size, network.parameters) for i in range(size): quantity = '%s/%i/%s/r' % (p.id, i, p.component) gen_plots_for_quantities['%s_%i_r' % (p.id, i)] = [quantity] gen_saves_for_quantities['%s_%i.r.dat' % (p.id, i)] = [quantity] if simulation.recordVariables: for var in simulation.recordVariables: to_rec = simulation.recordVariables[var] if ('all' in to_rec or p.id in to_rec): size = evaluate(p.size, network.parameters) for i in range(size): quantity = '%s/%i/%s/%s' % (p.id, i, p.component, var) gen_plots_for_quantities['%s_%i_%s' % (p.id, i, var)] = [ quantity ] gen_saves_for_quantities['%s_%i.%s.dat' % (p.id, i, var)] = [ quantity ] generate_lems_file_for_neuroml( simulation.id, nml_file_name, network.id, simulation.duration, simulation.dt, lems_file_name, target_dir=target_dir if target_dir else '.', nml_doc= nml_doc, # Use this if the nml doc has already been loaded (to avoid delay in reload) include_extra_files=included_files, gen_plots_for_all_v=False, plot_all_segments=False, gen_plots_for_quantities= gen_plots_for_quantities, # Dict with displays vs lists of quantity paths gen_plots_for_only_populations= pops_plot_save, # List of populations, all pops if = [] gen_saves_for_all_v=False, save_all_segments=False, gen_saves_for_only_populations= pops_plot_save, # List of populations, all pops if = [] gen_saves_for_quantities= gen_saves_for_quantities, # Dict with file names vs lists of quantity paths gen_spike_saves_for_all_somas=False, gen_spike_saves_for_only_populations= pops_spike_save, # List of populations, all pops if = [] gen_spike_saves_for_cells= {}, # Dict with file names vs lists of quantity paths spike_time_format='ID_TIME', copy_neuroml=True, lems_file_generate_seed=12345, report_file_name='report.%s.txt' % simulation.id, simulation_seed=simulation.seed if simulation.seed else 12345, verbose=True) lems_file_name = _locate_file(lems_file_name, target_dir) if simulator == 'jNeuroML': results = pynml.run_lems_with_jneuroml( lems_file_name, nogui=True, load_saved_data=return_results, reload_events=return_results) elif simulator == 'jNeuroML_NEURON': results = pynml.run_lems_with_jneuroml_neuron( lems_file_name, nogui=True, load_saved_data=return_results, reload_events=return_results) elif simulator == 'jNeuroML_NetPyNE': results = pynml.run_lems_with_jneuroml_netpyne( lems_file_name, nogui=True, verbose=True, load_saved_data=return_results, reload_events=return_results, num_processors=num_processors) print_v("Finished running LEMS file %s in %s (returning results: %s)" % (lems_file_name, simulator, return_results)) if return_results: traces, events = results _print_result_info(traces, events) return results # traces, events =
from neuron import h import sys ############################################################################### # NETWORK PARAMETERS ############################################################################### nml2_file_name = 'TestL23.net.nml' ############################################################################### # SIMULATION PARAMETERS ############################################################################### simConfig = specs.SimConfig( ) # object of class SimConfig to store the simulation configuration # Simulation parameters simConfig.duration = simConfig.tstop = 200.0 # Duration of the simulation, in ms simConfig.dt = 0.025 # Internal integration timestep to use # Seeds for randomizers (connectivity, input stimulation and cell locations) # Note: locations and connections should be fully specified by the structure of the NeuroML, # so seeds for conn & loc shouldn't affect networks structure/behaviour simConfig.seeds = {'conn': 0, 'stim': 12345, 'loc': 0} simConfig.createNEURONObj = 1 # create HOC objects when instantiating network simConfig.createPyStruct = 1 # create Python structure (simulator-independent) when instantiating network simConfig.verbose = False # show detailed messages simConfig.hParams['celsius'] = (305.15 - 273.15)
""" tut_gap.py Tutorial on using gap junctions """ from netpyne import specs, sim from netpyne.specs import Dict netParams = specs.NetParams( ) # object of class NetParams to store the network parameters simConfig = specs.SimConfig( ) # dictionary to store sets of simulation configurations ############################################################################### # NETWORK PARAMETERS ############################################################################### # Population parameters netParams.popParams['PYR1'] = { 'cellModel': 'HH', 'cellType': 'PYR', 'numCells': 1 } # add dict with params for this pop netParams.popParams['PYR2'] = { 'cellModel': 'HH', 'cellType': 'PYR', 'numCells': 1 } # add dict with params for this pop netParams.popParams['background'] = { 'cellModel': 'NetStim',
def setCfg(): return specs.SimConfig()
def set_params(input_rs_threshold): netParams = specs.NetParams() # object of class NetParams to store the network parameters simConfig = specs.SimConfig() # object of class SimConfig to store the simulation configuration ############################################################################### # NETWORK PARAMETERS ############################################################################### # Population parameters netParams.popParams['PYR'] = {'cellModel': 'PYR', 'cellType': 'PYR', 'numCells': 1, 'color': 'blue'} # Cell parameters ## PYR cell properties cellRule = netParams.importCellParams(label='PYR', conds= {'cellType': 'PYR', 'cellModel': 'PYR'}, fileName='Cells/pyr_23_asym_stripped.hoc', cellName='pyr_23_asym_stripped') ''' cellRule = netParams.importCellParams(label='PYR4', conds={'cellType': 'PYR', 'cellModel': 'PYR_Hay'}, fileName='Cells/fourcompartment.hoc', cellName='fourcompartment') netParams.popParams['BASK4'] = {'cellModel': 'BASK_Vierling', 'cellType': 'BASK', 'gridSpacing': 80.0, 'yRange': [netParams.sizeY, netParams.sizeY], 'color': 'yellow'} ''' currents_rs = [0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3] threshold_rs = input_rs_threshold for i,c in enumerate(currents_rs): current = c + threshold_rs delay = 200 + i*2000 netParams.stimSourceParams['ic'+str(i)] = {'type': 'IClamp', 'delay': delay, 'dur': 1000.0, 'amp': current} netParams.stimTargetParams['ic->PYR'+str(i)] = {'source': 'ic'+str(i), 'conds': {'popLabel': 'PYR'}, 'sec':'soma','loc':0.5} ############################################################################### # SIMULATION PARAMETERS ############################################################################### # Simulation parameters simConfig.hParams['celsius'] = 30.0 simConfig.duration = 20000 # Duration of the simulation, in ms simConfig.dt = 0.025 # Internal integration timestep to use simConfig.seeds = {'conn': 1, 'stim': 1, 'loc': 1} # Seeds for randomizers (connectivity, input stimulation and cell locations) simConfig.createNEURONObj = 1 # create HOC objects when instantiating network simConfig.createPyStruct = 1 # create Python structure (simulator-independent) when instantiating network simConfig.verbose = False # show detailed messages simConfig.printPopAvgRates = True #simConfig.verbose = True # Recording simConfig.recordCells = ['all'] # which cells to record from simConfig.recordStep = 0.1 # Step size in ms to save data (eg. V traces, LFP, etc) simConfig.recordTraces = {'V_soma':{'sec':'soma','loc':0.5,'var':'v'}} simConfig.hParams['cai0_ca_ion'] = 0.0001 # Saving simConfig.filename = 'output_files/orig_ion_channels/Data_{}'.format(input_rs_threshold) # Set file output name simConfig.saveFileStep = 1000 # step size in ms to save data to disk simConfig.saveJson = False # Whether or not to write spikes etc. to a .json file simConfig.analysis['plotShape']= {'includePost':[0], 'showSyns':1, 'synStyle':'.', 'synSiz':3, 'saveFig': 'output_files/asym_Shape.png'} return (netParams, simConfig)
def importModel(self, modelParameters): """ Imports a model stored in form of Python files. :param modelParameters: :return: """ if self.doIhaveInstOrSimData()['haveInstance']: # TODO: this must be integrated into the general lifecycle of "model change -> simulate" # Shouldn't be specific to Import sim.clearAll() try: # Get Current dir owd = os.getcwd() compileModMechFiles(modelParameters['compileMod'], modelParameters['modFolder']) with redirect_stdout(sys.__stdout__): # NetParams net_params_path = str(modelParameters["netParamsPath"]) sys.path.append(net_params_path) os.chdir(net_params_path) # Import Module net_params_module_name = importlib.import_module( str(modelParameters["netParamsModuleName"])) # Import Model attributes self.netParams = getattr( net_params_module_name, str(modelParameters["netParamsVariable"])) if isinstance(self.netParams, dict): self.netParams = specs.NetParams(self.netParams) if isinstance(self.simConfig, dict): self.simConfig = specs.SimConfig(self.simConfig) for key, value in self.netParams.cellParams.items(): if hasattr(value, 'todict'): self.netParams.cellParams[key] = value.todict() # SimConfig sim_config_path = str(modelParameters["simConfigPath"]) sys.path.append(sim_config_path) os.chdir(sim_config_path) # Import Module sim_config_module_name = importlib.import_module( str(modelParameters["simConfigModuleName"])) # Import Model attributes self.simConfig = getattr( sim_config_module_name, str(modelParameters["simConfigVariable"])) # TODO: when should sim.initialize be called? # Only on import or better before every simulation or network instantiation? sim.initialize() return utils.getJSONReply() except Exception: message = "Error while importing the NetPyNE model" logging.exception(message) return utils.getJSONError(message, sys.exc_info()) finally: os.chdir(owd)
def exportHLS(self, args): def convert2bool(string): return string.replace('true', 'True').replace('false', 'False').replace( 'null', 'False') def header(title, spacer='-'): return '\n# ' + title.upper() + ' ' + spacer * (77 - len(title)) + '\n' try: params = ['popParams', 'cellParams', 'synMechParams'] params += ['connParams', 'stimSourceParams', 'stimTargetParams'] fname = args['fileName'] if args['fileName'][ -3:] == '.py' else args['fileName'] + '.py' with open(fname, 'w') as script: script.write('from netpyne import specs, sim\n') script.write(header('documentation')) script.write( "''' Script generated with NetPyNE-UI. Please visit:\n") script.write( " - https://www.netpyne.org\n - https://github.com/MetaCell/NetPyNE-UI\n'''\n" ) script.write(header('script', spacer='=')) script.write('netParams = specs.NetParams()\n') script.write('simConfig = specs.SimConfig()\n') script.write(header('single value attributes')) for attr, value in list(self.netParams.__dict__.items()): if attr not in params: if value != getattr(specs.NetParams(), attr): script.write('netParams.' + attr + ' = ') script.write( convert2bool(json.dumps(value, indent=4)) + '\n') script.write(header('network attributes')) for param in params: for key, value in list( getattr(self.netParams, param).items()): script.write("netParams." + param + "['" + key + "'] = ") script.write( convert2bool(json.dumps(value, indent=4)) + '\n') script.write(header('network configuration')) for attr, value in list(self.simConfig.__dict__.items()): if value != getattr(specs.SimConfig(), attr): script.write('simConfig.' + attr + ' = ') script.write( convert2bool(json.dumps(value, indent=4)) + '\n') script.write(header('create simulate analyze network')) script.write( '# sim.createSimulateAnalyze(netParams=netParams, simConfig=simConfig)\n' ) script.write(header('end script', spacer='=')) with open(fname) as f: return f.read() except: return utils.getJSONError( "Error while importing the NetPyNE model", sys.exc_info())
# based on netpyne/doc/source/code/tut7.py from netpyne import sim, specs cfg, par = specs.SimConfig(), specs.NetParams() par.popParams['hop'] = {'cellType': 'PYR', 'cellModel': 'HH', 'numCells': 50} par.cellParams['PYR'] = { 'conds': { 'cellModel': 'HH', 'cellType': 'PYR' }, 'secs': { 'soma': { 'geom': { 'diam': 18.8, 'L': 18.8, 'Ra': 123.0 }, 'mechs': { 'hh': {} }, 'vinit': -70 } } } par.synMechParams['exc'] = {'mod': 'Exp2Syn', 'tau1': 0.1, 'tau2': 1.0, 'e': 0} par.synMechParams['inh'] = { 'mod': 'Exp2Syn', 'tau1': 0.1, 'tau2': 1.0, 'e': -80
def plot_batch_ind_raster(batchLabel, batchdatadir=batchdatadir, include=['allCells'], timeRange=None, maxSpikes=1e8, orderBy='gid', orderInverse=False, labels='legend', popRates=False, spikeHist=None, spikeHistBin=5, syncLines=False, figSize=(10, 8), saveData=None, showFig=False, save=True, outputdir='batch_figs'): """Plots individual raster plots for each parameter combination.""" from netpyne import specs if type(batchLabel) == str: params, data = batch_utils.load_batch(batchLabel, batchdatadir=batchdatadir) elif type(batchLabel) == tuple: batchLabel, params, data = batchLabel else: raise Exception() simLabels = data.keys() for simLabel in simLabels: print('Plotting sim: ' + simLabel) datum = data[simLabel] cfg = specs.SimConfig(datum['simConfig']) cfg.createNEURONObj = False sim.initialize() # create network object and set cfg and net params sim.loadAll('', data=datum, instantiate=False) sim.setSimCfg(cfg) try: print('Cells created: ' + str(len(sim.net.allCells))) except: print('Alternate sim loading...') sim.net.createPops() sim.net.createCells() sim.setupRecording() sim.gatherData() sim.allSimData = datum['simData'] if save: saveFig = batchdatadir + '/' + batchLabel + '/' + 'rasterFig' + simLabel + '.png' else: saveFig = None sim.analysis.plotRaster(include=include, timeRange=timeRange, maxSpikes=maxSpikes, orderBy=orderBy, orderInverse=orderInverse, labels=labels, popRates=popRates, spikeHist=spikeHist, spikeHistBin=spikeHistBin, syncLines=syncLines, figSize=figSize, saveData=saveData, saveFig=saveFig, showFig=showFig)
def createPythonScript(fname, netParams, simConfig): """ Function for/to <short description of `netpyne.conversion.pythonScript.createPythonScript`> Parameters ---------- fname : <type> <Short description of fname> **Default:** *required* netParams : <type> <Short description of netParams> **Default:** *required* simConfig : <type> <Short description of simConfig> **Default:** *required* """ import sys import json from netpyne import specs def replace(string): # convert bools and null from json to python return string.replace('true', 'True').replace('false', 'False').replace('null', '""') def remove(dictionary): # remove reserved keys such as __str__, __dict__ if isinstance(dictionary, dict): for key, value in list(dictionary.items()): if key.startswith('__'): dictionary.pop(key) else: remove(value) def addAttrToScript(attr, value, obj_name, class_instance, file): # write line of netpyne code if is different from default value if not hasattr(class_instance, attr) or value != getattr(class_instance, attr): file.write(obj_name + '.' + attr + ' = ' + replace(json.dumps(value, indent=4)) + '\n') def header(title, spacer='-'): # writes a header for the section return '\n# ' + title.upper() + ' ' + spacer * (77 - len(title)) + '\n' if isinstance(netParams, specs.NetParams): # convert netpyne.specs.netParams class to dict class netParams = netParams.todict() if isinstance(simConfig, specs.SimConfig): simConfig = simConfig.todict() # remove reserved keys like __str__, __dict__ remove(netParams) remove(simConfig) # network parameters params = ['popParams', 'cellParams', 'synMechParams'] params += ['connParams', 'stimSourceParams', 'stimTargetParams'] try: with open(fname if fname.endswith('.py') else fname + '.py', 'w') as file: file.write('from netpyne import specs, sim\n') file.write(header('documentation')) file.write("''' Please visit: https://www.netpyne.org '''\n") file.write( "# Python script automatically generated by NetPyNE v%s from netParams and simConfig objects\n" % __version__) file.write(header('script', spacer='=')) file.write('netParams = specs.NetParams()\n') file.write('simConfig = specs.SimConfig()\n') file.write(header('single valued attributes')) for key, value in list(netParams.items()): if key not in params: addAttrToScript(key, value, 'netParams', specs.NetParams(), file) file.write(header('network attributes')) for param in params: for key, value in list(netParams[param].items()): file.write("netParams." + param + "['" + key + "'] = " + replace(json.dumps(value, indent=4)) + '\n') file.write(header('network configuration')) for key, value in list(simConfig.items()): addAttrToScript(key, value, 'simConfig', specs.SimConfig(), file) file.write(header('create simulate analyze network')) file.write('import sys\n') file.write('if not "-neuroml" in sys.argv:\n') file.write( ' sim.createSimulateAnalyze(netParams=netParams, simConfig=simConfig)\n' ) file.write('else:\n') file.write( ' nml_reference = "NetPyNENetwork" if not simConfig.filename else simConfig.filename\n' ) file.write( ' sim.createExportNeuroML2(netParams=netParams, simConfig=simConfig, reference = nml_reference)\n' ) file.write(header('end script', spacer='=')) print(("script saved on " + fname)) except: print(('error saving file: %s' % (sys.exc_info()[1])))
def plot_batch_ind_conn(batchLabel, batchdatadir=batchdatadir, includePre=['all'], includePost=['all'], feature='strength', orderBy='gid', figSize=(10, 10), groupBy='pop', groupByIntervalPre=None, groupByIntervalPost=None, graphType='matrix', synOrConn='syn', synMech=None, connsFile=None, tagsFile=None, clim=None, fontSize=12, saveData=None, showFig=False, save=True, outputdir='batch_figs'): """Plots individual connectivity plots for each parameter combination.""" from netpyne import specs if type(batchLabel) == str: params, data = batch_utils.load_batch(batchLabel, batchdatadir=batchdatadir) elif type(batchLabel) == tuple: batchLabel, params, data = batchLabel else: raise Exception() simLabels = data.keys() for simLabel in simLabels: print('Plotting sim: ' + simLabel) datum = data[simLabel] cfg = specs.SimConfig(datum['simConfig']) cfg.createNEURONObj = False sim.initialize() # create network object and set cfg and net params sim.loadAll('', data=datum, instantiate=False) sim.setSimCfg(cfg) try: print('Cells created: ' + str(len(sim.net.allCells))) except: print('Alternate sim loading...') sim.net.createPops() sim.net.createCells() sim.setupRecording() sim.gatherData() sim.allSimData = datum['simData'] features = [ 'weight', 'delay', 'numConns', 'probability', 'strength', 'convergence', 'divergence' ] for feature in features: if save: saveFig = batchdatadir + '/' + batchLabel + '/' + 'connFig_' + feature + simLabel + '.png' else: saveFig = None sim.analysis.plotConn(includePre=includePre, includePost=includePost, feature=feature, orderBy=orderBy, figSize=figSize, groupBy=groupBy, groupByIntervalPre=groupByIntervalPre, groupByIntervalPost=groupByIntervalPost, graphType=graphType, synOrConn=synOrConn, synMech=synMech, connsFile=connsFile, tagsFile=tagsFile, clim=clim, fontSize=fontSize, saveData=saveData, saveFig=saveFig, showFig=showFig)
# -*- coding: utf-8 -*- """ Created on Wed Aug 10 14:23:15 2016 @author: Ryoha """ from netpyne import specs,sim netParams = specs.NetParams() simConfig = specs.SimConfig() #popParams netParams.addPopParams('S', {'cellType': 'S', 'numCells':1, 'cellModel': 'HH'}) netParams.addPopParams('M',{'cellType': 'M', 'numCells':1, 'cellModel':'HH'}) #cellRules cellRule = {'conds': {'popLabel': 'S', 'popLabel':'M'}, 'secs':{}} cellRule['secs']['soma'] = {'geom':{}, 'mechs':{}} cellRule['secs']['soma']['geom'] = {'diam': 18.8, 'L': 18.8, 'Ra': 123.0} cellRule['secs']['soma']['mechs']['hh'] = {'gnabar': 0.12, 'gkbar': 0.036, 'gl': 0.003, 'el': -70} cellRule['secs']['dend'] = {'geom' : {}, 'topol':{}, 'mechs':{}} cellRule['secs']['dend']['geom']= {'diam': 5.0, 'L': 150.0, 'Ra': 150.0, 'cm': 1} cellRule['secs']['dend']['mechs']['pas'] = {'g': 0.0000357, 'e': -70} cellRule['secs']['dend']['topol'] = {'parentSec': 'soma', 'parentX':1.0, 'childX': 0} netParams.addCellParams('rule', cellRule) #synMechs netParams.addSynMechParams('exc', {'mod': 'Exp2Syn', 'tau1': 0.1, 'tau2': 5.0, 'e': 0}) #connRules netParams.addConnParams('S->M',{'preConds': {'popLabel': 'S'},'postConds': {'popLabel': 'M'}, 'delay': 5, 'synsPerConn': 3, 'sec':'dend', 'loc': [0.3,0.6,0.9],'synMech':'exc'}) #stims netParams.addStimSourceParams('Input_1',{'type': 'VClamp', 'dur':[0,10,50],'amp':[20,40,50],'gain':1, 'rstim':5, 'tau1':5, 'tau2':3, 'i':5})
""" cfgIC.py """ from netpyne import specs import numpy as np def arr(start, end, incr): return np.array( [float("%.3f" % (x)) for x in np.arange(start, end + incr / 2, incr)]) cfg = specs.SimConfig() #simulation parameters (fixed timestep) #cfg.dt = 0.0125 #cfg.cvode_active = False cfg.cvode_active = True cfg.recordStims = False cfg.recordStep = 0.25 #toggle cell models to sim cfg.simso = True cfg.simtj = False cfg.simxso = False cfg.simxtj = False #netParam vars cfg.freqs = [1000] cfg.npulsess = [1] #threshold data for amplitude cfg.amps = arr(0.175, 0.225, 0.00625) #cfg.amps = arr( 1, 2, 0.1 )
} ### rates netParams.rxdParams['rates'] = { 'degradation': { 'species': 'buf', 'rate': '-1e-3 * buf' } } #------------------------------------------------------------------------------ # SIMULATION CONFIGURATION# #------------------------------------------------------------------------------ # Run parameters cfg = specs.SimConfig( ) # object of class cfg to store simulation configuration cfg.duration = 500 # Duration of the simulation, in ms cfg.recordTraces = { 'ca': { 'sec': 'soma', 'loc': 0.5, 'var': 'cai' }, 'buf': { 'sec': 'soma', 'loc': 0.5, 'var': 'bufi' }, 'cabuf': { 'sec': 'soma', 'loc': 0.5,