Files
doczyai-pipelines/fieldExtraction/scripts/compare_output.py
T
Alex Galarce 5b88185611 Merged in refactor/daip2-9-basic-code-refactor (pull request #337)
[daip2-9] basic code refactor - unreferenced functions and reorganize existing files

* remove unreferenced functions in ac_funcs.py

* move compare_output.py

* move file_counting.py

* move merge_issue_regex.py

* remove unreferenced functions in postprocessing_funcs.py

* clean up preprocessing_funcs.py

* move prerun.py

* move table_analysis.py and textract_template.py to scripts

* remove unreferenced functions in table_funcs.py

* moved tin_pull.py to scripts

* move detect_complex.py and remove merge_funcs.py

* clean up dict operations and utils, mark functions in utils for destinations

* removed errant import


Approved-by: Katon Minhas
2024-12-30 21:41:21 +00:00

50 lines
1.5 KiB
Python

import pandas as pd
import os
def read_and_compare_csvs(csv_file1, csv_file2):
# Read CSV files into pandas DataFrames
df1 = pd.read_csv(csv_file1)
df2 = pd.read_csv(csv_file2)
df2 = df2[df1.columns]
# Check for any differences
diff = (df1 != df2) & df1.notnull() & df2.notnull()
# Create a writer for Excel
writer = pd.ExcelWriter("data/differences.xlsx", engine="xlsxwriter")
df2.to_excel(writer, sheet_name="Differences", index=False)
# Get the xlsxwriter workbook and worksheet objects
workbook = writer.book
worksheet = writer.sheets["Differences"]
# Define a format for changed cells
highlight_fmt = workbook.add_format({"font_color": "red", "bg_color": "yellow"})
# Apply formatting to the changed cells
for col in df1.columns:
col_idx = (
df1.columns.get_loc(col) + 1
) # Excel index starts from 1, and there is an index column
for row in diff.index[diff[col]]:
value = df2.at[row, col]
if pd.isna(value):
value = (
"NaN" # Change this if you want a different representation for NaN
)
worksheet.write(row + 1, col_idx, value, highlight_fmt)
# Close the Pandas Excel writer and output the Excel file
writer.close()
# Example usage
base_path = "output/57-2883440-Cordia Anderson-Hopkins LCSW-ICMProviderAgreement_221673"
read_and_compare_csvs(
os.path.join(base_path, "combined_results_post_processed.csv"),
os.path.join(base_path, "test_3.5.csv"),
)