diff --git a/emodel_generalisation/__init__.py b/emodel_generalisation/__init__.py index 711b77f..6bdbb04 100644 --- a/emodel_generalisation/__init__.py +++ b/emodel_generalisation/__init__.py @@ -22,23 +22,10 @@ # pylint: disable=line-too-long import logging import os -from pathlib import Path os.environ["NEURON_MODULE_OPTIONS"] = "-nogui" logger = logging.getLogger(__name__) -_TMPDIR = os.environ.get("TMPDIR", None) -if _TMPDIR is not None: - try: - import neuron - - if (Path(_TMPDIR) / "x86_64").exists(): - if not neuron.load_mechanisms(_TMPDIR): - raise Exception("Could not load mod files") - except Exception as exc: # pylint: disable=broad-exception-caught - logger.debug("Could not load mod files from %s because of %s", _TMPDIR, exc) - os.environ["DASK_TEMPORARY_DIRECTORY"] = _TMPDIR - PARAM_LABELS = { # passive diff --git a/emodel_generalisation/model/nexus_converter.py b/emodel_generalisation/model/nexus_converter.py index f2764f8..ee292ac 100644 --- a/emodel_generalisation/model/nexus_converter.py +++ b/emodel_generalisation/model/nexus_converter.py @@ -8,6 +8,7 @@ from copy import copy from pathlib import Path +import neuron from tqdm import tqdm L = logging.getLogger(__name__) @@ -197,6 +198,19 @@ def convert_all_config(config_path, out_config_folder="config", mech_path="mecha json.dump(final, final_file, indent=4) +def load_mechanisms(): + """Load mechanisms if present in TMPDIR.""" + _TMPDIR = os.environ.get("TMPDIR", None) + if _TMPDIR is not None: + try: + if (Path(_TMPDIR) / "x86_64").exists(): + if not neuron.load_mechanisms(_TMPDIR): + raise Exception("Could not load mod files") + except Exception as exc: # pylint: disable=broad-exception-caught + L.debug("Could not load mod files from %s because of %s", _TMPDIR, exc) + os.environ["DASK_TEMPORARY_DIRECTORY"] = _TMPDIR + + def compile_mechanisms(mech_path="mechanisms", compiled_mech_path=None): """Compile mechanisms in custom location.""" if compiled_mech_path is None: @@ -208,3 +222,5 @@ def compile_mechanisms(mech_path="mechanisms", compiled_mech_path=None): os.chdir(compiled_mech_path) subprocess.run(f"nrnivmodl {mech_path}", shell=True, check=True) os.chdir(cwd) + # load mechs after compile + load_mechanisms()