#!/usr/bin/env python3 """ Create comparison CSV files showing original vs generated adjustments side-by-side. """ import csv from pathlib import Path OUTPUT_DIR = Path(__file__).parent / "output" COMPARISON_DIR = Path(__file__).parent / "comparisons" COMPARISON_DIR.mkdir(exist_ok=True) def create_comparison(original_file, generated_file, output_file, adjustment_type): """ Create a side-by-side comparison of original and generated adjustments. Args: original_file: Path to original adjustment CSV generated_file: Path to generated adjustment CSV output_file: Path to output comparison CSV adjustment_type: Type of adjustment (for display) """ print(f"\nCreating comparison for {adjustment_type}...") with open(original_file, 'r', encoding='utf-8') as f: original_rows = list(csv.reader(f)) with open(generated_file, 'r', encoding='utf-8') as f: generated_rows = list(csv.reader(f)) if not original_rows or not generated_rows: print(f" WARNING: Empty file(s)") return 0, 0 original_header = original_rows[0] if original_rows else [] generated_header = generated_rows[0] if generated_rows else [] comparison_header = list(generated_header) + list(original_header) + ["AMOUNT_MATCH"] comparison_rows = [comparison_header] max_rows = max(len(original_rows), len(generated_rows)) amount_matches = 0 total_rows = max_rows - 1 try: orig_amount_idx = next(i for i, h in enumerate(original_header) if 'Amount' in h) except StopIteration: orig_amount_idx = -1 try: gen_amount_idx = next(i for i, h in enumerate(generated_header) if 'Amount' in h) except StopIteration: gen_amount_idx = -1 for i in range(1, max_rows): original_row = original_rows[i] if i < len(original_rows) else [] generated_row = generated_rows[i] if i < len(generated_rows) else [] comparison_row = [] comparison_row.extend(generated_row) while len(comparison_row) < len(generated_header): comparison_row.append('') comparison_row.extend(original_row) while len(comparison_row) < len(generated_header) + len(original_header): comparison_row.append('') amount_match = 'N' if orig_amount_idx >= 0 and gen_amount_idx >= 0: orig_amount = original_row[orig_amount_idx] if orig_amount_idx < len(original_row) else '' gen_amount = generated_row[gen_amount_idx] if gen_amount_idx < len(generated_row) else '' orig_val = normalize_value(orig_amount) gen_val = normalize_value(gen_amount) if orig_val == gen_val: amount_match = 'Y' amount_matches += 1 else: try: if isinstance(orig_val, (int, float)) and isinstance(gen_val, (int, float)): diff = gen_val - orig_val amount_match = f'N ({diff:.2f})' except (TypeError, ValueError): pass comparison_row.append(amount_match) comparison_rows.append(comparison_row) with open(output_file, 'w', encoding='utf-8', newline='') as f: writer = csv.writer(f) writer.writerows(comparison_rows) match_percentage = (amount_matches / total_rows * 100) if total_rows > 0 else 0 print(f" Comparison saved to: {output_file}") print(f" Total rows: {total_rows}") print(f" Amount matches: {amount_matches}") print(f" Amount match rate: {match_percentage:.1f}%") return amount_matches, total_rows def normalize_value(value): """Normalize a value for comparison — handles commas, whitespace, numeric conversion.""" if not value or value in ['FALSE', '#N/A', '']: return '' normalized = str(value).replace(',', '').strip() try: return float(normalized) except (ValueError, AttributeError): return normalized.lower() def main(): print("=" * 70) print("CREATING COMPARISON FILES") print("=" * 70) total_all_matches = 0 total_all_rows = 0 types = [ ("cross_recoupment", "Cross Recoupment"), ("transfers", "Transfers"), ("overrides", "Overrides") ] for prefix, display_name in types: original_file = OUTPUT_DIR / f"original_{prefix}_adjustments.csv" generated_file = OUTPUT_DIR / f"generated_{prefix}_adjustments.csv" comparison_file = COMPARISON_DIR / f"comparison_{prefix}.csv" matches, rows = create_comparison( original_file, generated_file, comparison_file, display_name ) total_all_matches += matches total_all_rows += rows print("\n" + "=" * 70) print("COMPARISON COMPLETE!") print("=" * 70) print(f"\nOverall Statistics:") print(f" Total rows compared: {total_all_rows}") print(f" Total amount matches: {total_all_matches}") if total_all_rows > 0: overall_match_pct = (total_all_matches / total_all_rows * 100) print(f" Overall amount match rate: {overall_match_pct:.1f}%") print(f"\nComparison files saved in: {COMPARISON_DIR}") print(f"\nFormat: [Generated columns] | [Original columns] | AMOUNT_MATCH (Y/N)") if __name__ == "__main__": main()