ADR Suggestion New EasyScience base classes.
#161
Replies: 2 comments 3 replies
-
|
Disclaimer: I haven't spent much time studying our base classes. This looks mostly good to me, but with some questions/comments. I'm not sure if I like that the I find it convenient, perhaps almost required, to pass May be worth it to add |
Beta Was this translation helpful? Give feedback.
-
|
Really apreciate the changes that improve the definition of classes interface. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
General
As has been outlined in detail in #110, the current base classes
ObjBase,BasedBaseandCollectionBaseare over-engineered and convoluted classes which are difficult to edit and maintain.This is mostly caused by two key features: "Parameter injection", which allows dynamic classes with attributes defined at instantiation, and "interface switching" which allows on-the-fly changing of the interface between different third-party calculators without loss of the current state.
Both of these features are undesirable for different reasons. Parameter injection is undesirable because they encourage creation of classes with unknown or changing API's, making it extremely difficult to develop and maintain features using these classes.
Interface switching is undesirable due to improper separation of responsibilities and very confusing logic, as outlined in #160.
This ADR has an initial implementation which can be reviewed in #159
Current Implementation
The current implementation has already been outlined in #110
Proposed Implementation
I propose to remove the 3 base classes
ObjBase,BasedBaseandCollectionBasein the future, and replace them with new simpler classes. For now, the new base classes will coexist with the old base classes until all dependent libraries have switched.Specifically I propose 2 new base classes:
NewBasewhich is the new root-base class of EasyScience, replacingBasedBaseandModelBasewhich inherits fromNewBasebut adds functionality to accommodateParameterclass attributes.NewBase
This class will be renamed in the future, when we have properly switched to the new classes.
NewBaseinherits from no other class, unlikeBasedBasewhich inherits fromSerializerComponentto add serialization capabilities. InsteadNewBaseimplements those serialization capabilities itself directly, avoiding the need for an extra inheritance layer. Specifically it adds 3 methods:Which is needed for the
SerializerBaseclass to serialize theNewBaseclass. TheSerializerBase(which should be renamed), is a class with static methods which holds logic for generic serialization/deserialization. In EasyScience, serialization is handed by theas_dictmethod:Which serializes the class to a dictionary. For serialization to JSON or other formats, this dictionary can then be re-serialized. Deserialization is handled by the
from_dictmethod, which by default uses the generic deserialization of theSerializerBaseclass. Inherited classes should then overwrite this method for custom deserialization.Additionally, the
NewBaseclass implements theunique_nameattribute and the logic to theglobal_objectmap, as well as the optionaldisplay_nameattribute.Finally the class implements the dunder methods:
__dir____copy____deepcopy____repr__ModelBase
This class inherits from
NewBaseand adds functionality forParameterattributes which are modifiable for the user but not replaceable. This is done by implementing the setter method to simply overwrite the value of theParameterwhereas the getter method return theParameterinstance, allowing users direct access to the Parameters methods and attributes, likefixedormin, i.e.:When Parameter attributes are defined as such,
ModelBaseprovides functionality for correct deserialization by overwriting thefrom_dictmethod to set the underlying_my_paramattributes, since the class does not (and should not) takeParameterobjects as inputs in its constructor.Additionally
ModelBaseprovides 3 convenience method to get all its Parameters.get_fit_parameters, which recursively finds allDescriptorNumberorParameterattributes in the class:And also
get_fit_parameterswhich filters out dependentParametersandDescriptorNumbersto only provide fittable Parameters andget_free_parameterswhich additionally filters out all fixed Parameters.SerializerBase
The
SerializerBaseclass is the current class responsible for serialization/deserialization of objects. This class gets extended with adeserialize_dictstatic method, which takes a dictionary and deserializes it, using classes ownfrom_dictmethod if they're EasyScience classes, and the generic deserializer otherwise.This method allows classes themselves to define a
from_dictmethod to determine how they are deserialized, instead of just assuming that the__init__constructor works with a serialized dictionary, which it won´t forParameterattributes.Beta Was this translation helpful? Give feedback.
All reactions