1313import json
1414import logging
1515import os
16+ from pathlib import Path
1617import pprint
1718import re
1819import sys
4546
4647os .chdir (constants .NETPYNE_WORKDIR_PATH )
4748
48- neuron .nrn_dll_loaded .append (os .path .join (NETPYNE_WORKDIR_PATH , 'mod' )) # Avoids to load workspace modfiles twice
4949
5050class NetPyNEGeppetto :
5151
@@ -79,17 +79,6 @@ def __init__(self):
7979 if not simulations .local .is_running ():
8080 [experiments .set_to_error (e ) for e in running_exps ]
8181
82- # sys.stdin = open(os.path.join(constants.HERE, "stdin.txt"),'r') # FIXES library asking for input to download -- eg lfpykit models
83-
84- # from ipykernel import kernelbase
85-
86- # def raw_input(self, prompt=''):
87- # return "y"
88- # kernelbase.Kernel.raw_input = raw_input
89-
90- # from lfpykit.eegmegcalc import NYHeadModel
91- # NYHeadModel()
92-
9382 def getData (self ):
9483 return {
9584 "metadata" : metadata ,
@@ -511,7 +500,32 @@ def _create3D_shapes(self, json_path: str):
511500 # Load again because gatherData removed simData
512501 sim .loadSimData (json_path )
513502
503+ def loadFromIndexFile (self , json_path : str ):
504+ cfg , netParams = sim .loadModel (json_path , loadMechs = True , ignoreMechAlreadyExistsError = True )
505+ self .simConfig = cfg
506+ self .netParams = netParams
507+
508+ if isinstance (self .netParams , dict ):
509+ self .netParams = specs .NetParams (self .netParams )
510+
511+ if isinstance (self .simConfig , dict ):
512+ self .simConfig = specs .SimConfig (self .simConfig )
514513
514+ for key , value in self .netParams .cellParams .items ():
515+ if hasattr (value , 'todict' ):
516+ self .netParams .cellParams [key ] = value .todict ()
517+
518+ # TODO: when should sim.initialize be called?
519+ # Only on import or better before every simulation or network instantiation?
520+ sim .initialize ()
521+
522+ def saveToIndexFile (self , srcPath , dstPath , exportNetParamsAsPython , exportSimConfigAsPython ):
523+ sim .saveModel (netParams = self .netParams ,
524+ simConfig = self .simConfig ,
525+ srcPath = srcPath ,
526+ dstPath = dstPath ,
527+ exportNetParamsAsPython = exportNetParamsAsPython ,
528+ exportSimConfigAsPython = exportSimConfigAsPython )
515529
516530 def importModel (self , modelParameters ):
517531 """ Imports a model stored in form of Python files.
@@ -524,7 +538,7 @@ def importModel(self, modelParameters):
524538 # Shouldn't be specific to Import
525539 sim .clearAll ()
526540 try :
527- loadModMechFiles (modelParameters ['compileMod' ], modelParameters ['modFolder' ])
541+ loadModMechFiles (modelParameters ['compileMod' ], modelParameters ['modFolder' ], modelParameters . get ( "forceRecompile" , True ) )
528542 except Exception :
529543 message = "Error while importing/compiling mods"
530544 logging .exception (message )
@@ -566,7 +580,7 @@ def importModel(self, modelParameters):
566580 # Only on import or better before every simulation or network instantiation?
567581 sim .initialize ()
568582 return utils .getJSONReply ()
569- except Exception :
583+ except :
570584 message = "Error while importing the NetPyNE model"
571585 logging .exception (message )
572586 return utils .getJSONError (message , sys .exc_info ())
@@ -575,7 +589,7 @@ def importModel(self, modelParameters):
575589
576590 def importNeuroML (self , modelParameters ):
577591 from netpyne_ui .helpers import neuroml
578-
592+
579593
580594 try :
581595 # Get Current dir
@@ -584,9 +598,9 @@ def importNeuroML(self, modelParameters):
584598 with redirect_stdout (sys .__stdout__ ):
585599 # NetParams
586600 filename = str (modelParameters ["fileName" ])
587-
601+
588602 json_fname = neuroml .convertNeuroML2 (filename , compileMod = modelParameters ["compileMod" ])
589-
603+
590604 return self .loadModel (args = dict (
591605 compileMod = True ,
592606 modFolder = os .path .dirname (json_fname ),
@@ -605,7 +619,7 @@ def importNeuroML(self, modelParameters):
605619
606620 def importLEMS (self , modelParameters ):
607621 from netpyne_ui .helpers import neuroml
608-
622+
609623
610624 try :
611625 # Get Current dir
@@ -614,7 +628,7 @@ def importLEMS(self, modelParameters):
614628 with redirect_stdout (sys .__stdout__ ):
615629 # NetParams
616630 filename = str (modelParameters ["fileName" ])
617-
631+
618632 json_fname = neuroml .convertLEMSSimulation (filename )
619633
620634 return self .loadModel (args = dict (
@@ -789,10 +803,21 @@ def getPlotSettings(self, plot_name):
789803 return self .simConfig .analysis [plot_name ]
790804 return {}
791805
792- def getDirList (self , dir = None , onlyDirs = False , filterFiles = False ):
793- # Get Current dir
806+ def checkFileExists (self , path ):
807+ path = Path (path or '' )
808+ return path .exists ()
809+
810+ def getFullPath (self , dir , subDir ):
794811 if dir is None or dir == '' :
795- dir = os .path .join (os .getcwd (), constants .NETPYNE_WORKDIR_PATH )
812+ base = constants .NETPYNE_WORKDIR_PATH
813+ if subDir :
814+ base = os .path .join (base , subDir )
815+ dir = os .path .join (os .getcwd (), base )
816+ return dir
817+
818+ def getDirList (self , dir = None , onlyDirs = False , filterFiles = False , subDir = None ):
819+ # Get Current dir
820+ dir = self .getFullPath (dir , subDir )
796821 dir_list = []
797822 file_list = []
798823 for f in sorted (os .listdir (str (dir )), key = str .lower ):
@@ -810,6 +835,13 @@ def checkAvailablePlots(self):
810835 def getPlot (self , plotName , LFPflavour , theme = 'gui' ):
811836 try :
812837 with redirect_stdout (sys .__stdout__ ):
838+ availablePlots = self .checkAvailablePlots ()
839+ checkCondition = availablePlots .get (plotName .replace ('iplot' , 'plot' ), False )
840+
841+ if checkCondition is False :
842+ logging .info ("Plot " + plotName + " not available" )
843+ return - 1
844+
813845 args = self .getPlotSettings (plotName )
814846 if LFPflavour :
815847 args ['plots' ] = [LFPflavour ]
@@ -899,10 +931,10 @@ def getAvailableCellTypes(self):
899931 cell_types .add (p )
900932 return sorted (cell_types )
901933
902- def getAvailableRxDSections (self , selectedRegion ):
934+ def getAvailableRxDSections (self , selectedRegion = None ):
903935 sections = set ([])
904936 sections .add ('all' )
905- if selectedRegion in self .netParams .rxdParams .regions and self .netParams .rxdParams .regions [selectedRegion ].get ('cells' ):
937+ if selectedRegion and selectedRegion in self .netParams .rxdParams .regions and self .netParams .rxdParams .regions [selectedRegion ].get ('cells' ):
906938 if 'all' in self .netParams .rxdParams .regions [selectedRegion ]['cells' ]:
907939 for cellRule in self .netParams .cellParams :
908940 for cellSect in self .netParams .cellParams [cellRule ]['secs' ]:
0 commit comments