Domain: Healthcare

Use only Food_Nutrition_Messy_V1.csv and only:
food, Nutrition Density, Protein, Dietary Fiber, Potassium, Magnesium, Iron, Zinc, Vitamin C, Calcium, Sugars, Sodium, Saturated Fats, Cholesterol.
1. T1_raw_clean
  - Normalize food to lowercase and trim.
  - Remove rows where food is empty.
  - Convert listed numeric columns to numeric.


2. T2_food_unique(order-independent duplicate resolution)
  - Group by food.
  - For each required numeric column, take median within each food.
  - Derive family as first token of food.
  - Impute missing numeric values per column using family median; fallback to global median.


3. T3_top_rows
  - Sort by Nutrition Density descending, tie-break by food ascending.
  - Keep top 300 rows.


4. T4_family_stats
  - Group by family and compute:
    - family_n, fam_density_med, fam_sugar_med, fam_sodium_med, fam_protein_mean.


5. T5_target_families
  - Top 12 families by family_n descending, tie-break alphabetically.
  - Filter T3_top_rows to those families.


6. T6_density_band_map
  - Compute global quartile cut points on filtered Nutrition Density (q25, q50, q75).
  - Create DensityBand in D1..D4 using:
    - bins=[-inf, q25, q50, q75, inf]
    - labels=['D1','D2','D3','D4']
    - include_lowest=True
    - right=True
  - Therefore intervals are (-inf, q25], (q25, q50], (q50, q75], (q75, inf].


7. T7_flags
  - Compute thresholds on filtered rows:
    - sugar_p65, sodium_p60, chol_p55, protein_p50.
  - Add binary flags:
    - HighSugar, HighSodium, HighChol, HighProtein.


8. T8_segment
  - Group by (family, DensityBand) and compute:
    - n, k_sugar, k_sodium, k_chol, k_protein.


9. T9_joined_segment(multi-join + layered filters)
  - Join T8_segment with T4_family_stats on family.
  - Join band weights table {D1:0.10, D2:0.20, D3:0.30, D4:0.40} on DensityBand.
  - Compute:
    - risk_rate = (k_sugar + k_sodium + k_chol + 1) / (3*n + 2)
    - protect_rate = (k_protein + 1) / (n + 2)
  - Apply filters in order:
    - n >= 1
    - risk_rate >= 0.10
    - protect_rate >= 0.10
    - keep families with at least 2 distinct bands.
  - Compute:
    - SegmentScore = ln(1 + fam_protein_mean) * risk_rate * (1 - protect_rate/2) * sqrt(fam_density_med + 1).


10. T10_band_spread(self-join)
   - Self-join (family, DensityBand, SegmentScore) on family.
   - Keep pairs where DensityBand < DensityBand2.
   - Per family:
     - spread_mean = mean(SegmentScore2)
     - base_mean = mean(SegmentScore)
     - BandSpread = spread_mean - base_mean.


11. T11_final_segments
   - Join T9_joined_segment with T10_band_spread on family.
   - Fill missing BandSpread with 0.
   - weighted_seg = SegmentScore * band_w.


12. T12_family_final
   - Group by family and compute:
     - WeightedSignal = sum(weighted_seg)
     - Volatility = std(SegmentScore, ddof=0) / (mean(SegmentScore) + 1e-12)  (explicit zero-protection)
     - MeanSpread = mean(BandSpread)
     - total_n = sum(n)
     - band_count = nunique(DensityBand)
   - Keep families with total_n >= 4 and Volatility > 0.
   - Select top 8 by total_n descending, tie-break alphabetically.


13. Final metrics
   - EnergyRaw = WeightedSignal * (1 + MeanSpread)
   - energy_shift = -min(EnergyRaw) + 0.01
   - EnergyAdj = EnergyRaw + energy_shift
   - VolAdj = Volatility
   - P = EnergyAdj / sum(EnergyAdj), Q = VolAdj / sum(VolAdj)
   - hellinger_distance, bhattacharyya_coeff
   - consistency_delta = |hellinger_distance - sqrt(max(0.0, 1 - bhattacharyya_coeff))|
   - alignment_gap from cumulative P and cumulative Q after sorting by descending P
   - instability_index = hellinger_distance * (1 + alignment_gap).


