|
| 1 | +# ============================================================================== |
| 2 | +# Copyright 2020-2023 Intel Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ============================================================================== |
| 16 | + |
| 17 | +""" |
| 18 | +Temporary solution to fix the .json result files created from lgbm_mb.py. |
| 19 | +The result files are in an incompatible format for report_generator.py. |
| 20 | +Attempts to produce xlsx reports fail and create empty files. |
| 21 | +
|
| 22 | +After running this script on my-file.json, a new file my-file-fixed.json will be |
| 23 | +produced, containing a JSON version of the results in a compatible format. |
| 24 | +
|
| 25 | +Usage: |
| 26 | +
|
| 27 | + python fix-lgbm-mb-results.py my-file.json [another-file.json ...] |
| 28 | +
|
| 29 | +
|
| 30 | +Note: This is just a quick and dirty hack that does not fix the underlying |
| 31 | + issue. Rather than changing this file (if something breaks again), the |
| 32 | + original script lgbm_mb.py should be updated such that it produces valid |
| 33 | + JSON dumps again. |
| 34 | +""" |
| 35 | + |
| 36 | +from argparse import ArgumentParser |
| 37 | +import json |
| 38 | +from pathlib import Path |
| 39 | + |
| 40 | +def fix_file(fname: Path): |
| 41 | + with open(fname) as fp: |
| 42 | + data = json.load(fp) |
| 43 | + |
| 44 | + # copy all data (aux info etc) |
| 45 | + fixed = {} |
| 46 | + for key, val in data.items(): |
| 47 | + fixed[key] = val |
| 48 | + |
| 49 | + # reset the results - we'll fix them |
| 50 | + fixed["results"] = [] |
| 51 | + |
| 52 | + current_result = {} |
| 53 | + for result in data["results"]: |
| 54 | + if "algorithm" in result: |
| 55 | + # found a new algo / measurement |
| 56 | + current_result = result |
| 57 | + continue |
| 58 | + |
| 59 | + if "stage" in result: |
| 60 | + comb = current_result | result |
| 61 | + if "device" not in comb: |
| 62 | + comb["device"] = "none" |
| 63 | + |
| 64 | + if "time[s]" not in comb: |
| 65 | + comb["time[s]"] = result.get("training_time") or result["prediction_time"] |
| 66 | + |
| 67 | + if "algorithm_parameters" not in comb: |
| 68 | + comb["algorithm_paramters"] = {} |
| 69 | + |
| 70 | + if "accuracy[%]" in comb: |
| 71 | + comb["accuracy"] = comb["accuracy[%]"] |
| 72 | + |
| 73 | + replace_pairs = ( |
| 74 | + ("lgbm_train", "training"), |
| 75 | + ("lgbm_predict", "prediction"), |
| 76 | + ("daal4py_predict", "alternative_prediction"), |
| 77 | + ) |
| 78 | + for s, r in replace_pairs: |
| 79 | + comb["stage"] = comb["stage"].replace(s, r) |
| 80 | + |
| 81 | + fixed["results"].append(comb) |
| 82 | + |
| 83 | + out_fname = fname.stem + "-fixed.json" |
| 84 | + with open(out_fname, "w") as fp: |
| 85 | + json.dump(fixed, fp, indent=4) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + parser = ArgumentParser() |
| 90 | + parser.add_argument("filenames", nargs="+") |
| 91 | + args = parser.parse_args() |
| 92 | + for fname in args.filenames: |
| 93 | + fix_file(Path(fname)) |
0 commit comments