diff options
author | Daniel Friesel <daniel.friesel@uos.de> | 2021-04-20 11:55:22 +0200 |
---|---|---|
committer | Daniel Friesel <daniel.friesel@uos.de> | 2021-04-20 11:55:22 +0200 |
commit | 5b43e9b07d48bb65771a66760128fc85afc1b95e (patch) | |
tree | 8cd11b1162d6ba44f3047a2e6f2281fe99a5385c | |
parent | f48482529e34cc5d9f7d11681febb57905251b47 (diff) |
proper kconfig-webconf export
-rwxr-xr-x | bin/analyze-archive.py | 53 | ||||
-rw-r--r-- | lib/functions.py | 22 | ||||
-rw-r--r-- | lib/model.py | 7 | ||||
-rw-r--r-- | lib/parameters.py | 3 |
4 files changed, 36 insertions, 49 deletions
diff --git a/bin/analyze-archive.py b/bin/analyze-archive.py index 8b58478..268fd38 100755 --- a/bin/analyze-archive.py +++ b/bin/analyze-archive.py @@ -437,41 +437,6 @@ def print_splitinfo(param_names, info, prefix=""): print(f"{prefix}: UNKNOWN") -def _mogrify(function_str, parameter_names, regression_args): - for i in range(len(regression_args)): - function_str = function_str.replace( - f"regression_arg({i})", str(regression_args[i]) - ) - for parameter_name in parameter_names: - function_str = function_str.replace( - f"parameter({parameter_name})", f"""param["{parameter_name}"]""" - ) - for arg_num in range(10): - function_str = function_str.replace( - f"function_arg({arg_num})", f"args[{arg_num}]" - ) - function_str = function_str.replace("np.", "Math.") - return "#![modelfunction]" + function_str - - -def mogrify(model): - if type(model) is dict: - if "functionStr" in model: - model["function"] = _mogrify( - model["functionStr"], model["parameterNames"], model["regressionModel"] - ) - model.pop("functionStr") - model.pop("parameterNames") - model.pop("regressionModel") - else: - for k, v in model.items(): - model[k] = mogrify(v) - elif type(model) is list: - for i, elem in enumerate(model): - model[i] = mogrify(model[i]) - return model - - if __name__ == "__main__": ignored_trace_indexes = [] @@ -1201,21 +1166,11 @@ if __name__ == "__main__": "Note: v0 measurements do not embed the PTA used for benchmark generation. Estimating PTA from recorded observations." ) json_model = model.to_json() - mogrify(json_model) json_model_str = json.dumps(json_model, indent=2, sort_keys=True, cls=NpEncoder) - json_model_out = "" - for line in json_model_str.split("\n"): - match = re.fullmatch(r"(.*)\"#!\[modelfunction\](.*\")(.*)", line) - if match: - line = ( - match.group(1) - + "(param, args) => " - + json.loads('"' + match.group(2)) - + match.group(3) - ) - json_model_out += line + "\n" - with open(f"{args.export_webconf}.json", "w") as f: - f.write(json_model_out) + for function_str, function_body in model.webconf_function_map(): + json_model_str = json_model_str.replace(function_str, function_body) + with open(f"{args.export_webconf}.js", "w") as f: + f.write(json_model_str) with open(f"{args.export_webconf}.kconfig", "w") as f: f.write(get_kconfig(model)) diff --git a/lib/functions.py b/lib/functions.py index 33ecaca..81f6ab5 100644 --- a/lib/functions.py +++ b/lib/functions.py @@ -191,6 +191,9 @@ class ModelFunction: return self.function_error["mae"] return self.value_error["mae"] + def webconf_function_map(self): + return list() + def to_json(self): """Convert model to JSON.""" ret = { @@ -300,6 +303,12 @@ class SplitFunction(ModelFunction): param_value = param_list[self.param_index] return self.child[param_value].eval(param_list) + def webconf_function_map(self): + ret = list() + for child in self.child.values(): + ret.extend(child.webconf_function_map()) + return ret + def to_json(self): ret = super().to_json() ret.update( @@ -572,6 +581,19 @@ class AnalyticFunction(ModelFunction): """ return self._function(self.model_args, param_list) + def webconf_function_map(self): + js_buf = self.model_function + for i in range(len(self.model_args)): + js_buf = js_buf.replace(f"regression_arg({i})", str(self.model_args[i])) + for parameter_name in self._parameter_names: + js_buf = js_buf.replace( + f"parameter({parameter_name})", f"""param["{parameter_name}"]""" + ) + for arg_num in range(self._num_args): + js_buf = js_buf.replace(f"function_arg({arg_num})", f"args[{arg_num}]") + js_buf = "(param, args) => " + js_buf.replace("np.", "Math.") + return [(f'"{self.model_function}"', js_buf)] + def to_json(self): ret = super().to_json() ret.update( diff --git a/lib/model.py b/lib/model.py index 4585dfe..8cbb012 100644 --- a/lib/model.py +++ b/lib/model.py @@ -373,6 +373,13 @@ class AnalyticModel: return ret + def webconf_function_map(self) -> list: + ret = list() + for name in self.names: + for attr_model in self.attr_by_name[name].values(): + ret.extend(attr_model.webconf_function_map()) + return ret + def predict(self, trace, with_fitted=True, wth_lut=False): pass # TODO trace= ( (name, duration), (name, duration), ...) diff --git a/lib/parameters.py b/lib/parameters.py index a8a8451..d5339c5 100644 --- a/lib/parameters.py +++ b/lib/parameters.py @@ -537,6 +537,9 @@ class ModelAttribute: ret = {"mean": (self.mean, unit), "median": (self.median, unit)} return ret + def webconf_function_map(self): + return self.model_function.webconf_function_map() + @staticmethod def from_json(cls, name, attr, data): param_names = data["paramNames"] |