Skip to content

Commit 0442dda

Browse files
author
Lucas Rebscher
committed
#263 Improvement of Python import
* Network will be instantiated before every simulation * Reuse methods in Python import that we use for JSON import
1 parent 49707af commit 0442dda

2 files changed

Lines changed: 32 additions & 29 deletions

File tree

netpyne_ui/netpyne_geppetto.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,16 @@ def simulateNetPyNEModelInGeppetto(self, args):
100100
self.geppetto_model = self.model_interpreter.getGeppettoModel(sim)
101101
netpyne_model = sim
102102

103-
else: # single cpu computation
103+
else:
104104
logging.info("Starting simulation")
105-
if not 'usePrevInst' in args or not args['usePrevInst']:
106-
logging.debug('Instantiating single thread simulation')
107105

108-
# TODO: we should instantiate the network before simulation per default
106+
# TODO: (#263) we should instantiate the network here per default
109107
# or we need a mechanism to detect if the currently instantiated network matches with
110-
# the current net params and simConfig. When I import a new model or change sth and directly simulate
111-
# we run simulation with an old network but new config, this causes issues!
112-
netpyne_model = self.instantiateNetPyNEModel()
113-
self.geppetto_model = self.model_interpreter.getGeppettoModel(netpyne_model)
108+
# the current net params and simConfig, otherwise this can cause errors
109+
if not args.get('usePrevInst', False):
110+
logging.debug('Instantiating single thread simulation')
111+
netpyne_model = self.instantiateNetPyNEModel()
112+
self.geppetto_model = self.model_interpreter.getGeppettoModel(netpyne_model)
114113

115114
logging.debug('Running single thread simulation')
116115
netpyne_model = self.simulateNetPyNEModel()
@@ -168,7 +167,8 @@ def remove(dictionary):
168167
wake_up_geppetto = False
169168
if all([args[option] for option in ['loadNetParams', 'loadSimCfg', 'loadSimData', 'loadNet']]):
170169
wake_up_geppetto = True
171-
if self.doIhaveInstOrSimData()['haveInstance']: sim.clearAll()
170+
if self.doIhaveInstOrSimData()['haveInstance']:
171+
sim.clearAll()
172172
sim.initialize()
173173
sim.loadAll(args['jsonModelFolder'])
174174
self.netParams = sim.net.params
@@ -178,7 +178,8 @@ def remove(dictionary):
178178
else:
179179
if args['loadNet']:
180180
wake_up_geppetto = True
181-
if self.doIhaveInstOrSimData()['haveInstance']: sim.clearAll()
181+
if self.doIhaveInstOrSimData()['haveInstance']:
182+
sim.clearAll()
182183
sim.initialize()
183184
sim.loadNet(args['jsonModelFolder'])
184185

@@ -196,7 +197,8 @@ def remove(dictionary):
196197
remove(self.simConfig.todict())
197198