Rounding and output precision (explicit)
- Round all reported metric values to 4 decimal placesusing ROUND_HALF_UP.
- Final line only: round instability_index to 2 decimal placesusing ROUND_HALF_UP.


Response format (must follow exactly)
- FamilyOrder: ...
- rank_p=...
- density_q25=...
- density_q75=...
- energy_shift=...
- checkpoint_fusion_energyadj=...
- checkpoint_cheese_voladj=...
- checkpoint_premium_meanspread=...
- hellinger_distance=...
- bhattacharyya_coeff=...
- consistency_delta=...
- alignment_gap=...
- instability_index=...
- table_rows=...
- Final line must be exactly one number: instability_index rounded to 2 decimals, no units, no extra text.

Grading rubric:
criterion: FamilyOrder equals 'fusion, biscuit, cheese, premium, burrito, bean, chicken, double'.
weight: 10
criterion: rank_p equals 'burrito, premium, fusion, biscuit, double, bean, chicken, cheese'.
weight: 10
criterion: density_q25 equals 187.8577.
weight: 10
criterion: density_q75 equals 309.4000.
weight: 10
criterion: energy_shift equals 6.3715.
weight: 10
criterion: checkpoint_fusion_energyadj equals 28.8606.
weight: 10
criterion: checkpoint_cheese_voladj equals 0.3725.
weight: 10
criterion: checkpoint_premium_meanspread equals 1.2827.
weight: 10
criterion: hellinger_distance equals 0.4777.
weight: 10
criterion: bhattacharyya_coeff equals 0.7718.
weight: 10
criterion: consistency_delta equals 0.0000.
weight: 10
criterion: alignment_gap equals 0.2142.
weight: 10
criterion: instability_index equals 0.5800.
weight: 10
criterion: table_rows equals 8.
weight: 10
criterion: Final line is exactly '0.58'.
weight: 10

