Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jvesely/linear_combination #724

Merged
merged 3 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions psyneulink/components/functions/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -2162,25 +2162,25 @@ def function(self,
else:
# Hadamard scale, scalar offset
if isinstance(offset, numbers.Number):
result = np.product([np.sum([variable], axis=0), scale], axis=0)
result = np.product([np.sum([variable], axis=0), scale], axis=0) + offset
# Hadamard scale and offset
else:
hadamard_product = np.product([np.sum([variable], axis=0), scale], axis=0)
result = np.sum(np.append([hadamard_product], [offset], axis=0), axis=0)

elif operation is PRODUCT:
product = np.product(variable, axis=0)
product = np.product([variable], axis=0)
if isinstance(scale, numbers.Number):
# Scalar scale and offset
if isinstance(offset, numbers.Number):
result = product * scale + offset
# Scalar scale and Hadamard offset
else:
result = np.sum(np.append([product], [offset], axis=0), axis=0) + offset
result = np.sum(np.append([product * scale], [offset], axis=0), axis=0)
else:
# Hadamard scale, scalar offset
if isinstance(offset, numbers.Number):
result = np.product(np.append([product], [scale], axis=0), axis=0) + offset
result = np.product([product, scale], axis=0) + offset
# Hadamard scale and offset
else:
hadamard_product = np.product(np.append([product], [scale], axis=0), axis=0)
Expand Down
57 changes: 57 additions & 0 deletions tests/functions/test_combination.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import psyneulink as pnl
import psyneulink.components.functions.function as Function
import psyneulink.globals.keywords as kw
import numpy as np
import pytest

class TestReduce:

@pytest.mark.function
@pytest.mark.combination_function
def test_single_array(self):
R_function = pnl.Reduce(operation=pnl.SUM)
R_mechanism = pnl.ProcessingMechanism(function=pnl.Reduce(operation=pnl.SUM),
Expand All @@ -19,6 +24,8 @@ def test_single_array(self):
assert np.allclose(R_mechanism.execute([1, 2, 3, 4, 5]), [15.0])
# assert np.allclose(R_mechanism.execute([[1, 2, 3, 4, 5]]), [15.0])

@pytest.mark.function
@pytest.mark.combination_function
def test_column_vector(self):
R_function = pnl.Reduce(operation=pnl.SUM)
R_mechanism = pnl.ProcessingMechanism(function=pnl.Reduce(operation=pnl.SUM),
Expand All @@ -32,6 +39,8 @@ def test_column_vector(self):
assert np.allclose(R_mechanism.execute([[1], [2], [3], [4], [5]]), [1, 2, 3, 4, 5])
# assert np.allclose(R_mechanism.execute([[1], [2], [3], [4], [5]]), [15.0])

@pytest.mark.function
@pytest.mark.combination_function
def test_matrix(self):
R_function = pnl.Reduce(operation=pnl.SUM)
R_mechanism = pnl.ProcessingMechanism(function=pnl.Reduce(operation=pnl.SUM),
Expand All @@ -55,3 +64,51 @@ def test_matrix(self):
# # print("mech = ", R_mechanism.execute([[[1, 2], [3, 4, 5], [6, 7, 8, 9]]]))
# # print("mech = ", R_mechanism.execute([[[1, 2], [3, 4, 5], [6, 7, 8, 9]]]))
#


SIZE=5
#This gives us the correct 2d array
test_var = np.random.rand(1, SIZE)

RAND1_V = np.random.rand(1, SIZE)
RAND2_V = np.random.rand(1, SIZE)
RAND3_V = np.random.rand(1, SIZE)

RAND1_S = np.random.rand()
RAND2_S = np.random.rand()
RAND3_S = np.random.rand()

test_linear_combination_data = [
(Function.LinearCombination, test_var, {'scale':RAND1_S, 'offset':RAND2_S, 'operation':pnl.SUM}, test_var * RAND1_S + RAND2_S),
(Function.LinearCombination, test_var, {'scale':RAND1_S, 'offset':RAND2_V, 'operation':pnl.SUM}, test_var * RAND1_S + RAND2_V),
(Function.LinearCombination, test_var, {'scale':RAND1_V, 'offset':RAND2_S, 'operation':pnl.SUM}, test_var * RAND1_V + RAND2_S),
(Function.LinearCombination, test_var, {'scale':RAND1_V, 'offset':RAND2_V, 'operation':pnl.SUM}, test_var * RAND1_V + RAND2_V),

(Function.LinearCombination, test_var, {'scale':RAND1_S, 'offset':RAND2_S, 'operation':pnl.PRODUCT}, test_var * RAND1_S + RAND2_S),
(Function.LinearCombination, test_var, {'scale':RAND1_S, 'offset':RAND2_V, 'operation':pnl.PRODUCT}, test_var * RAND1_S + RAND2_V),
(Function.LinearCombination, test_var, {'scale':RAND1_V, 'offset':RAND2_S, 'operation':pnl.PRODUCT}, test_var * RAND1_V + RAND2_S),
(Function.LinearCombination, test_var, {'scale':RAND1_V, 'offset':RAND2_V, 'operation':pnl.PRODUCT}, test_var * RAND1_V + RAND2_V),
]

# use list, naming function produces ugly names
linear_combination_names = [
"COMBINE-1 SUM",
"COMBINE-1 SUM VECTOR OFFSET",
"COMBINE-1 SUM VECTOR SCALE",
"COMBINE-1 SUM VECTOR OFFSET SCALE",

"COMBINE-1 PRODUCT",
"COMBINE-1 PRODUCT VECTOR OFFSET",
"COMBINE-1 PRODUCT VECTOR SCALE",
"COMBINE-1 PRODUCT VECTOR OFFSET SCALE",
]

@pytest.mark.function
@pytest.mark.combination_function
@pytest.mark.parametrize("func, variable, params, expected", test_linear_combination_data, ids=linear_combination_names)
@pytest.mark.benchmark
def test_linear_combination_function(func, variable, params, expected, benchmark):
f = func(default_variable=variable, **params)
benchmark.group = "TransferFunction " + func.componentName;
res = benchmark(f.function, variable)
assert np.allclose(res, expected)