def cloneExperiment(self, payload: dict):
        """ Loads experiment from disk and replaces experiment in design with it.

        1. Replaces current experiment in design with copy of stored experiment.
        2. Replace current model specification with spec of stored experiment.

        :param payload: { name: str, replaceModelSpec: bool, replaceExperiment: bool }
        """
        name = payload.get('name')

        # Creates new Experiment in design based on `name` experiment.
        if payload.get('replaceExperiment', True):
            experiments.replace_current_with(name)

        # Replaces model specification
        if payload.get('replaceModelSpec', True):
            path = os.path.join(constants.EXPERIMENTS_FOLDER_PATH, name)
            if self.doIhaveInstOrSimData()['haveInstance']:
                sim.clearAll()

            sim.initialize()
            sim.loadNetParams(os.path.join(path, experiments.NET_PARAMS_FILE))
            sim.loadSimCfg(os.path.join(path, experiments.SIM_CONFIG_FILE))
            self.netParams = sim.net.params
            self.simConfig = sim.cfg
            netpyne_ui_utils.remove(self.simConfig.todict())
            netpyne_ui_utils.remove(self.netParams.todict())
Exemple #2
0
def create ():
  global pops,cells
  sim.initialize(netParams, simConfig)
  pops = sim.net.createPops()                      # instantiate network populations
  cells = sim.net.createCells()                     # instantiate network cells based on defined populations
  sim.net.addStims()
  sim.net.connectCells()                    # create connections between cells based on params
  sim.setupRecording()                      # setup variables to record for each cell (spikes, V traces, etc)
Exemple #3
0
    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"]))

                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:
            return utils.getJSONError(
                "Error while importing the NetPyNE model", sys.exc_info())
        finally:
            os.chdir(owd)
Exemple #4
0
def create():
    global pops, cells
    sim.initialize(netParams, simConfig)
    pops = sim.net.createPops()  # instantiate network populations
    cells = sim.net.createCells(
    )  # instantiate network cells based on defined populations
    sim.net.addStims()
    sim.net.connectCells()  # create connections between cells based on params
    sim.setupRecording(
    )  # setup variables to record for each cell (spikes, V traces, etc)
Exemple #5
0
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)
Exemple #6
0
    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 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()
Exemple #8
0
 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())
Exemple #9
0
    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 viewExperimentResult(self, payload: dict):
        """ Loads the output file of a simulated experiment trial.

        :param payload: {name: str, trial: str, onlyModelSpecification: bool}
        :return: geppetto model
        """
        name = payload.get("name", None)
        trial = payload.get("trial", None)
        only_model_spec = payload.get("onlyModelSpecification", False)

        file = experiments.get_trial_output_path(name, trial)
        if file is None or not os.path.exists(file):
            return utils.getJSONError(
                f"Couldn't find output file of condition. Please take a look at the simulation log.",
                "")

        if self.doIhaveInstOrSimData()['haveInstance']:
            sim.clearAll()

        sim.initialize()

        if only_model_spec:
            # Load only model specification
            sim.loadNetParams(file)
            sim.loadSimCfg(file)
            self.netParams = sim.net.params
            self.simConfig = sim.cfg
            netpyne_ui_utils.remove(self.simConfig.todict())
            netpyne_ui_utils.remove(self.netParams.todict())
            return
        else:
            # Load the complete simulation
            sim.loadAll(file)
            self._create3D_shapes(file)
            self.geppetto_model = self.model_interpreter.getGeppettoModel(sim)
            return json.loads(
                GeppettoModelSerializer.serialize(self.geppetto_model))
    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())
