Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LGBM incompatibilities with report_generator #143

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions modelbuilders_bench/lgbm_mb.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@
t_train, model_lgbm = bench.measure_function_time(lgbm.train, lgbm_params, lgbm_train,
params=params,
num_boost_round=params.n_estimators,
valid_sets=lgbm_train,
verbose_eval=False)
valid_sets=lgbm_train)
train_metric = None
if not X_train.equals(X_test):
y_train_pred = model_lgbm.predict(X_train)
Expand Down
96 changes: 96 additions & 0 deletions report_generator/fix-lgbm-mb-results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# ==============================================================================
# Copyright 2020-2023 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""
Temporary solution to fix the .json result files created from lgbm_mb.py.
The result files are in an incompatible format for report_generator.py.
Attempts to produce xlsx reports fail and create empty files.

After running this script on my-file.json, a new file my-file-fixed.json will be
produced, containing a JSON version of the results in a compatible format.

Usage:

python fix-lgbm-mb-results.py my-file.json [another-file.json ...]


Note: This is just a quick and dirty hack that does not fix the underlying
issue. Rather than changing this file (if something breaks again), the
original script lgbm_mb.py should be updated such that it produces valid
JSON dumps again.
"""

from argparse import ArgumentParser
import json
from pathlib import Path


def fix_file(fname: Path):
with open(fname) as fp:
data = json.load(fp)

# copy all data (aux info etc)
fixed = {}
for key, val in data.items():
fixed[key] = val

# reset the results - we'll fix them
fixed["results"] = []

current_result = {}
for result in data["results"]:
if "algorithm" in result:
# found a new algo / measurement
current_result = result
continue

if "stage" in result:
comb = current_result | result
if "device" not in comb:
comb["device"] = "none"

if "time[s]" not in comb:
comb["time[s]"] = (
result.get("training_time") or result["prediction_time"]
)

if "algorithm_parameters" not in comb:
comb["algorithm_paramters"] = {}

if "accuracy[%]" in comb:
comb["accuracy"] = comb["accuracy[%]"]

replace_pairs = (
("lgbm_train", "training"),
("lgbm_predict", "prediction"),
("daal4py_predict", "alternative_prediction"),
)
for s, r in replace_pairs:
comb["stage"] = comb["stage"].replace(s, r)

fixed["results"].append(comb)

out_fname = fname.stem + "-fixed.json"
with open(out_fname, "w") as fp:
json.dump(fixed, fp, indent=4)


if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("filenames", nargs="+")
args = parser.parse_args()
for fname in args.filenames:
fix_file(Path(fname))