-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
88 lines (60 loc) · 2.15 KB
/
api.py
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
from fastapi import FastAPI
from scraper import pull_current_data
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
app = FastAPI()
lower_bound = None
upper_bound = None
data = None
@app.get("/data")
async def data():
update()
return {"alert": check_anomaly()}
async def start():
global lower_bound, upper_bound, data
data = pd.read_csv(r"./hourly_electricity_data.csv",parse_dates=['period'],index_col='period',)
# Create a new figure and axes with increased size
plot()
Q1 = data['current'].quantile(0.25)
Q3 = data['current'].quantile(0.75)
# Calculate the interquartile range (IQR)
IQR = Q3 - Q1
# Determine the lower and upper bounds for outliers
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
async def check_anomaly(current):
global lower_bound, upper_bound
# Find anomalous points
return (current < lower_bound) | (current > upper_bound)
async def update():
global data
new_data = pull_current_data()
data.append(new_data)
plot()
async def plot():
global data
fig, ax = plt.subplots(figsize=(30, 20))
# For each day, plot the consumption trends
for date, group in data.groupby(data.index.date):
# Convert 'time' to matplotlib date format
time = [mdates.date2num(pd.Timestamp(dt.time().isoformat())) for dt in group.index]
ax.plot(time, group['current'], label=date)
# Set the x-axis label
ax.set_xlabel('Time')
# Format x-tick labels as 24-hour clock and set them for every record
ax.xaxis.set_major_locator(mdates.HourLocator(interval=1)) # to get a tick every hour
ax.xaxis.set_minor_locator(mdates.MinuteLocator(interval=30)) # to get a tick every 30 minutes
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S')) # format x-tick labels as 24-hour clock
# Set the y-axis label
ax.set_ylabel('Consumption')
# Set the title
ax.set_title('Half-hourly Consumption Trends')
# Display the legend
ax.legend()
fig.savefig(fname="./public/plot")
if __name__ == "__main__":
start()
app.run()