Spaces:
Sleeping
Sleeping
LiesDillen
commited on
Commit
•
e6d4283
1
Parent(s):
ad4ad85
first commit with independant interface
Browse files- interface.py +95 -0
interface.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import gradio as gr
|
4 |
+
import plotly.graph_objs as go
|
5 |
+
from plotly.subplots import make_subplots
|
6 |
+
import datetime
|
7 |
+
|
8 |
+
|
9 |
+
# Function to load data from a text file into a numpy array of the right format
|
10 |
+
def load_data(filename):
|
11 |
+
with open(filename, 'r') as file:
|
12 |
+
data = [int(line.strip()) for line in file]
|
13 |
+
data = np.array(data, dtype=int)
|
14 |
+
data[data == -100] = -1
|
15 |
+
return data.reshape((-1, 1))
|
16 |
+
|
17 |
+
|
18 |
+
def process_csv(file):
|
19 |
+
|
20 |
+
if file is None:
|
21 |
+
gr.Warning("No file uploaded")
|
22 |
+
|
23 |
+
# Load the data
|
24 |
+
acc_data = np.load(file)
|
25 |
+
predicted_labels = load_data('003_1_R.txt')
|
26 |
+
|
27 |
+
# Append the new columns
|
28 |
+
complete_array = np.hstack((acc_data, predicted_labels))
|
29 |
+
|
30 |
+
# Define the time axis
|
31 |
+
duration = 33.333333 # milliseconds
|
32 |
+
num_samples = complete_array.shape[0]
|
33 |
+
time_axis = np.arange(0, num_samples * duration, duration)
|
34 |
+
|
35 |
+
# Convert time axis to datetime objects
|
36 |
+
start_time = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
37 |
+
time_axis_datetime = [start_time + datetime.timedelta(milliseconds=int(t)) for t in time_axis]
|
38 |
+
|
39 |
+
# Convert datetime objects to formatted strings
|
40 |
+
time_axis_labels = [t.strftime('%H:%M:%S.%f')[:-3] for t in time_axis_datetime]
|
41 |
+
|
42 |
+
# Calculate the total number of predicted functional and non-functional activity
|
43 |
+
total_predicted_functional = np.sum(complete_array[:, 3] != 0)
|
44 |
+
total_predicted_non_functional = np.sum(complete_array[:, 3] == 0)
|
45 |
+
|
46 |
+
# Calculate percentages
|
47 |
+
predicted_functional_percentage = (total_predicted_functional / len(complete_array)) * 100
|
48 |
+
predicted_non_functional_percentage = (total_predicted_non_functional / len(complete_array)) * 100
|
49 |
+
|
50 |
+
# Formulate return string
|
51 |
+
return_string = f"Percentage of predicted functional activity: {predicted_functional_percentage:.2f}%\nPercentage of predicted non-functional activity: {predicted_non_functional_percentage:.2f}%"
|
52 |
+
|
53 |
+
# Create subplots
|
54 |
+
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, row_heights=[0.6, 0.4], specs=[[{"type": "scatter"}], [{"type": "scatter"}]])
|
55 |
+
|
56 |
+
# Add traces to the subplots
|
57 |
+
fig.add_trace(go.Scatter(x=time_axis_labels, y=complete_array[:, 0], mode='lines', name='Acc X', line=dict(width=0.75)), row=1, col=1)
|
58 |
+
fig.add_trace(go.Scatter(x=time_axis_labels, y=complete_array[:, 1], mode='lines', name='Acc Y', line=dict(width=0.75)), row=1, col=1)
|
59 |
+
fig.add_trace(go.Scatter(x=time_axis_labels, y=complete_array[:, 2], mode='lines', name='Acc Z', line=dict(width=0.75)), row=1, col=1)
|
60 |
+
fig.add_trace(go.Scatter(x=time_axis_labels, y=complete_array[:, 3], mode='lines', name='Predicted labels', line=dict(width=1)), row=2, col=1)
|
61 |
+
|
62 |
+
|
63 |
+
# Update layout
|
64 |
+
fig.update_layout(
|
65 |
+
title='Accelerometer Data with Annotated Labels',
|
66 |
+
xaxis=dict(title='Time (milliseconds)'),
|
67 |
+
yaxis=dict(title='Accelerometer Data'),
|
68 |
+
yaxis2=dict(title='Predicted'),
|
69 |
+
showlegend=True,
|
70 |
+
height=600
|
71 |
+
)
|
72 |
+
|
73 |
+
return return_string, fig
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
78 |
+
gr.Markdown(
|
79 |
+
"""
|
80 |
+
# Functional Upper Limb Activity Recognition Model
|
81 |
+
Upload your csv file containing accelerometer data to obtain a prediction on the amount of functional activity of the upper limbs.
|
82 |
+
""")
|
83 |
+
with gr.Row(equal_height=True):
|
84 |
+
with gr.Column():
|
85 |
+
input_file = gr.File(label="Upload CSV file")
|
86 |
+
with gr.Row():
|
87 |
+
submit_btn = gr.Button("Submit", variant='primary')
|
88 |
+
clear_btn = gr.ClearButton(input_file, variant='secondary')
|
89 |
+
output_text = gr.Textbox(label="Prediction statistics")
|
90 |
+
output_plot = gr.Plot(label="CSV Plot")
|
91 |
+
|
92 |
+
submit_btn.click(fn=process_csv, inputs=input_file, outputs=[output_text, output_plot])
|
93 |
+
clear_btn.click(fn=lambda: input_file.reinit())
|
94 |
+
|
95 |
+
demo.launch()
|