arcan3 commited on
Commit
fa2a5c2
·
1 Parent(s): 27ebbbd

added revision

Browse files
Files changed (4) hide show
  1. app.py +7 -3
  2. funcs/convertors.py +4 -1
  3. funcs/processor.py +2 -2
  4. funcs/som.py +25 -3
app.py CHANGED
@@ -178,7 +178,7 @@ def scores_to_dataframe(scores, start_time='2022-07-01 09:15:00+05:30', start_sc
178
  return df
179
 
180
  def get_som_mp4_v2(csv_file_box, slice_size_slider, sample_rate, window_size_slider, reducer=reducer10d, cluster=cluster_som):
181
- 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 = process_data(csv_file_box,
182
  slice_size_slider,
183
  sample_rate,
184
  window_size_slider)
@@ -217,7 +217,6 @@ def get_som_mp4_v2(csv_file_box, slice_size_slider, sample_rate, window_size_sli
217
  low=scores_df['low'],
218
  close=scores_df['close'])])
219
 
220
-
221
  # Write the processed data to a CSV file
222
  header = ['Gait', 'TS', 'State', 'Condition',
223
  'Shape1', 'Shape2', 'Shape3', 'Shape4',
@@ -239,8 +238,13 @@ def get_som_mp4_v2(csv_file_box, slice_size_slider, sample_rate, window_size_sli
239
  # f.write(response.content)
240
 
241
  # prediction = cluster_som.predict(embedding10d)
242
- som_video = cluster.plot_activation(embedding10d)
 
 
 
243
  som_video.write_videofile('som_sequence.mp4')
 
 
244
 
245
  return 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, 'som_sequence.mp4', 'animation.mp4', fig
246
  return 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, 'som_sequence.mp4', None
 
178
  return df
179
 
180
  def get_som_mp4_v2(csv_file_box, slice_size_slider, sample_rate, window_size_slider, reducer=reducer10d, cluster=cluster_som):
181
+ 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, time_list = process_data(csv_file_box,
182
  slice_size_slider,
183
  sample_rate,
184
  window_size_slider)
 
217
  low=scores_df['low'],
218
  close=scores_df['close'])])
219
 
 
220
  # Write the processed data to a CSV file
