Skip to content

Commit

Permalink
treewide: Use asarray(dtype=float) instead of asfarray
Browse files Browse the repository at this point in the history
asfarray is not present in Numpy 2+

Signed-off-by: Jan Vesely <[email protected]>
  • Loading branch information
jvesely committed Jan 9, 2025
1 parent c9fcf7a commit c8eb799
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
4 changes: 2 additions & 2 deletions psyneulink/core/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()))
Expand Down
2 changes: 1 addition & 1 deletion psyneulink/core/components/functions/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down
23 changes: 12 additions & 11 deletions psyneulink/core/components/functions/stateful/memoryfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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()):
Expand Down
4 changes: 2 additions & 2 deletions psyneulink/core/llvm/builder_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand Down

0 comments on commit c8eb799

Please sign in to comment.