Open lheagy opened 6 years ago
I think the generic serialization of a class is going to be tough. But inside of pymatsolver, this should be easy.
This snippet might help on the SimPEG
side of things?
import properties
import pymatsolver
import inspect
class SolverClass(properties.Property):
def validate(self, instance, value):
if not inspect.isclass(value):
self.error(
instance, value,
extra="The Solver must be a class."
)
if not hasattr(value, "__mul__"):
self.error(
instance, value,
extra="The Solver must have a `__mul__` method."
)
return value
def to_json(self, value, **kwargs):
cls_name = value.__name__
if hasattr(pymatsolver, cls_name):
return {
"module": "pymatsolver",
"version": pymatsolver.__version__,
"class": cls_name
}
return value
def from_json(self, value, **kwargs):
assert isinstance(value, dict)
assert "module" in value and value["module"] == "pymatsolver"
assert "version" in value and value["version"] == pymatsolver.__version__
if hasattr(pymatsolver, value["class"]):
return getattr(pymatsolver, value["class"])
raise ValueError('pymatsolver has no class "{}"'.format(value))
class Simulation(properties.HasProperties):
Solver = SolverClass("pymatsolver Solver Class")
test = pymatsolver.Pardiso
sim = Simulation(Solver=test)
data = sim.serialize()
print(data)
assert Simulation.deserialize(data).Solver is test
{
"Solver": {
"version": "0.1.2",
"module": "pymatsolver",
"class": "Pardiso"
},
"__class__": "Simulation"
}
Motivation
In developing a
Simulation
class in SimPEG (simpeg/simpeg#672), I want to be able to readily serialize and deserialize an instance of aSimulation
, so we are injectingproperties
throughout, which gives us serialization, deserialization and validation (thanks @fwkoch!!). Right now, the solvers are attached as the simulation as a class, and the solver_opts are attached to the simulation as a dict. We then instantiate a solver as needed inSimPEG
.State the problem
There are a couple potential snags with this approach
solver_opts
are not valid, we won't find out until we try and use the solversolver
class (aClass
property is one option aranzgeo/properties#163, we could also think about serializing / deserializing the name of the class as a string)Another approach?
We could instantiate the solver with its options and then call it to create Ainv. This would solve the serialization problem and also allow immediate validation of the solver options on its creation.
so the base class might look like:
There are a couple impacts of this within SimPEG. The one that comes to mind is: