|
|
import json |
|
|
import sys |
|
|
from collections import Counter |
|
|
|
|
|
input_filename = 'mtabvqa_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: |
|
|
|
|
|
|
|
|
tables_count = Counter() |
|
|
kept_count = 0 |
|
|
removed_count = 0 |
|
|
error_count = 0 |
|
|
skipped_lines = [] |
|
|
|
|
|
for line_num, line in enumerate(infile, 1): |
|
|
try: |
|
|
|
|
|
stripped_line = line.strip() |
|
|
if not stripped_line: |
|
|
print(f"Warning: Skipping empty line {line_num}.", file=sys.stderr) |
|
|
skipped_lines.append(line_num) |
|
|
continue |
|
|
|
|
|
data = json.loads(stripped_line) |
|
|
|
|
|
|
|
|
if 'table_image_ids' in data and isinstance(data.get('table_image_ids'), list): |
|
|
num_tables = len(data['table_image_ids']) |
|
|
|
|
|
|
|
|
tables_count[num_tables] += 1 |
|
|
|
|
|
if num_tables > 1: |
|
|
|
|
|
outfile.write(stripped_line + '\n') |
|
|
kept_count += 1 |
|
|
elif num_tables == 1: |
|
|
|
|
|
removed_file.write(stripped_line + '\n') |
|
|
removed_count += 1 |
|
|
else: |
|
|
print(f"Info: Line {line_num} has zero tables. Skipping.", file=sys.stderr) |
|
|
skipped_lines.append(line_num) |
|
|
else: |
|
|
|
|
|
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("\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}%)") |
|
|
|
|
|
|
|
|
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("\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) |
|
|
|