Files
Ai/crop_simulation/yield_prediction.py
T

39 lines
1.4 KiB
Python
Raw Normal View History

2026-04-25 17:22:41 +03:30
from __future__ import annotations
from typing import Any
2026-04-30 02:10:15 +03:30
from .growth_simulation import (
CurrentFarmChartSimulator,
GrowthSimulationError,
_fa_engine_name,
_fa_model_name,
)
2026-04-25 17:22:41 +03:30
def build_yield_prediction_payload(*, farm_uuid: str, plant_name: str | None = None) -> dict[str, Any]:
simulator = CurrentFarmChartSimulator()
result = simulator.simulate(farm_uuid=farm_uuid, plant_name=plant_name)
yield_estimate = float((result.get("metrics") or {}).get("yield_estimate") or 0.0)
predicted_yield_tons = round(max(yield_estimate / 1000.0, 0.0), 2)
return {
"farm_uuid": farm_uuid,
"plant_name": result.get("plant_name"),
"predictedYieldTons": predicted_yield_tons,
"predictedYieldRaw": round(yield_estimate, 2),
"unit": "تن",
2026-04-30 02:10:15 +03:30
"sourceUnit": "کیلوگرم در هکتار",
"simulationEngine": _fa_engine_name(result.get("engine")),
"simulationModel": _fa_model_name(result.get("model_name")),
2026-04-25 17:22:41 +03:30
"scenarioId": result.get("scenario_id"),
"simulationWarning": result.get("simulation_warning"),
"supportingMetrics": result.get("metrics") or {},
}
class YieldPredictionService:
def get_yield_prediction(self, *, farm_uuid: str, plant_name: str | None = None) -> dict[str, Any]:
try:
return build_yield_prediction_payload(farm_uuid=farm_uuid, plant_name=plant_name)
except GrowthSimulationError:
raise