Exemple #12
0
def gatherDataFromFiles(gatherLFP=True,
                        saveFolder=None,
                        simLabel=None,
                        sim=None,
                        fileType='pkl',
                        saveMerged=False):
    """
    Function to gather data from multiple files (from distributed or interval saving)

    Parameters
    ----------
    gatherLFP : bool
        Whether or not to gather LFP data.
        **Default:** ``True`` gathers LFP data if available.
        **Options:** ``False`` does not gather LFP data.

    saveFolder : str
        Name of the directory where data files are located.
        **Default:** ``None`` attempts to auto-locate the data directory.

    """

    import os

    if not sim:
        from netpyne import sim

    if getattr(sim, 'rank', None) is None:
        sim.initialize()
    sim.timing('start', 'gatherTime')

    if sim.rank == 0:

        fileType = fileType.lower()
        if fileType not in ['pkl', 'json']:
            print(
                f"Could not gather data from '.{fileType}' files. Only .pkl and .json are supported so far."
            )
            return False

        if not simLabel:
            simLabel = sim.cfg.simLabel

        if not saveFolder:
            saveFolder = sim.cfg.saveFolder

        nodeDataDir = os.path.join(saveFolder, simLabel + '_node_data')

        print(f"\nSearching for .{fileType} node files in {nodeDataDir} ...")

        simLabels = [
            f.replace(f'_node_0.{fileType}', '')
            for f in os.listdir(nodeDataDir)
            if f.endswith(f'_node_0.{fileType}')
        ]

        if len(simLabels) == 0:
            print(f"Could not gather data from files. No node files found.")
            return False

        mergedFiles = []
        for simLabel in simLabels:

            allSimData = Dict()
            allCells = []
            allPops = ODict()

            print('\nGathering data from files for simulation: %s ...' %
                  (simLabel))

            simDataVecs = ['spkt', 'spkid', 'stims'] + list(
                sim.cfg.recordTraces.keys())
            singleNodeVecs = ['t']

            if sim.cfg.recordDipolesHNN:
                _aggregateDipoles()
                simDataVecs.append('dipole')

            fileData = {'simData': sim.simData}
            fileList = sorted([
                f for f in os.listdir(nodeDataDir)
                if (f.startswith(simLabel +
                                 '_node') and f.endswith(f'.{fileType}'))
            ])

            for ifile, file in enumerate(fileList):

                print('  Merging data file: %s' % (file))

                with open(os.path.join(nodeDataDir, file), 'rb') as openFile:
                    if fileType == 'pkl':
                        data = pickle.load(openFile)
                    elif fileType == 'json':
                        import json
                        data = json.load(openFile)

                    if 'cells' in data.keys():
                        if fileType == 'pkl':
                            allCells.extend([
                                cell.__getstate__() for cell in data['cells']
                            ])
                        else:
                            allCells.extend(data['cells'])

                    if 'pops' in data.keys():
                        loadedPops = data['pops']
                        if fileType == 'pkl':
                            for popLabel, pop in loadedPops.items():
                                allPops[popLabel] = pop['tags']
                        elif fileType == 'json':
                            # if populations order is not preserved (which is inherently the case for JSON), need to sort them again
                            loadedPops = list(loadedPops.items())

                            def sort(popKeyAndValue):
                                # the assumption while sorting is that populations order corresponds to cell gids in this population
                                cellGids = popKeyAndValue[1]['cellGids']
                                if len(cellGids) > 0:
                                    return cellGids[0]
                                else:
                                    return -1

                            loadedPops.sort(key=sort)

                            for popLabel, pop in loadedPops:
                                allPops[popLabel] = pop['tags']

                    if 'simConfig' in data.keys():
                        setup.setSimCfg(data['simConfig'])
                    if 'net' in data and gatherLFP:
                        if 'recXElectrode' in data['net']:
                            xElectrode = data['net']['recXElectrode']
                            if False == isinstance(xElectrode, RecXElectrode):
                                xElectrode = RecXElectrode.fromJSON(xElectrode)
                            sim.net.recXElectrode = xElectrode

                    nodePopsCellGids = {
                        popLabel: list(pop['cellGids'])
                        for popLabel, pop in data['pops'].items()
                    }

                    if ifile == 0 and gatherLFP and 'LFP' in data['simData']:
                        lfpData = data['simData']['LFP']
                        if False == isinstance(lfpData, np.ndarray):
                            lfpData = np.array(lfpData)
                            data['simData']['LFP'] = lfpData

                        allSimData['LFP'] = np.zeros(lfpData.shape)
                        if 'LFPPops' in data['simData']:
                            allSimData['LFPPops'] = {
                                p: np.zeros(lfpData.shape)
                                for p in data['simData']['LFPPops'].keys()
                            }

                    for key, value in data['simData'].items():

                        if key in simDataVecs:

                            if isinstance(value, dict):
                                for key2, value2 in value.items():
                                    if isinstance(value2, dict):
                                        allSimData[key].update(
                                            Dict({key2: Dict()}))
                                        for stim, value3 in value2.items():
                                            allSimData[key][key2].update(
                                                {stim: list(value3)})
                                    elif key == 'dipole':
                                        allSimData[key][key2] = np.add(
                                            allSimData[key][key2],
                                            value2.as_numpy())
                                    else:
                                        allSimData[key].update(
                                            {key2: list(value2)})
                            else:
                                allSimData[key] = list(
                                    allSimData[key]) + list(value)

                        elif gatherLFP and key == 'LFP':
                            allSimData['LFP'] += np.array(value)

                        elif gatherLFP and key == 'LFPPops':
                            for p in value:
                                allSimData['LFPPops'][p] += np.array(value[p])

                        elif key == 'dipoleSum':
                            if key not in allSimData.keys():
                                allSimData[key] = value
                            else:
                                allSimData[key] += value

                        elif key not in singleNodeVecs:
                            allSimData[key].update(value)

                    if file == fileList[0]:
                        for key in singleNodeVecs:
                            allSimData[key] = list(fileData['simData'][key])
                        allPopsCellGids = {
                            popLabel: []
                            for popLabel in nodePopsCellGids
                        }
                    else:
                        for popLabel, popCellGids in nodePopsCellGids.items():
                            allPopsCellGids[popLabel].extend(popCellGids)

                    mergedFiles.append(file)

            if len(allSimData['spkt']) > 0:
                allSimData['spkt'], allSimData['spkid'] = zip(
                    *sorted(zip(allSimData['spkt'], allSimData['spkid'])))
                allSimData['spkt'], allSimData['spkid'] = list(
                    allSimData['spkt']), list(allSimData['spkid'])

            sim.allSimData = allSimData
            sim.net.allCells = sorted(allCells, key=lambda k: k['gid'])
            for popLabel, pop in allPops.items():
                pop['cellGids'] = sorted(allPopsCellGids[popLabel])
            sim.net.allPops = allPops

    ## Print statistics
    sim.pc.barrier()
    if sim.rank != 0:
        sim.pc.barrier()
    else:
        sim.timing('stop', 'gatherTime')
        if sim.cfg.timing:
            print(('  Done; gather time = %0.2f s.' %
                   sim.timingData['gatherTime']))

        if saveMerged:
            print('\nSaving merged data into single file ...')
            saved = sim.saveData()

            if len(saved) > 0:
                # if single file saved successfully, clean up node data
                for file in mergedFiles:
                    path = os.path.join(nodeDataDir, file)
                    os.remove(path)

        print('\nAnalyzing...')

        sim.totalSpikes = len(sim.allSimData['spkt'])
        sim.totalSynapses = sum(
            [len(cell['conns']) for cell in sim.net.allCells])
        if sim.cfg.createPyStruct:
            if sim.cfg.compactConnFormat:
                preGidIndex = sim.cfg.compactConnFormat.index(
                    'preGid') if 'preGid' in sim.cfg.compactConnFormat else 0
                sim.totalConnections = sum([
                    len(set([conn[preGidIndex] for conn in cell['conns']]))
                    for cell in sim.net.allCells
                ])
            else:
                sim.totalConnections = sum([
                    len(set([conn['preGid'] for conn in cell['conns']]))
                    for cell in sim.net.allCells
                ])
        else:
            sim.totalConnections = sim.totalSynapses
        sim.numCells = len(sim.net.allCells)

        if sim.totalSpikes > 0:
            sim.firingRate = float(
                sim.totalSpikes) / sim.numCells / sim.cfg.duration * 1e3
        else:
            sim.firingRate = 0
        if sim.numCells > 0:
            sim.connsPerCell = sim.totalConnections / float(sim.numCells)
            sim.synsPerCell = sim.totalSynapses / float(sim.numCells)
        else:
            sim.connsPerCell = 0
            sim.synsPerCell = 0

        print(('  Cells: %i' % (sim.numCells)))
        print(('  Connections: %i (%0.2f per cell)' %
               (sim.totalConnections, sim.connsPerCell)))
        if sim.totalSynapses != sim.totalConnections:
            print(('  Synaptic contacts: %i (%0.2f per cell)' %
                   (sim.totalSynapses, sim.synsPerCell)))
        print(('  Spikes: %i (%0.2f Hz)' % (sim.totalSpikes, sim.firingRate)))

        if 'runTime' in sim.timingData:
            print(('  Simulated time: %0.1f s; %i workers' %
                   (sim.cfg.duration / 1e3, sim.nhosts)))
            print(('  Run time: %0.2f s' % (sim.timingData['runTime'])))

            if sim.cfg.printPopAvgRates and not sim.cfg.gatherOnlySimData:
                trange = sim.cfg.printPopAvgRates if isinstance(
                    sim.cfg.printPopAvgRates, list) else None
                sim.allSimData['popRates'] = sim.analysis.popAvgRates(
                    tranges=trange)

            if 'plotfI' in sim.cfg.analysis:
                sim.analysis.calculatefI()

            sim.allSimData['avgRate'] = sim.firingRate
    def loadModel(self, args):
        """ Imports a model stored as file in json format.

        :param args:
        :return:
        """
        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 Exception:
            message = "Error while importing/compiling mods"
            logging.exception(message)
            return utils.getJSONError(message, 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
                    netpyne_ui_utils.remove(self.netParams.todict())
                    netpyne_ui_utils.remove(self.simConfig.todict())
                else:
                    if args['loadNet']:
                        wake_up_geppetto = True
                        if self.doIhaveInstOrSimData()['haveInstance']:
                            sim.clearAll()
                        sim.initialize()
                        sim.loadNet(args['jsonModelFolder'])

                    # TODO (https://github.com/Neurosim-lab/netpyne/issues/360)
                    if args['loadSimData']:
                        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
                        netpyne_ui_utils.remove(self.simConfig.todict())

                    if args['loadNetParams']:
                        if self.doIhaveInstOrSimData()['haveInstance']:
                            sim.clearAll()
                        sim.loadNetParams(args['jsonModelFolder'])
                        self.netParams = sim.net.params
                        netpyne_ui_utils.remove(self.netParams.todict())

                if wake_up_geppetto:
                    self._create3D_shapes(args['jsonModelFolder'])

                    # TODO: Fix me - gatherData will remove allSimData!
                    sim.gatherData()

                    self.geppetto_model = self.model_interpreter.getGeppettoModel(
                        sim)
                    return json.loads(
                        GeppettoModelSerializer.serialize(self.geppetto_model))
                else:
                    return utils.getJSONReply()
        except Exception:
            message = "Error while loading the NetPyNE model"
            logging.exception(message)
            return utils.getJSONError(message, sys.exc_info())
Exemple #14
0
    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())
