fix: refactor acc.py to remove code duplication in government_rag testenv#578
fix: refactor acc.py to remove code duplication in government_rag testenv#57831groot wants to merge 2 commits into
Conversation
…tenv Signed-off-by: Parv Agrawal <agrawalparv13@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 31groot The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/assign @MooreZheng |
There was a problem hiding this comment.
Code Review
This pull request attempts to refactor the accuracy calculation script in acc.py but leaves the code in an incomplete and broken state. Specifically, it introduces a syntax error (IndentationError) due to an empty if block and deletes necessary logic without replacing it. The reviewer has provided a complete, clean refactoring of the file to resolve these issues and properly deduplicate the accuracy calculation logic.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if real_y_pred[i] == real_y_true[i]: | ||
| province_acc[province]["correct"] += 1 | ||
|
|
||
| print("\n=== Accuracy Statistics ===") | ||
| print(f"Global Accuracy: {global_acc:.4f}") | ||
| print("\nProvince Accuracies:") | ||
| for province, stats in province_acc.items(): | ||
| acc = stats["correct"] / stats["total"] | ||
| print(f"{province}: {acc:.4f} ({stats['correct']}/{stats['total']})") | ||
|
|
||
| import json | ||
| from datetime import datetime | ||
|
|
||
| results = { | ||
| "global_accuracy": global_acc, | ||
| "province_accuracies": { | ||
| province: { | ||
| "accuracy": stats["correct"] / stats["total"], | ||
| "correct": stats["correct"], | ||
| "total": stats["total"] | ||
| } | ||
| for province, stats in province_acc.items() | ||
| }, | ||
| "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S") | ||
| } | ||
|
|
||
| with open("accuracy_results_local.json", "w", encoding="utf-8") as f: | ||
| json.dump(results, f, ensure_ascii=False, indent=4) | ||
|
|
||
| return global_acc | ||
|
|
||
|
|
||
| @ClassFactory.register(ClassType.GENERAL, alias="acc_other") | ||
| def acc_other(y_true, y_pred): | ||
| original_preds = y_pred.copy() | ||
|
|
||
| y_pred = [pred.split("||")[0] for pred in original_preds] | ||
| y_true = [pred.split("||")[1] for pred in original_preds] | ||
| y_location = [pred.split("||")[2] for pred in original_preds] | ||
| y_source = [pred.split("||")[3] for pred in original_preds] | ||
|
|
||
| real_y_pred = [] | ||
| real_y_true = [] | ||
| real_locations = [] | ||
|
|
||
| for i in range(len(y_pred)): | ||
| if y_source[i] == "[other]": | ||
| real_y_pred.append(get_last_letter(y_pred[i])) | ||
| real_y_true.append(y_true[i]) | ||
| real_locations.append(y_location[i]) | ||
|
|
||
| same_elements = [get_last_letter(real_y_pred[i]) == real_y_true[i] for i in range(len(real_y_pred))] | ||
| global_acc = sum(same_elements) / len(same_elements) | ||
|
|
||
| province_acc = {} | ||
| for i in range(len(real_y_pred)): | ||
| province = real_locations[i] | ||
| if province not in province_acc: | ||
| province_acc[province] = {"correct": 0, "total": 0} | ||
|
|
||
| province_acc[province]["total"] += 1 | ||
| if real_y_pred[i] == real_y_true[i]: | ||
| province_acc[province]["correct"] += 1 | ||
|
|
||
| print("\n=== Accuracy Statistics ===") | ||
| print(f"Global Accuracy: {global_acc:.4f}") | ||
| print("\nProvince Accuracies:") | ||
| for province, stats in province_acc.items(): | ||
| acc = stats["correct"] / stats["total"] | ||
| print(f"{province}: {acc:.4f} ({stats['correct']}/{stats['total']})") | ||
|
|
||
| import json | ||
| from datetime import datetime | ||
|
|
||
| results = { | ||
| "global_accuracy": global_acc, | ||
| "province_accuracies": { | ||
| province: { | ||
| "accuracy": stats["correct"] / stats["total"], | ||
| "correct": stats["correct"], | ||
| "total": stats["total"] | ||
| } | ||
| for province, stats in province_acc.items() | ||
| }, | ||
| "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S") | ||
| } | ||
|
|
||
| with open("accuracy_results_other.json", "w", encoding="utf-8") as f: | ||
| json.dump(results, f, ensure_ascii=False, indent=4) | ||
|
|
||
| return global_acc No newline at end of file | ||
|
|
There was a problem hiding this comment.
Critical Issue: Broken Refactoring & Syntax Error
It appears that this pull request was committed in an incomplete state. The changes introduce a syntax error (IndentationError) and do not implement the refactoring described in the PR description:
- Syntax Error: Line 181 contains an empty
ifblock (if real_y_pred[i] == real_y_true[i]:) with no indented body, which will cause Python to fail with anIndentationErrorimmediately upon import. - Missing Helper Function: The private helper function
_compute_accmentioned in the PR description is not defined anywhere in the file. - Incomplete Refactoring:
acc_modelandacc_globalare completely untouched and still contain the duplicated logic. - Deleted Code:
acc_otherhas been completely deleted without any replacement.
To achieve the desired refactoring and fix the syntax error, please replace the entire content of examples/government_rag/singletask_learning_bench/testenv/acc.py with the following clean, fully refactored implementation:
# Copyright 2022 The KubeEdge Authors.
#
# 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.
import json
from datetime import datetime
from sedna.common.class_factory import ClassType, ClassFactory
__all__ = ["acc"]
def get_last_letter(input_string):
if not input_string or not any(char.isalpha() for char in input_string):
return None
for char in reversed(input_string):
if 'A' <= char <= 'D':
return char
return None
def _compute_acc(y_pred, source_filter, output_file):
original_preds = y_pred.copy()
y_pred_split = [pred.split("||")[0] for pred in original_preds]
y_true_split = [pred.split("||")[1] for pred in original_preds]
y_location = [pred.split("||")[2] for pred in original_preds]
y_source = [pred.split("||")[3] for pred in original_preds]
real_y_pred = []
real_y_true = []
real_locations = []
for i in range(len(y_pred_split)):
if y_source[i] == source_filter:
real_y_pred.append(get_last_letter(y_pred_split[i]))
real_y_true.append(y_true_split[i])
real_locations.append(y_location[i])
if not real_y_pred:
print(f"No samples found for source filter: {source_filter}")
return 0.0
same_elements = [real_y_pred[i] == real_y_true[i] for i in range(len(real_y_pred))]
global_acc = sum(same_elements) / len(same_elements)
province_acc = {}
for i in range(len(real_y_pred)):
province = real_locations[i]
if province not in province_acc:
province_acc[province] = {"correct": 0, "total": 0}
province_acc[province]["total"] += 1
if real_y_pred[i] == real_y_true[i]:
province_acc[province]["correct"] += 1
print("\n=== Accuracy Statistics ===")
print(f"Global Accuracy: {global_acc:.4f}")
print("\nProvince Accuracies:")
for province, stats in province_acc.items():
acc = stats["correct"] / stats["total"]
print(f"{province}: {acc:.4f} ({stats['correct']}/{stats['total']})")
results = {
"global_accuracy": global_acc,
"province_accuracies": {
province: {
"accuracy": stats["correct"] / stats["total"],
"correct": stats["correct"],
"total": stats["total"]
}
for province, stats in province_acc.items()
},
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
with open(output_file, "w", encoding="utf-8") as f:
json.dump(results, f, ensure_ascii=False, indent=4)
return global_acc
@ClassFactory.register(ClassType.GENERAL, alias="acc_model")
def acc_model(y_true, y_pred):
return _compute_acc(y_pred, "[model]", "accuracy_results_model.json")
@ClassFactory.register(ClassType.GENERAL, alias="acc_global")
def acc_global(y_true, y_pred):
return _compute_acc(y_pred, "[global]", "accuracy_results_global.json")
@ClassFactory.register(ClassType.GENERAL, alias="acc_local")
def acc_local(y_true, y_pred):
return _compute_acc(y_pred, "[local]", "accuracy_results_local.json")
@ClassFactory.register(ClassType.GENERAL, alias="acc_other")
def acc_other(y_true, y_pred):
return _compute_acc(y_pred, "[other]", "accuracy_results_other.json") province_acc[province]["correct"] += 1
print("\n=== Accuracy Statistics ===")
print(f"Global Accuracy: {global_acc:.4f}")
print("\nProvince Accuracies:")
for province, stats in province_acc.items():
acc = stats["correct"] / stats["total"]
print(f"{province}: {acc:.4f} ({stats['correct']}/{stats['total']})")
import json
from datetime import datetime
results = {
"global_accuracy": global_acc,
"province_accuracies": {
province: {
"accuracy": stats["correct"] / stats["total"],
"correct": stats["correct"],
"total": stats["total"]
}
for province, stats in province_acc.items()
},
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
with open("accuracy_results_local.json", "w", encoding="utf-8") as f:
json.dump(results, f, ensure_ascii=False, indent=4)
return global_acc
@ClassFactory.register(ClassType.GENERAL, alias="acc_other")
def acc_other(y_true, y_pred):
original_preds = y_pred.copy()
y_pred = [pred.split("||")[0] for pred in original_preds]
y_true = [pred.split("||")[1] for pred in original_preds]
y_location = [pred.split("||")[2] for pred in original_preds]
y_source = [pred.split("||")[3] for pred in original_preds]
real_y_pred = []
real_y_true = []
real_locations = []
for i in range(len(y_pred)):
if y_source[i] == "[other]":
real_y_pred.append(get_last_letter(y_pred[i]))
real_y_true.append(y_true[i])
real_locations.append(y_location[i])
same_elements = [get_last_letter(real_y_pred[i]) == real_y_true[i] for i in range(len(real_y_pred))]
global_acc = sum(same_elements) / len(same_elements)
province_acc = {}
for i in range(len(real_y_pred)):
province = real_locations[i]
if province not in province_acc:
province_acc[province] = {"correct": 0, "total": 0}
province_acc[province]["total"] += 1
if real_y_pred[i] == real_y_true[i]:
province_acc[province]["correct"] += 1
print("\n=== Accuracy Statistics ===")
print(f"Global Accuracy: {global_acc:.4f}")
print("\nProvince Accuracies:")
for province, stats in province_acc.items():
acc = stats["correct"] / stats["total"]
print(f"{province}: {acc:.4f} ({stats['correct']}/{stats['total']})")
import json
from datetime import datetime
results = {
"global_accuracy": global_acc,
"province_accuracies": {
province: {
"accuracy": stats["correct"] / stats["total"],
"correct": stats["correct"],
"total": stats["total"]
}
for province, stats in province_acc.items()
},
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
with open("accuracy_results_other.json", "w", encoding="utf-8") as f:
json.dump(results, f, ensure_ascii=False, indent=4)
return global_acc…tenv Signed-off-by: Parv Agrawal <agrawalparv13@gmail.com>
What type of PR is this?
/kind cleanup
What this PR does / why we need it:
acc.pyin government_rag testenv contained 4 nearly identical functions(acc_model, acc_global, acc_local, acc_other) with ~150 lines of
duplicated logic. The only difference between them was the source filter
string and output filename.
Refactored into a single private
_compute_acc()helper function calledby all four registered metrics. Behavior is identical, but code is reduced
from ~150 lines to ~55 lines (-63%).
Changes:
_compute_acc(y_pred, source_filter, output_file)Which issue(s) this PR fixes:
Related to #563