From f670b77004f675d67443996c52e54a6abba0c7d0 Mon Sep 17 00:00:00 2001 From: jdcpni Date: Sat, 25 Jan 2025 12:51:19 -0500 Subject: [PATCH] =?UTF-8?q?[skip=20ci]=20=E2=80=A2=20transferfunctions.py?= =?UTF-8?q?=20=20=20TransferFunction:=20=20add=20=5Fset=5Fbounds()=20to=20?= =?UTF-8?q?be=20compatible=20with=20any=20assignments=20to=20scale=20and/o?= =?UTF-8?q?r=20offset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nonstateful/transferfunctions.py | 52 ++++++++++++------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/psyneulink/core/components/functions/nonstateful/transferfunctions.py b/psyneulink/core/components/functions/nonstateful/transferfunctions.py index 45121e080c..2b942b5c93 100644 --- a/psyneulink/core/components/functions/nonstateful/transferfunctions.py +++ b/psyneulink/core/components/functions/nonstateful/transferfunctions.py @@ -144,6 +144,38 @@ class Parameters(Function_Base.Parameters): """ bounds = None + def __init__(self, **kwargs): + super().__init__(**kwargs) + if (hasattr(self, 'scale') or hasattr(self, 'offset')) and self.bounds: + self._set_bounds() + + def _set_bounds(self): + """Reassign bounds based on scale and offset applied to function's output for default upper and lower bounds + """ + if hasattr(self, "scale"): + scale = self.scale if self.scale is not None else 1.0 + else: + scale = 1.0 + if hasattr(self, "offset"): + offset = self.offset if self.offset is not None else 0.0 + else: + offset = 0.0 + + # Deal with lower bound = None: + lower_bound = -np.inf if self.bounds[0] == None else self.bounds[0] + output_for_fct_lower_bound = scale * lower_bound + offset + + # Deal with upper bound = None: + upper_bound = np.inf if self.bounds[1] == None else self.bounds[1] + output_for_fct_upper_bound = scale * upper_bound + offset + + # Need to do this since scale could be negative, reversing upper and lower bounds: + lower_bound = min(output_for_fct_lower_bound, output_for_fct_upper_bound) + upper_bound = max(output_for_fct_lower_bound, output_for_fct_upper_bound) + + self.parameters.bounds.default_value = (lower_bound, upper_bound) + self.bounds = (lower_bound, upper_bound) + def _gen_llvm_function_body(self, ctx, builder, params, state, arg_in, arg_out, *, tags:frozenset): assert isinstance(arg_in.type.pointee, pnlvm.ir.ArrayType) @@ -994,26 +1026,6 @@ def __init__(self, owner=owner, prefs=prefs, ) - if self.scale or self.offset: - self._set_bounds() - - def _set_bounds(self): - """Reassign bounds based on scale and offset applied to function's output for default upper and lower bounds - """ - # Deal with lower bound = None: - lower_bound = -np.inf if self.bounds[0] == None else self.bounds[0] - output_for_fct_lower_bound = self.scale * lower_bound + self.offset - - # Deal with upper bound = None: - upper_bound = np.inf if self.bounds[1] == None else self.bounds[1] - output_for_fct_upper_bound = self.scale * upper_bound + self.offset - - # Need to do this since scale could be negative, reversing upper and lower bounds: - lower_bound = min(output_for_fct_lower_bound, output_for_fct_upper_bound) - upper_bound = max(output_for_fct_lower_bound, output_for_fct_upper_bound) - - self.parameters.bounds.default_value = (lower_bound, upper_bound) - self.bounds = (lower_bound, upper_bound) def _function(self, variable=None,