Jasper Siebelink
Caching of LSTM model
13afdbd
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
import json
from isolation_forest import apply_isolation_forest
from lstm import apply_lstm
from oc_svm import apply_oc_svm
# Title for Streamlit app
st.title('Cattle logfile analysis')
col1, col2 = st.columns(2)
# Content from upload
if 'json_content' not in st.session_state:
st.session_state.json_content = None
st.session_state.json_content = None
st.session_state.json_content = None
# Select dimensions
num_dimensions = st.selectbox('Select number of dimensions:', [1, 2, 3],
index=2)
# Select Algorithm
options = ["Isolation Forest", "One-Class Support Vector Machine", "Long Short-Term Memory", "None"]
algorithm_box = st.selectbox('Select algorithm:',
options,
index=0)
with col1:
with st.container(border=True):
uploaded_file = st.file_uploader("Upload JSON", type="json")
if uploaded_file:
st.session_state.json_content = json.loads(uploaded_file.getvalue())
# Content from local file
with col2:
with st.container(border=True):
st.write('Load embedded JSON')
if st.button('Load'):
with open('cattle_log.json', 'r') as file:
st.session_state.json_content = json.load(file)
if st.session_state.json_content:
X = []
# Iterate over each log entry in the log_content
for log_entry in st.session_state.json_content['logs']:
# Extract and convert the necessary attributes
total_today_str = log_entry['distanceTraveled']['totalToday'].rstrip('m')
heart_rate = int(log_entry['healthData']['heartRate']) # Assuming heart rate is always an integer
weight_str = log_entry['healthData']['weight'].rstrip('kg')
# Convert the distance and weight to floating-point values
total_today = float(total_today_str) # Convert distance to float
weight = float(weight_str) # Convert weight to float
# Create a 3D vector for the current log entry and append it to the list of vectors
vector_3d = [total_today, heart_rate, weight]
X.append(vector_3d)
# Convert X into a NumPy array for easier slicing
X = np.array(X)
# Generating synthetic data
rng = np.random.RandomState(42)
selected_algorithm_index = options.index(algorithm_box)
if selected_algorithm_index == 0:
plotted_result = apply_isolation_forest(rng,
X)
elif selected_algorithm_index == 1:
plotted_result = apply_oc_svm(X)
elif selected_algorithm_index == 2:
plotted_result = apply_lstm(X)
else:
plotted_result = array = [-1] * 50
# Create a figure
fig, ax = plt.subplots(figsize=(10, 7), subplot_kw={'projection': '3d'} if num_dimensions == 3 else {})
# Configure the plot based on the number of dimensions
if num_dimensions == 3:
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=['red' if pred == -1 else 'blue' for pred in plotted_result], s=50)
ax.set_xlabel("Distance travelled")
ax.set_ylabel("Heartrate")
ax.set_zlabel("Weight")
else:
x_axis = X[:, 0]
y_axis = np.zeros_like(X[:, 0]) if num_dimensions == 1 else X[:, 1]
ax.scatter(x_axis, y_axis, c=['red' if pred == -1 else 'blue' for pred in plotted_result], s=50)
ax.set_xlabel("Distance travelled")
ax.set_ylabel("Heartrate" if num_dimensions > 1 else "")
# Set common properties and show plot
ax.set_title(algorithm_box)
ax.grid(True)
st.pyplot(fig)