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_function_refactor #732

Merged
merged 4 commits into from
Mar 19, 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
71 changes: 29 additions & 42 deletions psyneulink/components/functions/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -2138,62 +2138,49 @@ def function(self,
# FIX FOR EFFICIENCY: CHANGE THIS AND WEIGHTS TO TRY/EXCEPT // OR IS IT EVEN NECESSARY, GIVEN VALIDATION ABOVE??
# Apply exponents if they were specified
if exponents is not None:
try:
variable = self._update_variable(variable ** exponents)
# Avoid divide by zero warning:
# make sure there are no zeros for an element that is assigned a negative exponent
# Allow during initialization because 0s are common in default_variable argument
if context is not None and INITIALIZING in context: # cxt-test
try:
variable = self._update_variable(variable ** exponents)
except ZeroDivisionError:
except ZeroDivisionError:
# Allow during initialization because 0s are common in
# default_variable argument
if context is not None and INITIALIZING in context: # cxt-test
variable = self._update_variable(np.ones_like(variable))
else:
# if this fails with ZeroDivisionError it should not be caught outside of initialization
variable = self._update_variable(variable ** exponents)
else:
# if this fails with ZeroDivisionError it should not be caught
# outside of initialization
raise

# Apply weights if they were specified
if weights is not None:
variable = self._update_variable(variable * weights)

# CALCULATE RESULT USING RELEVANT COMBINATION OPERATION AND MODULATION

if operation is SUM:
if isinstance(scale, numbers.Number):
# Scalar scale and offset
if isinstance(offset, numbers.Number):
result = np.sum(variable, axis=0) * scale + offset
# Scalar scale and Hadamard offset
else:
result = np.sum(np.append([variable * scale], [offset], axis=0), axis=0)
else:
# Hadamard scale, scalar offset
if isinstance(offset, numbers.Number):
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)

combination = np.sum(variable, axis=0)
elif operation is PRODUCT:
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 * scale], [offset], axis=0), axis=0)
else:
# Hadamard scale, scalar offset
if isinstance(offset, numbers.Number):
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)
result = np.sum(np.append([hadamard_product], [offset], axis=0), axis=0)

combination = np.product(variable, axis=0)
else:
raise FunctionError("Unrecognized operator ({0}) for LinearCombination function".
format(operation.self.Operation.SUM))
if isinstance(scale, numbers.Number):
product = combination * scale
# Scalar scale and offset
if isinstance(offset, numbers.Number):
result = product + offset
# Scalar scale and Hadamard offset
else:
result = np.sum([product, offset], axis=0)
else:
hadamard_product = np.product([combination, scale], axis=0)
# Hadamard scale, scalar offset
if isinstance(offset, numbers.Number):
result = hadamard_product + offset
# Hadamard scale and offset
else:
result = np.sum([hadamard_product, offset], axis=0)

return result

@property
Expand Down
40 changes: 27 additions & 13 deletions tests/functions/test_combination.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def test_matrix(self):
SIZE=5
#This gives us the correct 2d array
test_var = np.random.rand(1, SIZE)
test_var2 = np.random.rand(2, SIZE)

RAND1_V = np.random.rand(1, SIZE)
RAND2_V = np.random.rand(1, SIZE)
Expand All @@ -88,24 +89,37 @@ def test_matrix(self):
(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",
(Function.LinearCombination, test_var2, {'scale':RAND1_S, 'offset':RAND2_S, 'operation':pnl.SUM}, np.sum(test_var2, axis=0) * RAND1_S + RAND2_S),
# TODO: enable vector scale/offset when the validation is fixed
# (Function.LinearCombination, test_var2, {'scale':RAND1_S, 'offset':RAND2_V, 'operation':pnl.SUM}, np.sum(test_var2, axis=0) * RAND1_S + RAND2_V),
# (Function.LinearCombination, test_var2, {'scale':RAND1_V, 'offset':RAND2_S, 'operation':pnl.SUM}, np.sum(test_var2, axis=0) * RAND1_V + RAND2_S),
# (Function.LinearCombination, test_var2, {'scale':RAND1_V, 'offset':RAND2_V, 'operation':pnl.SUM}, np.sum(test_var2, axis=0) * RAND1_V + RAND2_V),

(Function.LinearCombination, test_var2, {'scale':RAND1_S, 'offset':RAND2_S, 'operation':pnl.PRODUCT}, np.product(test_var2, axis=0) * RAND1_S + RAND2_S),
# (Function.LinearCombination, test_var2, {'scale':RAND1_S, 'offset':RAND2_V, 'operation':pnl.PRODUCT}, np.product(test_var2, axis=0) * RAND1_S + RAND2_V),
# (Function.LinearCombination, test_var2, {'scale':RAND1_V, 'offset':RAND2_S, 'operation':pnl.PRODUCT}, np.product(test_var2, axis=0) * RAND1_V + RAND2_S),
# (Function.LinearCombination, test_var2, {'scale':RAND1_V, 'offset':RAND2_V, 'operation':pnl.PRODUCT}, np.product(test_var2, axis=0) * RAND1_V + RAND2_V),
]

# pytest naming function produces ugly names
def _naming_function(config):
_, var, params, _ = config
inputs = var.shape[0]
op = params['operation']
vector_string = ""
if not np.isscalar(params['scale']):
vector_string += " SCALE"
if not np.isscalar(params['offset']):
vector_string += " OFFSET"
if vector_string != "":
vector_string = " VECTOR" + vector_string
return "COMBINE-{} {}{}".format(inputs, op, vector_string)


@pytest.mark.function
@pytest.mark.combination_function
@pytest.mark.parametrize("func, variable, params, expected", test_linear_combination_data, ids=linear_combination_names)
@pytest.mark.parametrize("func, variable, params, expected", test_linear_combination_data, ids=list(map(_naming_function, test_linear_combination_data)))
@pytest.mark.benchmark
def test_linear_combination_function(func, variable, params, expected, benchmark):
f = func(default_variable=variable, **params)
Expand Down