From 1556104056563fcb274bf83e5d9814fad386a5f1 Mon Sep 17 00:00:00 2001 From: ilkilic Date: Tue, 19 Mar 2024 15:09:44 +0100 Subject: [PATCH 1/6] set morph_modifiers_hoc --- bluepyemodel/access_point/local.py | 2 +- bluepyemodel/model/model.py | 21 ++++++++++++++++--- .../model/neuron_model_configuration.py | 6 +++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/bluepyemodel/access_point/local.py b/bluepyemodel/access_point/local.py index f4a90d8b..a2ec9bed 100644 --- a/bluepyemodel/access_point/local.py +++ b/bluepyemodel/access_point/local.py @@ -481,7 +481,7 @@ def get_model_configuration(self, skip_get_available_morph=False): configuration.init_from_dict(parameters, self.get_morphologies()) configuration.mapping_multilocation = self.get_recipes().get("multiloc_map", None) - + configuration.morph_modifiers = self.get_recipes().get("morph_modifiers", None) return configuration def store_targets_configuration(self, configuration): diff --git a/bluepyemodel/model/model.py b/bluepyemodel/model/model.py index 0b901089..8be79181 100644 --- a/bluepyemodel/model/model.py +++ b/bluepyemodel/model/model.py @@ -250,12 +250,27 @@ def define_morphology( morph_modifiers_hoc = None do_replace_axon = True else: + morph_modifiers_hoc = [None] * len(morph_modifiers) for i, morph_modifier in enumerate(morph_modifiers): if isinstance(morph_modifier, list): - modifier_module = importlib.import_module(morph_modifier[0]) - morph_modifiers[i] = getattr(modifier_module, morph_modifier[1]) + try: + modifier_module = importlib.import_module(morph_modifier[0]) + morph_modifiers[i] = getattr(modifier_module, morph_modifier[1]) + if len(morph_modifier) > 2: + morph_modifiers_hoc[i] = morph_modifier[2] + else: + morph_modifiers_hoc[i] = getattr(modifiers, morph_modifier[1]+'_hoc') + except AttributeError: + raise ImportError(f"Cannot import {morph_modifier[1]} or/and {morph_modifier[1]+'_hoc'} from {morph_modifier[0]}") + except IndexError: + raise ValueError(f"a morph_modifier should be a list of the form ['path_to_module', 'name_of_function', 'optional_hoc_string'], got {morph_modifier}") + elif isinstance(morph_modifier, str): - morph_modifiers[i] = getattr(modifiers, morph_modifier) + try: + morph_modifiers[i] = getattr(modifiers, morph_modifier) + morph_modifiers_hoc[i] = getattr(modifiers, morph_modifier+'_hoc') + except AttributeError: + raise ImportError(f"Cannot import {morph_modifier} or/and {morph_modifier+'_hoc'} from bluepyemodel.evaluation.modifiers") elif not callable(morph_modifier): raise TypeError( "A morph modifier is not callable nor a string nor a list of two str" diff --git a/bluepyemodel/model/neuron_model_configuration.py b/bluepyemodel/model/neuron_model_configuration.py index 8cf8c0d8..d2db13d6 100644 --- a/bluepyemodel/model/neuron_model_configuration.py +++ b/bluepyemodel/model/neuron_model_configuration.py @@ -84,12 +84,12 @@ def __init__( the ``./morphology`` directory for the local access point or on Nexus for the Nexus access point. morph_modifiers (list): List of morphology modifiers. Each modifier has to be - informed by the path the file containing the modifier and the name of the - function. E.g: + informed by the path the file containing the modifier, the name of the + function and the hoc string. E.g: .. code-block:: - morph_modifiers = [["path_to_module", "name_of_function"], ...]. + morph_modifiers = [["path_to_module", "name_of_function", "hoc_string"], ...]. extra_mech_ids (list of 2-d tuples): extra nexus ids and types to add to related nexus ids. Must have shape: From 508fd50628243f8d909a0a75b1d13dc695ba54fa Mon Sep 17 00:00:00 2001 From: ilkilic Date: Tue, 19 Mar 2024 15:52:45 +0100 Subject: [PATCH 2/6] lint fix --- bluepyemodel/model/model.py | 28 +++++++++++++------ .../model/neuron_model_configuration.py | 2 +- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/bluepyemodel/model/model.py b/bluepyemodel/model/model.py index 8be79181..cf535162 100644 --- a/bluepyemodel/model/model.py +++ b/bluepyemodel/model/model.py @@ -259,18 +259,30 @@ def define_morphology( if len(morph_modifier) > 2: morph_modifiers_hoc[i] = morph_modifier[2] else: - morph_modifiers_hoc[i] = getattr(modifiers, morph_modifier[1]+'_hoc') - except AttributeError: - raise ImportError(f"Cannot import {morph_modifier[1]} or/and {morph_modifier[1]+'_hoc'} from {morph_modifier[0]}") - except IndexError: - raise ValueError(f"a morph_modifier should be a list of the form ['path_to_module', 'name_of_function', 'optional_hoc_string'], got {morph_modifier}") + morph_modifiers_hoc[i] = getattr(modifiers, morph_modifier[1] + "_hoc") + except AttributeError as exc: + raise ImportError( + f"Cannot import {morph_modifier[1]} or/and " + "{morph_modifier[1] + '_hoc'} " + "from {morph_modifier[0]}" + ) from exc + except IndexError as exc: + raise ValueError( + "a morph_modifier should be a list of the form " + "['path_to_module', 'name_of_function', " + f"'optional_hoc_string'], got {morph_modifier}" + ) from exc elif isinstance(morph_modifier, str): try: morph_modifiers[i] = getattr(modifiers, morph_modifier) - morph_modifiers_hoc[i] = getattr(modifiers, morph_modifier+'_hoc') - except AttributeError: - raise ImportError(f"Cannot import {morph_modifier} or/and {morph_modifier+'_hoc'} from bluepyemodel.evaluation.modifiers") + morph_modifiers_hoc[i] = getattr(modifiers, morph_modifier + "_hoc") + except AttributeError as exc: + raise ImportError( + f"Cannot import {morph_modifier} or / " + " and {morph_modifier + '_hoc'} from " + "bluepyemodel.evaluation.modifiers" + ) from exc elif not callable(morph_modifier): raise TypeError( "A morph modifier is not callable nor a string nor a list of two str" diff --git a/bluepyemodel/model/neuron_model_configuration.py b/bluepyemodel/model/neuron_model_configuration.py index d2db13d6..6700b787 100644 --- a/bluepyemodel/model/neuron_model_configuration.py +++ b/bluepyemodel/model/neuron_model_configuration.py @@ -89,7 +89,7 @@ def __init__( .. code-block:: - morph_modifiers = [["path_to_module", "name_of_function", "hoc_string"], ...]. + morph_modifiers = [["path_to_module", "name_of_function", "hoc_string"],] extra_mech_ids (list of 2-d tuples): extra nexus ids and types to add to related nexus ids. Must have shape: From c1513e94f3e0bfd14f2b58c777b3028556ad8d60 Mon Sep 17 00:00:00 2001 From: ilkilic Date: Tue, 19 Mar 2024 16:46:11 +0100 Subject: [PATCH 3/6] generate a warning instead of raising an error when hoc string is missing --- bluepyemodel/model/model.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/bluepyemodel/model/model.py b/bluepyemodel/model/model.py index cf535162..39e0c47e 100644 --- a/bluepyemodel/model/model.py +++ b/bluepyemodel/model/model.py @@ -260,12 +260,13 @@ def define_morphology( morph_modifiers_hoc[i] = morph_modifier[2] else: morph_modifiers_hoc[i] = getattr(modifiers, morph_modifier[1] + "_hoc") - except AttributeError as exc: - raise ImportError( - f"Cannot import {morph_modifier[1]} or/and " - "{morph_modifier[1] + '_hoc'} " - "from {morph_modifier[0]}" - ) from exc + except AttributeError: + logger.warning( + "Cannot import %s or %s from %s", + morph_modifier[1], + morph_modifier[1] + "_hoc", + morph_modifier[0], + ) except IndexError as exc: raise ValueError( "a morph_modifier should be a list of the form " @@ -277,12 +278,12 @@ def define_morphology( try: morph_modifiers[i] = getattr(modifiers, morph_modifier) morph_modifiers_hoc[i] = getattr(modifiers, morph_modifier + "_hoc") - except AttributeError as exc: - raise ImportError( - f"Cannot import {morph_modifier} or / " - " and {morph_modifier + '_hoc'} from " - "bluepyemodel.evaluation.modifiers" - ) from exc + except AttributeError: + logger.warning( + "Cannot import %s or %s from bluepyemodel.evaluation.modifiers", + morph_modifier, + morph_modifier + "_hoc", + ) elif not callable(morph_modifier): raise TypeError( "A morph modifier is not callable nor a string nor a list of two str" From d5c2bbd232d8141ec69b3340bc9c365541bed194 Mon Sep 17 00:00:00 2001 From: ilkilic Date: Tue, 19 Mar 2024 17:21:36 +0100 Subject: [PATCH 4/6] get the hoc string from a variable --- bluepyemodel/model/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bluepyemodel/model/model.py b/bluepyemodel/model/model.py index 39e0c47e..b7b4c42a 100644 --- a/bluepyemodel/model/model.py +++ b/bluepyemodel/model/model.py @@ -257,7 +257,7 @@ def define_morphology( modifier_module = importlib.import_module(morph_modifier[0]) morph_modifiers[i] = getattr(modifier_module, morph_modifier[1]) if len(morph_modifier) > 2: - morph_modifiers_hoc[i] = morph_modifier[2] + morph_modifiers_hoc[i] = getattr(modifier_module, morph_modifier[2]) else: morph_modifiers_hoc[i] = getattr(modifiers, morph_modifier[1] + "_hoc") except AttributeError: From ad35d70e427048c61eb5d7a63cdaa767553dcc58 Mon Sep 17 00:00:00 2001 From: ilkilic Date: Tue, 19 Mar 2024 17:43:32 +0100 Subject: [PATCH 5/6] improve warning message --- bluepyemodel/model/model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bluepyemodel/model/model.py b/bluepyemodel/model/model.py index b7b4c42a..8cf31fbf 100644 --- a/bluepyemodel/model/model.py +++ b/bluepyemodel/model/model.py @@ -262,7 +262,8 @@ def define_morphology( morph_modifiers_hoc[i] = getattr(modifiers, morph_modifier[1] + "_hoc") except AttributeError: logger.warning( - "Cannot import %s or %s from %s", + "Cannot import %s or %s from %s. Please inform the hoc string in " + "morph_modifiers if you want to export the emodel to hoc.", morph_modifier[1], morph_modifier[1] + "_hoc", morph_modifier[0], From 7b7af2e937863aae1ebc03f907ea31fcca92814e Mon Sep 17 00:00:00 2001 From: ilkilic Date: Tue, 19 Mar 2024 17:46:21 +0100 Subject: [PATCH 6/6] improve warning message --- bluepyemodel/model/model.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bluepyemodel/model/model.py b/bluepyemodel/model/model.py index 8cf31fbf..6c0746c1 100644 --- a/bluepyemodel/model/model.py +++ b/bluepyemodel/model/model.py @@ -281,7 +281,9 @@ def define_morphology( morph_modifiers_hoc[i] = getattr(modifiers, morph_modifier + "_hoc") except AttributeError: logger.warning( - "Cannot import %s or %s from bluepyemodel.evaluation.modifiers", + "Cannot import %s or %s from bluepyemodel.evaluation.modifiers." + "Please inform the hoc string in morph_modifiers if you want " + "to export the emodel to hoc.", morph_modifier, morph_modifier + "_hoc", )