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 ):
@@ -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 ())
@@ -287,18 +293,25 @@ def importNeuroML(self, modelParams):
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 :
@@ -545,7 +559,8 @@ def header(title, spacer='-'):
545559
546560 script .write (header ('end script' , spacer = '=' ))
547561
548- return utils .getJSONReply ()
562+ with open (fname ) as f :
563+ return f .read ()
549564
550565 except :
551566 return utils .getJSONError ("Error while importing the NetPyNE model" , sys .exc_info ())
@@ -652,6 +667,52 @@ def propagate_syn_mech_rename(self, new, old):
652667 self .netParams .stimTargetParams [label ]['synMech' ] = new
653668
654669
670+ def clearSim (self ):
671+ # clean up
672+ sim .pc .barrier ()
673+ sim .pc .gid_clear () # clear previous gid settings
674+
675+ # clean cells and simData in all nodes
676+ sim .clearObj ([cell .__dict__ if hasattr (cell , '__dict__' ) else cell for cell in sim .net .cells ])
677+ if 'stims' in list (sim .simData .keys ()):
678+ sim .clearObj ([stim for stim in sim .simData ['stims' ]])
679+
680+ for key in list (sim .simData .keys ()): del sim .simData [key ]
681+
682+ if hasattr (sim , 'net' ):
683+ for c in sim .net .cells : del c
684+ for p in sim .net .pops : del p
685+ if hasattr (sim .net , 'params' ):
686+ del sim .net .params
687+
688+
689+ # clean cells and simData gathered in master node
690+ if sim .rank == 0 :
691+ if hasattr (sim .net , 'allCells' ):
692+ sim .clearObj ([cell .__dict__ if hasattr (cell , '__dict__' ) else cell for cell in sim .net .allCells ])
693+ if hasattr (sim , 'allSimData' ):
694+ if 'stims' in list (sim .allSimData .keys ()):
695+ sim .clearObj ([stim for stim in sim .allSimData ['stims' ]])
696+ for key in list (sim .allSimData .keys ()): del sim .allSimData [key ]
697+ del sim .allSimData
698+
699+
700+ import matplotlib
701+ matplotlib .pyplot .clf ()
702+ matplotlib .pyplot .close ('all' )
703+
704+ if hasattr (sim , 'net' ):
705+ if hasattr (sim .net , 'allCells' ):
706+ for c in sim .net .allCells : del c
707+ del sim .net .allCells
708+ if hasattr (sim .net , 'allPops' ):
709+ for p in sim .net .allPops : del p
710+
711+ del sim .net
712+
713+ import gc ; gc .collect ()
714+
715+
655716logging .info ("Initialising NetPyNE UI" )
656717netpyne_geppetto = NetPyNEGeppetto ()
657718logging .info ("NetPyNE UI initialised" )
0 commit comments