Exemple #15
0
#     'timeRange': [400,600], 'binSize': 10, 'overlay':True, 'graphType': 'line', 'yaxis': 'count', 'saveData': True, 'saveFig': True, 'showFig': True})
# simConfig.addAnalysis('plot2Dnet', {'include': ['allCells']})
# simConfig.addAnalysis('plotConn', True)


###############################################################################
# RUN SIM
###############################################################################

#sim.createSimulateAnalyze(netParams = netParams, simConfig = simConfig)  # create and simulate network
# sim.createSimulate(netParams = netParams, simConfig = simConfig)  # create and simulate network
# sim.saveData()
# sim.loadSimulateAnalyze('mpiHHTut.pkl')
# sim.analysis.plotData()

sim.initialize(netParams = netParams, simConfig = simConfig)
sim.net.createPops()
sim.net.createCells()
sim.net.connectCells()
sim.net.addStims()
sim.setupRecording()

sim.simulate()

sim.analyze()

# ###############################################################################
# # MODIFY and RUN SIM
# ###############################################################################

# sim.net.modifyCells({'conds': {'label': 'PYR2sec'}, 
Exemple #16
0
    python init.py # Run simulation, optionally plot a raster

MPI usage:
    mpiexec -n 4 nrniv -python -mpi main.py

