import json import sys from collections import Counter input_filename = 'mtabvqa_query.jsonl' # Changed to mtabqa_query.jsonl output_filename = 'mtabqa_query_filtered.jsonl' removed_filename = 'mtabqa_query_removed_one_table.jsonl' print(f"Starting analysis of '{input_filename}'...") try: with open(input_filename, 'r', encoding='utf-8') as infile, \ open(output_filename, 'w', encoding='utf-8') as outfile, \ open(removed_filename, 'w', encoding='utf-8') as removed_file: # Counters for statistics tables_count = Counter() kept_count = 0 removed_count = 0 error_count = 0 skipped_lines = [] for line_num, line in enumerate(infile, 1): try: # Remove potential leading/trailing whitespace stripped_line = line.strip() if not stripped_line: # Skip empty lines print(f"Warning: Skipping empty line {line_num}.", file=sys.stderr) skipped_lines.append(line_num) continue data = json.loads(stripped_line) # Check if 'table_image_ids' key exists and is a list if 'table_image_ids' in data and isinstance(data.get('table_image_ids'), list): num_tables = len(data['table_image_ids']) # Update our counter tables_count[num_tables] += 1 if num_tables > 1: # Keep this line (write to output file) outfile.write(stripped_line + '\n') kept_count += 1 elif num_tables == 1: # Remove this line (write to removed file) removed_file.write(stripped_line + '\n') removed_count += 1 else: # num_tables == 0 or other unexpected cases print(f"Info: Line {line_num} has zero tables. Skipping.", file=sys.stderr) skipped_lines.append(line_num) else: # Handle cases where 'table_image_ids' key is missing or not a list print(f"Warning: Skipping line {line_num} due to missing or invalid 'table_image_ids' field.", file=sys.stderr) error_count += 1 skipped_lines.append(line_num) except json.JSONDecodeError: print(f"Error: Could not decode JSON on line {line_num}. Skipping.", file=sys.stderr) error_count += 1 skipped_lines.append(line_num) except Exception as e: print(f"An unexpected error occurred processing line {line_num}: {e}", file=sys.stderr) error_count += 1 skipped_lines.append(line_num) # Print distribution of tables per question print("\n--- Table Distribution Statistics ---") print(f"Total questions processed: {sum(tables_count.values())}") for table_count in sorted(tables_count.keys()): count = tables_count[table_count] percentage = (count / sum(tables_count.values())) * 100 print(f"Questions with {table_count} {'tables' if table_count != 1 else 'table'}: {count} ({percentage:.2f}%)") # Additional stats for multi-table questions multi_table_count = sum(tables_count[i] for i in tables_count if i > 1) multi_table_percentage = (multi_table_count / sum(tables_count.values())) * 100 if sum(tables_count.values()) > 0 else 0 print(f"\nQuestions with 2+ tables: {multi_table_count} ({multi_table_percentage:.2f}%)") # Print standard summary print("\n--- Processing Summary ---") print(f"Successfully processed {line_num} lines from '{input_filename}'.") print(f"Kept {kept_count} entries (more than one table) in '{output_filename}'.") print(f"Removed {removed_count} entries (exactly one table) to '{removed_filename}'.") if error_count > 0: print(f"Encountered {error_count} errors (JSON decoding or invalid format).") print(f"Problematic line numbers: {skipped_lines}") print("--------------------------\n") except FileNotFoundError: print(f"Error: Input file '{input_filename}' not found in the current directory.", file=sys.stderr) print("Please make sure the script is in the same directory as the .jsonl file or update the 'input_filename' variable.", file=sys.stderr) except Exception as e: print(f"A critical error occurred during file operations: {e}", file=sys.stderr)