Spaces:
Build error
Build error
the slice highlight problem is solved
Browse files- funcs/plot_func.py +1 -3
- funcs/som.py +0 -15
- requirements.txt +3 -0
- test.py +53 -44
funcs/plot_func.py
CHANGED
|
@@ -44,9 +44,7 @@ def plot_sensor_data_from_json(json_file, sensor, slice_select=1):
|
|
| 44 |
ax = plt.plot(data['time'].to_list(), data[sensor].to_list(), '-b')
|
| 45 |
|
| 46 |
df_temp = data[data['slice selection'] == int(slice_select)].reset_index()
|
| 47 |
-
|
| 48 |
-
# x = data['time'].to_list()
|
| 49 |
-
# ax = plt.plot(x, y, '-')
|
| 50 |
|
| 51 |
plt.xlabel("Timestamp")
|
| 52 |
plt.ylabel(sensor)
|
|
|
|
| 44 |
ax = plt.plot(data['time'].to_list(), data[sensor].to_list(), '-b')
|
| 45 |
|
| 46 |
df_temp = data[data['slice selection'] == int(slice_select)].reset_index()
|
| 47 |
+
ax = plt.plot(df_temp['time'].to_list(), df_temp[sensor].to_list(), '-r')
|
|
|
|
|
|
|
| 48 |
|
| 49 |
plt.xlabel("Timestamp")
|
| 50 |
plt.ylabel(sensor)
|
funcs/som.py
CHANGED
|
@@ -396,21 +396,6 @@ class ClusterSOM:
|
|
| 396 |
|
| 397 |
return video
|
| 398 |
|
| 399 |
-
|
| 400 |
-
# # Save the images as a GIF
|
| 401 |
-
# imageio.mimsave(f"{filename}.gif", images, duration=500, loop=1)
|
| 402 |
-
|
| 403 |
-
# # Load the gif
|
| 404 |
-
# gif_file = f"{filename}.gif" # Replace with the path to your GIF file
|
| 405 |
-
# clip = VideoFileClip(gif_file)
|
| 406 |
-
|
| 407 |
-
# # Convert the gif to mp4
|
| 408 |
-
# mp4_file = f"{filename}.mp4" # Replace with the desired output path
|
| 409 |
-
# clip.write_videofile(mp4_file, codec='libx264')
|
| 410 |
-
|
| 411 |
-
# # Close the clip to release resources
|
| 412 |
-
# clip.close()
|
| 413 |
-
|
| 414 |
def save(self, file_path):
|
| 415 |
"""
|
| 416 |
Save the ClusterSOM model to a file.
|
|
|
|
| 396 |
|
| 397 |
return video
|
| 398 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
def save(self, file_path):
|
| 400 |
"""
|
| 401 |
Save the ClusterSOM model to a file.
|
requirements.txt
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
|
|
| 1 |
aiofiles==23.1.0
|
| 2 |
aiohttp==3.8.4
|
| 3 |
aiosignal==1.3.1
|
| 4 |
altair==4.2.2
|
| 5 |
anyio==3.6.2
|
|
|
|
| 6 |
appnope==0.1.3
|
| 7 |
asttokens==2.2.1
|
| 8 |
async-timeout==4.0.2
|
|
@@ -86,6 +88,7 @@ pyparsing==3.0.9
|
|
| 86 |
pyrsistent==0.19.3
|
| 87 |
python-dateutil==2.8.2
|
| 88 |
python-multipart==0.0.6
|
|
|
|
| 89 |
pytz==2023.3
|
| 90 |
PyWavelets==1.4.1
|
| 91 |
PyYAML==6.0
|
|
|
|
| 1 |
+
absl-py==1.4.0
|
| 2 |
aiofiles==23.1.0
|
| 3 |
aiohttp==3.8.4
|
| 4 |
aiosignal==1.3.1
|
| 5 |
altair==4.2.2
|
| 6 |
anyio==3.6.2
|
| 7 |
+
apischema==0.18.0
|
| 8 |
appnope==0.1.3
|
| 9 |
asttokens==2.2.1
|
| 10 |
async-timeout==4.0.2
|
|
|
|
| 88 |
pyrsistent==0.19.3
|
| 89 |
python-dateutil==2.8.2
|
| 90 |
python-multipart==0.0.6
|
| 91 |
+
pytvlwcharts @ git+https://github.com/TechfaneTechnologies/pytvlwcharts.git@9bc0f70f9bc9569705947e3b7225e109cd01c631
|
| 92 |
pytz==2023.3
|
| 93 |
PyWavelets==1.4.1
|
| 94 |
PyYAML==6.0
|
test.py
CHANGED
|
@@ -1,47 +1,56 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import gradio as gr
|
| 3 |
import json
|
| 4 |
-
import os
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
-
|
| 7 |
-
from phate import PHATEAE
|
| 8 |
-
from funcs.som import ClusterSOM
|
| 9 |
-
from funcs.tools import numpy_to_native
|
| 10 |
-
|
| 11 |
-
from funcs.processor import process_data
|
| 12 |
-
from funcs.plot_func import plot_sensor_data_from_json
|
| 13 |
-
from funcs.dataloader import BaseDataset2, read_json_files
|
| 14 |
-
|
| 15 |
-
DEVICE = torch.device("cpu")
|
| 16 |
-
reducer10d = PHATEAE(epochs=30, n_components=10, lr=.0001, batch_size=128, t='auto', knn=8, relax=True, metric='euclidean')
|
| 17 |
-
reducer10d.load('models/r10d_2.pth')
|
| 18 |
-
|
| 19 |
-
cluster_som = ClusterSOM()
|
| 20 |
-
cluster_som.load("models/cluster_som2.pkl")
|
| 21 |
-
|
| 22 |
-
# ml inference
|
| 23 |
-
def get_som_mp4(file, slice_select, reducer=reducer10d, cluster=cluster_som):
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
train_x, train_y = read_json_files(file.name)
|
| 29 |
-
|
| 30 |
-
# Convert tensors to numpy arrays if necessary
|
| 31 |
-
if isinstance(train_x, torch.Tensor):
|
| 32 |
-
train_x = train_x.numpy()
|
| 33 |
-
if isinstance(train_y, torch.Tensor):
|
| 34 |
-
train_y = train_y.numpy()
|
| 35 |
-
|
| 36 |
-
# load the time series slices of the data 4*3*2*64 (feeds+axis*sensor*samples) + 5 for time diff
|
| 37 |
-
data = BaseDataset2(train_x.reshape(len(train_x), -1) / 32768, train_y)
|
| 38 |
-
|
| 39 |
-
#compute the 10 dimensional embeding vector
|
| 40 |
-
embedding10d = reducer.transform(data)
|
| 41 |
-
|
| 42 |
-
# prediction = cluster_som.predict(embedding10d)
|
| 43 |
-
fig = cluster.plot_activation_v2(embedding10d, slice_select)
|
| 44 |
-
plt.savefig('test.png')
|
| 45 |
-
return fig
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
sensor = 'GZ1'
|
| 8 |
+
slice_select = 2
|
| 9 |
+
|
| 10 |
+
json_file = '/Users/ankit/Documents/cabasus/output.json'
|
| 11 |
+
# Read the JSON file
|
| 12 |
+
try:
|
| 13 |
+
with open(json_file, "r") as f:
|
| 14 |
+
slices = json.load(f)
|
| 15 |
+
except:
|
| 16 |
+
with open(json_file.name, "r") as f:
|
| 17 |
+
slices = json.load(f)
|
| 18 |
+
|
| 19 |
+
# Concatenate the slices and create a new timestamp series with 20ms intervals
|
| 20 |
+
timestamps = []
|
| 21 |
+
sensor_data = []
|
| 22 |
+
slice_item = []
|
| 23 |
+
temp_end = 0
|
| 24 |
+
for slice_count, slice_dict in enumerate(slices):
|
| 25 |
+
start_timestamp = slice_dict["timestamp"]
|
| 26 |
+
slice_length = len(slice_dict[sensor])
|
| 27 |
+
|
| 28 |
+
slice_timestamps = [start_timestamp + 20 * i for i in range(temp_end, slice_length + temp_end)]
|
| 29 |
+
timestamps.extend(slice_timestamps)
|
| 30 |
+
sensor_data.extend(slice_dict[sensor])
|
| 31 |
+
|
| 32 |
+
temp_end += slice_length
|
| 33 |
+
slice_item.extend([slice_count+1]*len(slice_timestamps))
|
| 34 |
+
|
| 35 |
+
# Create a DataFrame with the sensor data
|
| 36 |
+
data = pd.DataFrame({sensor: sensor_data, 'slice selection': slice_item, 'time': timestamps})
|
| 37 |
+
|
| 38 |
+
# Plot the sensor data
|
| 39 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
| 40 |
+
ax = plt.plot(data['time'].to_list(), data[sensor].to_list(), '-b')
|
| 41 |
+
|
| 42 |
+
df_temp = data[data['slice selection'] == int(slice_select)].reset_index()
|
| 43 |
+
ax = plt.plot(df_temp['time'].to_list(), df_temp[sensor].to_list(), '-r')
|
| 44 |
+
|
| 45 |
+
plt.xlabel("Timestamp")
|
| 46 |
+
plt.ylabel(sensor)
|
| 47 |
+
plt.legend()
|
| 48 |
+
plt.tight_layout()
|
| 49 |
+
|
| 50 |
+
fig1, ax1 = plt.subplots(figsize=(12, 6))
|
| 51 |
+
ax1 = plt.plot(df_temp['time'].to_list(), df_temp[sensor].to_list())
|
| 52 |
+
|
| 53 |
+
plt.xlabel("Timestamp")
|
| 54 |
+
plt.ylabel(sensor)
|
| 55 |
+
plt.legend()
|
| 56 |
+
plt.tight_layout()
|