dBHz commited on
Commit
9483479
·
verified ·
1 Parent(s): 9f173a5

Upload 2 files

Browse files
Files changed (2) hide show
  1. board_plot.py +107 -0
  2. ring_plot.py +41 -0
board_plot.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import compress_pickle
3
+ import random
4
+ import os
5
+ import glob
6
+
7
+
8
+ def load_data(filename):
9
+ return compress_pickle.load(filename)
10
+
11
+
12
+ class Point:
13
+ def __init__(self, x, y, timestamp, force=1):
14
+ self.x = x
15
+ self.y = y
16
+ self.timestamp = timestamp
17
+ self.force = force
18
+
19
+ def __repr__(self):
20
+ return f"Point(x={self.x}, y={self.y}, timestamp={self.timestamp})"
21
+
22
+
23
+ def visualize_touch_data(
24
+ directory, start_time, end_time, name="raw", if_save=False, save_path=None
25
+ ):
26
+ files = glob.glob(os.path.join(directory, "*.gz"))
27
+ fig, ax = plt.subplots()
28
+
29
+ all_x_coords = []
30
+ all_y_coords = []
31
+ all_forces = []
32
+
33
+ for file in files:
34
+ try:
35
+ data = load_data(file)
36
+ filtered_data = [
37
+ point for point in data if start_time <= point.timestamp <= end_time
38
+ ]
39
+ if filtered_data:
40
+ for point in filtered_data:
41
+ x_coords = [contact.x for contact in point.contacts]
42
+ y_coords = [1 - contact.y for contact in point.contacts]
43
+ force = [contact.force for contact in point.contacts]
44
+
45
+ all_x_coords.extend(x_coords)
46
+ all_y_coords.extend(y_coords)
47
+ all_forces.extend(force)
48
+ except Exception as e:
49
+ print(f"Error loading {file}: {e}")
50
+
51
+ if all_x_coords:
52
+ sc = ax.scatter(all_x_coords, all_y_coords, c=all_forces, cmap='viridis', s=40)
53
+ plt.colorbar(sc, label="Force")
54
+
55
+ ax.set_xlabel("X Coordinate")
56
+ ax.set_ylabel("Y Coordinate")
57
+ ax.set_title(name)
58
+
59
+ if if_save and save_path:
60
+ os.makedirs(os.path.dirname(save_path), exist_ok=True)
61
+ plt.savefig(save_path)
62
+ else:
63
+ plt.show()
64
+
65
+
66
+ def plot_board_data(data_dir, user):
67
+ user_dir = os.path.join(data_dir, user)
68
+ action_dirs = glob.glob(os.path.join(user_dir, "*"))
69
+ print(action_dirs)
70
+ for action_dir in action_dirs:
71
+ timestamp_files = glob.glob(os.path.join(action_dir, "*_timestamp.txt"))
72
+ if not timestamp_files:
73
+ continue
74
+ j = random.randint(0, len(timestamp_files) - 1)
75
+ with open(timestamp_files[j], "r") as f:
76
+ lines = f.readlines()
77
+ timestamps = []
78
+ name = []
79
+ for line in lines:
80
+ timestamps.append(int(line.split()[0]))
81
+ name.append(line.split()[1])
82
+ i = random.randint(0, len(timestamps) - 2)
83
+ start_time = timestamps[i]
84
+ end_time = timestamps[i + 1]
85
+ if not os.path.exists(f"./images/board/{user}"):
86
+ os.makedirs(f"./images/board/{user}")
87
+ action = action_dir.split("\\")[-1]
88
+ if j % 2 == 0:
89
+ name = name[i]
90
+ name = name.lower()
91
+ else:
92
+ name = name[i]
93
+ name = name.upper()
94
+ visualize_touch_data(
95
+ action_dir,
96
+ start_time,
97
+ end_time,
98
+ f"{user}_{action}_{name}",
99
+ if_save=True,
100
+ save_path=f"./images/board/{user}/{user}_{action}_{name}.png",
101
+ )
102
+
103
+
104
+ if __name__ == "__main__":
105
+ directory = "./data"
106
+ user = "user_0"
107
+ plot_board_data(directory, user)
ring_plot.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import matplotlib.pyplot as plt
3
+ import os
4
+
5
+
6
+ def plot_ring_data(file_path="", if_save=True):
7
+ data_ring0 = np.fromfile(file_path, dtype=np.float64)
8
+ data_ring0 = data_ring0.reshape(-1, 7)
9
+ timestamp_path = file_path.replace("ring_0.bin", "timestamp.txt")
10
+ timestampes = []
11
+ with open(timestamp_path, "r", encoding="utf-8") as f:
12
+ for line in f.readlines():
13
+ timestampes.append(float(line.split(" ")[0]))
14
+ for i in range(len(timestampes)):
15
+ timestamp = timestampes[i]
16
+ closet_idx = np.argmin(np.abs(data_ring0[:, -1] - timestamp))
17
+ timestampes[i] = closet_idx
18
+ plt.figure(figsize=(20, 10))
19
+ plt.subplot(111)
20
+ plt.plot(data_ring0[:, 0], label="acc_x")
21
+ plt.plot(data_ring0[:, 1], label="acc_y")
22
+ plt.plot(data_ring0[:, 2], label="acc_z")
23
+ plt.plot(data_ring0[:, 3], label="gyro_x")
24
+ plt.plot(data_ring0[:, 4], label="gyro_y")
25
+ plt.plot(data_ring0[:, 5], label="gyro_z")
26
+ for timestamp in timestampes:
27
+ plt.axvline(x=timestamp, color="red")
28
+ plt.legend()
29
+ plt.title("ring0")
30
+ if if_save:
31
+ savepath = file_path.replace(".bin", ".png").replace("./data/", "./images/ring/")
32
+ if not os.path.exists(os.path.dirname(savepath)):
33
+ os.makedirs(os.path.dirname(savepath))
34
+ plt.savefig(savepath)
35
+ plt.close()
36
+ else:
37
+ plt.show()
38
+
39
+
40
+ if __name__ == "__main__":
41
+ plot_ring_data(file_path="./data/user_0/3/0_ring_0.bin")