Пример #1
0
    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))
Пример #2
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)
Пример #3
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)
Пример #4
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)
Пример #5
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())
Пример #6
0
    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())