77
88import pandas as pd
99from rich .console import Console
10- from rich .progress import Progress , SpinnerColumn , TextColumn , BarColumn , TaskProgressColumn
10+ from rich .progress import (
11+ Progress ,
12+ SpinnerColumn ,
13+ TextColumn ,
14+ BarColumn ,
15+ TaskProgressColumn ,
16+ )
1117from rich .table import Table
1218from rich import print
1319from dotenv import load_dotenv
2127
2228app = typer .Typer ()
2329
30+
2431@app .command ()
2532def evaluate (
2633 driver : Optional [str ] = typer .Argument (
2734 default = 'pymupdf' ,
2835 help = 'The Parxy driver to evaluate. If omitted defaults to pymupdf.' ,
2936 ),
3037 metrics : Optional [List [str ]] = typer .Option (
31- [" sequence_matcher" ],
38+ [' sequence_matcher' ],
3239 '--metric' ,
3340 '-m' ,
3441 help = 'The metric to evaluate.' ,
@@ -67,38 +74,40 @@ def evaluate(
6774 dir_okay = True ,
6875 ),
6976):
70-
7177 logging .basicConfig (
7278 level = logging .WARNING ,
73- format = " %(asctime)s : %(levelname)s : %(name)s : %(message)s"
79+ format = ' %(asctime)s : %(levelname)s : %(name)s : %(message)s' ,
7480 )
75-
76- metrics_name = [metric .lower ().strip ().replace ("-" , "_" ).replace (" " , "_" )
77- for metric in metrics if get_metric (metric )]
78-
81+
82+ metrics_name = [
83+ metric .lower ().strip ().replace ('-' , '_' ).replace (' ' , '_' )
84+ for metric in metrics
85+ if get_metric (metric )
86+ ]
87+
7988 if all_metrics is True :
8089 metrics_name = get_metrics_name ()
8190
8291 if not os .path .exists (input_folder ):
83- logging .debug (f" The specified input folder [{ input_folder } ] does not exist!" )
92+ logging .debug (f' The specified input folder [{ input_folder } ] does not exist!' )
8493 raise typer .Exit (code = 422 )
8594 if not os .path .exists (golden_folder ):
86- logging .debug (f" The specified golden folder [{ golden_folder } ] does not exist!" )
95+ logging .debug (f' The specified golden folder [{ golden_folder } ] does not exist!' )
8796 raise typer .Exit (code = 422 )
8897 if not os .path .exists (output_folder ):
8998 os .makedirs (output_folder )
9099
91100 if len (metrics_name ) == 0 :
92- logging .debug (f" The specified metrics are not implemented!" )
101+ logging .debug (f' The specified metrics are not implemented!' )
93102 raise typer .Exit (code = 422 )
94-
103+
95104 metrics_fn = list ([get_metric (metric ) for metric in metrics_name ])
96105
97106 console = Console ()
98-
99- logging .debug (f" Input folder: { input_folder } " )
100- logging .debug (f" Output folder: { output_folder } " )
101- logging .debug (f" Metrics: { metrics_name } " )
107+
108+ logging .debug (f' Input folder: { input_folder } ' )
109+ logging .debug (f' Output folder: { output_folder } ' )
110+ logging .debug (f' Metrics: { metrics_name } ' )
102111
103112 # Get total number of files to process
104113 files = os .listdir (input_folder )
@@ -107,37 +116,37 @@ def evaluate(
107116 res_list = []
108117 with Progress (
109118 SpinnerColumn (),
110- TextColumn (" [progress.description]{task.description}" ),
119+ TextColumn (' [progress.description]{task.description}' ),
111120 BarColumn (),
112121 TaskProgressColumn (),
113122 console = console ,
114- transient = True
123+ transient = True ,
115124 ) as progress :
116- task = progress .add_task (" Evaluating documents..." , total = total_files )
117-
125+ task = progress .add_task (' Evaluating documents...' , total = total_files )
126+
118127 for filename in files :
119- progress .update (task , description = f" Processing { filename } ..." )
128+ progress .update (task , description = f' Processing { filename } ...' )
120129
121130 # Read the parsing result
122- with open (os .path .join (input_folder , filename ), "r" ) as f :
131+ with open (os .path .join (input_folder , filename ), 'r' ) as f :
123132 doc = Document (** json .loads (f .read ()))
124133
125134 # Read the ground truth
126135 try :
127- with open (os .path .join (golden_folder , filename ), "r" ) as f :
136+ with open (os .path .join (golden_folder , filename ), 'r' ) as f :
128137 golden_doc = Document (** json .loads (f .read ()))
129138 except FileNotFoundError :
130- logging .error (f" File [{ filename } ] does not exist!" )
139+ logging .error (f' File [{ filename } ] does not exist!' )
131140 progress .advance (task )
132141 continue
133142
134143 base_data = {
135- " filename" : filename ,
136- " collection" : golden_doc .source_data [" collection" ],
137- " doc_category" : golden_doc .source_data [" doc_category" ],
138- " original_filename" : golden_doc .source_data [" original_filename" ],
139- " page_no" : golden_doc .source_data [" page_no" ],
140- " processing_time_seconds" : doc .source_data [" processing_time_seconds" ],
144+ ' filename' : filename ,
145+ ' collection' : golden_doc .source_data [' collection' ],
146+ ' doc_category' : golden_doc .source_data [' doc_category' ],
147+ ' original_filename' : golden_doc .source_data [' original_filename' ],
148+ ' page_no' : golden_doc .source_data [' page_no' ],
149+ ' processing_time_seconds' : doc .source_data [' processing_time_seconds' ],
141150 }
142151
143152 # merge all metrics dicts into one
@@ -150,28 +159,31 @@ def evaluate(
150159 res_list .append (row )
151160 progress .advance (task )
152161
153- timestamp_str = str (time .time ()).replace ("." , "" )
162+ timestamp_str = str (time .time ()).replace ('.' , '' )
154163 res_df = pd .DataFrame (res_list )
155- input_folder_name = input_folder .replace (os .sep , "/" ).replace (" \\ " , "/" )
156- input_folder_name = input_folder_name .split ("/" )[- 1 ].replace (" " , "_" ).lower ()
157- output_file = f" eval_{ input_folder_name } _{ timestamp_str } .csv"
164+ input_folder_name = input_folder .replace (os .sep , '/' ).replace (' \\ ' , '/' )
165+ input_folder_name = input_folder_name .split ('/' )[- 1 ].replace (' ' , '_' ).lower ()
166+ output_file = f' eval_{ input_folder_name } _{ timestamp_str } .csv'
158167 output_path = os .path .join (output_folder , output_file )
159168 res_df .to_csv (output_path , index = False )
160169
161- print (f"\n [green]✓[/green] Evaluation completed. Results saved to: [blue]{ output_path } [/blue]" )
162-
170+ print (
171+ f'\n [green]✓[/green] Evaluation completed. Results saved to: [blue]{ output_path } [/blue]'
172+ )
173+
163174 # Print evaluation statistics
164175 table = Table ()
165- table .add_column ("Metric" )
166- table .add_column ("Value" , justify = "right" , style = "green" )
167-
168- table .add_row ("Documents processed" , str (len (res_list )))
169- table .add_row ("Average parsing time" , f"{ res_df ['processing_time_seconds' ].mean ():.2f} s" )
170-
176+ table .add_column ('Metric' )
177+ table .add_column ('Value' , justify = 'right' , style = 'green' )
178+
179+ table .add_row ('Documents processed' , str (len (res_list )))
180+ table .add_row (
181+ 'Average parsing time' , f'{ res_df ["processing_time_seconds" ].mean ():.2f} s'
182+ )
183+
171184 for metric_column in metrics_name :
172185 if metric_column in res_df .columns and not res_df [metric_column ].isna ().all ():
173186 if res_df [metric_column ].dtype in ['float64' , 'int64' ]:
174- table .add_row (metric_column , f"{ res_df [metric_column ].mean ():.4f} " )
175-
176- console .print (table )
187+ table .add_row (metric_column , f'{ res_df [metric_column ].mean ():.4f} ' )
177188
189+ console .print (table )
0 commit comments