Skip to content

Commit

Permalink
feat: AnomalyError
Browse files Browse the repository at this point in the history
  • Loading branch information
stevemandl committed Jun 27, 2024
1 parent f636d4f commit 5d90456
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 10 additions & 4 deletions py_src/low_delta_t/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datetime import timedelta
import numpy as np
from requests.exceptions import ConnectionError as RequestConnectionError, HTTPError
from python_lib.utils import parse_event, fetch_trends, build_index, build_df
from python_lib.utils import parse_event, fetch_trends, build_index, build_df, AnomalyError

# the minimum acceptable length of the datapoints array
MIN_DATAPOINTS_LENGTH = int(7 * 24)
Expand Down Expand Up @@ -46,22 +46,28 @@ def run(event, _context):
# compute estimated design load from max(tons) over past year
max_tons = max(tons[point_name].values())
model_df = build_df(recent)
if any(x not in model_df for x in recent_points):
raise(AnomalyError(f"Missing trends from {recent_points}"))
# model partial load ratio for the recent period from all fetched data
model_df["PLR"] = model_df[tons_name] / max_tons
# model partial temperature ratio
model_df["PT"] = (model_df[oat_name] - model_df[stemp_name]) / 41
# DTpred = PLR^0.173 * PT^0.067 * 15.603
model_df["DT_PRED"] = (model_df["PLR"] ** 0.173) * (model_df["PT"] ** 0.067) * 15.603
# compare actual delta-t with modeled normal, and detect anomaly if it falls below model by more than variance
actual_dt = np.sum(model_df[rtemp_name] - model_df[stemp_name])
model_dt = np.sum(model_df["DT_PRED"])
actual_dt = np.mean(model_df[rtemp_name] - model_df[stemp_name])
model_dt = np.mean(model_df["DT_PRED"])
if actual_dt < model_dt * 0.9:
response[ "body" ] = f"""{point_name} actual_dt {actual_dt} model_dt {model_dt} for the period {start_time:%Y-%m-%d %H:%M} to {now:%Y-%m-%d %H:%M}"""
response[ "body" ] = f"""{point_name} actual_dt {actual_dt:.2f} model_dt {model_dt:.2f} for the period {start_time:%Y-%m-%d %H:%M} to {now:%Y-%m-%d %H:%M}"""
except RequestConnectionError as err:
response[
"body"
] = f"""{point_name} ConnectionError:
{err.response.text} {start_time:%Y-%m-%d %H:%M} to {now:%Y-%m-%d %H:%M}"""
except AnomalyError as err:
response[
"body"
] = f"""{point_name} Error: {err} {start_time:%Y-%m-%d %H:%M} to {now:%Y-%m-%d %H:%M}"""
except HTTPError as err:
response[
"body"
Expand Down
2 changes: 2 additions & 0 deletions py_src/python_lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
PORTAL_API_URL = os.environ.get("PORTAL_API_URL", "/")
QUERY_URL = f"{PORTAL_API_URL}query"

class AnomalyError(Exception):
"Anomaly Exception class"

def parse_event(event):
"""
Expand Down

0 comments on commit 5d90456

Please sign in to comment.