From af8b25f75c11988b67ce3e4de185b7a268a3c3f6 Mon Sep 17 00:00:00 2001 From: Kartik Pradeepan Date: Thu, 10 Oct 2024 13:48:23 -0400 Subject: [PATCH 1/5] Add load_region_layer_map_from_json --- .../brain_transformation/__init__.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/brainscore_vision/model_helpers/brain_transformation/__init__.py b/brainscore_vision/model_helpers/brain_transformation/__init__.py index a1a3c1dc7..60d7bb461 100644 --- a/brainscore_vision/model_helpers/brain_transformation/__init__.py +++ b/brainscore_vision/model_helpers/brain_transformation/__init__.py @@ -1,3 +1,6 @@ +import json +import os +from brainscore_core.plugin_management import import_plugin from brainscore_vision import load_benchmark from brainscore_vision.model_helpers.brain_transformation.temporal import TemporalAligned from brainscore_vision.model_interface import BrainModel @@ -30,7 +33,13 @@ def __init__(self, identifier, self.activations_model._extractor.set_visual_degrees(visual_degrees) # for microsaccades self._visual_degrees = visual_degrees # region-layer mapping + + # Attempt to load region_layer_map from JSON, if available + region_layer_map = self.load_region_layer_map_from_json(identifier) if region_layer_map is None else region_layer_map + + # Legacy Region Layer Mapping if region_layer_map is None: + print("Need to manually compute mappings.") layer_selection = LayerSelection(model_identifier=identifier, activations_model=activations_model, layers=layers, visual_degrees=visual_degrees) @@ -52,6 +61,20 @@ def __init__(self, identifier, }) self.do_behavior = False + def load_region_layer_map_from_json(self, identifier): + ''' + Attempts to load the region_layer_map from a JSON file in the model's directory + If file exists, load JSON. Otherwise, return None and proceed with legacy layer mapping + ''' + importer = import_plugin.ImportPlugin(library_root='brainscore_vision', plugin_type='models', identifier=identifier) + model_dir = importer.locate_plugin() + region_layer_map_path = os.path.join(model_dir, f'region_layer_map/{identifier}.json') + if os.path.exists(region_layer_map_path): + with open(region_layer_map_path, 'r') as region_layer_map_file: + self.region_layer_map = json.load(region_layer_map_file) + else: + self.region_layer_map + def visual_degrees(self) -> int: return self._visual_degrees From 938b460714cab196658c339c6b66aadd7615747e Mon Sep 17 00:00:00 2001 From: Kartik Pradeepan Date: Thu, 10 Oct 2024 15:42:01 -0400 Subject: [PATCH 2/5] Cleaned up load_region_layer_map_json function and added error handling. --- .../brain_transformation/__init__.py | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/brainscore_vision/model_helpers/brain_transformation/__init__.py b/brainscore_vision/model_helpers/brain_transformation/__init__.py index 60d7bb461..245f3f597 100644 --- a/brainscore_vision/model_helpers/brain_transformation/__init__.py +++ b/brainscore_vision/model_helpers/brain_transformation/__init__.py @@ -35,16 +35,16 @@ def __init__(self, identifier, # region-layer mapping # Attempt to load region_layer_map from JSON, if available - region_layer_map = self.load_region_layer_map_from_json(identifier) if region_layer_map is None else region_layer_map + region_layer_map = self.load_region_layer_map_json(identifier) if region_layer_map is None else region_layer_map - # Legacy Region Layer Mapping + # If region_layer_map is unavailable if region_layer_map is None: - print("Need to manually compute mappings.") layer_selection = LayerSelection(model_identifier=identifier, activations_model=activations_model, layers=layers, visual_degrees=visual_degrees) region_layer_map = RegionLayerMap(layer_selection=layer_selection, region_benchmarks=STANDARD_REGION_BENCHMARKS) + # neural layer_model = LayerMappedModel(identifier=identifier, activations_model=activations_model, region_layer_map=region_layer_map) @@ -61,19 +61,25 @@ def __init__(self, identifier, }) self.do_behavior = False - def load_region_layer_map_from_json(self, identifier): + def load_region_layer_map_json(self, identifier): ''' Attempts to load the region_layer_map from a JSON file in the model's directory If file exists, load JSON. Otherwise, return None and proceed with legacy layer mapping ''' - importer = import_plugin.ImportPlugin(library_root='brainscore_vision', plugin_type='models', identifier=identifier) - model_dir = importer.locate_plugin() - region_layer_map_path = os.path.join(model_dir, f'region_layer_map/{identifier}.json') - if os.path.exists(region_layer_map_path): - with open(region_layer_map_path, 'r') as region_layer_map_file: - self.region_layer_map = json.load(region_layer_map_file) - else: - self.region_layer_map + try: + importer = import_plugin.ImportPlugin(library_root='brainscore_vision', plugin_type='models', identifier=identifier) + model_dir = importer.locate_plugin() + region_layer_map_path = os.path.join(os.getcwd(), f'vision/models/{model_dir}', f'region_layer_map/{identifier}.json') + + if os.path.exists(region_layer_map_path): + with open(region_layer_map_path, 'r') as region_layer_map_file: + return json.load(region_layer_map_file) + else: + print(f"No region_layer_map file found for {identifier}, proceeding with default layer mapping") + return None + except Exception as e: + print(f"Error importing model to search for region_layer_map: {e}") + return None def visual_degrees(self) -> int: return self._visual_degrees From bc24c90c2c6e287d7a8913ba5eea62919104a468 Mon Sep 17 00:00:00 2001 From: Kartik Pradeepan Date: Sat, 12 Oct 2024 09:39:09 -0400 Subject: [PATCH 3/5] Replaced print with logging --- .../model_helpers/brain_transformation/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/brainscore_vision/model_helpers/brain_transformation/__init__.py b/brainscore_vision/model_helpers/brain_transformation/__init__.py index 245f3f597..88547fcfb 100644 --- a/brainscore_vision/model_helpers/brain_transformation/__init__.py +++ b/brainscore_vision/model_helpers/brain_transformation/__init__.py @@ -1,5 +1,7 @@ import json import os +import logging +from brainscore_vision.utils import fullname from brainscore_core.plugin_management import import_plugin from brainscore_vision import load_benchmark from brainscore_vision.model_helpers.brain_transformation.temporal import TemporalAligned @@ -25,6 +27,7 @@ class ModelCommitment(BrainModel): def __init__(self, identifier, activations_model, layers, behavioral_readout_layer=None, region_layer_map=None, visual_degrees=8): + self._logger = logging.getLogger(fullname(self)) self.layers = layers self.activations_model = activations_model # We set the visual degrees of the ActivationsExtractorHelper here to avoid changing its signature. @@ -73,12 +76,13 @@ def load_region_layer_map_json(self, identifier): if os.path.exists(region_layer_map_path): with open(region_layer_map_path, 'r') as region_layer_map_file: + self._logger.info(f"Successfully loaded region_layer_map for {identifier}") return json.load(region_layer_map_file) else: - print(f"No region_layer_map file found for {identifier}, proceeding with default layer mapping") + self._logger.info(f"No region_layer_map file found for {identifier}, proceeding with default layer mapping") return None except Exception as e: - print(f"Error importing model to search for region_layer_map: {e}") + self._logger.error(f"Error importing model to search for region_layer_map: {e}") return None def visual_degrees(self) -> int: From 4acf3147ca3f41a50b6a75e9e086d17b85a4f9c6 Mon Sep 17 00:00:00 2001 From: Kartik Pradeepan Date: Thu, 17 Oct 2024 08:51:17 -0400 Subject: [PATCH 4/5] Recommended fix: - logger - Path instead of OS - Avoid cwd --- .../model_helpers/brain_transformation/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/brainscore_vision/model_helpers/brain_transformation/__init__.py b/brainscore_vision/model_helpers/brain_transformation/__init__.py index 88547fcfb..92c7980d9 100644 --- a/brainscore_vision/model_helpers/brain_transformation/__init__.py +++ b/brainscore_vision/model_helpers/brain_transformation/__init__.py @@ -1,5 +1,5 @@ import json -import os +from pathlib import Path import logging from brainscore_vision.utils import fullname from brainscore_core.plugin_management import import_plugin @@ -38,6 +38,7 @@ def __init__(self, identifier, # region-layer mapping # Attempt to load region_layer_map from JSON, if available + print(f"LOADING REGION LAYER MAP") region_layer_map = self.load_region_layer_map_json(identifier) if region_layer_map is None else region_layer_map # If region_layer_map is unavailable @@ -72,10 +73,11 @@ def load_region_layer_map_json(self, identifier): try: importer = import_plugin.ImportPlugin(library_root='brainscore_vision', plugin_type='models', identifier=identifier) model_dir = importer.locate_plugin() - region_layer_map_path = os.path.join(os.getcwd(), f'vision/models/{model_dir}', f'region_layer_map/{identifier}.json') + project_root = Path(__file__).resolve().parent.parent + region_layer_map_path = project_root / 'vision' / 'models' / model_dir / 'region_layer_map' / f'{identifier}.json' - if os.path.exists(region_layer_map_path): - with open(region_layer_map_path, 'r') as region_layer_map_file: + if region_layer_map_path.exists(): + with region_layer_map_path.open('r') as region_layer_map_file: self._logger.info(f"Successfully loaded region_layer_map for {identifier}") return json.load(region_layer_map_file) else: From 579e8885e4b9675b5db8aa4bf8979e97b625db66 Mon Sep 17 00:00:00 2001 From: Kartik Pradeepan Date: Thu, 17 Oct 2024 08:53:35 -0400 Subject: [PATCH 5/5] Recommended fix: - logger - Path instead of OS - Avoid cwd --- .../model_helpers/brain_transformation/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/brainscore_vision/model_helpers/brain_transformation/__init__.py b/brainscore_vision/model_helpers/brain_transformation/__init__.py index 92c7980d9..553bf951d 100644 --- a/brainscore_vision/model_helpers/brain_transformation/__init__.py +++ b/brainscore_vision/model_helpers/brain_transformation/__init__.py @@ -38,7 +38,6 @@ def __init__(self, identifier, # region-layer mapping # Attempt to load region_layer_map from JSON, if available - print(f"LOADING REGION LAYER MAP") region_layer_map = self.load_region_layer_map_json(identifier) if region_layer_map is None else region_layer_map # If region_layer_map is unavailable @@ -75,7 +74,6 @@ def load_region_layer_map_json(self, identifier): model_dir = importer.locate_plugin() project_root = Path(__file__).resolve().parent.parent region_layer_map_path = project_root / 'vision' / 'models' / model_dir / 'region_layer_map' / f'{identifier}.json' - if region_layer_map_path.exists(): with region_layer_map_path.open('r') as region_layer_map_file: self._logger.info(f"Successfully loaded region_layer_map for {identifier}")