Source code for netpyne.conversion.pythonScript

Module for exporting models as Python scripts


from netpyne import __version__

[docs] def createPythonScript(fname, netParams, simConfig): """ Function for/to <short description of `netpyne.conversion.pythonScript.createPythonScript`> Parameters ---------- fname : <type> <Short description of fname> **Default:** *required* netParams : <type> <Short description of netParams> **Default:** *required* simConfig : <type> <Short description of simConfig> **Default:** *required* """ netParams = prepareNetParams(netParams) simConfig = prepareSimConfig(simConfig) writeToFile(fname, netParams, simConfig)
[docs] def createPythonNetParams(fname, netParams): netParams = prepareNetParams(netParams) writeToFile(fname, netParams=netParams, writeSimulation=False)
[docs] def createPythonSimConfig(fname, simConfig, varName=None): simConfig = prepareSimConfig(simConfig) if varName: writeToFile(fname, simConfig=simConfig, simConfigVarName=varName, writeSimulation=False) else: writeToFile(fname, simConfig=simConfig, writeSimulation=False)
[docs] def prepareNetParams(netParams): from netpyne import specs if isinstance(netParams, specs.NetParams): # convert netpyne.specs.netParams class to dict class netParams = netParams.todict() __removeReservedKeys(netParams) # like __str__, __dict__ return netParams
[docs] def prepareSimConfig(simConfig): from netpyne import specs if isinstance(simConfig, specs.SimConfig): # convert netpyne.specs.netParams class to dict class simConfig = simConfig.todict() __removeReservedKeys(simConfig) # like __str__, __dict__ return simConfig
[docs] def writeToFile(fname, netParams=None, simConfig=None, simConfigVarName='simConfig', writeHeader=True, writeSimulation=True): import sys import json from netpyne import specs def replace(string): # convert bools and null from json to python return string.replace('true', 'True').replace('false', 'False').replace('null', '""') def addAttrToScript(attr, value, obj_name, class_instance, file): # write line of netpyne code if is different from default value if not hasattr(class_instance, attr) or value != getattr(class_instance, attr): file.write(obj_name + '.' + attr + ' = ' + replace(json.dumps(value, indent=4)) + '\n') def header(title, spacer='-'): # writes a header for the section return '\n# ' + title.upper() + ' ' + spacer * (77 - len(title)) + '\n' try : with open(fname if fname.endswith('.py') else fname+'.py', 'w') as file: imports = 'from netpyne import specs' if writeSimulation: imports += ', sim' imports += '\n' file.write(imports) if writeHeader: file.write(header('documentation')) file.write("''' Please visit: '''\n") if netParams and simConfig: generatedFrom = 'netParams and simConfig objects' elif netParams and not simConfig: generatedFrom = 'netParams object' else: generatedFrom = 'simConfig object' file.write(f"# Python script automatically generated by NetPyNE v{__version__} from {generatedFrom}\n") file.write(header('script', spacer='=')) if netParams: file.write('netParams = specs.NetParams()\n') if simConfig: file.write(f'{simConfigVarName} = specs.SimConfig()\n') if netParams: file.write(header('single valued attributes')) defaultNetParams = specs.NetParams() multivalued = ['popParams' , 'cellParams', 'synMechParams', 'connParams', 'stimSourceParams', 'stimTargetParams'] for key, value in list(netParams.items()): if key not in multivalued: addAttrToScript(key, value, 'netParams', defaultNetParams, file) file.write(header('network attributes')) for param in multivalued: for key, value in list(netParams[param].items()): file.write("netParams." + param + "['" + key + "'] = " + replace(json.dumps(value, indent=4))+ '\n') if simConfig: file.write(header('network configuration')) defaultCfg = specs.SimConfig() for key, value in list(simConfig.items()): addAttrToScript(key, value, simConfigVarName, defaultCfg, file) if writeSimulation: file.write(header('create simulate analyze network')) file.write('import sys\n') file.write('if not "-neuroml" in sys.argv:\n') file.write(f' sim.createSimulateAnalyze(netParams=netParams, simConfig={simConfigVarName})\n') file.write('else:\n') file.write(f' nml_reference = "NetPyNENetwork" if not {simConfigVarName}.filename else {simConfigVarName}.filename\n') file.write(f' sim.createExportNeuroML2(netParams=netParams, simConfig={simConfigVarName}, reference = nml_reference)\n') file.write(header('end script', spacer='=')) print(("script saved on " + fname)) except: print(('error saving file: %s' %(sys.exc_info()[1])))
def __removeReservedKeys(dictionary): # remove reserved keys such as __str__, __dict__ if isinstance(dictionary, dict): for key, value in list(dictionary.items()): if key.startswith('__'): dictionary.pop(key) else: __removeReservedKeys(value)