3030from jupyter_geppetto import jupyter_geppetto , synchronization , utils
3131import imp
3232from contextlib import redirect_stdout , redirect_stderr
33+ from netpyne_ui .constants import NETPYNE_WORKDIR_PATH
3334
34-
35+ os . chdir ( NETPYNE_WORKDIR_PATH )
3536class NetPyNEGeppetto ():
3637
3738 def __init__ (self ):
@@ -59,7 +60,7 @@ def instantiateNetPyNEModelInGeppetto(self, args):
5960 netpyne_model = self .instantiateNetPyNEModel ()
6061 self .geppetto_model = self .model_interpreter .getGeppettoModel (netpyne_model )
6162
62- return json .loads (GeppettoModelSerializer () .serialize (self .geppetto_model ))
63+ return json .loads (GeppettoModelSerializer .serialize (self .geppetto_model ))
6364 except :
6465 return utils .getJSONError ("Error while instantiating the NetPyNE model" , sys .exc_info ())
6566
@@ -185,7 +186,7 @@ def remove(dictionary):
185186 sim .loadSimData (args ['jsonModelFolder' ])
186187 self .geppetto_model = self .model_interpreter .getGeppettoModel (sim )
187188
188- return json .loads (GeppettoModelSerializer () .serialize (self .geppetto_model ))
189+ return json .loads (GeppettoModelSerializer .serialize (self .geppetto_model ))
189190 else :
190191 return utils .getJSONReply ()
191192 except :
@@ -263,6 +264,11 @@ def exportModel(self, args):
263264 sim .cfg .saveJson = True
264265 sim .saveData (include )
265266 sim .cfg .saveJson = False
267+
268+ with open (f"{ sim .cfg .filename } .json" ) as json_file :
269+ data = json .load (json_file )
270+ return data
271+
266272 return utils .getJSONReply ()
267273 except :
268274 return utils .getJSONError ("Error while exporting the NetPyNE model" , sys .exc_info ())
@@ -281,24 +287,31 @@ def importNeuroML(self, modelParams):
281287 sim .initialize ()
282288 sim .importNeuroML2 (modelParams ['neuroMLFolder' ], simConfig = specs .SimConfig (), simulate = False , analyze = False )
283289 self .geppetto_model = self .model_interpreter .getGeppettoModel (sim )
284- return json .loads (GeppettoModelSerializer () .serialize (self .geppetto_model ))
290+ return json .loads (GeppettoModelSerializer .serialize (self .geppetto_model ))
285291
286292 except :
287293 return utils .getJSONError ("Error while exporting the NetPyNE model" , sys .exc_info ())
288294
289295 def deleteModel (self , modelParams ):
296+
290297 try :
291298 with redirect_stdout (sys .__stdout__ ):
292299 self .netParams = specs .NetParams ()
293300 self .simConfig = specs .SimConfig ()
294- self .netParams .todict ()
295- self .netParams .todict ()
296- if self .doIhaveInstOrSimData ()['haveInstance' ]: sim .clearAll ()
301+ sim .initialize (specs .NetParams (), specs .SimConfig ())
297302 self .geppetto_model = None
298- return utils .getJSONReply ()
299-
300303 except :
301304 return utils .getJSONError ("Error while exporting the NetPyNE model" , sys .exc_info ())
305+
306+ try :
307+ # This function fails is some keys don't exists
308+ # sim.clearAll()
309+ self .clearSim ()
310+
311+ except :
312+ pass
313+
314+ return utils .getJSONReply ()
302315
303316 def instantiateNetPyNEModel (self ):
304317 with redirect_stdout (sys .__stdout__ ):
@@ -360,16 +373,17 @@ def getPlotSettings(self, plot):
360373 def getDirList (self , dir = None , onlyDirs = False , filterFiles = False ):
361374 # Get Current dir
362375 if dir == None or dir == '' :
363- dir = os .getcwd ()
376+ dir = os .path . join ( os . getcwd (), NETPYNE_WORKDIR_PATH )
364377 dir_list = []
378+ file_list = []
365379 for f in sorted (os .listdir (str (dir )), key = str .lower ):
366380 ff = os .path .join (dir ,f )
367381 if os .path .isdir (ff ):
368- dir_list .insert ( 0 , {'title' : f , 'path' : ff , 'load' : False , 'children' : [{'title' : 'Loading...' }]})
382+ dir_list .append ( {'title' : f , 'path' : ff , 'load' : False , 'children' : [{'title' : 'Loading...' }]})
369383 elif not onlyDirs :
370384 if not filterFiles or os .path .isfile (ff ) and ff .endswith (filterFiles ):
371- dir_list .append ({'title' : f , 'path' : ff })
372- return dir_list
385+ file_list .append ({'title' : f , 'path' : ff })
386+ return dir_list + file_list
373387
374388 def getPlot (self , plotName , LFPflavour ):
375389 try :
@@ -541,7 +555,8 @@ def header(title, spacer='-'):
541555
542556 script .write (header ('end script' , spacer = '=' ))
543557
544- return utils .getJSONReply ()
558+ with open (fname ) as f :
559+ return f .read ()
545560
546561 except :
547562 return utils .getJSONError ("Error while importing the NetPyNE model" , sys .exc_info ())
@@ -648,6 +663,52 @@ def propagate_syn_mech_rename(self, new, old):
648663 self .netParams .stimTargetParams [label ]['synMech' ] = new
649664
650665
666+ def clearSim (self ):
667+ # clean up
668+ sim .pc .barrier ()
669+ sim .pc .gid_clear () # clear previous gid settings
670+
671+ # clean cells and simData in all nodes
672+ sim .clearObj ([cell .__dict__ if hasattr (cell , '__dict__' ) else cell for cell in sim .net .cells ])
673+ if 'stims' in list (sim .simData .keys ()):
674+ sim .clearObj ([stim for stim in sim .simData ['stims' ]])
675+
676+ for key in list (sim .simData .keys ()): del sim .simData [key ]
677+
678+ if hasattr (sim , 'net' ):
679+ for c in sim .net .cells : del c
680+ for p in sim .net .pops : del p
681+ if hasattr (sim .net , 'params' ):
682+ del sim .net .params
683+
684+
685+ # clean cells and simData gathered in master node
686+ if sim .rank == 0 :
687+ if hasattr (sim .net , 'allCells' ):
688+ sim .clearObj ([cell .__dict__ if hasattr (cell , '__dict__' ) else cell for cell in sim .net .allCells ])
689+ if hasattr (sim , 'allSimData' ):
690+ if 'stims' in list (sim .allSimData .keys ()):
691+ sim .clearObj ([stim for stim in sim .allSimData ['stims' ]])
692+ for key in list (sim .allSimData .keys ()): del sim .allSimData [key ]
693+ del sim .allSimData
694+
695+
696+ import matplotlib
697+ matplotlib .pyplot .clf ()
698+ matplotlib .pyplot .close ('all' )
699+
700+ if hasattr (sim , 'net' ):
701+ if hasattr (sim .net , 'allCells' ):
702+ for c in sim .net .allCells : del c
703+ del sim .net .allCells
704+ if hasattr (sim .net , 'allPops' ):
705+ for p in sim .net .allPops : del p
706+
707+ del sim .net
708+
709+ import gc ; gc .collect ()
710+
711+
651712logging .info ("Initialising NetPyNE UI" )
652713netpyne_geppetto = NetPyNEGeppetto ()
653714logging .info ("NetPyNE UI initialised" )
0 commit comments