From c8eb7999d09117b023462cef25d60f8204f91181 Mon Sep 17 00:00:00 2001 From: Jan Vesely Date: Wed, 8 Jan 2025 16:54:02 -0500 Subject: [PATCH] treewide: Use asarray(dtype=float) instead of asfarray asfarray is not present in Numpy 2+ Signed-off-by: Jan Vesely --- psyneulink/core/components/component.py | 4 ++-- .../core/components/functions/function.py | 2 +- .../functions/stateful/memoryfunctions.py | 23 ++++++++++--------- psyneulink/core/llvm/builder_context.py | 4 ++-- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/psyneulink/core/components/component.py b/psyneulink/core/components/component.py index 23d1cfbf8a..d35a0b12e5 100644 --- a/psyneulink/core/components/component.py +++ b/psyneulink/core/components/component.py @@ -1425,7 +1425,7 @@ def _get_state_initializer(self, context): def _convert(p): x = p.get(context) if p.name == 'matrix': # Flatten matrix - val = tuple(np.asfarray(x).flatten()) + val = tuple(np.asarray(x, dtype=float).ravel()) elif isinstance(x, np.random.RandomState): state = x.get_state(legacy=False) @@ -1626,7 +1626,7 @@ def _get_values(p): elif p.name == 'num_trials_per_estimate': # Should always be int return 0 if param is None else int(param) elif p.name == 'matrix': # Flatten matrix - return tuple(np.asfarray(param).flatten()) + return tuple(np.asarray(param, dtype=float).ravel()) return _convert(param) return tuple(map(_get_values, self._get_compilation_params())) diff --git a/psyneulink/core/components/functions/function.py b/psyneulink/core/components/functions/function.py index d2f832b74f..5feba2ace2 100644 --- a/psyneulink/core/components/functions/function.py +++ b/psyneulink/core/components/functions/function.py @@ -835,7 +835,7 @@ def convert_output_type(self, value, output_type=None): # Note: if 2D or 1D array has more than two items, generate exception elif output_type is FunctionOutputType.NP_0D_ARRAY: if object_has_single_value(value): - value = np.asfarray(value) + value = np.asarray(value, dtype=float) else: raise FunctionError(f"Can't convert value ({value}) with more than a single number to a raw number.") diff --git a/psyneulink/core/components/functions/stateful/memoryfunctions.py b/psyneulink/core/components/functions/stateful/memoryfunctions.py index 7466f968ab..13481db8da 100644 --- a/psyneulink/core/components/functions/stateful/memoryfunctions.py +++ b/psyneulink/core/components/functions/stateful/memoryfunctions.py @@ -1289,10 +1289,10 @@ def _parse_selection_function_variable(self, variable, context=None, distance_re distance_result = self.distance_function(self._parse_distance_function_variable(variable), context=context) # TEST PRINT: # print(distance_result, self.distance_function.defaults.value) - return np.asfarray([ + return np.asarray([ distance_result if i == 0 else np.zeros_like(distance_result) for i in range(self.defaults.max_entries) - ]) + ], dtype=float) def _validate(self, context=None): """Validate distance_function, selection_function and memory store""" @@ -1328,10 +1328,10 @@ def _validate(self, context=None): # Default to full memory selection_function = self.selection_function - test_var = np.asfarray([ + test_var = np.asarray([ distance_result if i == 0 else np.zeros_like(distance_result) for i in range(self._get_current_parameter_value('max_entries', context)) - ]) + ], dtype=float) try: result = np.asarray(selection_function(test_var, context=context)) except Exception as e: @@ -2313,10 +2313,10 @@ def _parse_selection_function_variable(self, variable, context=None): distance_result = self.distance_function.parameters.value._get(context) # TEST PRINT: # print(distance_result, self.distance_function.defaults.value) - return np.asfarray([ + return np.asarray([ distance_result if i == 0 else np.zeros_like(distance_result) for i in range(self.defaults.max_entries) - ]) + ], dtype=float) def _get_state_ids(self): return super()._get_state_ids() + ["ring_memory"] @@ -2553,9 +2553,10 @@ def _validate(self, context=None): # Default to full memory selection_function = self.selection_function - test_var = np.asfarray([distance_result if i==0 - else np.zeros_like(distance_result) - for i in range(self._get_current_parameter_value('max_entries', context))]) + test_var = np.asarray([distance_result if i==0 else np.zeros_like(distance_result) + for i in range(self._get_current_parameter_value('max_entries', context))], + dtype=float) + if isinstance(selection_function, type): selection_function = selection_function(default_variable=test_var, context=context) fct_string = 'Function type' @@ -2709,12 +2710,12 @@ def _function(self, # Store variable to dict: rate = self._get_current_parameter_value(RATE, context) if rate is not None: - key = np.asfarray(key) * np.asfarray(rate) + key = np.asarray(key, dtype=float) * np.asarray(rate, dtype=float) assert len(key) == len(variable[KEYS]), "{} vs. {}".format(key, variable[KEYS]) if noise is not None: # TODO: does val need noise? - key = np.asfarray(key) + np.asfarray(noise)[KEYS] + key = np.asarray(key, dtype=float) + np.asarray(noise, dtype=float)[KEYS] assert len(key) == len(variable[KEYS]), "{} vs. {}".format(key, variable[KEYS]) if storage_prob == 1.0 or (storage_prob > 0.0 and storage_prob > random_state.uniform()): diff --git a/psyneulink/core/llvm/builder_context.py b/psyneulink/core/llvm/builder_context.py index 7fcd4224cd..df06140744 100644 --- a/psyneulink/core/llvm/builder_context.py +++ b/psyneulink/core/llvm/builder_context.py @@ -485,7 +485,7 @@ def _param_struct(p): if isinstance(val, ContentAddressableList): return ir.LiteralStructType(self.get_param_struct_type(x) for x in val) elif p.name == 'matrix': # Flatten matrix - val = np.asfarray(val).flatten() + val = np.asarray(val, dtype=float).ravel() elif p.name == 'num_trials_per_estimate': # Should always be int val = np.int32(0) if val is None else np.int32(val) elif np.ndim(val) == 0 and component._is_param_modulated(p): @@ -508,7 +508,7 @@ def _state_struct(p): if isinstance(val, ContentAddressableList): return ir.LiteralStructType(self.get_state_struct_type(x) for x in val) if p.name == 'matrix': # Flatten matrix - val = np.asfarray(val).flatten() + val = np.asarray(val, dtype=float).ravel() struct = self.convert_python_struct_to_llvm_ir(val) return ir.ArrayType(struct, p.history_min_length + 1)