|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import logging |
| 4 | +from netpyne.specs import simConfig |
| 5 | + |
| 6 | +from pyneuroml import pynml |
| 7 | +from pyneuroml.lems import generate_lems_file_for_neuroml |
| 8 | +from pyneuroml.pynml import read_neuroml2_file |
| 9 | + |
| 10 | +from netpyne_ui.mod_utils import loadModMechFiles |
| 11 | + |
| 12 | + |
| 13 | +def convertLEMSSimulation(lemsFileName, compileMod=True): |
| 14 | + """Converts a LEMS Simulation file |
| 15 | +
|
| 16 | + Converts a LEMS Simulation file (https://docs.neuroml.org/Userdocs/LEMSSimulation.html) |
| 17 | + pointing to a NeuroML 2 file into the equivalent in NetPyNE |
| 18 | + Returns: |
| 19 | + simConfig, netParams for the model in NetPyNE |
| 20 | + """ |
| 21 | + current_path = os.getcwd() |
| 22 | + try: |
| 23 | + |
| 24 | + fullLemsFileName = os.path.abspath(lemsFileName) |
| 25 | + tmp_path = os.path.dirname(fullLemsFileName) |
| 26 | + if tmp_path: |
| 27 | + os.chdir(tmp_path) |
| 28 | + logging.info( |
| 29 | + "Importing LEMSSimulation with NeuroML 2 network from: %s" |
| 30 | + % fullLemsFileName |
| 31 | + ) |
| 32 | + |
| 33 | + result = pynml.run_lems_with_jneuroml_netpyne( |
| 34 | + lemsFileName, only_generate_json=True, exit_on_fail=False) |
| 35 | + |
| 36 | + if result == False: |
| 37 | + raise Exception("Error loading lems file") |
| 38 | + lems = pynml.read_lems_file(lemsFileName) |
| 39 | + |
| 40 | + np_json_fname = fullLemsFileName.replace('.xml','_netpyne_data.json') |
| 41 | + |
| 42 | + return np_json_fname |
| 43 | + finally: |
| 44 | + os.chdir(current_path) |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +def convertNeuroML2(nml2FileName, compileMod=True): |
| 50 | + """Loads a NeuroML 2 file into NetPyNE |
| 51 | + Loads a NeuroML 2 file into NetPyNE by creating a new LEMS Simulation |
| 52 | + file (https://docs.neuroml.org/Userdocs/LEMSSimulation.html) and using jNeuroML |
| 53 | + to convert it. |
| 54 | +
|
| 55 | + Returns: |
| 56 | + simConfig, netParams for the model in NetPyNE |
| 57 | + """ |
| 58 | + current_path = os.getcwd() |
| 59 | + try: |
| 60 | + fullNmlFileName = os.path.abspath(nml2FileName) |
| 61 | + work_path = os.path.dirname(fullNmlFileName) |
| 62 | + if not os.path.exists(work_path): |
| 63 | + os.makedirs(work_path) |
| 64 | + os.chdir(work_path) |
| 65 | + sys.path.append(work_path) |
| 66 | + |
| 67 | + |
| 68 | + logging.info( |
| 69 | + "Importing NeuroML 2 network from: %s" |
| 70 | + % fullNmlFileName |
| 71 | + ) |
| 72 | + nml_model = read_neuroml2_file(fullNmlFileName) |
| 73 | + |
| 74 | + target = nml_model.networks[0].id |
| 75 | + sim_id = "Sim_%s" % target |
| 76 | + duration = 1000 |
| 77 | + dt = 0.025 |
| 78 | + lems_file_name = os.path.join(os.path.dirname(fullNmlFileName), "LEMS_%s.xml" % sim_id) |
| 79 | + lems_file_name = "LEMS_%s.xml" % sim_id |
| 80 | + target_dir = os.path.dirname(fullNmlFileName) |
| 81 | + target_dir = "." |
| 82 | + |
| 83 | + generate_lems_file_for_neuroml( |
| 84 | + sim_id, |
| 85 | + fullNmlFileName, |
| 86 | + target, |
| 87 | + duration, |
| 88 | + dt, |
| 89 | + lems_file_name, |
| 90 | + target_dir, |
| 91 | + include_extra_files=["PyNN.xml"], |
| 92 | + gen_plots_for_all_v=True, |
| 93 | + plot_all_segments=False, |
| 94 | + gen_plots_for_quantities={}, # Dict with displays vs lists of quantity paths |
| 95 | + gen_plots_for_only_populations=[], # List of populations, all pops if = [] |
| 96 | + gen_saves_for_all_v=True, |
| 97 | + save_all_segments=False, |
| 98 | + gen_saves_for_only_populations=[], # List of populations, all pops if = [] |
| 99 | + gen_saves_for_quantities={}, # Dict with file names vs lists of quantity paths |
| 100 | + gen_spike_saves_for_all_somas=True, |
| 101 | + report_file_name="report.txt", |
| 102 | + copy_neuroml=True, |
| 103 | + verbose=True, |
| 104 | + ) |
| 105 | + os.chdir(work_path) |
| 106 | + res = convertLEMSSimulation(lems_file_name, compileMod=compileMod) |
| 107 | + finally: |
| 108 | + os.chdir(current_path) |
| 109 | + return res |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + |
| 114 | + if '-nml' in sys.argv: |
| 115 | + convertNeuroML2("../../NeuroML2/Spikers.net.nml") |
| 116 | + else: |
| 117 | + convertLEMSSimulation("LEMS_HHSimple.xml") |
0 commit comments