-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotfeatures.py
More file actions
122 lines (99 loc) · 2.94 KB
/
Copy pathplotfeatures.py
File metadata and controls
122 lines (99 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# plotfeatures.py
# -------------------------------------------------------------
# Generates 2D scatter feature plots from a combined feature CSV.
# Uses label inferred from event name (event_1_, event_2_, etc.)
# Saves all plots as JPEG files in feature_plots/.
# -------------------------------------------------------------
import os
from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt
from tkinter import Tk, filedialog
# 0 will mean "Unlabeled" (event_0_...)
LABEL_NAMES = {
0: "Unlabeled",
1: "Collision",
2: "Drop",
3: "Stoppage"
}
COLORS = {
0: "tab:brown",
1: "tab:blue",
2: "tab:orange",
3: "tab:green"
}
MARKERS = {
0: "P",
1: "o",
2: "s",
3: "^"
}
def get_label_from_event(name):
parts = name.split("_")
if len(parts) > 1:
try:
return int(parts[1])
except:
return 0
return 0
def main():
# Select feature CSV
print("[Message]: Select combined feature CSV...")
root = Tk()
root.withdraw()
path = filedialog.askopenfilename(
initialdir="event_features",
title="Select combined feature CSV",
filetypes=[("CSV files", "*.csv")]
)
root.destroy()
if not path:
return
print("[Message]: Loading CSV...")
df = pd.read_csv(path)
# Add label column if needed
if "label" not in df.columns and "event" in df.columns:
df["label"] = df["event"].apply(get_label_from_event)
# Collect numeric feature columns
numeric_cols = [
c for c in df.columns
if c.endswith(("_mean", "_std", "_rms", "_ptp", "_min", "_max"))
]
# Build all pairs
feature_pairs = []
for i in range(len(numeric_cols)):
for j in range(i + 1, len(numeric_cols)):
feature_pairs.append((numeric_cols[i], numeric_cols[j]))
# Output folder
ts = datetime.now().strftime("%m%d%Y_%H%M%S")
base_dir = "feature_plots"
out_dir = os.path.join(base_dir, f"plots_{ts}")
os.makedirs(out_dir, exist_ok=True)
print("[Message]: Generating plots...")
print(f"[Message]: Total plots = {len(feature_pairs)}")
# Generate scatter plots
for x, y in feature_pairs:
plt.figure(figsize=(7, 6))
for lbl in sorted(df["label"].unique()):
sub = df[df["label"] == lbl]
plt.scatter(
sub[x],
sub[y],
c=COLORS.get(lbl, "k"),
marker=MARKERS.get(lbl, "o"),
label=LABEL_NAMES.get(lbl, f"Label {lbl}"),
alpha=0.8
)
plt.xlabel(x)
plt.ylabel(y)
plt.title(f"{x} vs {y}")
plt.grid(True)
plt.legend()
plt.tight_layout()
filename = f"{x}_vs_{y}.jpeg"
plt.savefig(os.path.join(out_dir, filename), dpi=300)
plt.close()
print("[Saved]:", out_dir)
print("[Message]: All plots generated.\n")
if __name__ == "__main__":
main()