2019-08-03 03:20:09 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
2019-08-05 16:39:37 +03:00
|
|
|
from logging import INFO, basicConfig, getLogger
|
2019-08-03 03:20:09 +03:00
|
|
|
|
|
|
|
import numpy as np
|
2019-08-05 16:39:37 +03:00
|
|
|
import requests
|
2019-08-03 03:20:09 +03:00
|
|
|
|
2019-11-20 15:45:45 +03:00
|
|
|
from bugbug import bugzilla, db
|
2019-08-03 03:20:09 +03:00
|
|
|
from bugbug.models import get_model_class
|
2020-09-01 16:19:43 +03:00
|
|
|
from bugbug.utils import download_model
|
2019-08-03 03:20:09 +03:00
|
|
|
|
2019-08-05 16:39:37 +03:00
|
|
|
basicConfig(level=INFO)
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
2019-08-03 03:20:09 +03:00
|
|
|
|
2023-11-30 02:23:46 +03:00
|
|
|
def classify_bugs(model_name: str, bug_id: int) -> None:
|
|
|
|
model_file_name = f"{model_name}model"
|
2019-08-03 03:20:09 +03:00
|
|
|
|
2019-08-05 16:39:37 +03:00
|
|
|
if not os.path.exists(model_file_name):
|
2023-03-09 13:58:37 +03:00
|
|
|
logger.info("%s does not exist. Downloading the model....", model_file_name)
|
2019-08-05 16:39:37 +03:00
|
|
|
try:
|
2020-09-01 16:19:43 +03:00
|
|
|
download_model(model_name)
|
2019-08-05 16:39:37 +03:00
|
|
|
except requests.HTTPError:
|
|
|
|
logger.error(
|
2020-05-08 17:14:10 +03:00
|
|
|
"A pre-trained model is not available, you will need to train it yourself using the trainer script"
|
2019-08-05 16:39:37 +03:00
|
|
|
)
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
2019-08-03 03:20:09 +03:00
|
|
|
model_class = get_model_class(model_name)
|
|
|
|
model = model_class.load(model_file_name)
|
|
|
|
|
2019-08-08 22:33:51 +03:00
|
|
|
if bug_id:
|
|
|
|
bugs = bugzilla.get(bug_id).values()
|
|
|
|
assert bugs, f"A bug with a bug id of {bug_id} was not found"
|
|
|
|
else:
|
2019-11-20 15:45:45 +03:00
|
|
|
assert db.download(bugzilla.BUGS_DB)
|
2019-08-08 22:33:51 +03:00
|
|
|
bugs = bugzilla.get_bugs()
|
|
|
|
|
|
|
|
for bug in bugs:
|
2019-08-03 03:20:09 +03:00
|
|
|
print(
|
|
|
|
f'https://bugzilla.mozilla.org/show_bug.cgi?id={bug["id"]} - {bug["summary"]} '
|
|
|
|
)
|
|
|
|
|
|
|
|
if model.calculate_importance:
|
|
|
|
probas, importance = model.classify(
|
|
|
|
bug, probabilities=True, importances=True
|
|
|
|
)
|
|
|
|
|
|
|
|
model.print_feature_importances(
|
2019-10-07 21:14:34 +03:00
|
|
|
importance["importances"], class_probabilities=probas
|
2019-08-03 03:20:09 +03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
probas = model.classify(bug, probabilities=True, importances=False)
|
|
|
|
|
2019-11-10 20:17:39 +03:00
|
|
|
probability = probas[0]
|
|
|
|
pred_index = np.argmax(probability)
|
|
|
|
if len(probability) > 2:
|
|
|
|
pred_class = model.le.inverse_transform([pred_index])[0]
|
2019-08-03 03:20:09 +03:00
|
|
|
else:
|
2019-11-10 20:17:39 +03:00
|
|
|
pred_class = "Positive" if pred_index == 1 else "Negative"
|
|
|
|
print(f"{pred_class} {probability}")
|
2019-08-03 03:20:09 +03:00
|
|
|
input()
|
|
|
|
|
|
|
|
|
2020-09-01 16:24:21 +03:00
|
|
|
def main() -> None:
|
2019-08-03 03:20:09 +03:00
|
|
|
description = "Perform evaluation on bugs using the specified model"
|
|
|
|
parser = argparse.ArgumentParser(description=description)
|
|
|
|
|
|
|
|
parser.add_argument("model", help="Which model to use for evaluation")
|
2020-09-01 16:24:21 +03:00
|
|
|
parser.add_argument("--bug-id", help="Classify the given bug id", type=int)
|
2019-08-03 03:20:09 +03:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2023-11-30 02:23:46 +03:00
|
|
|
classify_bugs(args.model, args.bug_id)
|
2019-09-09 23:55:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|