From 8926b285f7eda4ca4e4b23b0dbaf89b2aa956154 Mon Sep 17 00:00:00 2001 From: Jan Vesely Date: Mon, 21 Oct 2024 13:12:00 -0400 Subject: [PATCH 1/3] test/IntegratorFunctions: Use issubclass to check Function class Fixes: ac83b2afb7835f8a3b6993a5fb7f2d0410fdb45b ("tests/IntegratorFunctions: Cleanup") Signed-off-by: Jan Vesely --- tests/functions/test_integrator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functions/test_integrator.py b/tests/functions/test_integrator.py index 3eb57d17f7..7c853f1dcd 100644 --- a/tests/functions/test_integrator.py +++ b/tests/functions/test_integrator.py @@ -207,7 +207,7 @@ def test_execute(func, func_mode, variable, noise, params, benchmark): if issubclass(func_class, (pnl.DriftDiffusionIntegrator, pnl.DriftOnASphereIntegrator)): pytest.skip("{} doesn't support functional noise".format(func_class.componentName)) - if 'DriftOnASphereIntegrator' in func[0].componentName: + if issubclass(func_class, pnl.DriftOnASphereIntegrator): params = {**params, 'dimension': len(variable) + 1} elif issubclass(func_class, pnl.AccumulatorIntegrator): From 37a4a97d9d2370bbb3356f9e1b28e5d2c77cd0e2 Mon Sep 17 00:00:00 2001 From: Jan Vesely Date: Mon, 21 Oct 2024 13:13:14 -0400 Subject: [PATCH 2/3] treewide: Allow Mapping type for 'params' in Component construction Signed-off-by: Jan Vesely --- psyneulink/_typing.py | 1 + .../nonstateful/transferfunctions.py | 4 ++-- .../functions/stateful/integratorfunctions.py | 24 +++++++++---------- .../functions/stateful/memoryfunctions.py | 9 +++---- .../functions/stateful/statefulfunction.py | 4 ++-- .../modulatory/controlprojection.py | 4 ++-- .../modulatory/gatingprojection.py | 4 ++-- .../modulatory/learningprojection.py | 6 ++--- psyneulink/core/compositions/composition.py | 4 ++-- .../compositions/autodiffcomposition.py | 6 ++--- .../library/compositions/compositionrunner.py | 14 +++++------ 11 files changed, 39 insertions(+), 41 deletions(-) diff --git a/psyneulink/_typing.py b/psyneulink/_typing.py index adbda77204..3a6fc21131 100644 --- a/psyneulink/_typing.py +++ b/psyneulink/_typing.py @@ -16,4 +16,5 @@ Dict as Dict, Iterable as Iterable, Set as Set, + Mapping as Mapping, ) diff --git a/psyneulink/core/components/functions/nonstateful/transferfunctions.py b/psyneulink/core/components/functions/nonstateful/transferfunctions.py index 1eac339052..5018ef1bcc 100644 --- a/psyneulink/core/components/functions/nonstateful/transferfunctions.py +++ b/psyneulink/core/components/functions/nonstateful/transferfunctions.py @@ -76,7 +76,7 @@ torch = None from beartype import beartype -from psyneulink._typing import Optional, Union, Callable +from psyneulink._typing import Callable, Mapping, Optional, Union from psyneulink.core import llvm as pnlvm from psyneulink.core.components.component import parameter_keywords @@ -3205,7 +3205,7 @@ def __init__(self, adapt_entropy_weighting: Optional[ValidParamSpecType] = None, output=None, per_item=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None): diff --git a/psyneulink/core/components/functions/stateful/integratorfunctions.py b/psyneulink/core/components/functions/stateful/integratorfunctions.py index 306fdaab01..eab72c5cdf 100644 --- a/psyneulink/core/components/functions/stateful/integratorfunctions.py +++ b/psyneulink/core/components/functions/stateful/integratorfunctions.py @@ -31,7 +31,7 @@ import numpy as np from beartype import beartype -from psyneulink._typing import Optional, Union, Callable +from psyneulink._typing import Callable, Mapping, Optional, Union from psyneulink.core import llvm as pnlvm from psyneulink.core.components.component import DefaultsFlexibility @@ -230,7 +230,7 @@ def __init__(self, rate=None, noise=None, initializer=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None, context=None, @@ -559,7 +559,7 @@ def __init__(self, increment=None, noise=None, initializer=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None): @@ -836,7 +836,7 @@ def __init__(self, noise=None, offset=None, initializer=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None): super().__init__( @@ -1072,7 +1072,7 @@ def __init__(self, noise=None, offset=None, initializer=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None): @@ -1592,7 +1592,7 @@ def __init__(self, long_term_rate=None, operation=None, offset=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None): @@ -2028,7 +2028,7 @@ def __init__(self, min_val: Optional[ValidParamSpecType] = None, noise=None, initializer=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None, # **kwargs @@ -2451,7 +2451,7 @@ def __init__( threshold=None, time_step_size=None, seed=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None, **kwargs @@ -3010,7 +3010,7 @@ def __init__(self, initializer=None, angle_function=None, seed=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None, **kwargs): @@ -3466,7 +3466,7 @@ def __init__( non_decision_time=None, time_step_size=None, starting_value=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, seed=None, owner=None, prefs: Optional[ValidPrefSet] = None, @@ -3761,7 +3761,7 @@ def __init__(self, offset=None, time_step_size=None, initializer=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None, **kwargs): @@ -4466,7 +4466,7 @@ def __init__(self, mode=None, uncorrelated_activity=None, integration_method=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None, **kwargs): diff --git a/psyneulink/core/components/functions/stateful/memoryfunctions.py b/psyneulink/core/components/functions/stateful/memoryfunctions.py index 38a0730752..47edfa9219 100644 --- a/psyneulink/core/components/functions/stateful/memoryfunctions.py +++ b/psyneulink/core/components/functions/stateful/memoryfunctions.py @@ -29,14 +29,11 @@ import warnings from collections import deque -from psyneulink._typing import Callable, List, Literal +from psyneulink._typing import Callable, List, Literal, Mapping, Optional, Union import numpy as np from beartype import beartype -from typing import Optional, Union -# from psyneulink._typing import - from psyneulink.core import llvm as pnlvm from psyneulink.core.components.functions.function import ( DEFAULT_SEED, FunctionError, _random_state_getter, _seed_setter, EPSILON, _noise_setter @@ -253,8 +250,8 @@ def __init__(self, history:Optional[int]=None, # history: Optional[int] = None, initializer=None, - params: Optional[dict] = None, - # params: Optional[dict] = None, + params: Optional[Mapping] = None, + # params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None ): diff --git a/psyneulink/core/components/functions/stateful/statefulfunction.py b/psyneulink/core/components/functions/stateful/statefulfunction.py index 3e25d51284..2aeed80c09 100644 --- a/psyneulink/core/components/functions/stateful/statefulfunction.py +++ b/psyneulink/core/components/functions/stateful/statefulfunction.py @@ -25,7 +25,7 @@ import numpy as np from beartype import beartype -from psyneulink._typing import Optional +from psyneulink._typing import Mapping, Optional from psyneulink.core import llvm as pnlvm from psyneulink.core.components.component import DefaultsFlexibility, _has_initializers_setter, ComponentsMeta @@ -235,7 +235,7 @@ def __init__(self, rate=None, noise=None, initializer=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, owner=None, prefs: Optional[ValidPrefSet] = None, context=None, diff --git a/psyneulink/core/components/projections/modulatory/controlprojection.py b/psyneulink/core/components/projections/modulatory/controlprojection.py index 71f6ff36fe..6c20811ad5 100644 --- a/psyneulink/core/components/projections/modulatory/controlprojection.py +++ b/psyneulink/core/components/projections/modulatory/controlprojection.py @@ -111,7 +111,7 @@ from beartype import beartype -from psyneulink._typing import Optional +from psyneulink._typing import Mapping, Optional from psyneulink.core.components.component import parameter_keywords from psyneulink.core.components.functions.nonstateful.transferfunctions import Linear @@ -243,7 +243,7 @@ def __init__(self, weight=None, exponent=None, function=None, - control_signal_params:Optional[dict]=None, + control_signal_params:Optional[Mapping]=None, params=None, name=None, prefs: Optional[ValidPrefSet] = None, diff --git a/psyneulink/core/components/projections/modulatory/gatingprojection.py b/psyneulink/core/components/projections/modulatory/gatingprojection.py index 746f861bee..37ca2c4959 100644 --- a/psyneulink/core/components/projections/modulatory/gatingprojection.py +++ b/psyneulink/core/components/projections/modulatory/gatingprojection.py @@ -101,7 +101,7 @@ """ from beartype import beartype -from psyneulink._typing import Optional +from psyneulink._typing import Optional, Mapping from psyneulink.core.components.component import parameter_keywords from psyneulink.core.components.functions.function import FunctionOutputType @@ -244,7 +244,7 @@ def __init__(self, function=None, weight=None, exponent=None, - gating_signal_params:Optional[dict]=None, + gating_signal_params:Optional[Mapping]=None, params=None, name=None, prefs: Optional[ValidPrefSet] = None, diff --git a/psyneulink/core/components/projections/modulatory/learningprojection.py b/psyneulink/core/components/projections/modulatory/learningprojection.py index 1d7f38d38f..8381376f12 100644 --- a/psyneulink/core/components/projections/modulatory/learningprojection.py +++ b/psyneulink/core/components/projections/modulatory/learningprojection.py @@ -204,7 +204,7 @@ import numpy as np from beartype import beartype -from psyneulink._typing import Optional, Union, Callable, Literal +from psyneulink._typing import Callable, Literal, Mapping, Optional, Union from psyneulink.core.components.component import parameter_keywords from psyneulink.core.components.functions.nonstateful.combinationfunctions import LinearCombination @@ -481,12 +481,12 @@ def __init__(self, error_function: Optional[Callable] = None, learning_function: Optional[Callable] = None, # FIX: 10/3/17 - TEST IF THIS OK AND REINSTATE IF SO - # learning_signal_params:Optional[dict]=None, + # learning_signal_params:Optional[Mapping]=None, # learning_rate: Optional[ValidParamSpecType] = None, learning_enabled: Optional[Union[bool, Literal['online', 'after']]] = None, weight=None, exponent=None, - params: Optional[dict] = None, + params: Optional[Mapping] = None, name=None, prefs: Optional[ValidPrefSet] = None, **kwargs diff --git a/psyneulink/core/compositions/composition.py b/psyneulink/core/compositions/composition.py index 455178747d..c10706330d 100644 --- a/psyneulink/core/compositions/composition.py +++ b/psyneulink/core/compositions/composition.py @@ -2897,7 +2897,7 @@ def input_function(env, result): from PIL import Image from beartype import beartype -from psyneulink._typing import Optional, Union, Literal, Type, Callable, List, Set +from psyneulink._typing import Callable, Literal, List, Mapping, Optional, Set, Type, Union from psyneulink.core import llvm as pnlvm from psyneulink.core.components.component import Component, ComponentError, ComponentsMeta @@ -11514,7 +11514,7 @@ def run( def learn( self, inputs: dict, - targets: Optional[dict] = None, + targets: Optional[Mapping] = None, num_trials: Optional[int] = None, epochs: int = 1, learning_rate: Optional[Union[int,float]]=None, diff --git a/psyneulink/library/compositions/autodiffcomposition.py b/psyneulink/library/compositions/autodiffcomposition.py index c851c32afd..27f2e1ac1b 100644 --- a/psyneulink/library/compositions/autodiffcomposition.py +++ b/psyneulink/library/compositions/autodiffcomposition.py @@ -330,7 +330,6 @@ import collections from packaging import version from pathlib import Path, PosixPath -from typing import Optional try: import torch @@ -343,6 +342,7 @@ from psyneulink.library.compositions.pytorchwrappers import PytorchCompositionWrapper from psyneulink.library.compositions.pytorchshowgraph import PytorchShowGraph +from psyneulink._typing import Mapping, Optional from psyneulink.core.components.mechanisms.processing.processingmechanism import ProcessingMechanism from psyneulink.core.components.mechanisms.processing.compositioninterfacemechanism import CompositionInterfaceMechanism from psyneulink.core.components.mechanisms.modulatory.modulatorymechanism import ModulatoryMechanism_Base @@ -1456,8 +1456,8 @@ def execute(self, runtime_params=None, execution_mode:pnlvm.ExecutionMode = pnlvm.ExecutionMode.PyTorch, skip_initialization=False, - synch_with_pnl_options:Optional[dict]=None, - retain_in_pnl_options:Optional[dict]=None, + synch_with_pnl_options:Optional[Mapping]=None, + retain_in_pnl_options:Optional[Mapping]=None, report_output:ReportOutput=ReportOutput.OFF, report_params:ReportOutput=ReportParams.OFF, report_progress:ReportProgress=ReportProgress.OFF, diff --git a/psyneulink/library/compositions/compositionrunner.py b/psyneulink/library/compositions/compositionrunner.py index 6888977be4..4d45ccc6d0 100644 --- a/psyneulink/library/compositions/compositionrunner.py +++ b/psyneulink/library/compositions/compositionrunner.py @@ -9,9 +9,9 @@ # ********************************************* AutodiffComposition ************************************************* import numpy as np -from typing import Optional from types import GeneratorType +from psyneulink._typing import Mapping, Optional from psyneulink.core.llvm import ExecutionMode from psyneulink.core.compositions.composition import Composition from psyneulink.core.compositions.report import Report, ReportProgress, ReportDevices, LEARN_REPORT, PROGRESS_REPORT @@ -55,8 +55,8 @@ def _batch_inputs(self, minibatch_size: int = 1, optimizations_per_minibatch: int = 1, randomize: bool = True, - synch_with_pnl_options:Optional[dict] = None, - retain_in_pnl_options:Optional[dict] = None, + synch_with_pnl_options:Optional[Mapping] = None, + retain_in_pnl_options:Optional[Mapping] = None, call_before_minibatch=None, call_after_minibatch=None, early_stopper=None, @@ -161,8 +161,8 @@ def _batch_function_inputs(self, num_trials: int, batch_size: int = 1, optimizations_per_minibatch: int = 1, - synch_with_pnl_options:Optional[dict] = None, - retain_in_pnl_options:Optional[dict] = None, + synch_with_pnl_options:Optional[Mapping] = None, + retain_in_pnl_options:Optional[Mapping] = None, call_before_minibatch=None, call_after_minibatch=None, early_stopper=None, @@ -223,8 +223,8 @@ def run_learning(self, patience: int = None, min_delta: int = 0, randomize_minibatches: bool = True, - synch_with_pnl_options:Optional[dict] = None, - retain_in_pnl_options:Optional[dict] = None, + synch_with_pnl_options:Optional[Mapping] = None, + retain_in_pnl_options:Optional[Mapping] = None, call_before_minibatch = None, call_after_minibatch = None, context=None, From 4cfd3d837ff1118060815eded4a3b2fe972815b1 Mon Sep 17 00:00:00 2001 From: Jan Vesely Date: Mon, 21 Oct 2024 13:16:12 -0400 Subject: [PATCH 3/3] tests: Convert dict test arguments to an immutable MappingProxyType Signed-off-by: Jan Vesely --- conftest.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/conftest.py b/conftest.py index e486c1cbe2..d473349c7e 100644 --- a/conftest.py +++ b/conftest.py @@ -6,6 +6,7 @@ import pytest import re import sys +import types import graph_scheduler as gs import psyneulink @@ -46,6 +47,13 @@ def pytest_runtest_setup(item): # Check that all 'cuda' tests are also marked 'llvm' assert 'llvm' in item.keywords or 'cuda' not in item.keywords + # It the item is a parametrized function. It has a 'callspec' attribute. + # Convert any dict arguments to an unmutable MappingProxyType. + if hasattr(item, 'callspec'): + for k, v in item.callspec.params.items(): + if isinstance(v, dict): + item.callspec.params[k] = types.MappingProxyType(v) + for m in marks_default_skip: if m in item.keywords and not item.config.getvalue(m): pytest.skip('{0} tests not requested'.format(m))