221
  header = ['Gait', 'TS', 'State', 'Condition',
222
  'Shape1', 'Shape2', 'Shape3', 'Shape4',
 
238
  # f.write(response.content)
239
 
240
  # prediction = cluster_som.predict(embedding10d)
241
+
242
+ # passing the time values for each slice
243
+
244
+ som_video = cluster.plot_activation(embedding10d, times=time_list)
245
  som_video.write_videofile('som_sequence.mp4')
246
+
247
+ # return 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, som_video, 'animation.mp4', fig
248
 
249
  return 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, 'som_sequence.mp4', 'animation.mp4', fig
250
  return 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, 'som_sequence.mp4', None
funcs/convertors.py CHANGED
@@ -31,6 +31,7 @@ def slice_csv_to_json(input_file, slice_size=64, min_slice_size=16, sample_rate=
31
 
32
  slices = []
33
  start_index = 0
 
34
  for i, precise_slice_point in enumerate(precise_slice_points):
35
  end_index = round(precise_slice_point / sample_rate)
36
  if i == 0:
@@ -69,6 +70,8 @@ def slice_csv_to_json(input_file, slice_size=64, min_slice_size=16, sample_rate=
69
  slice_data["timestamp"] = timestamp
70
  slice_data["time_diff"] = time_diff
71
  slice_data["precise_time_diff"] = precise_time_diff
 
 
72
 
73
  if end_index - start_index < slice_size:
74
  pad_size = slice_size - (end_index - start_index)
@@ -89,7 +92,7 @@ def slice_csv_to_json(input_file, slice_size=64, min_slice_size=16, sample_rate=
89
  if debug:
90
  plot_slices(original_data[gz_columns[0]], data[gz_columns[0]], precise_slice_points, precise_slice_points, sample_rate, data.index.values[0])
91
 
92
- return 'output.json', len(slices)
93
 
94
 
95
  def slice_csv_to_json_v2(input_file, slice_size=64, min_slice_size=10, sample_rate=20):
 
31
 
32
  slices = []
33
  start_index = 0
34
+ list_time_diff_for_activation = []
35
  for i, precise_slice_point in enumerate(precise_slice_points):
36
  end_index = round(precise_slice_point / sample_rate)
37
  if i == 0:
 
70
  slice_data["timestamp"] = timestamp
71
  slice_data["time_diff"] = time_diff
72
  slice_data["precise_time_diff"] = precise_time_diff
73
+ list_time_diff_for_activation.append(slice_data["precise_time_diff"])
74
+
75
 
76
  if end_index - start_index < slice_size:
77
  pad_size = slice_size - (end_index - start_index)
 
92
  if debug:
93
  plot_slices(original_data[gz_columns[0]], data[gz_columns[0]], precise_slice_points, precise_slice_points, sample_rate, data.index.values[0])
94
 
95
+ return 'output.json', len(slices), list_time_diff_for_activation
96
 
97
 
98
  def slice_csv_to_json_v2(input_file, slice_size=64, min_slice_size=10, sample_rate=20):
funcs/processor.py CHANGED
@@ -73,10 +73,10 @@ def process_data(input_file, slice_size=64, sample_rate=20, window_size=40, min_
73
  # Save the resulting DataFrame to a new file
74
  data.to_csv('output.csv', sep=";", na_rep="NaN", float_format="%.0f")
75
 
76
- file, len_ = slice_csv_to_json('output.csv', slice_size, min_slice_size, sample_rate, window_size=window_size)
77
  # file, len_ = slice_csv_to_json_v2('output.csv', slice_size, min_slice_size, sample_rate)
78
  # get the plot automatically
79
  sensor_fig, slice_fig, get_all_slice, slice_json, overlay_fig = plot_sensor_data_from_json(file, "GZ1") # with the csv file
80
  # overlay_fig = plot_overlay_data_from_json(file, ["GZ1", "GZ2", "GZ3", "GZ4"])
81
 
82
- return 'output.csv', file, f'num of slices found: {len_}', sensor_fig, overlay_fig, gr.Slider.update(interactive=True, maximum=len_, minimum=1, value=1), slice_fig, get_all_slice, slice_json
 
73
  # Save the resulting DataFrame to a new file
74
  data.to_csv('output.csv', sep=";", na_rep="NaN", float_format="%.0f")
75
 
76
+ file, len_, time_list = slice_csv_to_json('output.csv', slice_size, min_slice_size, sample_rate, window_size=window_size)
77
  # file, len_ = slice_csv_to_json_v2('output.csv', slice_size, min_slice_size, sample_rate)
78
  # get the plot automatically
79
  sensor_fig, slice_fig, get_all_slice, slice_json, overlay_fig = plot_sensor_data_from_json(file, "GZ1") # with the csv file
80
  # overlay_fig = plot_overlay_data_from_json(file, ["GZ1", "GZ2", "GZ3", "GZ4"])
81
 
82
+ return 'output.csv', file, f'num of slices found: {len_}', sensor_fig, overlay_fig, gr.Slider.update(interactive=True, maximum=len_, minimum=1, value=1), slice_fig, get_all_slice, slice_json, time_list
funcs/som.py CHANGED
@@ -7,7 +7,7 @@ import numpy as np
7
  import matplotlib.pyplot as plt
8
 
9
  from tqdm import tqdm
10
- from moviepy.editor import ImageSequenceClip
11
 
12
  class ClusterSOM:
13
  def __init__(self):
@@ -125,7 +125,7 @@ class ClusterSOM:
125
 
126
  return fig, axes
127
 
128
- def plot_activation(self, data, start=None, end=None):
129
  """
130
  Generate a GIF visualization of the prediction output using the activation maps of individual SOMs.
131
  """
@@ -189,8 +189,30 @@ class ClusterSOM:
189
  # if times is None:
190
  # times = [500 for _ in range(len(images))]
191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  # Create the video using moviepy and save it as a mp4 file
193
- video = ImageSequenceClip(images, fps=30)
194
 
195
  return video
196
 
 
7
  import matplotlib.pyplot as plt
8
 
9
  from tqdm import tqdm
10
+ from moviepy.editor import ImageSequenceClip, VideoFileClip
11
 
12
  class ClusterSOM:
13
  def __init__(self):
 
125
 
126
  return fig, axes
127
 
128
+ def plot_activation(self, data, start=None, end=None, times=None):
129
  """
130
  Generate a GIF visualization of the prediction output using the activation maps of individual SOMs.
131
  """
 
189
  # if times is None:
190
  # times = [500 for _ in range(len(images))]
191
 
192
+ # # Make sure `times` has the same length as `images`.
193
+ # times = times[1:]
194
+ # times = [int(t) for t in times]
195
+ # if len(times) != len(images):
196
+ # raise ValueError("`times` must have the same length as the number of frames.")
197
+
198
+ # # Save the images as a GIF with custom durations.
199
+ # imageio.mimsave("som_gif.gif", images, duration=[t / 1000 for t in times], loop=1)
200
+
201
+ # # Load the gif
202
+ # gif_file = "som_gif.gif"
203
+ # clip = VideoFileClip(gif_file)
204
+
205
+ # # Convert the gif to mp4
206
+ # mp4_file = "som_gif.mp4"
207
+ # clip.write_videofile(mp4_file, codec='libx264')
208
+
209
+ # # Close the clip to release resources
210
+ # clip.close()
211
+
212
+ # return "som_gif.mp4"
213
+
214
  # Create the video using moviepy and save it as a mp4 file
215
+ video = ImageSequenceClip(images, fps=2)
216
 
217
  return video
218