|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "7c126ceb-e4a6-47d8-8a83-08bf9159d09f", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "# Wilcoxon Testing to check if the agreement scores before and after (imprrovement of scores) is significant or not\n", |
| 11 | + "\n", |
| 12 | + "\"\"\"\n", |
| 13 | + "wilcoxon_test.py\n", |
| 14 | + "\n", |
| 15 | + "This script performs Wilcoxon signed-rank tests to compare\n", |
| 16 | + "regional RXAI disagreement scores (segmented) against\n", |
| 17 | + "global disagreement scores (non-segmented).\n", |
| 18 | + "\n", |
| 19 | + "Inputs:\n", |
| 20 | + " - regional_article_level_fa_batch{n}.csv\n", |
| 21 | + " - global_fa_article_level_scores_batch{n}.csv\n", |
| 22 | + "\n", |
| 23 | + "Outputs:\n", |
| 24 | + " - wilcoxon_all_batches_k234.csv with p-values\n", |
| 25 | + "\"\"\"\n", |
| 26 | + "\n", |
| 27 | + "\n", |
| 28 | + "\n", |
| 29 | + "# Perform Wilcoxon signed-rank tests to compare RXAI (regional) vs Global disagreement scores.\n", |
| 30 | + "\n", |
| 31 | + "# Note: This script applies only to top-k based metrics\n", |
| 32 | + "# (Feature Agreement, Rank Agreement, Semantic Alignment Score).\n", |
| 33 | + "# It is not intended for relative ranking metrics such as\n", |
| 34 | + "# Spearman correlation or Pairwise Rank Agreement.\n", |
| 35 | + "\n", |
| 36 | + "\n", |
| 37 | + "import pandas as pd\n", |
| 38 | + "from scipy.stats import wilcoxon\n", |
| 39 | + "\n", |
| 40 | + "# Configuration\n", |
| 41 | + "method_pairs = [\n", |
| 42 | + " 'attention_vs_deeplift',\n", |
| 43 | + " 'attention_vs_lime',\n", |
| 44 | + " 'attention_vs_gradient_shap',\n", |
| 45 | + " 'deeplift_vs_lime',\n", |
| 46 | + " 'deeplift_vs_gradient_shap',\n", |
| 47 | + " 'lime_vs_gradient_shap'\n", |
| 48 | + "]\n", |
| 49 | + "k_values = [2, 3, 4] # Top-k values\n", |
| 50 | + "total_batches = 11 # Number of CSV batches\n", |
| 51 | + "\n", |
| 52 | + "all_batch_results = [] # Collect all test results\n", |
| 53 | + "\n", |
| 54 | + "for batch in range(1, total_batches + 1):\n", |
| 55 | + " print(f\"\\n Processing Batch {batch}...\")\n", |
| 56 | + "\n", |
| 57 | + " try:\n", |
| 58 | + " # File paths\n", |
| 59 | + " regional_path = f\"regional_article_level_fa_batch{batch}.csv\"\n", |
| 60 | + " global_path = f\"global_fa_article_level_scores_batch{batch}.csv\"\n", |
| 61 | + "\n", |
| 62 | + " # Load CSVs\n", |
| 63 | + " regional_df = pd.read_csv(regional_path)\n", |
| 64 | + " global_df = pd.read_csv(global_path)\n", |
| 65 | + " except Exception as e:\n", |
| 66 | + " print(f\" Failed to load batch {batch}: {e}\")\n", |
| 67 | + " continue\n", |
| 68 | + "\n", |
| 69 | + " # Compare each method pair at each k\n", |
| 70 | + " for pair in method_pairs:\n", |
| 71 | + " for k in k_values:\n", |
| 72 | + " print(f\" Method: {pair}, k={k}\")\n", |
| 73 | + "\n", |
| 74 | + " # Regional FA column (article-level scores with segmentation)\n", |
| 75 | + " if pair not in regional_df.columns:\n", |
| 76 | + " print(f\" Column '{pair}' not in regional batch {batch}\")\n", |
| 77 | + " continue\n", |
| 78 | + " regional_k = regional_df[regional_df['k'] == k][pair]\n", |
| 79 | + "\n", |
| 80 | + " # Global FA column (article-level scores without segmentation)\n", |
| 81 | + " global_col_name = f\"{pair}_k{k}\"\n", |
| 82 | + " if global_col_name not in global_df.columns:\n", |
| 83 | + " print(f\" Column '{global_col_name}' not in global batch {batch}\")\n", |
| 84 | + " continue\n", |
| 85 | + " global_k = global_df[global_col_name]\n", |
| 86 | + "\n", |
| 87 | + " # Align lengths\n", |
| 88 | + " min_len = min(len(regional_k), len(global_k))\n", |
| 89 | + " if min_len == 0:\n", |
| 90 | + " print(f\" No data to compare for k={k}\")\n", |
| 91 | + " continue\n", |
| 92 | + " regional_k = regional_k.iloc[:min_len]\n", |
| 93 | + " global_k = global_k.iloc[:min_len]\n", |
| 94 | + "\n", |
| 95 | + " # Wilcoxon signed-rank test\n", |
| 96 | + " try:\n", |
| 97 | + " _, p_value = wilcoxon(regional_k, global_k)\n", |
| 98 | + " except ValueError:\n", |
| 99 | + " print(f\" All differences zero, assigning p=1.0\")\n", |
| 100 | + " p_value = 1.0\n", |
| 101 | + "\n", |
| 102 | + " print(f\" p-value: {p_value:.3e}\")\n", |
| 103 | + "\n", |
| 104 | + " # Store results\n", |
| 105 | + " all_batch_results.append({\n", |
| 106 | + " 'batch': batch,\n", |
| 107 | + " 'method_pair': pair,\n", |
| 108 | + " 'k': k,\n", |
| 109 | + " 'p_value': p_value\n", |
| 110 | + " })\n", |
| 111 | + "\n", |
| 112 | + "# Save results across all batches\n", |
| 113 | + "df_results = pd.DataFrame(all_batch_results)\n", |
| 114 | + "df_results.to_csv(\"wilcoxon_all_batches_k234.csv\", index=False)\n", |
| 115 | + "\n", |
| 116 | + "print(\"\\n All batches processed. Results saved to 'wilcoxon_all_batches_k234.csv'.\")\n" |
| 117 | + ] |
| 118 | + } |
| 119 | + ], |
| 120 | + "metadata": { |
| 121 | + "kernelspec": { |
| 122 | + "display_name": "Python 3 (ipykernel)", |
| 123 | + "language": "python", |
| 124 | + "name": "python3" |
| 125 | + }, |
| 126 | + "language_info": { |
| 127 | + "codemirror_mode": { |
| 128 | + "name": "ipython", |
| 129 | + "version": 3 |
| 130 | + }, |
| 131 | + "file_extension": ".py", |
| 132 | + "mimetype": "text/x-python", |
| 133 | + "name": "python", |
| 134 | + "nbconvert_exporter": "python", |
| 135 | + "pygments_lexer": "ipython3", |
| 136 | + "version": "3.10.13" |
| 137 | + } |
| 138 | + }, |
| 139 | + "nbformat": 4, |
| 140 | + "nbformat_minor": 5 |
| 141 | +} |
0 commit comments