Skip to content

Remove old parameter #94

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
merged 41 commits into from
Jan 20, 2025
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
deb6446
Update test to new_parameter
henrikjacobsenfys Jan 2, 2025
c459b25
Add check if parameter is a number
henrikjacobsenfys Jan 2, 2025
53b2e7b
Remove old Variable, move new_variable to variable
henrikjacobsenfys Jan 3, 2025
1e1b7af
fixed _get_linkable_attributes and get_fit_parameters
henrikjacobsenfys Jan 3, 2025
b5e59e3
Fx __init__
henrikjacobsenfys Jan 3, 2025
2b553f3
fix __setattr__
henrikjacobsenfys Jan 3, 2025
5c3dd60
Fixed get_parameters
henrikjacobsenfys Jan 3, 2025
99d52e0
remove unused imports, ObjectClasses is finished
henrikjacobsenfys Jan 3, 2025
9ee9ddd
Update Groups.py to new parameter
henrikjacobsenfys Jan 3, 2025
d4c50d6
fix type checking
henrikjacobsenfys Jan 3, 2025
9c297fb
Fix test referring to new_variable
henrikjacobsenfys Jan 3, 2025
f0fbecd
Added TODO on outcommented test
henrikjacobsenfys Jan 3, 2025
4281e1c
rename tests from new_variable to variable
henrikjacobsenfys Jan 3, 2025
ad79069
Remove old tests
henrikjacobsenfys Jan 13, 2025
309aa6c
remove old variable, make ruff happy
henrikjacobsenfys Jan 13, 2025
e693123
Remove old tests
henrikjacobsenfys Jan 13, 2025
e627690
Update test of polynomial
henrikjacobsenfys Jan 13, 2025
d8ddac4
Update imports in tests to make the appear in list of tests
henrikjacobsenfys Jan 13, 2025
c9791ae
Updated virtual
henrikjacobsenfys Jan 13, 2025
14b22a3
Updated bounds in parameter and added _global_object to DescriptorBas…
henrikjacobsenfys Jan 13, 2025
ce2f5a5
Updated error setter to handle None as input
henrikjacobsenfys Jan 15, 2025
37f4fd0
Update tests to handle units
henrikjacobsenfys Jan 15, 2025
f97fe4f
investigated tests of undo
henrikjacobsenfys Jan 15, 2025
2608594
Added undo/redo functionality to convert_unit
henrikjacobsenfys Jan 15, 2025
988f0bf
Added test of undo/redo for Parameter
henrikjacobsenfys Jan 15, 2025
d10f3f8
Added test of undo/redo for DescriptorBool
henrikjacobsenfys Jan 15, 2025
2296471
Added test of undo/redo for DescriptorStr
henrikjacobsenfys Jan 15, 2025
b6dcc20
Fix failing tests
henrikjacobsenfys Jan 15, 2025
a99c405
removed old TODOs
henrikjacobsenfys Jan 15, 2025
13565b1
More cleanup when move to new variable
henrikjacobsenfys Jan 15, 2025
d05d8ec
clean minimizer_base
henrikjacobsenfys Jan 17, 2025
2302e7a
clean minimizer_bumps
henrikjacobsenfys Jan 17, 2025
5c9b641
clean minimize_dfo
henrikjacobsenfys Jan 17, 2025
84390b8
clean minimizer_lmfit
henrikjacobsenfys Jan 17, 2025
1b83cd3
Updated todos
henrikjacobsenfys Jan 17, 2025
596be0b
update test to new parameter
henrikjacobsenfys Jan 17, 2025
2f9f1f7
update tests
henrikjacobsenfys Jan 17, 2025
4365760
make ruff happy
henrikjacobsenfys Jan 17, 2025
b9213ad
Reintroduced tests that I should not have deleted
henrikjacobsenfys Jan 17, 2025
f4bc0ed
Remove unused code
henrikjacobsenfys Jan 20, 2025
0f962de
added tests of error (sqrt of variance)
henrikjacobsenfys Jan 20, 2025
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
44 changes: 7 additions & 37 deletions src/easyscience/Constraints.py
Original file line number Diff line number Diff line change
@@ -194,13 +194,9 @@ def __init__(self, dependent_obj: V, operator: str, value: Number):
super(NumericConstraint, self).__init__(dependent_obj, operator=operator, value=value)

