{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Step 2: Create Benchmarks from Assets\n", "\n", "This notebook generates document splitting benchmarks using all strategies.\n", "\n", "**Prerequisites**: Assets created from notebook 01\n", "\n", "**Output**: Benchmark datasets with ground truth for train/test/validation splits" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "sys.path.append('../src/benchmarks')\n", "\n", "from services.asset_loader import AssetLoader\n", "from services.split_manager import SplitManager\n", "from services.benchmark_generator import BenchmarkGenerator\n", "from services.benchmark_writer import BenchmarkWriter\n", "from services.shuffle_strategies import get_strategy, STRATEGIES" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Configuration" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ASSETS_PATH = '../data/assets'\n", "OUTPUT_PATH = '../data/benchmarks'\n", "SPLIT_MAPPING_PATH = '../data/metadata/split_mapping.json'\n", "\n", "# Number of spliced documents per split\n", "NUM_TRAIN = 800\n", "NUM_TEST = 200\n", "NUM_VAL = 500\n", "\n", "# Size: small (5-20 pages) or large (20-500 pages)\n", "SIZE = 'small'\n", "MIN_PAGES = 5 if SIZE == 'small' else 20\n", "MAX_PAGES = 20 if SIZE == 'small' else 500\n", "\n", "RANDOM_SEED = 42\n", "\n", "print(f\"Configuration:\")\n", "print(f\" Size: {SIZE} ({MIN_PAGES}-{MAX_PAGES} pages)\")\n", "print(f\" Train: {NUM_TRAIN}, Test: {NUM_TEST}, Val: {NUM_VAL}\")\n", "print(f\" Strategies: {list(STRATEGIES.keys())}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Assets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "loader = AssetLoader(assets_path=ASSETS_PATH)\n", "documents_by_type = loader.load_all_documents()\n", "\n", "print(f\"Loaded {loader.total_documents} documents across {len(documents_by_type)} types\")\n", "for doc_type, docs in documents_by_type.items():\n", " print(f\" {doc_type}: {len(docs)} documents\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Train/Test/Validation Split" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "split_manager = SplitManager(random_seed=RANDOM_SEED)\n", "splits = split_manager.create_split(documents_by_type)\n", "\n", "# Save split mapping\n", "split_manager.save_split(splits, SPLIT_MAPPING_PATH)\n", "\n", "print(f\"\\nSplit statistics:\")\n", "for split_name in ['train', 'test', 'validation']:\n", " total = sum(len(docs) for docs in splits[split_name].values())\n", " print(f\" {split_name}: {total} documents\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate All Benchmarks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "all_strategies = ['mono_seq', 'mono_rand', 'poly_seq', 'poly_int', 'poly_rand']\n", "\n", "for strategy_name in all_strategies:\n", " print(f\"\\n{'='*60}\")\n", " print(f\"Processing strategy: {strategy_name}\")\n", " print(f\"{'='*60}\")\n", " \n", " # Initialize strategy\n", " strategy = get_strategy(\n", " strategy_name,\n", " min_pages=MIN_PAGES,\n", " max_pages=MAX_PAGES,\n", " random_seed=RANDOM_SEED\n", " )\n", " \n", " generator = BenchmarkGenerator(strategy=strategy)\n", " output_path = f'{OUTPUT_PATH}/{strategy_name}/{SIZE}'\n", " writer = BenchmarkWriter(output_base_path=output_path, assets_path=ASSETS_PATH)\n", " \n", " # Generate train\n", " print(f\"Generating train...\")\n", " train_benchmark = generator.generate_for_split(\n", " documents_by_type=documents_by_type,\n", " doc_names_for_split=splits['train'],\n", " num_spliced_docs=NUM_TRAIN,\n", " split_name='train',\n", " benchmark_name=strategy_name\n", " )\n", " writer.save_benchmark_set(train_benchmark, 'train')\n", " \n", " # Generate test\n", " print(f\"Generating test...\")\n", " test_benchmark = generator.generate_for_split(\n", " documents_by_type=documents_by_type,\n", " doc_names_for_split=splits['test'],\n", " num_spliced_docs=NUM_TEST,\n", " split_name='test',\n", " benchmark_name=strategy_name\n", " )\n", " writer.save_benchmark_set(test_benchmark, 'test')\n", " \n", " # Generate validation\n", " print(f\"Generating validation...\")\n", " val_benchmark = generator.generate_for_split(\n", " documents_by_type=documents_by_type,\n", " doc_names_for_split=splits['validation'],\n", " num_spliced_docs=NUM_VAL,\n", " split_name='validation',\n", " benchmark_name=strategy_name\n", " )\n", " writer.save_benchmark_set(val_benchmark, 'validation')\n", " \n", " print(f\"✅ Completed {strategy_name}\")\n", "\n", "print(f\"\\n{'='*60}\")\n", "print(f\"✅ All benchmarks generated!\")\n", "print(f\"{'='*60}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"\\n✅ Benchmark creation complete!\")\n", "print(f\"\\nOutput structure:\")\n", "for strategy_name in all_strategies:\n", " print(f\"\\n{OUTPUT_PATH}/{strategy_name}/{SIZE}/\")\n", " print(f\" ├── train.csv ({NUM_TRAIN} documents)\")\n", " print(f\" ├── test.csv ({NUM_TEST} documents)\")\n", " print(f\" ├── validation.csv ({NUM_VAL} documents)\")\n", " print(f\" └── ground_truth_json/\")\n", " print(f\" ├── train/ (JSON per document)\")\n", " print(f\" ├── test/ (JSON per document)\")\n", " print(f\" └── validation/ (JSON per document)\")\n", "print(f\"\\nSplit mapping: {SPLIT_MAPPING_PATH}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.8.0" } }, "nbformat": 4, "nbformat_minor": 4 }