-
Notifications
You must be signed in to change notification settings - Fork 0
Ship evaluated T-24h delay prediction model #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,5 +13,3 @@ build/ | |
| .vercel | ||
| data/raw/ | ||
| data/processed/ | ||
| artifacts/models/ | ||
| artifacts/reports/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import math | ||
| from functools import lru_cache | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from app.models import ModelPrediction, ScheduledFlight | ||
|
|
||
|
|
||
| MODEL_PATH = Path(__file__).resolve().parent.parent / "artifacts" / "models" / "delay-logistic-v1.json" | ||
|
|
||
|
|
||
| class PortableLogisticModel: | ||
| """Small, inspectable JSON logistic model with no pickle execution risk.""" | ||
|
|
||
| def __init__(self, payload: dict[str, Any]) -> None: | ||
| self.payload = payload | ||
| self.threshold = float(payload["threshold"]) | ||
| self.version = str(payload["model_version"]) | ||
|
|
||
| @classmethod | ||
| def from_path(cls, path: Path = MODEL_PATH) -> "PortableLogisticModel": | ||
| return cls(json.loads(path.read_text(encoding="utf-8"))) | ||
|
|
||
| def predict(self, flight: ScheduledFlight) -> ModelPrediction: | ||
| values = _model_values(flight) | ||
| linear = float(self.payload["intercept"]) | ||
|
|
||
| numeric = self.payload["numeric"] | ||
| for name, value in values.items(): | ||
| if name not in numeric: | ||
| continue | ||
| spec = numeric[name] | ||
| scale = float(spec["scale"]) or 1.0 | ||
| standardized = (float(value) - float(spec["mean"])) / scale | ||
| linear += standardized * float(spec["coefficient"]) | ||
|
|
||
| categorical = self.payload["categorical"] | ||
| for name in ("Reporting_Airline", "Origin", "Dest"): | ||
| category = str(values[name]) | ||
| linear += float(categorical[name].get(category, 0.0)) | ||
|
|
||
| probability = 1.0 / (1.0 + math.exp(-max(-35.0, min(35.0, linear)))) | ||
| probability = round(probability, 4) | ||
| return ModelPrediction( | ||
| probability=probability, | ||
| predicted_delayed=probability >= self.threshold, | ||
| threshold=self.threshold, | ||
| model_version=self.version, | ||
| caveat="Retrospective BTS schedule-data estimate; not a live operational guarantee.", | ||
| ) | ||
|
|
||
| def metadata(self) -> dict[str, Any]: | ||
| return { | ||
| "model_version": self.version, | ||
| "prediction_time": self.payload["prediction_time"], | ||
| "threshold": self.threshold, | ||
| "trained_at": self.payload["trained_at"], | ||
| "data_manifest_sha256": self.payload["data_manifest_sha256"], | ||
| "test_metrics": self.payload["test_metrics"], | ||
| "limitations": self.payload["limitations"], | ||
| } | ||
|
|
||
|
|
||
| def _model_values(flight: ScheduledFlight) -> dict[str, float | str]: | ||
| hour = flight.crs_departure_time // 100 | ||
| angle = 2.0 * math.pi * hour / 24.0 | ||
| return { | ||
| "Month": float(flight.flight_date.month), | ||
| "DayOfWeek": float(flight.flight_date.isoweekday()), | ||
| "DepHourSin": math.sin(angle), | ||
| "DepHourCos": math.cos(angle), | ||
| "CRSElapsedTime": float(flight.crs_elapsed_time), | ||
| "Distance": float(flight.distance), | ||
| "DistanceGroup": float(flight.distance_group), | ||
| "Reporting_Airline": flight.reporting_airline, | ||
| "Origin": flight.origin, | ||
| "Dest": flight.destination, | ||
| } | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_delay_model() -> PortableLogisticModel: | ||
| return PortableLogisticModel.from_path() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In every Docker/Compose deployment, this resolves to
/app/artifacts/models/delay-logistic-v1.json, but the checkedDockerfile:8-10copies onlyrequirements.txtandapp/. Consequently both new model routes raiseFileNotFoundErrorand return 500 responses when first invoked, even though/healthremains successful; copy the artifact into the image or package it as application data.Useful? React with 👍 / 👎.