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