File size: 11,349 Bytes
881f9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""
TSFM MCP Server — Time-Series Forecasting and anomaly detection for Smart Grid transformers.

TSFM = Time-Series Foundation Model.  In the full project this server will call
a fine-tuned TSFM (e.g., Chronos or a Llama-based forecaster served via vLLM).
This skeleton implements lightweight statistical baselines so the server is
functional end-to-end for scenario testing before the model is integrated.

Baseline methods used here:
  - RUL forecast:      returns the label from rul_labels.csv + a linear projection
  - Anomaly detection: z-score over a rolling window
  - Trend analysis:    linear regression slope over a requested time period

Tools exposed to the LLM agent:
  get_rul                — current remaining useful life estimate for a transformer
  forecast_rul           — project RUL N days into the future
  detect_anomalies       — flag sensor readings that exceed a z-score threshold
  trend_analysis         — slope and direction of a sensor over a time window

Data source: data/processed/rul_labels.csv, sensor_readings.csv
"""

from __future__ import annotations

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[2]))

import numpy as np
import pandas as pd
from mcp.server.fastmcp import FastMCP

from data.constants import HI_FULL_HEALTH_DAYS
from mcp_servers.base import load_rul_labels, load_sensor_readings

mcp = FastMCP("smart-grid-tsfm")

_rul: pd.DataFrame | None = None
_readings: pd.DataFrame | None = None


def _confidence_from_history(num_points: int, horizon_days: int = 0) -> float:
    """Simple bounded baseline confidence for deterministic synthetic labels."""
    history_factor = min(0.99, 0.55 + num_points / 120)
    horizon_penalty = min(0.45, max(0, horizon_days) / 900)
    return round(max(0.1, history_factor - horizon_penalty), 3)


def _get_rul() -> pd.DataFrame:
    global _rul
    if _rul is None:
        _rul = load_rul_labels()
    return _rul


def _get_readings() -> pd.DataFrame:
    global _readings
    if _readings is None:
        _readings = load_sensor_readings()
    return _readings


# ---------------------------------------------------------------------------
# Tools
# ---------------------------------------------------------------------------


@mcp.tool()
def get_rul(transformer_id: str) -> dict:
    """
    Return the most recent remaining useful life (RUL) estimate for a transformer.

    RUL is sourced from the FDD & RUL dataset labels and represents the number
    of days of useful operation remaining before the transformer is expected to
    require major maintenance or replacement.

    Args:
        transformer_id: Asset identifier, e.g. "T-018".

    Returns:
        Dict with keys: transformer_id, as_of_date, rul_days, health_index,
        fdd_category, interpretation.
        Returns an error dict if not found.
    """
    df = _get_rul()
    subset = df[df["transformer_id"] == transformer_id].sort_values("timestamp")
    if subset.empty:
        return {"error": f"No RUL data found for '{transformer_id}'."}

    latest = subset.iloc[-1]
    rul_days = int(latest["rul_days"])
    fdd_category = (
        int(latest["fdd_category"]) if pd.notna(latest["fdd_category"]) else None
    )
    as_of_ts = latest["timestamp"]

    if rul_days >= 730:
        interpretation = "Healthy — no immediate action required."
    elif rul_days >= 180:
        interpretation = "Aging — schedule routine inspection within 6 months."
    elif rul_days >= 30:
        interpretation = "Degraded — maintenance recommended within 30 days."
    else:
        interpretation = "Critical — immediate inspection required."

    return {
        "transformer_id": transformer_id,
        "as_of_date": str(as_of_ts.date()) if pd.notna(as_of_ts) else None,
        "rul_days": rul_days,
        "health_index": round(float(latest["health_index"]), 4),
        "fdd_category": fdd_category,
        "confidence": _confidence_from_history(len(subset)),
        "interpretation": interpretation,
    }


@mcp.tool()
def forecast_rul(transformer_id: str, horizon_days: int = 30) -> dict:
    """
    Project the RUL forward by `horizon_days` using a linear degradation model.

    This is a statistical baseline. The full project will replace this with a
    TSFM (Time-Series Foundation Model) inference call via vLLM.

    Args:
        transformer_id: Asset identifier, e.g. "T-007".
        horizon_days:   Number of days to project forward (default 30, max 365).

    Returns:
        Dict with keys: transformer_id, current_rul_days, forecast_date,
        projected_rul_days, projected_health_index, confidence, method.
    """
    df = _get_rul()
    subset = df[df["transformer_id"] == transformer_id].sort_values("timestamp")
    if subset.empty:
        return {"error": f"No RUL data found for '{transformer_id}'."}

    if horizon_days < 0 or horizon_days > 365:
        return {
            "error": "horizon_days must be between 0 and 365.",
            "provided_horizon_days": horizon_days,
        }
    latest = subset.iloc[-1]
    current_rul = int(latest["rul_days"])
    latest_ts = latest["timestamp"]

    # Linear model: assume 1 RUL-day consumed per calendar day
    projected_rul = max(0, current_rul - horizon_days)
    forecast_date = (
        pd.to_datetime(latest_ts) + pd.Timedelta(days=horizon_days)
        if pd.notna(latest_ts)
        else None
    )
    projected_hi = (
        0.0
        if current_rul <= 0
        else round(min(1.0, projected_rul / HI_FULL_HEALTH_DAYS), 4)
    )

    return {
        "transformer_id": transformer_id,
        "current_rul_days": current_rul,
        "forecast_date": (
            str(forecast_date.date()) if forecast_date is not None else None
        ),
        "projected_rul_days": projected_rul,
        "projected_health_index": projected_hi,
        "confidence": _confidence_from_history(len(subset), horizon_days),
        "method": "linear_degradation_baseline",
    }


@mcp.tool()
def detect_anomalies(
    transformer_id: str,
    sensor_id: str,
    window_size: int = 24,
    z_threshold: float = 3.0,
) -> dict:
    """
    Detect anomalous sensor readings using a rolling z-score method.

    A reading is flagged as anomalous when it deviates more than `z_threshold`
    standard deviations from the rolling mean over `window_size` readings.

    Args:
        transformer_id: Asset identifier, e.g. "T-016".
        sensor_id:      Sensor to analyse, e.g. "dga_h2_ppm".
        window_size:    Rolling window size in readings (default 24).
        z_threshold:    Number of standard deviations to flag (default 3.0).

    Returns:
        Dict with keys: transformer_id, sensor_id, total_readings,
        anomaly_count, anomaly_rate_pct, anomalies (list of flagged readings
        with timestamp, value, z_score).
    """
    df = _get_readings()
    subset = (
        df[(df["transformer_id"] == transformer_id) & (df["sensor_id"] == sensor_id)]
        .copy()
        .sort_values("timestamp")
    )

    if subset.empty:
        return {
            "error": f"No readings for transformer='{transformer_id}' "
            f"sensor='{sensor_id}'."
        }

    vals = subset["value"].astype(float)
    rolling_mean = vals.rolling(window_size, min_periods=1).mean()
    rolling_std = vals.rolling(window_size, min_periods=1).std()
    rolling_std = rolling_std.where(
        rolling_std > 0, other=rolling_mean.abs() * 0.001 + 1e-3
    )
    z_scores = ((vals - rolling_mean) / rolling_std).abs()

    anomaly_mask = z_scores > z_threshold
    anomalies_df = subset[anomaly_mask][["timestamp", "value"]].copy()
    anomalies_df["z_score"] = z_scores[anomaly_mask].round(3).values

    anomalies = anomalies_df.head(50).to_dict(orient="records")
    # Ensure timestamps are strings
    for row in anomalies:
        row["timestamp"] = str(row["timestamp"])

    return {
        "transformer_id": transformer_id,
        "sensor_id": sensor_id,
        "window_size": window_size,
        "z_threshold": z_threshold,
        "total_readings": len(subset),
        "anomaly_count": int(anomaly_mask.sum()),
        "anomaly_rate_pct": round(100 * anomaly_mask.mean(), 2),
        "anomalies": anomalies,
    }


@mcp.tool()
def trend_analysis(
    transformer_id: str,
    sensor_id: str,
    start_time: str | None = None,
    end_time: str | None = None,
) -> dict:
    """
    Compute the trend (slope) of a sensor's readings over a time window.

    Uses ordinary least squares linear regression over the selected readings.
    A positive slope means the sensor value is increasing over time.

    Args:
        transformer_id: Asset identifier, e.g. "T-012".
        sensor_id:      Sensor to analyse, e.g. "dga_c2h2_ppm".
        start_time:     ISO-8601 start of window (optional).
        end_time:       ISO-8601 end of window (optional).

    Returns:
        Dict with keys: transformer_id, sensor_id, num_readings,
        start_time, end_time, mean_value, min_value, max_value,
        slope_per_day, direction ("increasing"/"decreasing"/"stable"),
        r_squared.
    """
    df = _get_readings()
    subset = df[
        (df["transformer_id"] == transformer_id) & (df["sensor_id"] == sensor_id)
    ].copy()

    if subset.empty:
        return {
            "error": f"No readings for transformer='{transformer_id}' "
            f"sensor='{sensor_id}'."
        }

    subset["timestamp"] = pd.to_datetime(subset["timestamp"])
    if start_time:
        subset = subset[subset["timestamp"] >= pd.to_datetime(start_time)]
    if end_time:
        subset = subset[subset["timestamp"] <= pd.to_datetime(end_time)]

    subset = subset.sort_values("timestamp")
    if len(subset) < 2:
        return {
            "error": "Not enough readings in the specified window for trend analysis."
        }

    # Convert timestamps to numeric (days since first reading)
    t0 = subset["timestamp"].iloc[0]
    days = (subset["timestamp"] - t0).dt.total_seconds() / 86400
    vals = subset["value"].astype(float)

    # OLS: slope and R²
    coeffs = np.polyfit(days, vals, 1)
    slope = float(coeffs[0])
    y_hat = np.polyval(coeffs, days)
    ss_res = float(np.sum((vals - y_hat) ** 2))
    ss_tot = float(np.sum((vals - vals.mean()) ** 2))
    r2 = 1 - ss_res / ss_tot if ss_tot > 0 else 0.0

    # Direction: stable if slope < 1% of mean per day
    mean_val = float(vals.mean())
    rel_slope = abs(slope) / (mean_val + 1e-9)
    if rel_slope < 0.01:
        direction = "stable"
    elif slope > 0:
        direction = "increasing"
    else:
        direction = "decreasing"

    return {
        "transformer_id": transformer_id,
        "sensor_id": sensor_id,
        "num_readings": len(subset),
        "start_time": str(subset["timestamp"].iloc[0]),
        "end_time": str(subset["timestamp"].iloc[-1]),
        "mean_value": round(mean_val, 4),
        "min_value": round(float(vals.min()), 4),
        "max_value": round(float(vals.max()), 4),
        "slope_per_day": round(slope, 6),
        "direction": direction,
        "r_squared": round(r2, 4),
    }


# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    mcp.run()