File size: 6,934 Bytes
165da3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
{
 "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
}