@@ -59,7 +59,7 @@ def instantiateNetPyNEModelInGeppetto(self, args):
5959 netpyne_model = self .instantiateNetPyNEModel ()
6060 self .geppetto_model = self .model_interpreter .getGeppettoModel (netpyne_model )
6161
62- return json .loads (GeppettoModelSerializer () .serialize (self .geppetto_model ))
62+ return json .loads (GeppettoModelSerializer .serialize (self .geppetto_model ))
6363 except :
6464 return utils .getJSONError ("Error while instantiating the NetPyNE model" , sys .exc_info ())
6565
@@ -185,7 +185,7 @@ def remove(dictionary):
185185 sim .loadSimData (args ['jsonModelFolder' ])
186186 self .geppetto_model = self .model_interpreter .getGeppettoModel (sim )
187187
188- return json .loads (GeppettoModelSerializer () .serialize (self .geppetto_model ))
188+ return json .loads (GeppettoModelSerializer .serialize (self .geppetto_model ))
189189 else :
190190 return utils .getJSONReply ()
191191 except :
@@ -263,6 +263,11 @@ def exportModel(self, args):
263263 sim .cfg .saveJson = True
264264 sim .saveData (include )
265265 sim .cfg .saveJson = False
266+
267+ with open (f"{ sim .cfg .filename } .json" ) as json_file :
268+ data = json .load (json_file )
269+ return data
270+
266271 return utils .getJSONReply ()
267272 except :
268273 return utils .getJSONError ("Error while exporting the NetPyNE model" , sys .exc_info ())
@@ -281,24 +286,31 @@ def importNeuroML(self, modelParams):
281286 sim .initialize ()
282287 sim .importNeuroML2 (modelParams ['neuroMLFolder' ], simConfig = specs .SimConfig (), simulate = False , analyze = False )
283288 self .geppetto_model = self .model_interpreter .getGeppettoModel (sim )
284- return json .loads (GeppettoModelSerializer () .serialize (self .geppetto_model ))
289+ return json .loads (GeppettoModelSerializer .serialize (self .geppetto_model ))
285290
286291 except :
287292 return utils .getJSONError ("Error while exporting the NetPyNE model" , sys .exc_info ())
288293
289294 def deleteModel (self , modelParams ):
295+
290296 try :
291297 with redirect_stdout (sys .__stdout__ ):
292298 self .netParams = specs .NetParams ()
293299 self .simConfig = specs .SimConfig ()
294- self .netParams .todict ()
295- self .netParams .todict ()
296- if self .doIhaveInstOrSimData ()['haveInstance' ]: sim .clearAll ()
300+ sim .initialize (specs .NetParams (), specs .SimConfig ())
297301 self .geppetto_model = None
298- return utils .getJSONReply ()
299-
300302 except :
301303 return utils .getJSONError ("Error while exporting the NetPyNE model" , sys .exc_info ())
304+
305+ try :
306+ # This function fails is some keys don't exists
307+ # sim.clearAll()
308+ self .clearSim ()
309+
310+ except :
311+ pass
312+
313+ return utils .getJSONReply ()
302314
303315 def instantiateNetPyNEModel (self ):
304316 with redirect_stdout (sys .__stdout__ ):
@@ -542,7 +554,8 @@ def header(title, spacer='-'):
542554
543555 script .write (header ('end script' , spacer = '=' ))
544556
545- return utils .getJSONReply ()
557+ with open (fname ) as f :
558+ return f .read ()
546559
547560 except :
548561 return utils .getJSONError ("Error while importing the NetPyNE model" , sys .exc_info ())
@@ -649,6 +662,52 @@ def propagate_syn_mech_rename(self, new, old):
649662 self .netParams .stimTargetParams [label ]['synMech' ] = new
650663
651664
665+ def clearSim (self ):
666+ # clean up
667+ sim .pc .barrier ()
668+ sim .pc .gid_clear () # clear previous gid settings
669+
670+ # clean cells and simData in all nodes
671+ sim .clearObj ([cell .__dict__ if hasattr (cell , '__dict__' ) else cell for cell in sim .net .cells ])
672+ if 'stims' in list (sim .simData .keys ()):
673+ sim .clearObj ([stim for stim in sim .simData ['stims' ]])
674+
675+ for key in list (sim .simData .keys ()): del sim .simData [key ]
676+
677+ if hasattr (sim , 'net' ):
678+ for c in sim .net .cells : del c
679+ for p in sim .net .pops : del p
680+ if hasattr (sim .net , 'params' ):
681+ del sim .net .params
682+
683+
684+ # clean cells and simData gathered in master node
685+ if sim .rank == 0 :
686+ if hasattr (sim .net , 'allCells' ):
687+ sim .clearObj ([cell .__dict__ if hasattr (cell , '__dict__' ) else cell for cell in sim .net .allCells ])
688+ if hasattr (sim , 'allSimData' ):
689+ if 'stims' in list (sim .allSimData .keys ()):
690+ sim .clearObj ([stim for stim in sim .allSimData ['stims' ]])
691+ for key in list (sim .allSimData .keys ()): del sim .allSimData [key ]
692+ del sim .allSimData
693+
694+
695+ import matplotlib
696+ matplotlib .pyplot .clf ()
697+ matplotlib .pyplot .close ('all' )
698+
699+ if hasattr (sim , 'net' ):
700+ if hasattr (sim .net , 'allCells' ):
701+ for c in sim .net .allCells : del c
702+ del sim .net .allCells
703+ if hasattr (sim .net , 'allPops' ):
704+ for p in sim .net .allPops : del p
705+
706+ del sim .net
707+
708+ import gc ; gc .collect ()
709+
710+
652711logging .info ("Initialising NetPyNE UI" )
653712netpyne_geppetto = NetPyNEGeppetto ()
654713logging .info ("NetPyNE UI initialised" )
0 commit comments