Skip to content
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: 8 additions & 0 deletions src/easyscience/Objects/Variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,14 @@ def fixed(self, value: bool):
raise ValueError
self._fixed = value

@property
def free(self) -> bool:
return not self.fixed

@free.setter
def free(self, value: bool) -> None:
self.fixed = not value

@property
def error(self) -> float:
"""
Expand Down
4 changes: 3 additions & 1 deletion src/easyscience/Objects/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class ComponentSerializer:

_CORE = True

def __deepcopy__(self, memo):
return self.from_dict(self.as_dict())

def encode(self, skip: Optional[List[str]] = None, encoder: Optional[EC] = None, **kwargs) -> Any:
"""
Use an encoder to covert an EasyScience object into another format. Default is to a dictionary using `DictSerializer`.
Expand All @@ -41,7 +44,6 @@ def encode(self, skip: Optional[List[str]] = None, encoder: Optional[EC] = None,
:param kwargs: Any additional key word arguments to be passed to the encoder
:return: encoded object containing all information to reform an EasyScience object.
"""

if encoder is None:
encoder = DictSerializer
encoder_obj = encoder()
Expand Down
8 changes: 8 additions & 0 deletions src/easyscience/Objects/new_variable/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ def fixed(self, fixed: bool) -> None:
raise ValueError(f'{fixed=} must be a boolean. Got {type(fixed)}')
self._fixed = fixed

@property
def free(self) -> bool:
return not self.fixed

@free.setter
def free(self, value: bool) -> None:
self.fixed = not value

@property
def bounds(self) -> Tuple[numbers.Number, numbers.Number]:
"""
Expand Down
6 changes: 1 addition & 5 deletions src/easyscience/Utils/io/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,7 @@ def _convert_from_dict(d):
mod = __import__(modname, globals(), locals(), [classname], 0)
if hasattr(mod, classname):
cls_ = getattr(mod, classname)
data = {
k: BaseEncoderDecoder._convert_from_dict(v)
for k, v in d.items()
if not (k.startswith('@') or k == 'unique_name')
}
data = {k: BaseEncoderDecoder._convert_from_dict(v) for k, v in d.items() if not k.startswith('@')}
return cls_(**data)
elif np is not None and modname == 'numpy' and classname == 'array':
if d['dtype'].startswith('complex'):
Expand Down
2 changes: 1 addition & 1 deletion src/easyscience/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.1.0'
__version__ = '1.1.1'
17 changes: 17 additions & 0 deletions tests/unit_tests/Objects/test_BaseObj.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,23 @@ def check_dict(check, item):
check_dict(expected, obtained)


def test_baseobj_dict_roundtrip(clear, setup_pars: dict):
# When
name = setup_pars["name"]
del setup_pars["name"]
obj = BaseObj(name, **setup_pars, unique_name='special_name')
obj_dict = obj.as_dict()

global_object.map._clear()

# Then
new_obj = BaseObj.from_dict(obj_dict)

# Expect
new_obj_dict = new_obj.as_dict()
assert obj_dict == new_obj_dict


def test_baseobj_dir(setup_pars):
name = setup_pars["name"]
del setup_pars["name"]
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/utils/io_tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def __init__(self, name: str = "A", **kwargs):


class B(BaseObj):
def __init__(self, a, b):
super(B, self).__init__("B", a=a)
def __init__(self, a, b, unique_name):
super(B, self).__init__("B", a=a, unique_name=unique_name)
self.b = b


Expand Down
8 changes: 4 additions & 4 deletions tests/unit_tests/utils/io_tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,16 @@ def test_custom_class_encode_data(dp_kwargs: dict, dp_cls: Type[DescriptorNumber

def test_custom_class_full_encode_with_numpy():
class B(BaseObj):
def __init__(self, a, b):
super(B, self).__init__("B", a=a)
def __init__(self, a, b, unique_name):
super(B, self).__init__("B", a=a, unique_name=unique_name)
self.b = b
# Same as in __init__.py for easyscience
try:
version = metadata.version('easyscience') # 'easyscience' is the name of the package in 'setup.py
except metadata.PackageNotFoundError:
version = '0.0.0'

obj = B(DescriptorNumber("a", 1.0, unique_name="a"), np.array([1.0, 2.0, 3.0]))
obj = B(DescriptorNumber("a", 1.0, unique_name="a"), np.array([1.0, 2.0, 3.0]), unique_name="B_0")
full_enc = obj.encode(encoder=DictSerializer, full_encode=True)
expected = {
"@module": "tests.unit_tests.utils.io_tests.test_dict",
Expand Down Expand Up @@ -248,7 +248,7 @@ def __init__(self, a, b):

def test_custom_class_full_decode_with_numpy():
global_object.map._clear()
obj = B(DescriptorNumber("a", 1.0), np.array([1.0, 2.0, 3.0]))
obj = B(DescriptorNumber("a", 1.0), np.array([1.0, 2.0, 3.0]), unique_name="B_0")
full_enc = obj.encode(encoder=DictSerializer, full_encode=True)
global_object.map._clear()
obj2 = B.decode(full_enc, decoder=DictSerializer)
Expand Down