def _parse_operator(self, obj: V, *args, **kwargs) -> Number:
## TODO clean when full move to new_variable
import easyscience.Objects.new_variable.parameter
## TODO Probably needs to be updated when DescriptorArray is implemented

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

if isinstance(value, list):
value = np.array(value)
@@ -258,13 +254,7 @@ def __init__(self, dependent_obj: V, operator: str, value: str):
super(SelfConstraint, self).__init__(dependent_obj, operator=operator, value=value)

def _parse_operator(self, obj: V, *args, **kwargs) -> Number:
## TODO clean when full move to new_variable
import easyscience.Objects.new_variable.parameter

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

self.aeval.symtable['value1'] = value
self.aeval.symtable['value2'] = getattr(obj, self.value)
@@ -322,13 +312,7 @@ def __init__(self, dependent_obj: V, operator: str, independent_obj: V):
self.external = True

def _parse_operator(self, obj: V, *args, **kwargs) -> Number:
## TODO clean when full move to new_variable
import easyscience.Objects.new_variable.parameter

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

self.aeval.symtable['value1'] = value
try:
@@ -417,16 +401,11 @@ def __init__(
self.external = True

def _parse_operator(self, independent_objs: List[V], *args, **kwargs) -> Number:
import easyscience.Objects.new_variable.parameter

in_str = ''
value = None
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_no_call_back
else:
self.aeval.symtable['p' + str(self.independent_obj_ids[idx])] = obj.raw_value
self.aeval.symtable['p' + str(self.independent_obj_ids[idx])] = obj.value_no_call_back

in_str += ' p' + str(self.independent_obj_ids[idx])
if idx < len(self.operator):
@@ -485,25 +464,16 @@ def __init__(
self.external = True

def _parse_operator(self, obj: V, *args, **kwargs) -> Number:
import easyscience.Objects.new_variable.parameter

self.aeval.symtable[f'f{id(self.function)}'] = self.function
value_str = f'r_value = f{id(self.function)}('
if isinstance(obj, list):
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_no_call_back},'
else:
value_str += f'{o.raw_value},'
value_str += f'{o.value_no_call_back},'

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

value_str += ')'
try:
18 changes: 9 additions & 9 deletions src/easyscience/Objects/Groups.py
Original file line number Diff line number Diff line change
@@ -15,17 +15,17 @@
from typing import List
from typing import Optional
from typing import Tuple
from typing import TypeVar
from typing import Union

from easyscience.global_object.undo_redo import NotarizedDict
from easyscience.Objects.new_variable.descriptor_base import DescriptorBase
from easyscience.Objects.ObjectClasses import BasedBase
from easyscience.Objects.ObjectClasses import Descriptor
from easyscience.Objects.variable.descriptor_base import DescriptorBase

if TYPE_CHECKING:
from easyscience.Objects.Inferface import iF
from easyscience.Objects.ObjectClasses import B
from easyscience.Objects.Variable import V
V = TypeVar('V', bound=DescriptorBase)


class BaseCollection(BasedBase, MutableSequence):
@@ -69,7 +69,7 @@ def __init__(
_kwargs[key] = item
kwargs = _kwargs
for item in list(kwargs.values()) + _args:
if not issubclass(type(item), (Descriptor, DescriptorBase, BasedBase)):
if not issubclass(type(item), (DescriptorBase, BasedBase)):
raise AttributeError('A collection can only be formed from easyscience objects.')
args = _args
_kwargs = {}
@@ -102,12 +102,12 @@ def insert(self, index: int, value: Union[V, B]) -> None:
:param index: Index for EasyScience object to be inserted.
:type index: int
:param value: Object to be inserted.
:type value: Union[BasedBase, Descriptor]
:type value: Union[BasedBase, DescriptorBase]
:return: None
:rtype: None
"""
t_ = type(value)
if issubclass(t_, (BasedBase, Descriptor, DescriptorBase)):
if issubclass(t_, (BasedBase, DescriptorBase)):
update_key = list(self._kwargs.keys())
values = list(self._kwargs.values())
# Update the internal dict
@@ -124,7 +124,7 @@ def insert(self, index: int, value: Union[V, B]) -> None:

def __getitem__(self, idx: Union[int, slice]) -> Union[V, B]:
"""
Get an item in the collection based on it's index.
Get an item in the collection based on its index.

:param idx: index or slice of the collection.
:type idx: Union[int, slice]
@@ -168,7 +168,7 @@ def __setitem__(self, key: int, value: Union[B, V]) -> None:
if isinstance(value, Number): # noqa: S3827
item = self.__getitem__(key)
item.value = value
elif issubclass(type(value), (BasedBase, Descriptor, DescriptorBase)):
elif issubclass(type(value), (BasedBase, DescriptorBase)):
update_key = list(self._kwargs.keys())
values = list(self._kwargs.values())
old_item = values[key]
@@ -242,7 +242,7 @@ def sort(self, mapping: Callable[[Union[B, V]], Any], reverse: bool = False) ->
"""
Sort the collection according to the given mapping.

:param mapping: mapping function to sort the collection. i.e. lambda parameter: parameter.raw_value
:param mapping: mapping function to sort the collection. i.e. lambda parameter: parameter.value
:type mapping: Callable
:param reverse: Reverse the sorting.
:type reverse: bool
9 changes: 2 additions & 7 deletions src/easyscience/Objects/Inferface.py
Original file line number Diff line number Diff line change
@@ -152,7 +152,6 @@ def generate_bindings(self, model, *args, ifun=None, **kwargs):
:return: binding property
:rtype: property
"""
import easyscience.Objects.new_variable.parameter

class_links = self.__interface_obj.create(model)
props = model._get_linkable_attributes()
@@ -164,12 +163,8 @@ def generate_bindings(self, model, *args, ifun=None, **kwargs):
idx = props_names.index(item_key)
prop = props[idx]

## TODO clean when full move to new_variable
if isinstance(prop, easyscience.Objects.new_variable.parameter.Parameter):
# Should be fetched this way to ensure we don't get value from callback
prop_value = prop.value_no_call_back
else:
prop_value = prop.raw_value
# Should be fetched this way to ensure we don't get value from callback
prop_value = prop.value_no_call_back

prop._callback = item.make_prop(item_key)
prop._callback.fset(prop_value)
36 changes: 14 additions & 22 deletions src/easyscience/Objects/ObjectClasses.py
Original file line number Diff line number Diff line change
@@ -16,21 +16,18 @@
from typing import Optional
from typing import Set
from typing import TypeVar
from typing import Union

from easyscience import global_object
from easyscience.Utils.classTools import addLoggedProp

from .core import ComponentSerializer
from .new_variable import Parameter as NewParameter
from .new_variable.descriptor_base import DescriptorBase
from .Variable import Descriptor
from .Variable import Parameter
from .variable import Parameter
from .variable.descriptor_base import DescriptorBase

if TYPE_CHECKING:
from easyscience.Constraints import C
from easyscience.Objects.Inferface import iF
from easyscience.Objects.Variable import V
V = TypeVar('V', bound=DescriptorBase)


class BasedBase(ComponentSerializer):
@@ -160,8 +157,7 @@ def constraints(self) -> List[C]:
constraints.append(con[key])
return constraints

## TODO clean when full move to new_variable
def get_parameters(self) -> Union[List[Parameter], List[NewParameter]]:
def get_parameters(self) -> List[Parameter]:
"""
Get all parameter objects as a list.

@@ -171,11 +167,10 @@ def get_parameters(self) -> Union[List[Parameter], List[NewParameter]]:
for key, item in self._kwargs.items():
if hasattr(item, 'get_parameters'):
par_list = [*par_list, *item.get_parameters()]
elif isinstance(item, Parameter) or isinstance(item, NewParameter):
elif isinstance(item, Parameter):
par_list.append(item)
return par_list

## TODO clean when full move to new_variable
def _get_linkable_attributes(self) -> List[V]:
"""
Get all objects which can be linked against as a list.
@@ -186,12 +181,11 @@ def _get_linkable_attributes(self) -> List[V]:
for key, item in self._kwargs.items():
if hasattr(item, '_get_linkable_attributes'):
item_list = [*item_list, *item._get_linkable_attributes()]
elif issubclass(type(item), (Descriptor, DescriptorBase)):
elif issubclass(type(item), (DescriptorBase)):
item_list.append(item)
return item_list

## TODO clean when full move to new_variable
def get_fit_parameters(self) -> Union[List[Parameter], List[NewParameter]]:
def get_fit_parameters(self) -> List[Parameter]:
"""
Get all objects which can be fitted (and are not fixed) as a list.

@@ -201,7 +195,7 @@ def get_fit_parameters(self) -> Union[List[Parameter], List[NewParameter]]:
for key, item in self._kwargs.items():
if hasattr(item, 'get_fit_parameters'):
fit_list = [*fit_list, *item.get_fit_parameters()]
elif isinstance(item, Parameter) or isinstance(item, NewParameter):
elif isinstance(item, Parameter):
if item.enabled and not item.fixed:
fit_list.append(item)
return fit_list
@@ -235,7 +229,6 @@ class BaseObj(BasedBase):
cheat with `BaseObj(*[Descriptor(...), Parameter(...), ...])`.
"""

## TODO clean when full move to new_variable
def __init__(
self,
name: str,
@@ -253,15 +246,15 @@ def __init__(
super(BaseObj, self).__init__(name=name, unique_name=unique_name)
# If Parameter or Descriptor is given as arguments...
for arg in args:
if issubclass(type(arg), (BaseObj, Descriptor, DescriptorBase)):
if issubclass(type(arg), (BaseObj, DescriptorBase)):
kwargs[getattr(arg, 'name')] = arg
# Set kwargs, also useful for serialization
known_keys = self.__dict__.keys()
self._kwargs = kwargs
for key in kwargs.keys():
if key in known_keys:
raise AttributeError('Kwargs cannot overwrite class attributes in BaseObj.')
if issubclass(type(kwargs[key]), (BasedBase, Descriptor, DescriptorBase)) or 'BaseCollection' in [
if issubclass(type(kwargs[key]), (BasedBase, DescriptorBase)) or 'BaseCollection' in [
c.__name__ for c in type(kwargs[key]).__bases__
]:
self._global_object.map.add_edge(self, kwargs[key])
@@ -310,7 +303,6 @@ def __init__(self, foo: Parameter, bar: Parameter):
test_class=BaseObj,
)

## TODO clean when full move to new_variable
def __setattr__(self, key: str, value: BV) -> None:
# Assume that the annotation is a ClassVar
old_obj = None
@@ -323,12 +315,12 @@ def __setattr__(self, key: str, value: BV) -> None:
self.__class__.__annotations__[key].__args__,
)
):
if issubclass(type(getattr(self, key, None)), (BasedBase, Descriptor, DescriptorBase)):
if issubclass(type(getattr(self, key, None)), (BasedBase, DescriptorBase)):
old_obj = self.__getattribute__(key)
self._global_object.map.prune_vertex_from_edge(self, old_obj)
self._add_component(key, value)
else:
if hasattr(self, key) and issubclass(type(value), (BasedBase, Descriptor, DescriptorBase)):
if hasattr(self, key) and issubclass(type(value), (BasedBase, DescriptorBase)):
old_obj = self.__getattribute__(key)
self._global_object.map.prune_vertex_from_edge(self, old_obj)
self._global_object.map.add_edge(self, value)
@@ -352,8 +344,8 @@ def getter(obj: BV) -> BV:
@staticmethod
def __setter(key: str) -> Callable[[BV], None]:
def setter(obj: BV, value: float) -> None:
if issubclass(obj._kwargs[key].__class__, (Descriptor, DescriptorBase)) and not issubclass(
value.__class__, (Descriptor, DescriptorBase)
if issubclass(obj._kwargs[key].__class__, (DescriptorBase)) and not issubclass(
value.__class__, (DescriptorBase)
):
obj._kwargs[key].value = value
else:
Loading
Loading