Spaces:
Sleeping
Sleeping
File size: 4,972 Bytes
6886978 | 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 | """
modules/circular_formulation_engine.py
First-pass Circular Formulation Engine (CFE) for IntelliForm Enterprise.
Treats end-of-life fate as a first-class optimization and reporting axis.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Dict, List
import pandas as pd
@dataclass
class FateComponentScores:
biodegradation: float
aquatic_safety: float
atmospheric_safety: float
leachate_safety: float
@dataclass
class CircularFormulationReport:
blend: Dict[str, float]
waste_score: float
circular_grade: str
fate_scores: FateComponentScores
lifecycle_notes: List[str] = field(default_factory=list)
high_risk_ingredients: List[str] = field(default_factory=list)
recommended_actions: List[str] = field(default_factory=list)
def _clamp(value: float, lower: float = 0.0, upper: float = 100.0) -> float:
return max(lower, min(upper, value))
def _safe_numeric(row: pd.Series, key: str, default: float) -> float:
try:
value = row.get(key, default)
if pd.isna(value):
return float(default)
return float(value)
except Exception:
return float(default)
def _circular_grade(score: float) -> str:
if score >= 85:
return "A"
if score >= 70:
return "B"
if score >= 55:
return "C"
if score >= 40:
return "D"
return "F"
def run_circular_formulation_engine(blend: Dict[str, float], db: pd.DataFrame) -> CircularFormulationReport:
if not blend:
zero = FateComponentScores(0.0, 0.0, 0.0, 0.0)
return CircularFormulationReport({}, 0.0, "F", zero)
idx = db.set_index("Ingredient") if "Ingredient" in db.columns else db
bio_sum = 0.0
aqua_sum = 0.0
atmos_sum = 0.0
leach_sum = 0.0
weighted_total = 0.0
lifecycle_notes: List[str] = []
high_risk: List[str] = []
actions: List[str] = []
for ingredient, pct in blend.items():
if ingredient not in idx.index:
continue
row = idx.loc[ingredient]
weight = float(pct) / 100.0
weighted_total += weight
biodeg = _safe_numeric(row, "Biodegradability", _safe_numeric(row, "Bio_based_pct", 50.0) * 0.9)
ecotox_raw = _safe_numeric(row, "Ecotoxicity_Score", 6.0)
carbon_raw = _safe_numeric(row, "CarbonFootprint_kgCO2eq", 2.5)
renew_raw = _safe_numeric(row, "Renewability_Score", _safe_numeric(row, "Bio_based_pct", 50.0))
reach_flag = str(row.get("REACH_Flag", "Amber"))
aquatic_safety = _clamp(ecotox_raw * 10.0)
atmospheric_safety = _clamp((1.0 - min(carbon_raw, 5.0) / 5.0) * 100.0)
leachate_safety = _clamp((renew_raw * 0.55) + (20.0 if reach_flag == "Green" else 0.0) + (5.0 if reach_flag == "Amber" else -10.0))
bio_sum += biodeg * weight
aqua_sum += aquatic_safety * weight
atmos_sum += atmospheric_safety * weight
leach_sum += leachate_safety * weight
if biodeg < 45 or aquatic_safety < 45 or leachate_safety < 40:
high_risk.append(ingredient)
if weighted_total <= 0:
weighted_total = 1.0
fate = FateComponentScores(
biodegradation=round(bio_sum / weighted_total, 1),
aquatic_safety=round(aqua_sum / weighted_total, 1),
atmospheric_safety=round(atmos_sum / weighted_total, 1),
leachate_safety=round(leach_sum / weighted_total, 1),
)
waste_score = round(
(0.35 * fate.biodegradation)
+ (0.25 * fate.aquatic_safety)
+ (0.20 * fate.atmospheric_safety)
+ (0.20 * fate.leachate_safety),
1,
)
if high_risk:
lifecycle_notes.append(
f"High-risk end-of-life contributors detected: {', '.join(high_risk[:5])}"
)
actions.append("Substitute or lower the share of ingredients with weak biodegradation or leachate safety scores.")
else:
lifecycle_notes.append("No obvious high-risk end-of-life ingredients were detected in the current blend.")
if fate.atmospheric_safety < 55:
actions.append("Reduce carbon-intense or volatile components to improve atmospheric and lifecycle performance.")
if fate.aquatic_safety < 60:
actions.append("Prioritize ingredients with better aquatic safety profiles before pilot validation.")
if not actions:
actions.append("Current blend is a strong candidate for Circular Formulation Engine advancement and passport generation.")
lifecycle_notes.append(
"Waste Score combines biodegradation, aquatic safety, atmospheric safety, and leachate safety into a single circularity signal."
)
return CircularFormulationReport(
blend=blend,
waste_score=waste_score,
circular_grade=_circular_grade(waste_score),
fate_scores=fate,
lifecycle_notes=lifecycle_notes,
high_risk_ingredients=high_risk,
recommended_actions=actions,
)
|