Skip to content

Commit

Permalink
treewide: Prefer ravel() instead of flatten()
Browse files Browse the repository at this point in the history
Unless flatten()'s copying semantics are required.

Signed-off-by: Jan Vesely <[email protected]>
  • Loading branch information
jvesely committed Jan 10, 2025
1 parent 4a5472c commit b772fc3
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2132,8 +2132,7 @@ def _function(self,

# Find the optimal value(s)
optimal_value_count = 1
value_sample_pairs = zip(all_values.flatten(),
[all_samples[:,i] for i in range(all_samples.shape[1])])
value_sample_pairs = zip(all_values.ravel(), [all_samples[:,i] for i in range(all_samples.shape[1])])
optimal_value, optimal_sample = next(value_sample_pairs)

# The algorithm below implements "Reservoir sampling"[0]. This
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3160,7 +3160,7 @@ def _function(self,
previous_value = self.parameters.previous_value._get(context)

try:
variable = variable.flatten()
variable = variable.ravel()
drift = variable if len(variable) == dimension - 1 else np.full(dimension - 1, variable)

except ValueError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ def __init__(

# If the include mask is 2D, make it 1D
if likelihood_include_mask.ndim == 2:
likelihood_include_mask = likelihood_include_mask.flatten()
likelihood_include_mask = likelihood_include_mask.ravel()

self.likelihood_include_mask = likelihood_include_mask

Expand Down
2 changes: 1 addition & 1 deletion psyneulink/core/globals/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,7 @@ def _deliver_value(self, value, context=None):
context=execution_id,
value=ndArray(
shape=list(value.shape),
data=list(value.flatten())
data=list(value.ravel())
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ def _validate_params(self, request_set, target_set=None, context=None):
for i, learning_signal in enumerate(learning_signals[:num_match_fields]):
learning_signal_shape = learning_signal.parameters.matrix._get(context).shape
if concatenate_queries:
memory_matrix_field_shape = np.array([np.concatenate(row, dtype=object).flatten()
memory_matrix_field_shape = np.array([np.concatenate(row, dtype=object).ravel()
for row in memory_matrix[:,0:num_keys]]).T.shape
else:
memory_matrix_field_shape = np.array(memory_matrix[:,key_indices[i]].tolist()).T.shape
Expand Down
19 changes: 11 additions & 8 deletions tests/composition/test_autodiffcomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,9 +1300,7 @@ def test_xor_nested_train_then_no_train(self, num_epochs, learning_rate,

# -----------------------------------------------------------------

xor_autodiff = AutodiffComposition(
learning_rate=learning_rate,
)
xor_autodiff = AutodiffComposition(learning_rate=learning_rate)

xor_autodiff.add_node(xor_in)
xor_autodiff.add_node(xor_hid)
Expand All @@ -1319,15 +1317,20 @@ def test_xor_nested_train_then_no_train(self, num_epochs, learning_rate,
parentComposition = pnl.Composition()
parentComposition.add_node(xor_autodiff)

input = {xor_autodiff: input_dict}
no_training_input = {xor_autodiff: no_training_input_dict}

learning_context = Context()
xor_autodiff.learn(inputs=input_dict, execution_mode=autodiff_mode, epochs=num_epochs, context=learning_context, patience=patience, min_delta=min_delta)
result1 = np.array(xor_autodiff.learning_results).flatten()
np.testing.assert_allclose(result1, np.array(xor_targets).flatten(), atol=0.1)
result2 = parentComposition.run(inputs=no_training_input, execution_mode=autodiff_mode, context=learning_context)
xor_autodiff.learn(inputs=input_dict,
execution_mode=autodiff_mode,
epochs=num_epochs,
context=learning_context,
patience=patience,
min_delta=min_delta)

result1 = np.array(xor_autodiff.learning_results).ravel()
np.testing.assert_allclose(result1, np.array(xor_targets).ravel(), atol=0.1)

result2 = parentComposition.run(inputs=no_training_input, execution_mode=autodiff_mode, context=learning_context)
np.testing.assert_allclose(result2, [[0]], atol=0.1)

@pytest.mark.parametrize(
Expand Down
4 changes: 2 additions & 2 deletions tests/composition/test_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2470,7 +2470,7 @@ def test_modulation_simple(self, cost, expected, exp_values, comp_mode):
ret = comp.run(inputs={mech: [2]}, num_trials=1, execution_mode=comp_mode)
np.testing.assert_allclose(ret, expected)
if comp_mode == pnl.ExecutionMode.Python:
np.testing.assert_allclose(comp.controller.function.saved_values.flatten(), exp_values)
np.testing.assert_allclose(comp.controller.function.saved_values.ravel(), exp_values)

@pytest.mark.benchmark
@pytest.mark.control
Expand Down Expand Up @@ -3360,7 +3360,7 @@ def comp_run(inputs, execution_mode):

np.testing.assert_array_equal(results, result)
if mode == pnl.ExecutionMode.Python:
np.testing.assert_array_equal(saved_values.flatten(), [0.75, 1.5, 2.25])
np.testing.assert_array_equal(saved_values.ravel(), [0.75, 1.5, 2.25])

def test_model_based_ocm_with_buffer(self):

Expand Down
2 changes: 1 addition & 1 deletion tests/composition/test_emcomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_structure(self,
params.update({'softmax_gain': softmax_gain})

em = EMComposition(**params)
assert np.hstack(np.array(em.memory, dtype=object).flatten()).size < 30
assert np.hstack(np.array(em.memory, dtype=object).ravel()).size < 30

# Validate basic structure
assert len(em.memory) == memory_capacity
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_greedy_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def action_fn(variable):
# FIXME: The results are 'close' for both Philox and MT,
# because they're dominated by costs
# FIX: Requires 1e-5 tolerance
np.testing.assert_allclose(np.asarray(ocm.function.saved_values).flatten(),
np.testing.assert_allclose(np.asarray(ocm.function.saved_values).ravel(),
[-2.66258741, -22027.9970321, -22028.17515945, -44053.59867802,
-22028.06045185, -44053.4048842, -44053.40736234, -66078.90687915],
rtol=1e-5, atol=1e-5)

0 comments on commit b772fc3

Please sign in to comment.