summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/functions.py27
-rw-r--r--lib/model.py17
-rw-r--r--lib/parameters.py9
3 files changed, 50 insertions, 3 deletions
diff --git a/lib/functions.py b/lib/functions.py
index 8e43dcb..f3e5ac8 100644
--- a/lib/functions.py
+++ b/lib/functions.py
@@ -190,6 +190,9 @@ class StaticFunction(ModelFunction):
"""
return self.value
+ def to_json(self):
+ return {"type": "static", "value": self.value}
+
class StaticInfo(ModelInfo):
def __init__(self, data):
@@ -198,6 +201,9 @@ class StaticInfo(ModelInfo):
self.median = np.median(data)
self.std = np.std(data)
+ def to_json(self):
+ return "FIXME"
+
class SplitFunction(ModelFunction):
def __init__(self, param_index, child):
@@ -223,6 +229,13 @@ class SplitFunction(ModelFunction):
param_value = param_list[self.param_index]
return self.child[param_value].eval(param_list, arg_list)
+ def to_json(self):
+ return {
+ "type": "split",
+ "paramIndex": self.param_index,
+ "child": dict([[k, v.to_json()] for k, v in self.child.items()]),
+ }
+
class SplitInfo(ModelInfo):
def __init__(self, param_index, child):
@@ -230,6 +243,9 @@ class SplitInfo(ModelInfo):
self.param_index = param_index
self.child = child
+ def to_json(self):
+ return "FIXME"
+
class AnalyticFunction(ModelFunction):
"""
@@ -415,6 +431,14 @@ class AnalyticFunction(ModelFunction):
return self._function(param_list, arg_list)
return self._function(self.model_args, param_list)
+ def to_json(self):
+ return {
+ "type": "analytic",
+ "functionStr": self.model_function,
+ "dependsOnParam": self._dependson,
+ "regressionModel": list(self.model_args),
+ }
+
class AnalyticInfo(ModelInfo):
def __init__(self, fit_result, function):
@@ -422,6 +446,9 @@ class AnalyticInfo(ModelInfo):
self.fit_result = fit_result
self.function = function
+ def to_json(self):
+ return "FIXME"
+
class analytic:
"""
diff --git a/lib/model.py b/lib/model.py
index 5979c70..be63a8a 100644
--- a/lib/model.py
+++ b/lib/model.py
@@ -311,9 +311,20 @@ class AnalyticModel:
return {"by_name": detailed_results}
- def to_json(self):
- # TODO
- pass
+ def to_json(self) -> dict:
+ """
+ Return JSON encoding of this AnalyticModel.
+ """
+ ret = {
+ "parameters": self.parameters,
+ "name": dict([[name, dict()] for name in self.names]),
+ }
+
+ for name in self.names:
+ for attr_name, attr in self.attr_by_name[name].items():
+ ret["name"][name][attr_name] = attr.to_json()
+
+ return ret
def predict(self, trace, with_fitted=True, wth_lut=False):
pass
diff --git a/lib/parameters.py b/lib/parameters.py
index 368b24c..fa01804 100644
--- a/lib/parameters.py
+++ b/lib/parameters.py
@@ -600,6 +600,15 @@ class ModelAttribute:
mean = np.mean(self.data)
return f"ModelAttribute<{self.name}, {self.attr}, mean={mean}>"
+ def to_json(self):
+ ret = {
+ "paramNames": self.param_names,
+ "argCount": self.arg_count,
+ "modelFunction": self.model_function.to_json(),
+ "modelInfo": self.model_info.to_json(),
+ }
+ return ret
+
def get_static(self, use_mean=False):
if use_mean:
return self.mean