22netpyne_geppetto.py
33Initialise NetPyNE Geppetto, this class contains methods to connect NetPyNE with the Geppetto based UI
44"""
5- import StringIO
5+ import io
66import json
77import os
88import importlib
1414import traceback
1515
1616
17- from netpyne import specs , sim , analysis , utils
18- from netpyne .metadata import metadata , api
19- from netpyne_model_interpreter import NetPyNEModelInterpreter
17+ from netpyne import specs , sim , analysis
18+ from netpyne .specs .utils import validateFunction
19+ from netpyne .conversion .neuronPyHoc import mechVarList
20+ from netpyne .metadata import metadata
21+ from netpyne_ui .netpyne_model_interpreter import NetPyNEModelInterpreter
2022from pygeppetto .model .model_serializer import GeppettoModelSerializer
2123import matplotlib .pyplot as plt
2224from pygeppetto import ui
2729from shutil import copyfile
2830from jupyter_geppetto .geppetto_comm import GeppettoJupyterModelSync , GeppettoJupyterGUISync
2931from jupyter_geppetto .geppetto_comm import GeppettoCoreAPI as G
32+ import imp
3033
3134
3235
@@ -108,7 +111,7 @@ def importModel(self, modelParameters):
108111
109112 self .compileModMechFiles (modelParameters ['compileMod' ], modelParameters ['modFolder' ])
110113
111- import netpyne_geppetto
114+ from . import netpyne_geppetto
112115
113116 if modelParameters ['importFormat' ]== 'py' :
114117 # NetParams
@@ -120,7 +123,7 @@ def importModel(self, modelParameters):
120123 # Import Model attributes
121124 netpyne_geppetto .netParams = getattr (netParamsModuleName , str (modelParameters ["netParamsVariable" ]))
122125
123- for key , value in netpyne_geppetto .netParams .cellParams .iteritems ():
126+ for key , value in netpyne_geppetto .netParams .cellParams .items ():
124127 if hasattr (value , 'todict' ):
125128 netpyne_geppetto .netParams .cellParams [key ] = value .todict ()
126129
@@ -159,7 +162,7 @@ def importCellTemplate(self, modelParameters, modFolder, compileMod):
159162 # Get Current dir
160163 owd = os .getcwd ()
161164
162- from netpyne_geppetto import netParams
165+ from . netpyne_geppetto import netParams
163166
164167 self .compileModMechFiles (compileMod , modFolder )
165168
@@ -185,7 +188,7 @@ def exportModel(self, modelParameters):
185188 return self .getJSONError ("Error while exporting the NetPyNE model" ,traceback .format_exc ())
186189
187190 def instantiateNetPyNEModel (self ):
188- import sys ; reload (sys )
191+ import sys ; imp . reload (sys )
189192 sim .initialize (netParams , simConfig ) # create network object and set cfg and net params
190193 sim .net .createPops () # instantiate network populations
191194 sim .net .createCells () # instantiate network cells based on defined populations
@@ -198,7 +201,7 @@ def instantiateNetPyNEModel(self):
198201 return sim
199202
200203 def simulateNetPyNEModel (self ):
201- import sys ; reload (sys )
204+ import sys ; imp . reload (sys )
202205 sim .setupRecording ()
203206 sim .simulate ()
204207 sim .saveData ()
@@ -244,15 +247,15 @@ def getPlot(self, plotName, LFPflavour):
244247 return [ui .getSVG (fig [0 ])].__str__ ()
245248 elif isinstance (fig , dict ):
246249 svgs = []
247- for key , value in fig .iteritems ():
250+ for key , value in fig .items ():
248251 logging .debug ("Found plot for " + key )
249252 svgs .append (ui .getSVG (value ))
250253 return svgs .__str__ ()
251254 else :
252255 return [ui .getSVG (fig )].__str__ ()
253256
254257 def getAvailablePops (self ):
255- return netParams .popParams .keys ()
258+ return list ( netParams .popParams .keys () )
256259
257260 def getAvailableCellModels (self ):
258261 cellModels = set ([])
@@ -275,29 +278,29 @@ def getAvailableCellTypes(self):
275278 def getAvailableSections (self ):
276279 sections = {}
277280 for cellRule in netParams .cellParams :
278- sections [cellRule ] = netParams .cellParams [cellRule ]['secs' ].keys ()
281+ sections [cellRule ] = list ( netParams .cellParams [cellRule ]['secs' ].keys () )
279282 return sections
280283
281284 def getAvailableStimSources (self ):
282- return netParams .stimSourceParams .keys ()
285+ return list ( netParams .stimSourceParams .keys () )
283286
284287 def getAvailableSynMech (self ):
285- return netParams .synMechParams .keys ()
288+ return list ( netParams .synMechParams .keys () )
286289
287290 def getAvailableMechs (self ):
288- mechs = utils . mechVarList ()['mechs' ]
289- for key in mechs .keys ():
291+ mechs = mechVarList ()['mechs' ]
292+ for key in list ( mechs .keys () ):
290293 if 'ion' in key : del mechs [key ]
291294 for key in ["morphology" , "capacitance" , "extracellular" ]: del mechs [key ]
292- return mechs .keys ()
295+ return list ( mechs .keys () )
293296
294297 def getMechParams (self , mechanism ):
295- params = utils . mechVarList ()['mechs' ][mechanism ]
298+ params = mechVarList ()['mechs' ][mechanism ]
296299 return [value [:- (len (mechanism ) + 1 )] for value in params ]
297300
298301 def getAvailablePlots (self ):
299302 plots = ["plotRaster" , "plotSpikeHist" , "plotSpikeStats" ,"plotRatePSD" , "plotTraces" , "plotLFP" , "plotShape" , "plot2Dnet" , "plotConn" , "granger" ]
300- return [plot for plot in plots if plot not in simConfig .analysis .keys ()]
303+ return [plot for plot in plots if plot not in list ( simConfig .analysis .keys () )]
301304
302305 def deleteParam (self , paramToDel ):
303306 logging .debug ("Checking if netParams." + paramToDel + " is not null" )
@@ -308,7 +311,7 @@ def deleteParam(self, paramToDel):
308311 logging .debug ('Parameter ' + paramToDel + ' is null, not deleted' )
309312
310313 def validateFunction (self , functionString ):
311- return utils . ValidateFunction (functionString , netParams .__dict__ )
314+ return ValidateFunction (functionString , netParams .__dict__ )
312315
313316 def generateScript (self , metadata ):
314317 def convert2bool (string ):
@@ -332,20 +335,20 @@ def header(title, spacer='-'):
332335 script .write ('netParams = specs.NetParams()\n ' )
333336 script .write ('simConfig = specs.SimConfig()\n ' )
334337 script .write (header ('single value attributes' ))
335- for attr , value in netParams .__dict__ .items ():
338+ for attr , value in list ( netParams .__dict__ .items () ):
336339 if attr not in params :
337340 if value != getattr (specs .NetParams (), attr ):
338341 script .write ('netParams.' + attr + ' = ' )
339342 script .write (convert2bool (json .dumps (value , indent = 4 ))+ '\n ' )
340343
341344 script .write (header ('network attributes' ))
342345 for param in params :
343- for key , value in getattr (netParams , param ).items ():
346+ for key , value in list ( getattr (netParams , param ).items () ):
344347 script .write ("netParams." + param + "['" + key + "'] = " )
345348 script .write (convert2bool (json .dumps (value , indent = 4 ))+ '\n ' )
346349
347350 script .write (header ('network configuration' ))
348- for attr , value in simConfig .__dict__ .items ():
351+ for attr , value in list ( simConfig .__dict__ .items () ):
349352 if value != getattr (specs .SimConfig (), attr ):
350353 script .write ('netParams.' + attr + ' = ' )
351354 script .write (convert2bool (json .dumps (value , indent = 4 ))+ '\n ' )
@@ -427,8 +430,11 @@ def globalMessageHandler(identifier, command, parameters):
427430 response = eval (command )
428431 else :
429432 response = eval (command + '(*parameters)' )
433+ import sys
434+ imp .reload (sys )
435+ print (type (response ))
430436 GeppettoJupyterModelSync .events_controller .triggerEvent (
431- "receive_python_message" , {'id' : identifier , 'response' : response })
437+ "receive_python_message" , {'id' : identifier , 'response' : response . decode ( "utf-8" ) if isinstance ( response , bytes ) else response })
432438 except :
433439 response = netpyne_geppetto .getJSONError ("Error while executing command " + command ,traceback .format_exc ())
434440 GeppettoJupyterModelSync .events_controller .triggerEvent (
@@ -481,7 +487,7 @@ def GeppettoInit():
481487
482488GeppettoJupyterModelSync .current_model .original_model = json .dumps ({'netParams' : netParams .__dict__ ,
483489 'simConfig' : simConfig .__dict__ ,
484- 'metadata' : metadata . metadata ,
490+ 'metadata' : metadata ,
485491 'requirement' : 'from netpyne_ui.netpyne_geppetto import *' ,
486492 'isDocker' : os .path .isfile ('/.dockerenv' ),
487493 'currentFolder' : os .getcwd ()})
0 commit comments