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())
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)
def deleteModel(self, modelParams): try: with redirect_stdout(sys.__stdout__): self.netParams = specs.NetParams() self.simConfig = specs.SimConfig() self.netParams.todict() self.netParams.todict() if self.doIhaveInstOrSimData()['haveInstance']: sim.clearAll() self.geppetto_model = None return utils.getJSONReply() except: return utils.getJSONError( "Error while exporting the NetPyNE model", sys.exc_info())
def deleteModel(self, modelParams): try: with redirect_stdout(sys.__stdout__): self.netParams = specs.NetParams() self.simConfig = specs.SimConfig() sim.initialize(specs.NetParams(), specs.SimConfig()) self.geppetto_model = None except Exception: message = "Error while exporting the NetPyNE model" logging.exception(message) return utils.getJSONError(message, sys.exc_info()) try: sim.clearAll() except: logging.exception("Failed to clear simulation") return utils.getJSONReply()
def 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 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())
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())