Skip to content

66 constraints do not seem to work when there is a calculator set #67

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

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
2 changes: 1 addition & 1 deletion src/easyscience/Objects/new_variable/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __init__(
@property
def value_no_call_back(self) -> numbers.Number:
"""
Get the currently hold value of self surpassing call back.
Get the currently hold value of self suppressing call back.

:return: Value of self without unit.
"""
Expand Down
14 changes: 7 additions & 7 deletions src/easyscience/fitting/Constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def _parse_operator(self, obj: V, *args, **kwargs) -> Number:
import easyscience.Objects.new_variable.parameter

if isinstance(obj, easyscience.Objects.new_variable.parameter.Parameter):
value = obj.value
value = obj.value_no_call_back
else:
value = obj.raw_value

Expand Down Expand Up @@ -260,7 +260,7 @@ def _parse_operator(self, obj: V, *args, **kwargs) -> Number:
import easyscience.Objects.new_variable.parameter

if isinstance(obj, easyscience.Objects.new_variable.parameter.Parameter):
value = obj.value
value = obj.value_no_call_back
else:
value = obj.raw_value

Expand Down Expand Up @@ -323,7 +323,7 @@ def _parse_operator(self, obj: V, *args, **kwargs) -> Number:
import easyscience.Objects.new_variable.parameter

if isinstance(obj, easyscience.Objects.new_variable.parameter.Parameter):
value = obj.value
value = obj.value_no_call_back
else:
value = obj.raw_value

Expand Down Expand Up @@ -420,7 +420,7 @@ def _parse_operator(self, independent_objs: List[V], *args, **kwargs) -> Number:
for idx, obj in enumerate(independent_objs):
## TODO clean when full move to new_variable
if isinstance(obj, easyscience.Objects.new_variable.parameter.Parameter):
self.aeval.symtable['p' + str(self.independent_obj_ids[idx])] = obj.value
self.aeval.symtable['p' + str(self.independent_obj_ids[idx])] = obj.value_no_call_back
else:
self.aeval.symtable['p' + str(self.independent_obj_ids[idx])] = obj.raw_value

Expand Down Expand Up @@ -488,15 +488,15 @@ def _parse_operator(self, obj: V, *args, **kwargs) -> Number:
for o in obj:
## TODO clean when full move to new_variable
if isinstance(o, easyscience.Objects.new_variable.parameter.Parameter):
value_str += f'{o.value},'
value_str += f'{o.value_no_call_back},'
else:
value_str += f'{o.raw_value},'

value_str = value_str[:-1]
else:
## TODO clean when full move to new_variable
if isinstance(o, easyscience.Objects.new_variable.parameter.Parameter):
value_str += f'{obj.value}'
if isinstance(obj, easyscience.Objects.new_variable.parameter.Parameter):
value_str += f'{obj.value_no_call_back}'
else:
value_str += f'{obj.raw_value}'

Expand Down
27 changes: 15 additions & 12 deletions tests/unit_tests/Fitting/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Tuple

import pytest
from unittest.mock import MagicMock

from easyscience.fitting.Constraints import NumericConstraint
from easyscience.fitting.Constraints import ObjConstraint
Expand All @@ -17,7 +18,9 @@

@pytest.fixture
def twoPars() -> Tuple[List[Parameter], List[int]]:
return [Parameter("a", 1), Parameter("b", 2)], [1, 2]
mock_callback = MagicMock()
mock_callback.fget = MagicMock(return_value=-10)
return [Parameter("a", 1, callback=mock_callback), Parameter("b", 2, callback=mock_callback)], [1, 2]


@pytest.fixture
Expand All @@ -35,12 +38,12 @@ def test_NumericConstraints_Equals(twoPars):
# Should skip
c = NumericConstraint(twoPars[0][0], "==", value)
c()
assert twoPars[0][0].value == twoPars[1][0]
assert twoPars[0][0].value_no_call_back == twoPars[1][0]

# Should update to new value
c = NumericConstraint(twoPars[0][1], "==", value)
c()
assert twoPars[0][1].value == value
assert twoPars[0][1].value_no_call_back == value


def test_NumericConstraints_Greater(twoPars):
Expand All @@ -49,12 +52,12 @@ def test_NumericConstraints_Greater(twoPars):
# Should update to new value
c = NumericConstraint(twoPars[0][0], ">", value)
c()
assert twoPars[0][0].value == value
assert twoPars[0][0].value_no_call_back == value

# Should skip
c = NumericConstraint(twoPars[0][1], ">", value)
c()
assert twoPars[0][1].value == twoPars[1][1]
assert twoPars[0][1].value_no_call_back == twoPars[1][1]


def test_NumericConstraints_Less(twoPars):
Expand All @@ -63,12 +66,12 @@ def test_NumericConstraints_Less(twoPars):
# Should skip
c = NumericConstraint(twoPars[0][0], "<", value)
c()
assert twoPars[0][0].value == twoPars[1][0]
assert twoPars[0][0].value_no_call_back == twoPars[1][0]

# Should update to new value
c = NumericConstraint(twoPars[0][1], "<", value)
c()
assert twoPars[0][1].value == value
assert twoPars[0][1].value_no_call_back == value


@pytest.mark.parametrize("multiplication_factor", [None, 1, 2, 3, 4.5])
Expand All @@ -80,15 +83,15 @@ def test_ObjConstraintMultiply(twoPars, multiplication_factor):
operator_str = f"{multiplication_factor}*"
c = ObjConstraint(twoPars[0][0], operator_str, twoPars[0][1])
c()
assert twoPars[0][0].value == multiplication_factor * twoPars[1][1]
assert twoPars[0][0].value_no_call_back == multiplication_factor * twoPars[1][1]


@pytest.mark.parametrize("division_factor", [1, 2, 3, 4.5])
def test_ObjConstraintDivide(twoPars, division_factor):
operator_str = f"{division_factor}/"
c = ObjConstraint(twoPars[0][0], operator_str, twoPars[0][1])
c()
assert twoPars[0][0].value == division_factor / twoPars[1][1]
assert twoPars[0][0].value_no_call_back == division_factor / twoPars[1][1]


def test_ObjConstraint_Multiple(threePars):
Expand All @@ -103,9 +106,9 @@ def test_ObjConstraint_Multiple(threePars):
p0.user_constraints["num_2"] = ObjConstraint(p2, "", p0)

p0.value = value
assert p0.value == value
assert p1.value == value
assert p2.value == value
assert p0.value_no_call_back == value
assert p1.value_no_call_back == value
assert p2.value_no_call_back == value


def test_ConstraintEnable_Disable(twoPars):
Expand Down
Loading