Contributors: [email protected]
"""

from netpyne import sim
from neuron import h,gui
cfg, netParams = sim.readCmdLineArgs(simConfigDefault= 'cfg.py', netParamsDefault= 'M1_detailed.py')
 #M1_cell originally, test change rn


sim.initialize(
    simConfig=cfg, 
    netParams=netParams)  # create network object and set cfg and net params
sim.net.createPops()                  # instantiate network populations
sim.net.createCells()                 # instantiate network cells based on defined populations
sim.net.connectCells()                # create connections between cells based on params
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)
sim.analysis.plotData()               # plot spike raster


c=sim.net.cells
pt=next(cell for cell in c if cell.tags['cellType']=='PT')

Exemple #17
0
Usage:
    python init.py # Run simulation, optionally plot a raster

MPI usage:
    mpiexec -n 4 nrniv -python -mpi init.py

Contributors: [email protected]
"""

#import matplotlib; matplotlib.use('Agg')  # to avoid graphics error in servers
from netpyne import sim

cfg, netParams = sim.readCmdLineArgs()

sim.initialize(
    simConfig=cfg,
    netParams=netParams)  # create network object and set cfg and net params
sim.net.createPops()  # instantiate network populations
sim.net.createCells()  # instantiate network cells based on defined populations
sim.net.connectCells()  # create connections between cells based on params
sim.net.addStims()  # add network stimulation
sim.setupRecording()  # setup variables to record (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
#sim.analysis.plotData()       # plot spike raster etc

