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
1515import re
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 ()
@@ -245,15 +248,15 @@ def getPlot(self, plotName, LFPflavour):
245248 return [ui .getSVG (fig [0 ])].__str__ ()
246249 elif isinstance (fig , dict ):
247250 svgs = []
248- for key , value in fig .iteritems ():
251+ for key , value in fig .items ():
249252 logging .debug ("Found plot for " + key )
250253 svgs .append (ui .getSVG (value ))
251254 return svgs .__str__ ()
252255 else :
253256 return [ui .getSVG (fig )].__str__ ()
254257
255258 def getAvailablePops (self ):
256- return netParams .popParams .keys ()
259+ return list ( netParams .popParams .keys () )
257260
258261 def getAvailableCellModels (self ):
259262 cellModels = set ([])
@@ -276,29 +279,29 @@ def getAvailableCellTypes(self):
276279 def getAvailableSections (self ):
277280 sections = {}
278281 for cellRule in netParams .cellParams :
279- sections [cellRule ] = netParams .cellParams [cellRule ]['secs' ].keys ()
282+ sections [cellRule ] = list ( netParams .cellParams [cellRule ]['secs' ].keys () )
280283 return sections
281284
282285 def getAvailableStimSources (self ):
283- return netParams .stimSourceParams .keys ()
286+ return list ( netParams .stimSourceParams .keys () )
284287
285288 def getAvailableSynMech (self ):
286- return netParams .synMechParams .keys ()
289+ return list ( netParams .synMechParams .keys () )
287290
288291 def getAvailableMechs (self ):
289- mechs = utils . mechVarList ()['mechs' ]
290- for key in mechs .keys ():
292+ mechs = mechVarList ()['mechs' ]
293+ for key in list ( mechs .keys () ):
291294 if 'ion' in key : del mechs [key ]
292295 for key in ["morphology" , "capacitance" , "extracellular" ]: del mechs [key ]
293- return mechs .keys ()
296+ return list ( mechs .keys () )
294297
295298 def getMechParams (self , mechanism ):
296- params = utils . mechVarList ()['mechs' ][mechanism ]
299+ params = mechVarList ()['mechs' ][mechanism ]
297300 return [value [:- (len (mechanism ) + 1 )] for value in params ]
298301
299302 def getAvailablePlots (self ):
300303 plots = ["plotRaster" , "plotSpikeHist" , "plotSpikeStats" ,"plotRatePSD" , "plotTraces" , "plotLFP" , "plotShape" , "plot2Dnet" , "plotConn" , "granger" ]
301- return [plot for plot in plots if plot not in simConfig .analysis .keys ()]
304+ return [plot for plot in plots if plot not in list ( simConfig .analysis .keys () )]
302305
303306 def deleteParam (self , paramToDel ):
304307 logging .debug ("Checking if netParams." + paramToDel + " is not null" )
@@ -309,7 +312,7 @@ def deleteParam(self, paramToDel):
309312 logging .debug ('Parameter ' + paramToDel + ' is null, not deleted' )
310313
311314 def validateFunction (self , functionString ):
312- return utils . ValidateFunction (functionString , netParams .__dict__ )
315+ return ValidateFunction (functionString , netParams .__dict__ )
313316
314317 def generateScript (self , metadata ):
315318 def convert2bool (string ):
@@ -333,20 +336,20 @@ def header(title, spacer='-'):
333336 script .write ('netParams = specs.NetParams()\n ' )
334337 script .write ('simConfig = specs.SimConfig()\n ' )
335338 script .write (header ('single value attributes' ))
336- for attr , value in netParams .__dict__ .items ():
339+ for attr , value in list ( netParams .__dict__ .items () ):
337340 if attr not in params :
338341 if value != getattr (specs .NetParams (), attr ):
339342 script .write ('netParams.' + attr + ' = ' )
340343 script .write (convert2bool (json .dumps (value , indent = 4 ))+ '\n ' )
341344
342345 script .write (header ('network attributes' ))
343346 for param in params :
344- for key , value in getattr (netParams , param ).items ():
347+ for key , value in list ( getattr (netParams , param ).items () ):
345348 script .write ("netParams." + param + "['" + key + "'] = " )
346349 script .write (convert2bool (json .dumps (value , indent = 4 ))+ '\n ' )
347350
348351 script .write (header ('network configuration' ))
349- for attr , value in simConfig .__dict__ .items ():
352+ for attr , value in list ( simConfig .__dict__ .items () ):
350353 if value != getattr (specs .SimConfig (), attr ):
351354 script .write ('netParams.' + attr + ' = ' )
352355 script .write (convert2bool (json .dumps (value , indent = 4 ))+ '\n ' )
@@ -428,8 +431,11 @@ def globalMessageHandler(identifier, command, parameters):
428431 response = eval (command )
429432 else :
430433 response = eval (command + '(*parameters)' )
434+ import sys
435+ imp .reload (sys )
436+ print (type (response ))
431437 GeppettoJupyterModelSync .events_controller .triggerEvent (
432- "receive_python_message" , {'id' : identifier , 'response' : response })
438+ "receive_python_message" , {'id' : identifier , 'response' : response . decode ( "utf-8" ) if isinstance ( response , bytes ) else response })
433439 except :
434440 response = netpyne_geppetto .getJSONError ("Error while executing command " + command ,traceback .format_exc ())
435441 GeppettoJupyterModelSync .events_controller .triggerEvent (
@@ -482,7 +488,7 @@ def GeppettoInit():
482488
483489GeppettoJupyterModelSync .current_model .original_model = json .dumps ({'netParams' : netParams .__dict__ ,
484490 'simConfig' : simConfig .__dict__ ,
485- 'metadata' : metadata . metadata ,
491+ 'metadata' : metadata ,
486492 'requirement' : 'from netpyne_ui.netpyne_geppetto import *' ,
487493 'isDocker' : os .path .isfile ('/.dockerenv' ),
488494 'currentFolder' : os .getcwd ()})
0 commit comments