| import pandas as pd |
| from sklearn.metrics import accuracy_score |
| import argparse |
| import os |
|
|
| def process_tsv_files(parent_dir, output_csv, is_qa=None): |
| results = [] |
| |
| |
| for root, _, files in os.walk(parent_dir): |
| for file in files: |
| if file.endswith(".tsv"): |
| file_path = os.path.join(root, file) |
|
|
| try: |
| df = pd.read_csv(file_path, sep="\t") |
| |
| if is_qa is None: |
| if "gold_label" in df.columns and "prediction" in df.columns: |
| y_gold = df["gold_label"].tolist() |
| y_pred = df["prediction"].tolist() |
| |
| accuracy = accuracy_score(y_gold, y_pred) |
| results.append([file_path, accuracy]) |
| else: |
| print(f"Skipping {file_path}: Required columns not found.") |
|
|
| |
| elif is_qa == "y": |
| for neg_value in df["prediction"].unique(): |
| if neg_value.startswith("not_"): |
| evaluating_label = neg_value.replace("not_", "") |
|
|
| |
| df["gold_label"] = df["gold_label"].apply( |
| lambda x: x if x == evaluating_label else neg_value |
| ) |
|
|
| if "gold_label" in df.columns and "prediction" in df.columns: |
| y_gold = df["gold_label"].tolist() |
| y_pred = df["prediction"].tolist() |
| |
| accuracy = accuracy_score(y_gold, y_pred) |
| results.append([file_path, accuracy]) |
| else: |
| print(f"Skipping {file_path}: Required columns not found.") |
|
|
| except Exception as e: |
| print(f"Error processing {file_path}: {e}") |
| |
| |
| results_df = pd.DataFrame(results, columns=["File Path", "Accuracy"]) |
| results_df.to_csv(output_csv, index=False) |
| print(f"Results saved to {output_csv}") |
|
|
| def main(): |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--parent_dir", type=str, required=True, help="Path to the parent folder") |
| parser.add_argument("--output_csv", type=str, required=True, help="Path to the output CSV file") |
| parser.add_argument("--is_qa", type=str, help="Are we evaluating qa inference?") |
| args = parser.parse_args() |
| |
| process_tsv_files(args.parent_dir, args.output_csv, args.is_qa) |
|
|
| if __name__ == "__main__": |
| main() |