Skip to content

Commit 44acd3f

Browse files
committed
Merge branch 'feat-timeseries-split' into experimental
2 parents c778194 + 2a42475 commit 44acd3f

File tree

8 files changed

+215
-187
lines changed

8 files changed

+215
-187
lines changed

pyneuroml/archive/__init__.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import argparse
1010
import logging
11-
import os
1211
import pathlib
1312
import shutil
1413
import typing
@@ -18,6 +17,7 @@
1817
import pyneuroml.sedml as pynmls
1918
import pyneuroml.utils as pynmlu
2019
import pyneuroml.utils.cli as pynmluc
20+
from pyneuroml.utils.misc import chdir
2121

2222
logger = logging.getLogger(__name__)
2323
logger.setLevel(logging.INFO)
@@ -170,22 +170,18 @@ def create_combine_archive(
170170
zipfile_name = rootfile
171171

172172
# change to directory of rootfile
173-
thispath = os.getcwd()
174-
os.chdir(rootdir)
175-
176-
lems_def_dir = None
177-
if len(filelist) == 0:
178-
lems_def_dir = pynmlu.get_model_file_list(
179-
rootfile, filelist, rootdir, lems_def_dir
180-
)
181-
182-
create_combine_archive_manifest(rootfile, filelist + extra_files, rootdir)
183-
filelist.append("manifest.xml")
173+
with chdir(rootdir):
174+
lems_def_dir = None
175+
if len(filelist) == 0:
176+
lems_def_dir = pynmlu.get_model_file_list(
177+
rootfile, filelist, rootdir, lems_def_dir
178+
)
179+
create_combine_archive_manifest(rootfile, filelist + extra_files, rootdir)
180+
filelist.append("manifest.xml")
184181

185-
with ZipFile(zipfile_name + zipfile_extension, "w") as archive:
186-
for f in filelist + extra_files:
187-
archive.write(f)
188-
os.chdir(thispath)
182+
with ZipFile(zipfile_name + zipfile_extension, "w") as archive:
183+
for f in filelist + extra_files:
184+
archive.write(f)
189185

190186
if lems_def_dir is not None:
191187
logger.info(f"Removing LEMS definitions directory {lems_def_dir}")

pyneuroml/nsgr.py

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from zipfile import ZipFile
1515

1616
from pyneuroml.runners import generate_sim_scripts_in_folder
17+
from pyneuroml.utils.misc import chdir
1718

1819
logger = logging.getLogger(__name__)
1920
logger.setLevel(logging.DEBUG)
@@ -126,8 +127,6 @@ def run_on_nsg(
126127
# NSG requires that the top level directory exist
127128
nsg_dir = pathlib.Path(zipfile_name.replace(".zip", ""))
128129

129-
cwd = pathlib.Path.cwd()
130-
131130
tdir = generate_sim_scripts_in_folder(
132131
engine=engine,
133132
lems_file_name=lems_file_name,
@@ -139,44 +138,43 @@ def run_on_nsg(
139138
logger.info("Generating zip file")
140139
runner_file = ""
141140

142-
os.chdir(str(tdir))
143-
generated_files = os.listdir(nsg_dir)
144-
145-
print(f"Generated files are {generated_files}")
146-
147-
with ZipFile(zipfile_name, "w") as archive:
148-
for f in generated_files:
149-
if engine == "jneuroml_neuron":
150-
if f.endswith("_nrn.py"):
151-
runner_file = f
152-
elif engine == "jneuroml_netpyne":
153-
if f.endswith("_netpyne.py"):
154-
runner_file = f
155-
fpath = pathlib.Path(f)
156-
moved_path = nsg_dir / fpath
157-
archive.write(str(moved_path))
158-
159-
logger.debug("Printing testParam.properties")
160-
nsg_sim_config_dict["filename_"] = runner_file
161-
logger.debug(f"NSG sim config is: {nsg_sim_config_dict}")
162-
163-
with open("testParam.properties", "w") as file:
164-
for key, val in nsg_sim_config_dict.items():
165-
print(f"{key}={val}", file=file)
166-
167-
logger.debug("Printing testInput.properties")
168-
with open("testInput.properties", "w") as file:
169-
print(f"infile_=@./{zipfile_name}", file=file)
170-
171-
print(f"{zipfile_name} generated")
172-
# uses argv, where the first argument is the script itself, so we must pass
173-
# something as the 0th index of the list
174-
if not dry_run:
175-
if nsgr_submit(["", ".", "validate"]) == 0:
176-
print("Attempting to submit to NSGR")
177-
return nsgr_submit(["", ".", "run"])
178-
else:
179-
print("Dry run mode enabled. Not submitting to NSG.")
180-
181-
os.chdir(str(cwd))
141+
with chdir(str(tdir)):
142+
generated_files = os.listdir(nsg_dir)
143+
144+
print(f"Generated files are {generated_files}")
145+
146+
with ZipFile(zipfile_name, "w") as archive:
147+
for f in generated_files:
148+
if engine == "jneuroml_neuron":
149+
if f.endswith("_nrn.py"):
150+
runner_file = f
151+
elif engine == "jneuroml_netpyne":
152+
if f.endswith("_netpyne.py"):
153+
runner_file = f
154+
fpath = pathlib.Path(f)
155+
moved_path = nsg_dir / fpath
156+
archive.write(str(moved_path))
157+
158+
logger.debug("Printing testParam.properties")
159+
nsg_sim_config_dict["filename_"] = runner_file
160+
logger.debug(f"NSG sim config is: {nsg_sim_config_dict}")
161+
162+
with open("testParam.properties", "w") as file:
163+
for key, val in nsg_sim_config_dict.items():
164+
print(f"{key}={val}", file=file)
165+
166+
logger.debug("Printing testInput.properties")
167+
with open("testInput.properties", "w") as file:
168+
print(f"infile_=@./{zipfile_name}", file=file)
169+
170+
print(f"{zipfile_name} generated")
171+
# uses argv, where the first argument is the script itself, so we must pass
172+
# something as the 0th index of the list
173+
if not dry_run:
174+
if nsgr_submit(["", ".", "validate"]) == 0:
175+
print("Attempting to submit to NSGR")
176+
return nsgr_submit(["", ".", "run"])
177+
else:
178+
print("Dry run mode enabled. Not submitting to NSG.")
179+
182180
return tdir

pyneuroml/runners.py

Lines changed: 77 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import pyneuroml.utils.simdata as pynmls
3131
from pyneuroml import DEFAULTS, __version__
3232
from pyneuroml.errors import UNKNOWN_ERR
33+
from pyneuroml.utils.misc import chdir
3334

3435
logger = logging.getLogger(__name__)
3536
logger.setLevel(logging.INFO)
@@ -1209,7 +1210,6 @@ def generate_sim_scripts_in_folder(
12091210
if root_dir is None:
12101211
root_dir = "."
12111212

1212-
cwd = Path.cwd()
12131213
tdir = pyneuroml.utils.get_pyneuroml_tempdir(rootdir=run_dir, prefix="pyneuroml")
12141214
os.mkdir(tdir)
12151215

@@ -1220,94 +1220,89 @@ def generate_sim_scripts_in_folder(
12201220

12211221
# change to root_dir, so that we're in the directory where the lems file
12221222
# is
1223-
os.chdir(root_dir)
1224-
1225-
logger.debug("Getting list of model files")
1226-
model_file_list = [] # type: list
1227-
lems_def_dir = None
1228-
lems_def_dir = pyneuroml.utils.get_model_file_list(
1229-
lems_file_name, model_file_list, root_dir, lems_def_dir
1230-
)
1231-
1232-
logger.debug(f"Model file list is {model_file_list}")
1233-
1234-
for model_file in model_file_list:
1235-
logger.debug(f"Copying: {model_file} -> {tdir}/{model_file}")
1236-
# if model file has directory structures in it, recreate the dirs in
1237-
# the temporary directory
1238-
if len(model_file.split("/")) > 1:
1239-
# throw error if files in parent directories are referred to
1240-
if "../" in model_file:
1241-
raise ValueError(
1242-
"""
1243-
Cannot handle parent directories because we
1244-
cannot create these directories correctly in
1245-
the temporary location. Please re-organize
1246-
your code such that all included files are in
1247-
sub-directories of the root directory where the
1248-
main file resides.
1249-
"""
1250-
)
1251-
1252-
model_file_path = pathlib.Path(tdir + "/" + model_file)
1253-
parent = model_file_path.parent
1254-
parent.mkdir(parents=True, exist_ok=True)
1255-
shutil.copy(model_file, tdir + "/" + model_file)
1256-
1257-
if lems_def_dir is not None:
1258-
logger.info(f"Removing LEMS definitions directory {lems_def_dir}")
1259-
shutil.rmtree(lems_def_dir)
1260-
1261-
os.chdir(tdir)
1262-
logger.info(f"Working in {tdir}")
1263-
start_time = time.time() - 1.0
1264-
1265-
if engine == "jneuroml_neuron":
1266-
run_lems_with(
1267-
engine,
1268-
lems_file_name=Path(lems_file_name).name,
1269-
compile_mods=False,
1270-
only_generate_scripts=True,
1271-
*engine_args,
1272-
**engine_kwargs,
1273-
)
1274-
elif engine == "jneuroml_netpyne":
1275-
run_lems_with(
1276-
engine,
1277-
lems_file_name=Path(lems_file_name).name,
1278-
only_generate_scripts=True,
1279-
*engine_args,
1280-
**engine_kwargs,
1223+
with chdir(root_dir):
1224+
logger.debug("Getting list of model files")
1225+
model_file_list = [] # type: list
1226+
lems_def_dir = None
1227+
lems_def_dir = pyneuroml.utils.get_model_file_list(
1228+
lems_file_name, model_file_list, root_dir, lems_def_dir
12811229
)
12821230

1283-
generated_files = pyneuroml.utils.get_files_generated_after(
1284-
start_time, ignore_suffixes=["xml", "nml"]
1285-
)
1231+
logger.debug(f"Model file list is {model_file_list}")
1232+
1233+
for model_file in model_file_list:
1234+
logger.debug(f"Copying: {model_file} -> {tdir}/{model_file}")
1235+
# if model file has directory structures in it, recreate the dirs in
1236+
# the temporary directory
1237+
if len(model_file.split("/")) > 1:
1238+
# throw error if files in parent directories are referred to
1239+
if "../" in model_file:
1240+
raise ValueError(
1241+
"""
1242+
Cannot handle parent directories because we
1243+
cannot create these directories correctly in
1244+
the temporary location. Please re-organize
1245+
your code such that all included files are in
1246+
sub-directories of the root directory where the
1247+
main file resides.
1248+
"""
1249+
)
1250+
1251+
model_file_path = pathlib.Path(tdir + "/" + model_file)
1252+
parent = model_file_path.parent
1253+
parent.mkdir(parents=True, exist_ok=True)
1254+
shutil.copy(model_file, tdir + "/" + model_file)
1255+
1256+
if lems_def_dir is not None:
1257+
logger.info(f"Removing LEMS definitions directory {lems_def_dir}")
1258+
shutil.rmtree(lems_def_dir)
1259+
1260+
with chdir(tdir):
1261+
logger.info(f"Working in {tdir}")
1262+
start_time = time.time() - 1.0
1263+
1264+
if engine == "jneuroml_neuron":
1265+
run_lems_with(
1266+
engine,
1267+
lems_file_name=Path(lems_file_name).name,
1268+
compile_mods=False,
1269+
only_generate_scripts=True,
1270+
*engine_args,
1271+
**engine_kwargs,
1272+
)
1273+
elif engine == "jneuroml_netpyne":
1274+
run_lems_with(
1275+
engine,
1276+
lems_file_name=Path(lems_file_name).name,
1277+
only_generate_scripts=True,
1278+
*engine_args,
1279+
**engine_kwargs,
1280+
)
12861281

1287-
# For NetPyNE, the channels are converted to NEURON mod files, but the
1288-
# network and cells are imported from the nml files.
1289-
# So we include all the model files too.
1290-
if engine == "jneuroml_netpyne":
1291-
generated_files.extend(model_file_list)
1282+
generated_files = pyneuroml.utils.get_files_generated_after(
1283+
start_time, ignore_suffixes=["xml", "nml"]
1284+
)
12921285

1293-
logger.debug(f"Generated files are: {generated_files}")
1286+
# For NetPyNE, the channels are converted to NEURON mod files, but the
1287+
# network and cells are imported from the nml files.
1288+
# So we include all the model files too.
1289+
if engine == "jneuroml_netpyne":
1290+
generated_files.extend(model_file_list)
12941291

1295-
if generated_files_dir_name is None:
1296-
generated_files_dir_name = Path(tdir).name + "_generated"
1297-
logger.debug(
1298-
f"Creating directory and moving generated files to it: {generated_files_dir_name}"
1299-
)
1292+
logger.debug(f"Generated files are: {generated_files}")
13001293

1301-
for f in generated_files:
1302-
fpath = pathlib.Path(f)
1303-
moved_path = generated_files_dir_name / fpath
1304-
# use os.renames because pathlib.Path.rename does not move
1305-
# recursively and so cannot move files within directories
1306-
os.renames(fpath, moved_path)
1294+
if generated_files_dir_name is None:
1295+
generated_files_dir_name = Path(tdir).name + "_generated"
1296+
logger.debug(
1297+
f"Creating directory and moving generated files to it: {generated_files_dir_name}"
1298+
)
13071299

1308-
# return to original directory
1309-
# doesn't affect scripts much, but does affect our tests
1310-
os.chdir(str(cwd))
1300+
for f in generated_files:
1301+
fpath = pathlib.Path(f)
1302+
moved_path = generated_files_dir_name / fpath
1303+
# use os.renames because pathlib.Path.rename does not move
1304+
# recursively and so cannot move files within directories
1305+
os.renames(fpath, moved_path)
13111306

13121307
return tdir
13131308

pyneuroml/utils/__init__.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -713,17 +713,18 @@ def get_model_file_list(
713713
filelist.append(str(fullrootfile_rel))
714714

715715
if str(rootfile_name).endswith(".nml"):
716-
print(f"Processing NML file: {fullrootfile_rel}")
716+
logger.info(f"Processing NML file: {fullrootfile_rel}")
717717
rootdoc = read_neuroml2_file(fullrootfile_rel)
718718
logger.debug(f"Has includes: {rootdoc.includes}")
719719

720720
for inc in rootdoc.includes:
721-
logger.debug(f"Processing includes: {inc.href} in {str(rootdirpath)}")
721+
logger.debug(f"NML: Processing includes: {inc.href} in {str(rootdirpath)}")
722722
lems_def_dir = get_model_file_list(
723723
inc.href, filelist, str(rootdirpath_rel), lems_def_dir
724724
)
725725

726726
elif str(rootfile_name).endswith(".xml"):
727+
logger.info(f"Processing LEMS file: {fullrootfile_rel}")
727728
# extract the standard NeuroML2 LEMS definitions into a directory
728729
# so that the LEMS parser can find them
729730
if lems_def_dir is None:
@@ -734,17 +735,25 @@ def get_model_file_list(
734735
model.import_from_file(fullrootfile_rel)
735736

736737
for inc in model.included_files:
737-
# `inc` includes the folder name, but we want to keep the file name
738-
# and the directory in which it is located separtely as the
739-
# directory may have to be passed on recursively to other included
740-
# files. So, we separate the name out.
741-
incfile = pathlib.Path(inc).name
742-
logger.debug(f"Processing include file {incfile} ({inc})")
738+
# `inc` includes the complete path relative to the current
739+
# directory, which may repeat information such as the "rootdirpath"
740+
# that we're tracking outselves. So, we need to do some massaging
741+
# here to get the path to inc relative to the rootdirpath_rel
742+
# again
743+
incfile_path = pathlib.Path(inc)
744+
incfile = incfile_path.name
745+
746+
if incfile_path.is_relative_to(rootdirpath_rel):
747+
incfile_path_rel = incfile_path.relative_to(rootdirpath_rel)
748+
else:
749+
incfile_path_rel = incfile_path
750+
751+
logger.debug(f"LEMS: Processing include file {incfile} ({inc})")
743752
if incfile in STANDARD_LEMS_FILES:
744753
logger.debug(f"Ignoring NeuroML2 standard LEMS file: {inc}")
745754
continue
746755
lems_def_dir = get_model_file_list(
747-
incfile, filelist, str(rootdirpath_rel), lems_def_dir
756+
str(incfile_path_rel), filelist, str(rootdirpath_rel), lems_def_dir
748757
)
749758

750759
elif str(rootfile_name).endswith(".sedml"):
@@ -754,6 +763,7 @@ def get_model_file_list(
754763
logger.error("Please install optional dependencies to use SED-ML features:")
755764
logger.error("pip install pyneuroml[combine]")
756765

766+
logger.info(f"Processing SED-ML file: {fullrootfile_rel}")
757767
rootdoc = libsedml.readSedMLFromFile(fullrootfile_rel)
758768

759769
# there should only be one model

0 commit comments

Comments
 (0)