Spaces:
Build error
Build error
arcan3
commited on
Commit
·
611742b
1
Parent(s):
247dc37
added json labelling file
Browse files- .gitignore +1 -0
- app.py +32 -5
- funcs/plot_func.py +26 -4
- funcs/processor.py +2 -2
.gitignore
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
# Byte-compiled / optimized / DLL files
|
2 |
*.mp4
|
3 |
*.xz
|
|
|
4 |
*.gif
|
5 |
*.zip
|
6 |
*.png
|
|
|
1 |
# Byte-compiled / optimized / DLL files
|
2 |
*.mp4
|
3 |
*.xz
|
4 |
+
*.json
|
5 |
*.gif
|
6 |
*.zip
|
7 |
*.png
|
app.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
import torch
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
from phate import PHATEAE
|
5 |
from funcs.som import ClusterSOM
|
|
|
6 |
|
7 |
from funcs.processor import process_data
|
8 |
from funcs.plot_func import plot_sensor_data_from_json
|
@@ -40,6 +43,23 @@ def get_som_mp4(file, slice_select=1, reducer=reducer10d, cluster=cluster_som):
|
|
40 |
|
41 |
return fig
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
with gr.Blocks(title='Cabasus') as cabasus_sensor:
|
44 |
title = gr.Markdown("<h2><center>Data gathering and processing</center></h2>")
|
45 |
with gr.Tab("Convert"):
|
@@ -67,20 +87,27 @@ with gr.Blocks(title='Cabasus') as cabasus_sensor:
|
|
67 |
get_all_slice = gr.Plot(label="Real Signal Plot")
|
68 |
|
69 |
som_create = gr.Button('generate som')
|
|
|
70 |
with gr.Row():
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
slices_per_leg = gr.Textbox(label="Number of slices found per LEG")
|
74 |
|
75 |
csv_file_box.change(process_data, inputs=[csv_file_box, slice_size_slider, sample_rate, window_size_slider],
|
76 |
-
outputs=[processed_file_box, json_file_box, slices_per_leg, plot_box_leg, plot_box_overlay, slice_slider, plot_slice_leg, get_all_slice])
|
77 |
leg_dropdown.change(plot_sensor_data_from_json, inputs=[json_file_box, leg_dropdown, slice_slider],
|
78 |
-
outputs=[plot_box_leg, plot_slice_leg, get_all_slice])
|
79 |
repeat_process.click(process_data, inputs=[csv_file_box, slice_size_slider, sample_rate, window_size_slider],
|
80 |
-
outputs=[processed_file_box, json_file_box, slices_per_leg, plot_box_leg, plot_box_overlay, slice_slider, plot_slice_leg, get_all_slice])
|
81 |
slice_slider.change(plot_sensor_data_from_json, inputs=[json_file_box, leg_dropdown, slice_slider],
|
82 |
-
outputs=[plot_box_leg, plot_slice_leg, get_all_slice])
|
83 |
|
84 |
som_create.click(get_som_mp4, inputs=[json_file_box, slice_slider], outputs=[som_figures])
|
|
|
85 |
|
86 |
cabasus_sensor.queue(concurrency_count=2).launch(debug=True)
|
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
+
import json
|
4 |
+
import os
|
5 |
|
6 |
from phate import PHATEAE
|
7 |
from funcs.som import ClusterSOM
|
8 |
+
from funcs.tools import numpy_to_native
|
9 |
|
10 |
from funcs.processor import process_data
|
11 |
from funcs.plot_func import plot_sensor_data_from_json
|
|
|
43 |
|
44 |
return fig
|
45 |
|
46 |
+
def attach_label_to_json(json_file, label_text):
|
47 |
+
# Read the JSON file
|
48 |
+
try:
|
49 |
+
with open(json_file, "r") as f:
|
50 |
+
slices = json.load(f)
|
51 |
+
except:
|
52 |
+
with open(json_file.name, "r") as f:
|
53 |
+
slices = json.load(f)
|
54 |
+
|
55 |
+
slices['label'] = label_text
|
56 |
+
|
57 |
+
with open(f'manual_labelled_{os.path.basename(json_file.name)}', "w") as f:
|
58 |
+
json.dump(numpy_to_native(slices), f, indent=2)
|
59 |
+
|
60 |
+
return f'manual_labelled_{os.path.basename(json_file.name)}'
|
61 |
+
|
62 |
+
|
63 |
with gr.Blocks(title='Cabasus') as cabasus_sensor:
|
64 |
title = gr.Markdown("<h2><center>Data gathering and processing</center></h2>")
|
65 |
with gr.Tab("Convert"):
|
|
|
87 |
get_all_slice = gr.Plot(label="Real Signal Plot")
|
88 |
|
89 |
som_create = gr.Button('generate som')
|
90 |
+
som_figures = gr.Plot(label="som activations")
|
91 |
with gr.Row():
|
92 |
+
slice_json_box = gr.File(label='Slice json file')
|
93 |
+
with gr.Column():
|
94 |
+
label_name = gr.Textbox(label="enter the label name")
|
95 |
+
button_label_Add = gr.Button('attach label')
|
96 |
+
slice_json_label_box = gr.File(label='Slice json labelled file')
|
97 |
+
|
98 |
|
99 |
slices_per_leg = gr.Textbox(label="Number of slices found per LEG")
|
100 |
|
101 |
csv_file_box.change(process_data, inputs=[csv_file_box, slice_size_slider, sample_rate, window_size_slider],
|
102 |
+
outputs=[processed_file_box, json_file_box, slices_per_leg, plot_box_leg, plot_box_overlay, slice_slider, plot_slice_leg, get_all_slice, slice_json_box])
|
103 |
leg_dropdown.change(plot_sensor_data_from_json, inputs=[json_file_box, leg_dropdown, slice_slider],
|
104 |
+
outputs=[plot_box_leg, plot_slice_leg, get_all_slice, slice_json_box])
|
105 |
repeat_process.click(process_data, inputs=[csv_file_box, slice_size_slider, sample_rate, window_size_slider],
|
106 |
+
outputs=[processed_file_box, json_file_box, slices_per_leg, plot_box_leg, plot_box_overlay, slice_slider, plot_slice_leg, get_all_slice, slice_json_box])
|
107 |
slice_slider.change(plot_sensor_data_from_json, inputs=[json_file_box, leg_dropdown, slice_slider],
|
108 |
+
outputs=[plot_box_leg, plot_slice_leg, get_all_slice, slice_json_box])
|
109 |
|
110 |
som_create.click(get_som_mp4, inputs=[json_file_box, slice_slider], outputs=[som_figures])
|
111 |
+
button_label_Add.click(attach_label_to_json, inputs=[slice_json_box, label_name], outputs=[slice_json_label_box])
|
112 |
|
113 |
cabasus_sensor.queue(concurrency_count=2).launch(debug=True)
|
funcs/plot_func.py
CHANGED
@@ -6,7 +6,7 @@ import pandas as pd
|
|
6 |
import seaborn as sns
|
7 |
import matplotlib.pyplot as plt
|
8 |
|
9 |
-
from funcs.
|
10 |
|
11 |
matplotlib.use('Agg')
|
12 |
plt.style.use('ggplot')
|
@@ -61,7 +61,9 @@ def plot_sensor_data_from_json(json_file, sensor, slice_select=1):
|
|
61 |
plt.legend()
|
62 |
plt.tight_layout()
|
63 |
|
64 |
-
|
|
|
|
|
65 |
|
66 |
def plot_overlay_data_from_json(json_file, sensors, use_precise_timestamp=False):
|
67 |
# Read the JSON file
|
@@ -120,6 +122,9 @@ def plot_slices(original_signal, imputed_signal, precise_slice_points, normal_sl
|
|
120 |
return True
|
121 |
|
122 |
def plot_other_sensor_with_same_timestamp(json_file, sensor, slice_select):
|
|
|
|
|
|
|
123 |
# Read the JSON file
|
124 |
try:
|
125 |
with open(json_file, "r") as f:
|
@@ -132,6 +137,7 @@ def plot_other_sensor_with_same_timestamp(json_file, sensor, slice_select):
|
|
132 |
timestamps = []
|
133 |
sensor_data = []
|
134 |
slice_item = []
|
|
|
135 |
|
136 |
for slice_count, slice_dict in enumerate(slices):
|
137 |
if slice_count+1 == slice_select:
|
@@ -141,7 +147,7 @@ def plot_other_sensor_with_same_timestamp(json_file, sensor, slice_select):
|
|
141 |
for slist in slice_dict.keys():
|
142 |
if slist[-1] != sensor[-1]:
|
143 |
continue
|
144 |
-
|
145 |
slice_length = len(slice_dict[slist])
|
146 |
|
147 |
slice_timestamps = [start_timestamp + 20 * i for i in range(slice_length)]
|
@@ -163,4 +169,20 @@ def plot_other_sensor_with_same_timestamp(json_file, sensor, slice_select):
|
|
163 |
plt.legend()
|
164 |
plt.tight_layout()
|
165 |
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import seaborn as sns
|
7 |
import matplotlib.pyplot as plt
|
8 |
|
9 |
+
from funcs.tools import numpy_to_native
|
10 |
|
11 |
matplotlib.use('Agg')
|
12 |
plt.style.use('ggplot')
|
|
|
61 |
plt.legend()
|
62 |
plt.tight_layout()
|
63 |
|
64 |
+
fig2, file = plot_other_sensor_with_same_timestamp(json_file, sensor, slice_select)
|
65 |
+
|
66 |
+
return fig, fig1, fig2, file
|
67 |
|
68 |
def plot_overlay_data_from_json(json_file, sensors, use_precise_timestamp=False):
|
69 |
# Read the JSON file
|
|
|
122 |
return True
|
123 |
|
124 |
def plot_other_sensor_with_same_timestamp(json_file, sensor, slice_select):
|
125 |
+
constant_keys = [f"{sensor}_precise_time_diff", "precise_timestamp",
|
126 |
+
'timestamp', "time_diff", "precise_time_diff"]
|
127 |
+
|
128 |
# Read the JSON file
|
129 |
try:
|
130 |
with open(json_file, "r") as f:
|
|
|
137 |
timestamps = []
|
138 |
sensor_data = []
|
139 |
slice_item = []
|
140 |
+
slice_recorded = []
|
141 |
|
142 |
for slice_count, slice_dict in enumerate(slices):
|
143 |
if slice_count+1 == slice_select:
|
|
|
147 |
for slist in slice_dict.keys():
|
148 |
if slist[-1] != sensor[-1]:
|
149 |
continue
|
150 |
+
slice_recorded.append(slist)
|
151 |
slice_length = len(slice_dict[slist])
|
152 |
|
153 |
slice_timestamps = [start_timestamp + 20 * i for i in range(slice_length)]
|
|
|
169 |
plt.legend()
|
170 |
plt.tight_layout()
|
171 |
|
172 |
+
#create a new dictionary
|
173 |
+
total_keys = slice_recorded + constant_keys
|
174 |
+
json_dict = {}
|
175 |
+
for tkeys in total_keys:
|
176 |
+
json_dict[tkeys] = slice_dict[tkeys]
|
177 |
+
# json_dict = {'data': sensor_data, 'sensor': slice_item, 'time': timestamps,
|
178 |
+
# f"{sensor}_precise_time_diff": slice_dict[f"{sensor}_precise_time_diff"],
|
179 |
+
# "precise_timestamp": slice_dict["precise_timestamp"],
|
180 |
+
# "timestamp": slice_dict["timestamp"],
|
181 |
+
# "time_diff": slice_dict["time_diff"],
|
182 |
+
# "precise_time_diff": slice_dict["precise_time_diff"],
|
183 |
+
# "label": 'unknown'}
|
184 |
+
|
185 |
+
with open(f'slice_{slice_select}.json', "w") as f:
|
186 |
+
json.dump(numpy_to_native(json_dict), f, indent=2)
|
187 |
+
|
188 |
+
return fig, f'slice_{slice_select}.json'
|
funcs/processor.py
CHANGED
@@ -78,7 +78,7 @@ def process_data(input_file, slice_size=64, min_slice_size=16, sample_rate=20, w
|
|
78 |
file, len_ = slice_csv_to_json('output.csv', slice_size, min_slice_size, sample_rate, window_size=window_size)
|
79 |
|
80 |
# get the plot automatically
|
81 |
-
sensor_fig, slice_fig, get_all_slice = plot_sensor_data_from_json(file, "GZ1")
|
82 |
overlay_fig = plot_overlay_data_from_json(file, ["GZ1", "GZ2", "GZ3", "GZ4"], use_precise_timestamp=True)
|
83 |
|
84 |
-
return 'output.csv', file, f'{len_}', sensor_fig, overlay_fig, gr.Slider.update(interactive=True, maximum=len_, minimum=1, value=1), slice_fig, get_all_slice
|
|
|
78 |
file, len_ = slice_csv_to_json('output.csv', slice_size, min_slice_size, sample_rate, window_size=window_size)
|
79 |
|
80 |
# get the plot automatically
|
81 |
+
sensor_fig, slice_fig, get_all_slice, slice_json = plot_sensor_data_from_json(file, "GZ1")
|
82 |
overlay_fig = plot_overlay_data_from_json(file, ["GZ1", "GZ2", "GZ3", "GZ4"], use_precise_timestamp=True)
|
83 |
|
84 |
+
return 'output.csv', file, f'{len_}', sensor_fig, overlay_fig, gr.Slider.update(interactive=True, maximum=len_, minimum=1, value=1), slice_fig, get_all_slice, slice_json
|