198199
if args['loadNetParams']:
199-
if self.doIhaveInstOrSimData()['haveInstance']: sim.clearAll()
200+
if self.doIhaveInstOrSimData()['haveInstance']:
201+
sim.clearAll()
200202
sim.loadNetParams(args['jsonModelFolder'])
201203
self.netParams = sim.net.params
202204
remove(self.netParams.todict())
@@ -223,8 +225,7 @@ def importModel(self, modelParameters):
223225
:param modelParameters:
224226
:return:
225227
"""
226-
# TODO: Still need attributes check to avoid exception with empty sim
227-
if getattr(sim, "pc", None):
228+
if self.doIhaveInstOrSimData()['haveInstance']:
228229
# TODO: this must be integrated into the general lifecycle of "model change -> simulate"
229230
# Shouldn't be specific to Import
230231
sim.clearAll()
@@ -237,26 +238,26 @@ def importModel(self, modelParameters):
237238

238239
with redirect_stdout(sys.__stdout__):
239240
# NetParams
240-
netParamsPath = str(modelParameters["netParamsPath"])
241-
sys.path.append(netParamsPath)
242-
os.chdir(netParamsPath)
241+
net_params_path = str(modelParameters["netParamsPath"])
242+
sys.path.append(net_params_path)
243+
os.chdir(net_params_path)
243244
# Import Module
244-
netParamsModuleName = importlib.import_module(str(modelParameters["netParamsModuleName"]))
245+
net_params_module_name = importlib.import_module(str(modelParameters["netParamsModuleName"]))
245246
# Import Model attributes
246-
self.netParams = getattr(netParamsModuleName, str(modelParameters["netParamsVariable"]))
247+
self.netParams = getattr(net_params_module_name, str(modelParameters["netParamsVariable"]))
247248

248249
for key, value in self.netParams.cellParams.items():
249250
if hasattr(value, 'todict'):
250251
self.netParams.cellParams[key] = value.todict()
251252

252253
# SimConfig
253-
simConfigPath = str(modelParameters["simConfigPath"])
254-
sys.path.append(simConfigPath)
255-
os.chdir(simConfigPath)
254+
sim_config_path = str(modelParameters["simConfigPath"])
255+
sys.path.append(sim_config_path)
256+
os.chdir(sim_config_path)
256257
# Import Module
257-
simConfigModuleName = importlib.import_module(str(modelParameters["simConfigModuleName"]))
258+
sim_config_module_name = importlib.import_module(str(modelParameters["simConfigModuleName"]))
258259
# Import Model attributes
259-
self.simConfig = getattr(simConfigModuleName, str(modelParameters["simConfigVariable"]))
260+
self.simConfig = getattr(sim_config_module_name, str(modelParameters["simConfigVariable"]))
260261

261262
# TODO: when should sim.initialize be called?
262263
# Only on import or better before every simulation or network instantiation?
@@ -333,7 +334,6 @@ def importNeuroML(self, modelParams):
333334
return utils.getJSONError("Error while exporting the NetPyNE model", sys.exc_info())
334335

335336
def deleteModel(self, modelParams):
336-
337337
try:
338338
with redirect_stdout(sys.__stdout__):
339339
self.netParams = specs.NetParams()
@@ -346,8 +346,8 @@ def deleteModel(self, modelParams):
346346
try:
347347
# This function fails is some keys don't exists
348348
# sim.clearAll()
349+
# TODO: as part of #264 we should remove the method and use clearAll intstead
349350
self.clearSim()
350-
351351
except:
352352
pass
353353

@@ -371,7 +371,11 @@ def simulateNetPyNEModel(self):
371371
sim.saveData()
372372
return sim
373373

374-
def doIhaveInstOrSimData(self): # return [bool, bool] telling if we have an instance and simulated data
374+
def doIhaveInstOrSimData(self):
375+
""" Telling if we have an instance or simulated data.
376+
377+
return [bool, bool]
378+
"""
375379
with redirect_stdout(sys.__stdout__):
376380
out = [False, False]
377381
if hasattr(sim, 'net'):

webapp/redux/middleware/middleware.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {
22
UPDATE_CARDS, CREATE_NETWORK, CREATE_SIMULATE_NETWORK, PYTHON_CALL, SIMULATE_NETWORK, SHOW_NETWORK,
3-
editModel, EDIT_MODEL, LOAD_TUTORIAL, RESET_MODEL, setDefaultWidgets,
3+
editModel, EDIT_MODEL, LOAD_TUTORIAL, RESET_MODEL,
44
} from '../actions/general';
5-
import FLEXLAYOUT_DEFAULT_STATE from '../../components/layout/defaultLayout';
65
import { openBackendErrorDialog } from '../actions/errors';
76
import { closeDrawerDialogBox } from '../actions/drawer';
87
import Utils from '../../Utils';
@@ -68,7 +67,7 @@ export default (store) => (next) => (action) => {
6867
break;
6968
}
7069
case SIMULATE_NETWORK:
71-
simulateNetwork({ parallelSimulation: false, usePrevInst: true }).then(toNetworkCallback(false), pythonErrorCallback);
70+
simulateNetwork({ parallelSimulation: false, usePrevInst: false }).then(toNetworkCallback(false), pythonErrorCallback);
7271
break;
7372
case PYTHON_CALL: {
7473
const callback = (response) => {

0 commit comments

Comments
 (0)