# connDict = {}

# for pop in sim.net.pops.keys():
Exemple #18
0
init.py
Starting script to run NetPyNE-based model.
Usage:  python init.py  # Run simulation, optionally plot a raster
MPI usage:  mpiexec -n 4 nrniv -python -mpi init.py
Contributors: [email protected]
"""

from netpyne import sim

# read cfg and netParams from command line arguments
# if there are no command line args, looks for cfg.py and netParams.py in curdir
# Reads command line arguments using syntax: python file.py [simConfig=filepath] [netParams=filepath]
cfg, netParams = sim.readCmdLineArgs()

# create network object and set cfg and net params	
sim.initialize(simConfig = cfg, netParams = netParams)

# instantiate network populations 
sim.net.createPops()

# instantiate network cells based on defined populations
sim.net.createCells()

# create connections between cells based on params
sim.net.connectCells()

# add network stimulation
sim.net.addStims()

# setup variables to record for each cell (spikes, V traces, etc)								
sim.setupRecording()
Exemple #19
0
simConfig.recordStim = True
#sinConfig.saveDataInclude = ['simData']
simConfig.saveFolder = 'D:\School work\TMS\nhp_tms_microcircuit\netpyne\dataset'
simConfig.saveTxt = True
simConfig.filename = 'raster2_spon_inon_Inh'  # file output name
#simConfig.savePickle = False           #Save params, network and sim output to pickle file

#simConfig.analysis['plotRaster'] = True
#simConfig.analysis['plotRaster'] = {'include':[('inInh'),('Inh')],'spikeHist':'overlay','spikeHistBin': 5,'saveData': True}
#simConfig.analysis['plotTraces'] = {'include':[('L23exc',0),('L5exc',0),('Inh',0)],'overlay':False,'oneFigPer':'trace'}
#simConfig.analysis['plotTraces'] = {'include':[('L23exc',0),('L5exc',0),('Inh',0)]}
simConfig.analysis['plot2Dnet'] = False
#simConfig.analysis['plotTraces'] = {'include': [('L23exc'),('L5exc'),('Inh')], 'saveData': 'gsyn_TMS.pkl'}                     # Plot recorded traces for this list of cells

sim.initialize(  # create network object and set cfg and net params
    simConfig=
    simConfig,  # pass simulation config and network params as arguments
    netParams=netParams)
sim.net.createPops()  # instantiate network populations
sim.net.createCells()  # instantiate network cells based on defined populations
sim.net.connectCells()  # create connections between cells based on params

#Poisson firing rate for each poisson inputs
frinh = np.random.normal(16, 0, 100)  #FRmean 16 Hz
frl23 = np.random.normal(8, 0, 100)  #FRmean 8 Hz
frl5 = np.random.normal(10, 0, 100)  #FRmean 10 Hz
i = 0
j = 0
k = 0
for x in range(0, len(sim.net.cells)):
    if 'inInh' in sim.net.cells[x].tags['pop']:
        sim.net.cells[x].params['interval'] = 1000 / frinh[i]
Exemple #20
0
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)
Exemple #21
0
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 =
Exemple #22
0
import params  # import parameters file
from netpyne import sim  # import netpyne init module
from arm import Arm
from neuron import h
from time import time, sleep
from pylab import radians, inf, ceil

###############################################################################
# Set up Network
###############################################################################

sim.initialize(simConfig=params.simConfig, netParams=params.netParams)
sim.net.createPops()  # instantiate network populations
sim.net.createCells()  # instantiate network cells based on defined populations
sim.net.connectCells()  # create connections between cells based on params
sim.net.addStims()
sim.setupRecording(
)  # setup variables to record for each cell (spikes, V traces, etc)

###############################################################################
# Set up virtual arm, proprioceptive/motor encoding and RL
###############################################################################

# Arm parameters
sim.useArm = 1  # include arm in simulation
sim.animArm = 1  # show arm animation
sim.graphsArm = 1  #  plot arm graphs
sim.updateInterval = 20  # delay between arm updated (ms)
sim.initArmMovement = 50  # time at which to start moving arm (ms)
sim.armLen = [0.4634 - 0.173, 0.7169 - 0.4634
              ]  # elbow - shoulder from MSM;radioulnar - elbow from MSM;
Exemple #23
0
def calcSynDensity(cellType=None,
                   fixedSomaY=None,
                   dendLGrid=None,
                   lenY=30,
                   spacing=50,
                   scracmData=None,
                   scracmFile=None,
                   densFile=None,
                   includeWnorm=True,
                   show=True,
                   save=''):
    # create net
    #cfg, netParams = sim.readCmdLineArgs()
    sim.initialize(simConfig=setCfg(), netParams=setNetParams()
                   )  # create network object and set cfg and net params
    sim.net.createPops()  # instantiate network populations
    sim.net.createCells(
    )  # instantiate network cells based on defined populations

    # load sCRACM data and set grid
    gridY, scracm1d, dens1d = loadSCRACM(lenY, spacing, scracmData, scracmFile,
                                         densFile)

    # align grid with cell soma
    cell = next((c for c in sim.net.cells if c.tags['cellType'] == cellType),
                None)
    #print cellType,cell
    if cellType: gridY = alignGrid(cell, gridY, fixedSomaY=fixedSomaY)

    # calculate weightNorm avg and dend length for each grid position
    if includeWnorm: wnormGrid = weightNormGrid(cell, gridY, spacing)
    if dendLGrid == None:
        dendLGrid = dendLengthGrid(cell, gridY, spacing)

    # normalize
    if includeWnorm: wnormGridNorm = [x / max(wnormGrid) for x in wnormGrid]
    dendLGridNorm = [x / max(dendLGrid) for x in dendLGrid]
    scracm1dNorm = [x / max(scracm1d) for x in scracm1d]
    if densFile: dens1dNorm = [x / max(dens1d) for x in dens1d]

    # calculate syn density = scracm / (wnorm* dendL)
    if includeWnorm:
        dens1dEstim = [
            scracm / (wnorm + dendL) for scracm, wnorm, dendL in zip(
                scracm1dNorm, wnormGridNorm, dendLGridNorm)
        ]
    else:
        dens1dEstim = [
            scracm / np.sqrt(dendL)
            for scracm, dendL in zip(scracm1dNorm, dendLGridNorm)
        ]

    dens1dEstim = [
        x if x not in [np.inf, -np.inf] else 0.0 for x in dens1dEstim
    ]
    dens1dEstim = [x if not np.isnan(x) else 0.0 for x in dens1dEstim]
    dens1dEstimNorm = [x / max(dens1dEstim) for x in dens1dEstim]

    # plot dens1d vs dens1dEstimate
    lw = 2.0
    plt.figure(figsize=(12, 6))
    plt.plot(scracm1dNorm, 'y', linewidth=lw, label='scracm')
    plt.plot(dendLGridNorm, linewidth=lw, label='dendL')
    if includeWnorm: plt.plot(wnormGridNorm, 'k', linewidth=lw, label='wnorm')
    if densFile: plt.plot(dens1dNorm, 'g', linewidth=lw, label='syn density')
    plt.plot(dens1dEstimNorm, 'r', linewidth=lw, label='syn density (aprox)')
    plt.xlabel('normalized cortical depth ')
    plt.ylabel('normalized strength')
    plt.legend()
    if save: plt.savefig(save)
    if show: plt.show()

    if densFile: return gridY, dens1dNorm
    else: return gridY, dens1dEstimNorm
Exemple #24
0
simConfig.filename = 'model_output'  # Set file output name
simConfig.savePickle = False        # Save params, network and sim output to pickle file

simConfig.addAnalysis('plotRaster', {'syncLines': True})      # Plot a raster
simConfig.addAnalysis('plotTraces', {'include': [1]})      # Plot recorded traces for this list of cells
simConfig.addAnalysis('plot2Dnet', True)           # plot 2D visualization of cell positions and connections


###############################################################################
# EXECUTION CODE (via netpyne)
###############################################################################
from netpyne import sim

# Create network and run simulation
sim.initialize(                       # create network object and set cfg and net params
    simConfig = simConfig,   # pass simulation config and network params as arguments
    netParams = netParams)   
sim.net.createPops()                      # instantiate network populations
sim.net.createCells()                     # instantiate network cells based on defined populations
sim.net.connectCells()                    # create connections between cells based on params
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)
sim.analysis.plotData()                   # plot spike raster


# ###############################################################################
# # INTERACTING WITH INSTANTIATED NETWORK
# ###############################################################################
Exemple #25
0
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)
Exemple #26
0
init.py

Starting script to run NetPyNE-based RxD model.

Usage:
    python init.py # Run simulation, optionally plot a raster

MPI usage:
    mpiexec -n 4 nrniv -python -mpi init.py

Contributors: [email protected]
"""

