Skip to content

Commit

Permalink
base collection introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
andped10 committed Sep 23, 2024
1 parent 0aa0c61 commit 4836517
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 58 deletions.
41 changes: 41 additions & 0 deletions src/easyreflectometry/sample/collections/base_collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from copy import deepcopy
from typing import List

from easyscience.Objects.Groups import BaseCollection as EasyBaseCollection

from easyreflectometry.parameter_utils import yaml_dump


class BaseCollection(EasyBaseCollection):
def __init__(
self,
name: str,
interface,
*args,
**kwargs,
):
super().__init__(name, *args, **kwargs)
self.interface = interface

def __repr__(self) -> str:
"""
String representation of the collection.
:return: a string representation of the collection
"""
return yaml_dump(self._dict_repr)

@property
def _dict_repr(self) -> dict:
"""
A simplified dict representation.
:return: Simple dictionary
"""
return {self.name: [i._dict_repr for i in self]}

def _make_defalut_collection(self, default_collection: List, interface):
elements = deepcopy(default_collection)
for element in elements:
element.interface = interface
return elements
Original file line number Diff line number Diff line change
@@ -1,38 +1,10 @@
from copy import deepcopy
from typing import List
from typing import Optional

from easyscience.Objects.Groups import BaseCollection

from easyreflectometry.parameter_utils import yaml_dump
from .base_collection import BaseCollection


class BaseElementCollection(BaseCollection):
def __init__(
self,
name: str,
interface,
*args,
**kwargs,
):
super().__init__(name, *args, **kwargs)
self.interface = interface

@property
def names(self) -> list:
"""
:returns: list of names for the elements in the collection.
"""
return [i.name for i in self]

def __repr__(self) -> str:
"""
String representation of the collection.
:return: a string representation of the collection
"""
return yaml_dump(self._dict_repr)

@property
def _dict_repr(self) -> dict:
"""
Expand All @@ -53,9 +25,3 @@ def as_dict(self, skip: Optional[List[str]] = None) -> dict:
for collection_element in self:
this_dict['data'].append(collection_element.as_dict(skip=skip))
return this_dict

def _make_defalut_collection(self, default_collection: List, interface):
elements = deepcopy(default_collection)
for element in elements:
element.interface = interface
return elements
25 changes: 2 additions & 23 deletions src/easyreflectometry/sample/collections/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@

__author__ = 'github.com/arm61'

from copy import deepcopy
from typing import List
from typing import Optional

from easyscience.Objects.Groups import BaseCollection

from easyreflectometry.parameter_utils import yaml_dump

from ..assemblies.base_assembly import BaseAssembly
from ..assemblies.multilayer import Multilayer
from ..assemblies.repeating_multilayer import RepeatingMultilayer
from ..assemblies.surfactant_layer import SurfactantLayer
from ..elements.layers.layer import Layer
from .base_collection import BaseCollection

# NR_DEFAULT_ASSEMBLIES = 2
DEFAULT_COLLECTION = [Multilayer(), Multilayer()]


Expand Down Expand Up @@ -49,8 +44,7 @@ def __init__(
for assembly in assemblies:
if not issubclass(type(assembly), BaseAssembly):
raise ValueError('The elements must be an Assembly.')
super().__init__(name, *assemblies, **kwargs)
self.interface = interface
super().__init__(name, interface, *assemblies, **kwargs)

def add_assembly(self, assembly: Optional[BaseAssembly] = None):
"""Add an assembly to the sample.
Expand Down Expand Up @@ -144,15 +138,6 @@ def _disable_changes_to_outermost_layers(self):
self.subphase.thickness.enabled = False

# Representation
@property
def _dict_repr(self) -> dict:
"""A simplified dict representation."""
return {self.name: [i._dict_repr for i in self]}

def __repr__(self) -> str:
"""String representation of the sample."""
return yaml_dump(self._dict_repr)

def as_dict(self, skip: list = None) -> dict:
"""Produces a cleaned dict using a custom as_dict method to skip necessary things.
The resulting dict matches the parameters in __init__
Expand All @@ -166,9 +151,3 @@ def as_dict(self, skip: list = None) -> dict:
this_dict['data'][i] = assembly.as_dict(skip=skip)
this_dict['populate_if_none'] = self.populate_if_none
return this_dict

def _make_defalut_collection(self, default_collection: List, interface):
elements = deepcopy(default_collection)
for element in elements:
element.interface = interface
return elements

0 comments on commit 4836517

Please sign in to comment.