saraNour commited on
Commit
2fd53b5
·
verified ·
1 Parent(s): d2fbfa0

Upload notebooks/phase4_audit_executed.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. notebooks/phase4_audit_executed.ipynb +504 -0
notebooks/phase4_audit_executed.ipynb ADDED
@@ -0,0 +1,504 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Phase 4 — Variant Assignment Audit\n",
8
+ "\n",
9
+ "This notebook performs a complete audit of Phase 4 outputs.\n",
10
+ "\n",
11
+ "**Data sources (local):**\n",
12
+ "- `product_variant_mapping.parquet`\n",
13
+ "- `reference_product_variants.parquet`\n",
14
+ "- `reference_product_variant_summary.parquet`\n",
15
+ "- Phase 3: `product_group_mapping.csv`\n",
16
+ "\n",
17
+ "**Requirements:** `pandas`, `pyarrow`"
18
+ ]
19
+ },
20
+ {
21
+ "cell_type": "code",
22
+ "execution_count": 1,
23
+ "metadata": {
24
+ "execution": {
25
+ "iopub.execute_input": "2026-07-30T18:12:38.402356Z",
26
+ "iopub.status.busy": "2026-07-30T18:12:38.399520Z",
27
+ "iopub.status.idle": "2026-07-30T18:12:39.660727Z",
28
+ "shell.execute_reply": "2026-07-30T18:12:39.656653Z"
29
+ }
30
+ },
31
+ "outputs": [
32
+ {
33
+ "name": "stdout",
34
+ "output_type": "stream",
35
+ "text": [
36
+ "Environment: local\n"
37
+ ]
38
+ }
39
+ ],
40
+ "source": [
41
+ "import pandas as pd\n",
42
+ "import numpy as np\n",
43
+ "import json\n",
44
+ "import os\n",
45
+ "\n",
46
+ "# Detect environment: local or Colab\n",
47
+ "LOCAL = os.path.exists('../../phase4/outputs')\n",
48
+ "\n",
49
+ "if LOCAL:\n",
50
+ " P4_OUT = '../../phase4/outputs'\n",
51
+ " P3_OUT = '../../phase3/outputs'\n",
52
+ "else:\n",
53
+ " # Colab: download from HuggingFace\n",
54
+ " P4_OUT = 'https://huggingface.co/datasets/saraNour/compliments-reference-phase4/resolve/main'\n",
55
+ " P3_OUT = 'https://huggingface.co/datasets/saraNour/compliments-reference-phase3/resolve/main'\n",
56
+ "\n",
57
+ "def load(path, filename):\n",
58
+ " full = f'{path}/{filename}'\n",
59
+ " print(f'Loading {full}...')\n",
60
+ " if filename.endswith('.parquet'):\n",
61
+ " return pd.read_parquet(full)\n",
62
+ " return pd.read_csv(full)\n",
63
+ "\n",
64
+ "print(f'Environment: {\"local\" if LOCAL else \"colab\"}')"
65
+ ]
66
+ },
67
+ {
68
+ "cell_type": "markdown",
69
+ "metadata": {},
70
+ "source": [
71
+ "## 1. Load Data"
72
+ ]
73
+ },
74
+ {
75
+ "cell_type": "code",
76
+ "execution_count": 2,
77
+ "metadata": {
78
+ "execution": {
79
+ "iopub.execute_input": "2026-07-30T18:12:39.761213Z",
80
+ "iopub.status.busy": "2026-07-30T18:12:39.758817Z",
81
+ "iopub.status.idle": "2026-07-30T18:12:40.454808Z",
82
+ "shell.execute_reply": "2026-07-30T18:12:40.448258Z"
83
+ }
84
+ },
85
+ "outputs": [
86
+ {
87
+ "name": "stdout",
88
+ "output_type": "stream",
89
+ "text": [
90
+ "Loading ../../phase4/outputs/product_variant_mapping.parquet...\n"
91
+ ]
92
+ },
93
+ {
94
+ "name": "stdout",
95
+ "output_type": "stream",
96
+ "text": [
97
+ "Loading ../../phase4/outputs/reference_product_variants.parquet...\n",
98
+ "Loading ../../phase4/outputs/reference_product_variant_summary.parquet...\n",
99
+ "Loading ../../phase3/outputs/product_group_mapping.csv...\n"
100
+ ]
101
+ },
102
+ {
103
+ "name": "stdout",
104
+ "output_type": "stream",
105
+ "text": [
106
+ "\n",
107
+ "variant_mapping: 4440 rows, 11 cols\n",
108
+ "variant_table: 4299 rows, 21 cols\n",
109
+ "variant_summary: 3691 rows, 8 cols\n",
110
+ "phase3_mapping: 4440 rows, 14 cols\n"
111
+ ]
112
+ }
113
+ ],
114
+ "source": [
115
+ "variant_mapping = load(P4_OUT, 'product_variant_mapping.parquet')\n",
116
+ "variant_table = load(P4_OUT, 'reference_product_variants.parquet')\n",
117
+ "variant_summary = load(P4_OUT, 'reference_product_variant_summary.parquet')\n",
118
+ "phase3_mapping = load(P3_OUT, 'product_group_mapping.csv')\n",
119
+ "\n",
120
+ "print(f'\\nvariant_mapping: {len(variant_mapping)} rows, {len(variant_mapping.columns)} cols')\n",
121
+ "print(f'variant_table: {len(variant_table)} rows, {len(variant_table.columns)} cols')\n",
122
+ "print(f'variant_summary: {len(variant_summary)} rows, {len(variant_summary.columns)} cols')\n",
123
+ "print(f'phase3_mapping: {len(phase3_mapping)} rows, {len(phase3_mapping.columns)} cols')"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "markdown",
128
+ "metadata": {},
129
+ "source": [
130
+ "## 2. Input Traceability"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "code",
135
+ "execution_count": 3,
136
+ "metadata": {
137
+ "execution": {
138
+ "iopub.execute_input": "2026-07-30T18:12:40.476995Z",
139
+ "iopub.status.busy": "2026-07-30T18:12:40.476325Z",
140
+ "iopub.status.idle": "2026-07-30T18:12:40.664950Z",
141
+ "shell.execute_reply": "2026-07-30T18:12:40.663047Z"
142
+ }
143
+ },
144
+ "outputs": [
145
+ {
146
+ "name": "stdout",
147
+ "output_type": "stream",
148
+ "text": [
149
+ "Phase 3 products: 4440\n",
150
+ "Phase 4 products: 4440\n",
151
+ "Missing from Phase 4: 0\n",
152
+ "Extra in Phase 4: 0\n"
153
+ ]
154
+ },
155
+ {
156
+ "name": "stdout",
157
+ "output_type": "stream",
158
+ "text": [
159
+ "\n",
160
+ "Phase 3 groups: 3691\n",
161
+ "Phase 4 groups: 3691\n",
162
+ "Missing groups: 0\n",
163
+ "\n",
164
+ "Phase 3 rows: 4440\n",
165
+ "Phase 4 rows: 4440\n",
166
+ "Row difference: 0\n"
167
+ ]
168
+ }
169
+ ],
170
+ "source": [
171
+ "phase3_ids = set(phase3_mapping['external_id'].astype(str).unique())\n",
172
+ "phase4_ids = set(variant_mapping['external_id'].astype(str).unique())\n",
173
+ "\n",
174
+ "print(f'Phase 3 products: {len(phase3_ids)}')\n",
175
+ "print(f'Phase 4 products: {len(phase4_ids)}')\n",
176
+ "print(f'Missing from Phase 4: {len(phase3_ids - phase4_ids)}')\n",
177
+ "print(f'Extra in Phase 4: {len(phase4_ids - phase3_ids)}')\n",
178
+ "\n",
179
+ "phase3_groups = set(phase3_mapping['group_id'].astype(str).unique())\n",
180
+ "phase4_groups = set(variant_mapping['group_id'].astype(str).unique())\n",
181
+ "print(f'\\nPhase 3 groups: {len(phase3_groups)}')\n",
182
+ "print(f'Phase 4 groups: {len(phase4_groups)}')\n",
183
+ "print(f'Missing groups: {len(phase3_groups - phase4_groups)}')\n",
184
+ "\n",
185
+ "print(f'\\nPhase 3 rows: {len(phase3_mapping)}')\n",
186
+ "print(f'Phase 4 rows: {len(variant_mapping)}')\n",
187
+ "print(f'Row difference: {len(variant_mapping) - len(phase3_mapping)}')"
188
+ ]
189
+ },
190
+ {
191
+ "cell_type": "markdown",
192
+ "metadata": {},
193
+ "source": [
194
+ "## 3. Integrity Checks"
195
+ ]
196
+ },
197
+ {
198
+ "cell_type": "code",
199
+ "execution_count": 4,
200
+ "metadata": {
201
+ "execution": {
202
+ "iopub.execute_input": "2026-07-30T18:12:40.686030Z",
203
+ "iopub.status.busy": "2026-07-30T18:12:40.685413Z",
204
+ "iopub.status.idle": "2026-07-30T18:12:40.817377Z",
205
+ "shell.execute_reply": "2026-07-30T18:12:40.814780Z"
206
+ }
207
+ },
208
+ "outputs": [
209
+ {
210
+ "name": "stdout",
211
+ "output_type": "stream",
212
+ "text": [
213
+ "Check 1: Every product has exactly one variant: True\n",
214
+ "Check 2: Every variant belongs to one group: True\n"
215
+ ]
216
+ },
217
+ {
218
+ "name": "stdout",
219
+ "output_type": "stream",
220
+ "text": [
221
+ "Check 3: No orphan variants (0): True\n",
222
+ "Check 4: No duplicate assignments (0): True\n",
223
+ "Check 5: Summary matches mapping: True\n",
224
+ "Check 6: All products have IDs: True\n",
225
+ "\n",
226
+ "=== OVERALL: PASS ===\n"
227
+ ]
228
+ }
229
+ ],
230
+ "source": [
231
+ "products_with_variants = variant_mapping.groupby('external_id')['variant_id'].nunique()\n",
232
+ "check1 = products_with_variants.max() == 1\n",
233
+ "print(f'Check 1: Every product has exactly one variant: {check1}')\n",
234
+ "\n",
235
+ "variant_to_group = variant_mapping.groupby('variant_id')['group_id'].nunique()\n",
236
+ "check2 = variant_to_group.max() == 1\n",
237
+ "print(f'Check 2: Every variant belongs to one group: {check2}')\n",
238
+ "\n",
239
+ "variant_ids_in_mapping = set(variant_mapping['variant_id'].unique())\n",
240
+ "variant_ids_in_table = set(variant_table['variant_id'].unique())\n",
241
+ "orphans = len(variant_ids_in_table - variant_ids_in_mapping)\n",
242
+ "check3 = orphans == 0\n",
243
+ "print(f'Check 3: No orphan variants ({orphans}): {check3}')\n",
244
+ "\n",
245
+ "dupes = (products_with_variants > 1).sum()\n",
246
+ "check4 = dupes == 0\n",
247
+ "print(f'Check 4: No duplicate assignments ({dupes}): {check4}')\n",
248
+ "\n",
249
+ "check5 = len(variant_summary) == variant_mapping['group_id'].nunique()\n",
250
+ "print(f'Check 5: Summary matches mapping: {check5}')\n",
251
+ "\n",
252
+ "null_groups = variant_mapping['group_id'].isna().sum()\n",
253
+ "null_variants = variant_mapping['variant_id'].isna().sum()\n",
254
+ "check6 = null_groups == 0 and null_variants == 0\n",
255
+ "print(f'Check 6: All products have IDs: {check6}')\n",
256
+ "\n",
257
+ "all_pass = all([check1, check2, check3, check4, check5, check6])\n",
258
+ "print(f'\\n=== OVERALL: {\"PASS\" if all_pass else \"FAIL\"} ===')"
259
+ ]
260
+ },
261
+ {
262
+ "cell_type": "markdown",
263
+ "metadata": {},
264
+ "source": [
265
+ "## 4. Variant Statistics"
266
+ ]
267
+ },
268
+ {
269
+ "cell_type": "code",
270
+ "execution_count": 5,
271
+ "metadata": {
272
+ "execution": {
273
+ "iopub.execute_input": "2026-07-30T18:12:40.823021Z",
274
+ "iopub.status.busy": "2026-07-30T18:12:40.821543Z",
275
+ "iopub.status.idle": "2026-07-30T18:12:40.869046Z",
276
+ "shell.execute_reply": "2026-07-30T18:12:40.865073Z"
277
+ }
278
+ },
279
+ "outputs": [
280
+ {
281
+ "name": "stdout",
282
+ "output_type": "stream",
283
+ "text": [
284
+ "Total products: 4440\n",
285
+ "Total unique groups: 3691\n",
286
+ "Total unique variants: 4299\n",
287
+ "\n",
288
+ "Variants per group: mean=1.16, max=10\n",
289
+ " 1 variant: 3235 (87.6%)\n",
290
+ " 2 variant: 357 (9.7%)\n",
291
+ " 3 variant: 67 (1.8%)\n",
292
+ " 4+ variants: 32 (0.9%)\n"
293
+ ]
294
+ },
295
+ {
296
+ "name": "stdout",
297
+ "output_type": "stream",
298
+ "text": [
299
+ "\n",
300
+ "Products per variant: mean=1.03, max=4\n",
301
+ " 1 product: 4168 (97.0%)\n",
302
+ " 2+ products: 131 (3.0%)\n"
303
+ ]
304
+ }
305
+ ],
306
+ "source": [
307
+ "print(f'Total products: {len(variant_mapping)}')\n",
308
+ "print(f'Total unique groups: {variant_mapping[\"group_id\"].nunique()}')\n",
309
+ "print(f'Total unique variants: {variant_mapping[\"variant_id\"].nunique()}')\n",
310
+ "\n",
311
+ "variants_per_group = variant_mapping.groupby('group_id')['variant_id'].nunique()\n",
312
+ "print(f'\\nVariants per group: mean={variants_per_group.mean():.2f}, max={variants_per_group.max()}')\n",
313
+ "for n in [1, 2, 3]:\n",
314
+ " c = (variants_per_group == n).sum()\n",
315
+ " print(f' {n} variant: {c} ({c/len(variants_per_group)*100:.1f}%)')\n",
316
+ "c = (variants_per_group >= 4).sum()\n",
317
+ "print(f' 4+ variants: {c} ({c/len(variants_per_group)*100:.1f}%)')\n",
318
+ "\n",
319
+ "products_per_variant = variant_mapping.groupby('variant_id')['external_id'].nunique()\n",
320
+ "print(f'\\nProducts per variant: mean={products_per_variant.mean():.2f}, max={products_per_variant.max()}')\n",
321
+ "c1 = (products_per_variant == 1).sum()\n",
322
+ "c2 = (products_per_variant > 1).sum()\n",
323
+ "print(f' 1 product: {c1} ({c1/len(products_per_variant)*100:.1f}%)')\n",
324
+ "print(f' 2+ products: {c2} ({c2/len(products_per_variant)*100:.1f}%)')"
325
+ ]
326
+ },
327
+ {
328
+ "cell_type": "markdown",
329
+ "metadata": {},
330
+ "source": [
331
+ "## 5. Peanut Butter Example"
332
+ ]
333
+ },
334
+ {
335
+ "cell_type": "code",
336
+ "execution_count": 6,
337
+ "metadata": {
338
+ "execution": {
339
+ "iopub.execute_input": "2026-07-30T18:12:40.876028Z",
340
+ "iopub.status.busy": "2026-07-30T18:12:40.874281Z",
341
+ "iopub.status.idle": "2026-07-30T18:12:41.062392Z",
342
+ "shell.execute_reply": "2026-07-30T18:12:41.059654Z"
343
+ }
344
+ },
345
+ "outputs": [
346
+ {
347
+ "name": "stdout",
348
+ "output_type": "stream",
349
+ "text": [
350
+ "Peanut Butter: 29 products, 21 groups, 28 variants\n",
351
+ "\n",
352
+ "Group 03e904ad... | Milk Chocolate Peanut Butter Bites | 1 products | 1 variants\n",
353
+ " Milk Chocolate Peanut Butter Bites | size=340g | variant_key=amt340.0|unitg\n",
354
+ "Group 0da73841... | Snack Peanut Butter Wafer Rolls Dippers Raspberry | 1 products | 1 variants\n",
355
+ " Snack Peanut Butter Wafer Rolls Dippers Raspberry | size=240g | variant_key=amt240.0|unitg\n",
356
+ "Group 104375ed... | Peanut Butter With Honey | 1 products | 1 variants\n",
357
+ " Peanut Butter With Honey | size=500g | variant_key=amt500.0|unitg\n",
358
+ "Group 1375b508... | Ice Cream Chocolate Peanut Butter | 1 products | 1 variants\n",
359
+ " Ice Cream Chocolate Peanut Butter | size=1.5L | variant_key=amt1.5|unitl\n"
360
+ ]
361
+ },
362
+ {
363
+ "name": "stdout",
364
+ "output_type": "stream",
365
+ "text": [
366
+ "Group 1845cac0... | Filled Pretzels Peanut Butter | 1 products | 1 variants\n",
367
+ " Filled Pretzels Peanut Butter | size=680g | variant_key=amt680.0|unitg\n",
368
+ "Group 1ce8dc8f... | S'mores Kit Peanut Butter | 1 products | 1 variants\n",
369
+ " S'mores Kit Peanut Butter | size=446g | variant_key=amt446.0|unitg\n",
370
+ "Group 1d7958d7... | Snack Peanut Butter Wafer Rolls With Dipper Strawberry | 1 products | 1 variants\n",
371
+ " Snack Peanut Butter Wafer Rolls With Dipper Strawberry | size=240g | variant_key=amt240.0|unitg\n",
372
+ "Group 35e31165... | Dipper Sticks Peanut Butter Chocolate | 1 products | 1 variants\n",
373
+ " Dipper Sticks Peanut Butter Chocolate | size=240g | variant_key=amt240.0|unitg\n",
374
+ "Group 3822ff9a... | Compliments Organic Peanut Butter Crunchy | 1 products | 1 variants\n",
375
+ " Compliments Organic Peanut Butter Crunchy | size=500g | variant_key=amt500.0|unitg\n",
376
+ "Group 5bfec681... | Light Smooth Peanut Butter | 3 products | 3 variants\n",
377
+ " Light Smooth Peanut Butter | size=2kg | variant_key=amt2.0|unitkg\n",
378
+ " Smooth Light Peanut Butter | size=1kg | variant_key=amt1.0|unitkg\n",
379
+ " Smooth Light Peanut Butter | size=500g | variant_key=amt500.0|unitg\n",
380
+ "Group 805b971b... | Smooth Peanut Butter Dark Roast | 1 products | 1 variants\n",
381
+ " Smooth Peanut Butter Dark Roast | size=1kg | variant_key=amt1.0|unitkg\n",
382
+ "Group 97ad569d... | Compliments Peanut Butter Crunchy 100% Natural | 1 products | 1 variants\n",
383
+ " Compliments Peanut Butter Crunchy 100% Natural | size=500g | variant_key=amt500.0|unitg\n",
384
+ "Group b09ff83f... | Smooth Peanut Butter | 3 products | 3 variants\n",
385
+ " Smooth Peanut Butter | size=500g | variant_key=amt500.0|unitg\n",
386
+ " Peanut Butter Smooth | size=2kg | variant_key=amt2.0|unitkg\n",
387
+ " Smooth Peanut Butter | size=1kg | variant_key=amt1.0|unitkg\n"
388
+ ]
389
+ },
390
+ {
391
+ "name": "stdout",
392
+ "output_type": "stream",
393
+ "text": [
394
+ "Group bc4d1309... | Peanut Butter Crunchy | 3 products | 3 variants\n",
395
+ " Peanut Butter Crunchy | size=2kg | variant_key=amt2.0|unitkg\n",
396
+ " Peanut Butter Crunchy | size=500g | variant_key=amt500.0|unitg\n",
397
+ " Peanut Butter Crunchy | size=1kg | variant_key=amt1.0|unitkg\n",
398
+ "Group d36e44e9... | Dipped Granola Bars Peanut Butter | 1 products | 1 variants\n",
399
+ " Dipped Granola Bars Peanut Butter | size=156g | variant_key=amt156.0|unitg\n",
400
+ "Group d9eb5924... | Baking Chips Peanut Butter | 1 products | 1 variants\n",
401
+ " Baking Chips Peanut Butter | size=225g | variant_key=amt225.0|unitg\n",
402
+ "Group e78cdee5... | Compliments Organic Peanut Butter Smooth | 1 products | 1 variants\n",
403
+ " Compliments Organic Peanut Butter Smooth | size=500g | variant_key=amt500.0|unitg\n",
404
+ "Group e9bc48d6... | Compliments 100% Natural Smooth Peanut Butter | 2 products | 2 variants\n"
405
+ ]
406
+ },
407
+ {
408
+ "name": "stdout",
409
+ "output_type": "stream",
410
+ "text": [
411
+ " Compliments 100% Natural Smooth Peanut Butter | size=1kg | variant_key=amt1.0|unitkg\n",
412
+ " Compliments 100% Natural Smooth Peanut Butter | size=500g | variant_key=amt500.0|unitg\n",
413
+ "Group f1c7a47b... | Peanut Butter Spread Cinnamon Sugar | 1 products | 1 variants\n",
414
+ " Peanut Butter Spread Cinnamon Sugar | size=500g | variant_key=amt500.0|unitg\n",
415
+ "Group f570c38e... | Cookies Peanut Butter | 2 products | 1 variants\n",
416
+ " Cookies Peanut Butter | size=300g | variant_key=amt300.0|unitg\n",
417
+ " Cookies Peanut Butter | size=300g | variant_key=amt300.0|unitg\n",
418
+ "Group fea9d818... | Dog Treat Chewies Peanut Butter | 1 products | 1 variants\n",
419
+ " Dog Treat Chewies Peanut Butter | size=80g | variant_key=amt80.0|unitg\n"
420
+ ]
421
+ }
422
+ ],
423
+ "source": [
424
+ "pb = variant_mapping[variant_mapping['core_title'].str.contains('peanut butter', case=False, na=False)].copy()\n",
425
+ "pb = pb.merge(variant_summary[['group_id', 'group_name', 'product_domain', 'taxonomy']], on='group_id', how='left')\n",
426
+ "\n",
427
+ "print(f'Peanut Butter: {len(pb)} products, {pb[\"group_id\"].nunique()} groups, {pb[\"variant_id\"].nunique()} variants\\n')\n",
428
+ "\n",
429
+ "for gid in sorted(pb['group_id'].unique()):\n",
430
+ " grp = pb[pb['group_id'] == gid]\n",
431
+ " variants = grp['variant_id'].nunique()\n",
432
+ " print(f'Group {gid[:8]}... | {grp[\"group_name\"].iloc[0]} | {len(grp)} products | {variants} variants')\n",
433
+ " for _, row in grp.iterrows():\n",
434
+ " print(f' {row[\"core_title\"]} | size={row[\"size\"]} | variant_key={row[\"variant_key\"]}')"
435
+ ]
436
+ },
437
+ {
438
+ "cell_type": "markdown",
439
+ "metadata": {},
440
+ "source": [
441
+ "## 6. Save Audit Files"
442
+ ]
443
+ },
444
+ {
445
+ "cell_type": "code",
446
+ "execution_count": 7,
447
+ "metadata": {
448
+ "execution": {
449
+ "iopub.execute_input": "2026-07-30T18:12:41.067640Z",
450
+ "iopub.status.busy": "2026-07-30T18:12:41.067137Z",
451
+ "iopub.status.idle": "2026-07-30T18:12:41.099820Z",
452
+ "shell.execute_reply": "2026-07-30T18:12:41.097192Z"
453
+ }
454
+ },
455
+ "outputs": [
456
+ {
457
+ "name": "stdout",
458
+ "output_type": "stream",
459
+ "text": [
460
+ "Saved peanut_butter_variant_example.csv\n",
461
+ "Saved phase4_audit_summary.csv\n",
462
+ "\n",
463
+ "Audit complete.\n"
464
+ ]
465
+ }
466
+ ],
467
+ "source": [
468
+ "pb[['external_id', 'upc', 'group_id', 'variant_id', 'core_title', 'original_title', 'brand', 'size', 'variant_key', 'product_domain', 'taxonomy']].to_csv('peanut_butter_variant_example.csv', index=False)\n",
469
+ "print('Saved peanut_butter_variant_example.csv')\n",
470
+ "\n",
471
+ "audit_rows = [\n",
472
+ " {'metric': 'total_products', 'value': str(len(variant_mapping))},\n",
473
+ " {'metric': 'total_groups', 'value': str(variant_mapping['group_id'].nunique())},\n",
474
+ " {'metric': 'total_variants', 'value': str(variant_mapping['variant_id'].nunique())},\n",
475
+ " {'metric': 'validation', 'value': 'PASS' if all_pass else 'FAIL'},\n",
476
+ "]\n",
477
+ "pd.DataFrame(audit_rows).to_csv('phase4_audit_summary.csv', index=False)\n",
478
+ "print('Saved phase4_audit_summary.csv')\n",
479
+ "print('\\nAudit complete.')"
480
+ ]
481
+ }
482
+ ],
483
+ "metadata": {
484
+ "kernelspec": {
485
+ "display_name": "Python 3",
486
+ "language": "python",
487
+ "name": "python3"
488
+ },
489
+ "language_info": {
490
+ "codemirror_mode": {
491
+ "name": "ipython",
492
+ "version": 3
493
+ },
494
+ "file_extension": ".py",
495
+ "mimetype": "text/x-python",
496
+ "name": "python",
497
+ "nbconvert_exporter": "python",
498
+ "pygments_lexer": "ipython3",
499
+ "version": "3.13.7"
500
+ }
501
+ },
502
+ "nbformat": 4,
503
+ "nbformat_minor": 4
504
+ }