Beispiel #1
0
    def instantiateModelInGeppetto(self):
        """

        Instantiates model in geppetto
        Runs simulation

        Returns
        -------
        JSON
            serialized geppetto model

        """
        try:
            netpyne_model = self.instantiateModel()
            self.geppetto_model = self.model_interpreter.getGeppettoModel(
                netpyne_model)
            logging.debug('Running single thread simulation')
            self.simulateModel()

            return json.loads(
                GeppettoModelSerializer.serialize(self.geppetto_model))
        except Exception as e:
            print(e)
            return utils.getJSONError(
                "Error while instantiating the NetPyNE model", e)
    def simulate_single_model(self,
                              experiment: model.Experiment = None,
                              use_prev_inst: bool = False):
        if experiment:
            working_directory = self._prepare_simulation_files(
                experiment, use_prev_inst)

            simulations.run(
                parallel=self.run_config.parallel,
                cores=self.run_config.cores,
                asynchronous=self.run_config.asynchronous,
                method=simulations.MPI_BULLETIN,
                working_directory=working_directory,
            )

            if self.run_config.asynchronous:
                message = "Experiment is pending! " \
                  f"Results will be stored in your workspace at ./{os.path.join(constants.EXPERIMENTS_FOLDER, experiment.name)}"
                return utils.getJSONError(message, "")
            else:
                sim.load(f'{constants.MODEL_OUTPUT_FILENAME}.json')
                self.geppetto_model = self.model_interpreter.getGeppettoModel(
                    sim)
                response = json.loads(
                    GeppettoModelSerializer.serialize(self.geppetto_model))
                return response

        else:
            # Run in same process
            if not use_prev_inst:
                logging.debug('Instantiating single thread simulation')
                netpyne_model = self.instantiateNetPyNEModel()

                self.geppetto_model = self.model_interpreter.getGeppettoModel(
                    netpyne_model)

            simulations.run()

            if self.geppetto_model:
                response = json.loads(
                    GeppettoModelSerializer.serialize(self.geppetto_model))
                return response
Beispiel #3
0
    def simulateNetPyNEModelInGeppetto(self, args):
        try:
            with redirect_stdout(sys.__stdout__):
                # TODO mpi is not finding  libmpi.dylib.. set LD_LIBRARY_PATH to openmpi bin folder, but nothing
                if args['parallelSimulation']:
                    logging.debug('Running parallel simulation')
                    if not 'usePrevInst' in args or not args['usePrevInst']:
                        self.netParams.save("netParams.json")
                        self.simConfig.saveJson = True
                        self.simConfig.save("simParams.json")
                        template = os.path.join(os.path.dirname(__file__),
                                                'template.py')
                    else:
                        sim.cfg.saveJson = True
                        oldName = sim.cfg.filename
                        sim.cfg.filename = 'model_output'
                        sim.saveData()
                        sim.cfg.filename = oldName
                        template = os.path.join(os.path.dirname(__file__),
                                                'template2.py')
                    copyfile(template, './init.py')

                    cp = subprocess.run([
                        "mpiexec", "-n", args['cores'], "nrniv", "-mpi",
                        "-python", "init.py"
                    ],
                                        capture_output=True)
                    print(cp.stdout.decode() + cp.stderr.decode())
                    if cp.returncode != 0:
                        return utils.getJSONError(
                            "Error while simulating the NetPyNE model",
                            cp.stderr.decode())
                    sim.load('model_output.json')
                    self.geppetto_model = self.model_interpreter.getGeppettoModel(
                        sim)
                    netpyne_model = sim

                else:
                    logging.info("Starting simulation")

                    if not args.get('usePrevInst', False):
                        logging.debug('Instantiating single thread simulation')
                        netpyne_model = self.instantiateNetPyNEModel()
                        self.geppetto_model = self.model_interpreter.getGeppettoModel(
                            netpyne_model)

                    logging.debug('Running single thread simulation')
                    netpyne_model = self.simulateNetPyNEModel()

                return json.loads(
                    GeppettoModelSerializer.serialize(self.geppetto_model))
        except:
            return utils.getJSONError(
                "Error while simulating the NetPyNE model", sys.exc_info())
Beispiel #4
0
    def instantiateNetPyNEModelInGeppetto(self, args):
        try:
            with redirect_stdout(sys.__stdout__):
                if not 'usePrevInst' in args or not args['usePrevInst']:
                    netpyne_model = self.instantiateNetPyNEModel()
                    self.geppetto_model = self.model_interpreter.getGeppettoModel(
                        netpyne_model)

                return json.loads(
                    GeppettoModelSerializer.serialize(self.geppetto_model))
        except:
            return utils.getJSONError(
                "Error while instantiating the NetPyNE model", sys.exc_info())
Beispiel #5
0
    def getModelInGeppetto(self):
        """

        Gets model in geppetto

        Returns
        -------
        JSON
            serialized geppetto model

        """
        return json.loads(GeppettoModelSerializer().serialize(
            self.geppetto_model))
    def instantiateNetPyNEModelInGeppetto(self, args):
        try:
            with redirect_stdout(sys.__stdout__):
                if not args.get("usePrevInst", False):
                    netpyne_model = self.instantiateNetPyNEModel()
                    self.geppetto_model = self.model_interpreter.getGeppettoModel(
                        netpyne_model)

                return json.loads(
                    GeppettoModelSerializer.serialize(self.geppetto_model))
        except Exception as e:
            message = "Error while instantiating the NetPyNE model"
            logging.exception(message)
            return utils.getJSONError(message, sys.exc_info())
Beispiel #7
0
def load(request):
    if request.method == 'GET':

        model_interpreter = NWBModelInterpreter()
        geppetto_model = model_interpreter.importType(
            './test_data/brain_observatory.nwb', '', '', '')
        serialized_model = GeppettoModelSerializer().serialize(geppetto_model)

        # TODO serialise and store geppetto model in session (temporary stored in settings)
        settings.GEPPETTO_MODEL = geppetto_model

        return Response(serialized_model)
    elif request.method == 'POST':
        return Response("Post model")
Beispiel #8
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 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))
Beispiel #10
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())
Beispiel #11
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())