Golden answer:
```Python
import numpy as np
import pandas as pd
from decimal import Decimal, ROUND_HALF_UP

def round4(x):
    return f"{Decimal(str(float(x))).quantize(Decimal('0.0001'), rounding=ROUND_HALF_UP):.4f}"

def round2(x):
    return f"{Decimal(str(float(x))).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP):.2f}"

benefit_cols = ["Protein", "Dietary Fiber", "Potassium", "Magnesium", "Iron", "Zinc", "Vitamin C", "Calcium"]
risk_cols = ["Sugars", "Sodium", "Saturated Fats", "Cholesterol"]
required_cols = ["Nutrition Density"] + benefit_cols + risk_cols

T1_raw = pd.read_csv("Food_Nutrition_Messy_V1.csv")
T1_raw["food"] = T1_raw["food"].astype(str).str.strip().str.lower()
T1_raw = T1_raw[T1_raw["food"].ne("") & T1_raw["food"].ne("nan")]
for c in required_cols:
    T1_raw[c] = pd.to_numeric(T1_raw[c], errors="coerce")

# T2_food_unique
T2 = T1_raw.groupby("food", as_index=False)[required_cols].median()
T2["family"] = T2["food"].str.split().str[0]
for c in required_cols:
    T2[c] = T2[c].fillna(T2.groupby("family")[c].transform("median")).fillna(T2[c].median())

# T3_top_rows
T3 = T2.sort_values(["Nutrition Density", "food"], ascending=[False, True]).head(300).copy()

# T4_family_stats
T4 = T3.groupby("family", as_index=False).agg(
    family_n=("food", "size"),
    fam_density_med=("Nutrition Density", "median"),
    fam_sugar_med=("Sugars", "median"),
    fam_sodium_med=("Sodium", "median"),
    fam_protein_mean=("Protein", "mean"),
)

# T5_target_families
T5 = T4.sort_values(["family_n", "family"], ascending=[False, True]).head(12).copy()
T3 = T3[T3["family"].isin(T5["family"])].copy()

# T6_density_band_map
q25, q50, q75 = np.quantile(T3["Nutrition Density"], [0.25, 0.5, 0.75])
T3["DensityBand"] = pd.cut(
    T3["Nutrition Density"],
    bins=[-np.inf, q25, q50, q75, np.inf],
    labels=["D1", "D2", "D3", "D4"],
    include_lowest=True,
    right=True,
)

# T7_flags
sugar_p65 = float(np.quantile(T3["Sugars"], 0.65))
sodium_p60 = float(np.quantile(T3["Sodium"], 0.60))
chol_p55 = float(np.quantile(T3["Cholesterol"], 0.55))
protein_p50 = float(np.quantile(T3["Protein"], 0.50))

T7 = T3[["food", "family", "DensityBand", "Protein", "Sugars", "Sodium", "Cholesterol"]].copy()
T7["HighSugar"] = (T7["Sugars"] >= sugar_p65).astype(int)
T7["HighSodium"] = (T7["Sodium"] >= sodium_p60).astype(int)
T7["HighChol"] = (T7["Cholesterol"] >= chol_p55).astype(int)
T7["HighProtein"] = (T7["Protein"] >= protein_p50).astype(int)

# T8_segment
T8 = T7.groupby(["family", "DensityBand"], as_index=False, observed=True).agg(
    n=("food", "size"),
    k_sugar=("HighSugar", "sum"),
    k_sodium=("HighSodium", "sum"),
    k_chol=("HighChol", "sum"),
    k_protein=("HighProtein", "sum"),
)

# T9_joined_segment
W = pd.DataFrame({"DensityBand": ["D1", "D2", "D3", "D4"], "band_w": [0.10, 0.20, 0.30, 0.40]})
T9 = T8.merge(T4, on="family", how="inner").merge(W, on="DensityBand", how="inner")
T9 = T9[T9["n"] >= 1].copy()
T9["risk_rate"] = (T9["k_sugar"] + T9["k_sodium"] + T9["k_chol"] + 1) / (3 * T9["n"] + 2)
T9["protect_rate"] = (T9["k_protein"] + 1) / (T9["n"] + 2)
T9 = T9[(T9["risk_rate"] >= 0.10) & (T9["protect_rate"] >= 0.10)].copy()
keep_families = T9.groupby("family", as_index=False).agg(bands=("DensityBand", "nunique"))
T9 = T9[T9["family"].isin(keep_families[keep_families["bands"] >= 2]["family"])].copy()
T9["SegmentScore"] = (
    np.log1p(T9["fam_protein_mean"]) * T9["risk_rate"] * (1 - T9["protect_rate"] / 2) * np.sqrt(T9["fam_density_med"] + 1)
)

# T10_band_spread (self join)
S = T9[["family", "DensityBand", "SegmentScore"]].copy()
S2 = S.rename(columns={"DensityBand": "DensityBand2", "SegmentScore": "SegmentScore2"})
T10 = S.merge(S2, on="family", how="inner")
T10 = T10[T10["DensityBand"] < T10["DensityBand2"]].copy()
spread = T10.groupby("family", as_index=False).agg(spread_mean=("SegmentScore2", "mean"))
base_mean = S.groupby("family", as_index=False).agg(base_mean=("SegmentScore", "mean"))
spread = spread.merge(base_mean, on="family", how="left")
spread["BandSpread"] = spread["spread_mean"] - spread["base_mean"]
spread = spread[["family", "BandSpread"]]

# T11_final_segments
T11 = T9.merge(spread, on="family", how="left")
T11["BandSpread"] = T11["BandSpread"].fillna(0.0)
T11["weighted_seg"] = T11["SegmentScore"] * T11["band_w"]

# T12_family_final
T12 = T11.groupby("family", as_index=False).agg(
    WeightedSignal=("weighted_seg", "sum"),
    Volatility=("SegmentScore", lambda s: float(np.std(s, ddof=0) / (np.mean(s) + 1e-12))),
    MeanSpread=("BandSpread", "mean"),
    total_n=("n", "sum"),
    band_count=("DensityBand", "nunique"),
)
T12 = T12[(T12["total_n"] >= 4) & (T12["Volatility"] > 0)].copy()
T12 = T12.sort_values(["total_n", "family"], ascending=[False, True]).head(8).sort_values("family").reset_index(drop=True)

energy_raw = T12["WeightedSignal"] * (1 + T12["MeanSpread"])
energy_shift = float(-energy_raw.min() + 0.01)
T12["EnergyAdj"] = energy_raw + energy_shift
T12["VolAdj"] = T12["Volatility"]

P = (T12["EnergyAdj"] / T12["EnergyAdj"].sum()).to_numpy(dtype=float)
Q = (T12["VolAdj"] / T12["VolAdj"].sum()).to_numpy(dtype=float)

hellinger_distance = float(np.sqrt(0.5 * np.sum((np.sqrt(P) - np.sqrt(Q)) ** 2)))
bhattacharyya_coeff = float(np.sum(np.sqrt(P * Q)))
consistency_delta = float(abs(hellinger_distance - np.sqrt(max(0.0, 1 - bhattacharyya_coeff))))

order_idx = np.argsort(-P)
alignment_gap = float(np.mean(np.abs(np.cumsum(P[order_idx]) - np.cumsum(Q[order_idx]))))
instability_index = float(hellinger_distance * (1 + alignment_gap))

family_order = ", ".join(T12.sort_values(["total_n", "family"], ascending=[False, True])["family"])
rank_p = ", ".join(T12.iloc[np.argsort(-P)]["family"])

checkpoint_fusion_energyadj = float(T12.loc[T12["family"] == "fusion", "EnergyAdj"].iloc[0])
checkpoint_cheese_voladj = float(T12.loc[T12["family"] == "cheese", "VolAdj"].iloc[0])
checkpoint_premium_meanspread = float(T12.loc[T12["family"] == "premium", "MeanSpread"].iloc[0])

print("FamilyOrder:", family_order)
print(f"rank_p={rank_p}")
print(f"density_q25={round4(q25)}")
print(f"density_q75={round4(q75)}")
print(f"energy_shift={round4(energy_shift)}")
print(f"checkpoint_fusion_energyadj={round4(checkpoint_fusion_energyadj)}")
print(f"checkpoint_cheese_voladj={round4(checkpoint_cheese_voladj)}")
print(f"checkpoint_premium_meanspread={round4(checkpoint_premium_meanspread)}")
print(f"hellinger_distance={round4(hellinger_distance)}")
print(f"bhattacharyya_coeff={round4(bhattacharyya_coeff)}")
print(f"consistency_delta={round4(consistency_delta)}")
print(f"alignment_gap={round4(alignment_gap)}")
print(f"instability_index={round4(instability_index)}")
print(f"table_rows={len(T12)}")
print(round2(instability_index))
```
Explanation:
Cleans/imputes a messy nutrition file, picks top foods and families, builds weighted per-(family×density-band) risk/protection SegmentScores plus a band-spread adjustment, aggregates to 8 families’ EnergyAdj vs VolAdj, then computes an instability_index from Hellinger distance plus cumulative alignment gap and prints checkpoints.

GTFA:
FamilyOrder: fusion, biscuit, cheese, premium, burrito, bean, chicken, double
rank_p=burrito, premium, fusion, biscuit, double, bean, chicken, cheese
density_q25=187.8577
density_q75=309.4000
energy_shift=6.3715
checkpoint_fusion_energyadj=28.8606
checkpoint_cheese_voladj=0.3725
checkpoint_premium_meanspread=1.2827
hellinger_distance=0.4777
bhattacharyya_coeff=0.7718
consistency_delta=0.0000
alignment_gap=0.2142
instability_index=0.5800
table_rows=8
0.58