Add json deserializer to SimSirModel
@@ -6,7 +6,7 @@ changed
"""
from __future__ import annotations
-from json import dumps
+from json import dumps, loads
from typing import Dict, Generator, Tuple
@@ -18,7 +18,8 @@ from .parameters import Parameters
class SimSirModel:
- def __init__(self, p: Parameters) -> SimSirModel:
+ def __init__(self, p: Parameters = None) -> SimSirModel:
+ if p:
# TODO missing initial recovered value
susceptible = p.susceptible
recovered = 0.0
@@ -105,6 +106,19 @@ class SimSirModel:
return result
return dumps(self, default=to_json_helper, sort_keys=True)
+ @classmethod
+ def from_json(cls, json):
+ state = loads(json)
+ model = cls()
+ model_dict = model.__dict__
+
+ for key, value in state.items():
+ if isinstance(value, dict):
+ value = pd.DataFrame.from_dict(value)
+ model_dict[key] = value
+
+ return model
+
def sir(
s: float, i: float, r: float, beta: float, gamma: float, n: float