from netpyne import sim
from netParams import netParams
from cfg import cfg

# --------------------------------
# Instantiate network
# --------------------------------
sim.initialize(netParams,
               cfg)  # create network object and set cfg and net params
sim.net.createPops()  # instantiate network populations
sim.net.createCells()  # instantiate network cells based on defined populations
sim.net.connectCells()  # create connections between cells based on params
sim.net.addStims()  # add external stimulation to cells (IClamps etc)
sim.net.addRxD()  # add reaction-diffusion (RxD)
sim.setupRecording(
)  # setup variables to record for each cell (spikes, V traces, etc)
sim.simulate()
sim.analyze()
Exemple #27
0
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)
Exemple #28
0
# simConfig.addAnalysis('plotSpikeHist', {'include': ['PYR', 'allNetStims', 'background2', ('PYR',[5,6,7,8])],
#     'timeRange': [400,600], 'binSize': 10, 'overlay':True, 'graphType': 'line', 'yaxis': 'count', 'saveData': True, 'saveFig': True, 'showFig': True})
# simConfig.addAnalysis('plot2Dnet', {'include': ['allCells']})
# simConfig.addAnalysis('plotConn', True)

###############################################################################
# RUN SIM
###############################################################################

#sim.createSimulateAnalyze(netParams = netParams, simConfig = simConfig)  # create and simulate network
# sim.createSimulate(netParams = netParams, simConfig = simConfig)  # create and simulate network
# sim.saveData()
# sim.loadSimulateAnalyze('mpiHHTut.pkl')
# sim.analysis.plotData()

sim.initialize(netParams=netParams, simConfig=simConfig)
sim.net.createPops()
sim.net.createCells()
sim.net.connectCells()
sim.net.addStims()
sim.setupRecording()

sim.simulate()

sim.analyze()

# ###############################################################################
# # MODIFY and RUN SIM
# ###############################################################################

# sim.net.modifyCells({'conds': {'label': 'PYR2sec'},