-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
172 lines (155 loc) · 7.24 KB
/
main.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
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
import streamlit as st
import pandas as pd
import altair as alt
import plotly.express as px
import os
import time
import sys
from datetime import datetime, timedelta
from plt_setup import finastra_theme
@st.cache(show_spinner=False, suppress_st_warning=True,
allow_output_mutation=True)
def load_data(data_category='validation'):
data1 = pd.read_csv(f'./data/{data_category}_1.csv')
data1.drop(columns=["Unnamed: 0"], inplace=True)
data2 = pd.read_csv(f'./data/{data_category}_2.csv')
data2.drop(columns=["Unnamed: 0"], inplace=True)
data = pd.concat([data1, data2])
data.set_index('timestamp', inplace=True)
return data
def filter_on_date(df, start, end):
end = end + timedelta(days=1)
df = df[(pd.to_datetime(df.index) >= pd.to_datetime(start)) &
(pd.to_datetime(df.index) <= pd.to_datetime(end))]
return df
def main():
###### CUSTOMIZE COLOR THEME ######
alt.themes.register("finastra", finastra_theme)
alt.themes.enable("finastra")
violet, fuchsia = ["#694ED6", "#C137A2"]
###### SET UP PAGE ######
icon_path = os.path.join("./img", "icon.png")
st.set_page_config(page_title="SAI-Board", page_icon=icon_path,
layout='centered', initial_sidebar_state="collapsed")
_, logo, _ = st.columns(3)
logo.image(icon_path, width=200)
style = ("text-align:center; padding: 0px; font-family: arial black;, "
"font-size: 400%")
title = f"<h1 style='{style}'>SAI<sup>Board</sup></h1><br><br>"
st.write(title, unsafe_allow_html=True)
####### CREATE SIDEBAR CATEGORY FILTER######
st.sidebar.title("Filter Options")
date_place = st.sidebar.empty()
with st.spinner(text="Fetching Data..."):
data = load_data()
###### DATE WIDGET ######
start = str(data.index.min()).split(' ')[0]
end = str(data.index.max()).split(' ')[0]
start = datetime.strptime(start, "%Y-%m-%d")
end = datetime.strptime(end, "%Y-%m-%d")
selected_dates = date_place.date_input("Select a Date Range",
value=[start, end], min_value=start, max_value=end, key=None)
time.sleep(0.8) # Allow user some time to select the two dates -- hacky :D
start, end = selected_dates
data = filter_on_date(data, start, end)
###### DISPLAY DATA ######
URL_Expander = st.expander(f"View Raw Data:", True)
URL_Expander.write(f"### {len(data):,d} Matching Data by seconds ")
display_cols = data.columns
URL_Expander.write(data[display_cols])
###### CHART: METRIC OVER TIME ######
st.markdown("---")
st.header("Data Monitoring")
col1, col2, col3 = st.columns((1, 1, 3))
hour = col1.selectbox("Hour", range(0, 23))
min = col2.selectbox("Minute", range(0, 60))
sensor = col3.selectbox("Sensors", data.columns[:-1])
df = data.copy()
df = df[hour*3600:(hour+1)*3600].copy()
df = df[min*60:(min+1)*60].copy()
df1 = df.loc[:, [sensor]].reset_index()[5:]
df2 = df.loc[:, [sensor]].rolling(5).mean().reset_index()[5:]
df1["WHO"] = sensor
df2["WHO"] = "Rolling Mean"
plot_df = pd.concat([df1, df2]).reset_index(drop=True)
plot_df["timestamp"] = pd.to_datetime(plot_df["timestamp"])
metric_chart = alt.Chart(plot_df, title="Trends Over Time", padding={"left": 10, "top": 1, "right": 10, "bottom": 1}
).mark_line().encode(
x=alt.X("yearmonthdatehoursminutesseconds(timestamp):O", title="DATE"),
y=alt.Y(f"{sensor}:Q", scale=alt.Scale(type="linear")),
color=alt.Color("WHO", legend=None),
strokeDash=alt.StrokeDash("WHO", sort=None,
legend=alt.Legend(
title=None, symbolType="stroke", symbolFillColor="gray",
symbolStrokeWidth=4, orient="top",
),
),
tooltip=["timestamp", alt.Tooltip(sensor, format=".3f")]
)
metric_chart = metric_chart.properties(
height=340,
width=200
).interactive()
st.altair_chart(metric_chart, use_container_width=True)
###Most Recent Attack Chart###
st.markdown("---")
df = data.loc[:, ["attack"]].copy()
df['filter'] = df["attack"]==1
df_attack = df.loc[df['filter']==True]
attack_start = df_attack.index[0]
cnt = len(df.loc[:attack_start])
while True:
if df.iloc[cnt, :]['filter']==False:
attack_end = df.index[cnt]
break
else:
cnt+=1
st.header("Recent Attack")
st.subheader(str(datetime.strptime(attack_end ,"%Y-%m-%d %H:%M:%S")-datetime.strptime(attack_start, "%Y-%m-%d %H:%M:%S"))+" attack remains")
sensor = st.selectbox("Attacked Sensors", data.columns[:-1])
col1, col2 = st.columns((1, 4))
metric_options = [5, 10, 30, 60]
roll = col1.radio("Rolling Window", options=metric_options)
df = data.loc[attack_start:attack_end].copy()
df1 = df.loc[:, [sensor]].reset_index()[roll:]
df2 = df.loc[:, [sensor]].rolling(roll).mean().reset_index()[roll:]
df1["WHO"] = sensor
df2["WHO"] = "Rolling Mean"
plot_df = pd.concat([df1, df2]).reset_index(drop=True)
plot_df["timestamp"] = pd.to_datetime(plot_df["timestamp"])
metric_chart = alt.Chart(plot_df, title="Trends Over Time", padding={"left": 10, "top": 1, "right": 10, "bottom": 1}
).mark_line().encode(
x=alt.X("yearmonthdatehoursminutesseconds(timestamp):O", title="DATE"),
y=alt.Y(f"{sensor}:Q", scale=alt.Scale(type="linear")),
color=alt.Color("WHO", legend=None),
strokeDash=alt.StrokeDash("WHO", sort=None,
legend=alt.Legend(
title=None, symbolType="stroke", symbolFillColor="gray",
symbolStrokeWidth=4, orient="top",
),
),
tooltip=["timestamp", alt.Tooltip(sensor, format=".3f")]
)
metric_chart = metric_chart.properties(
height=340,
width=200
).interactive()
col2.altair_chart(metric_chart, use_container_width=True)
###Model Interpretability###
st.markdown("---")
ranking = st.sidebar.slider("Select Number of Important Sensor Ranking", 1, 20, value=5)
rel_rank = pd.read_csv('./data/ranking.csv')
top_ranking = rel_rank[:ranking]
conf_plot = alt.Chart(top_ranking, title=f"Top {ranking} important sensors", padding={"left": 1, "top": 10, "right": 1, "bottom": 1}
).mark_bar().encode(
x=alt.X("ABS_MEAN:Q", title="Confidence"),
y=alt.Y("Sensors:N", sort="-x", title="Sensors"),
tooltip=["Sensors", alt.Tooltip("ABS_MEAN", format=".3f")],
color=alt.Color("ABS_MEAN:Q", scale=alt.Scale(), legend=None)
).properties(
height=25 * ranking + 90
).configure_axis(grid=False)
st.altair_chart(conf_plot, use_container_width=True)
if __name__ == '__main__':
main()
alt.themes.enable("default")