File size: 3,675 Bytes
ed76b91
 
 
 
 
06b052d
0f6f971
06b052d
 
ed76b91
06b052d
ed76b91
9ab5be6
 
 
06b052d
 
 
 
 
 
 
 
 
 
 
7f01234
0f6f971
 
 
06b052d
9ab5be6
 
 
 
06b052d
9ab5be6
 
 
 
 
 
 
06b052d
ed76b91
 
06b052d
ed76b91
 
 
06b052d
ed76b91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f6f971
 
06b052d
 
0f6f971
06b052d
7f01234
0f6f971
7f01234
 
ed76b91
06b052d
 
 
 
ed76b91
06b052d
ed76b91
 
 
06b052d
 
 
ed76b91
06b052d
 
 
 
 
0f6f971
06b052d
 
ed76b91
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
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)