-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot.py
More file actions
295 lines (239 loc) · 11.2 KB
/
plot.py
File metadata and controls
295 lines (239 loc) · 11.2 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import patches
import tkinter as tk
from tkinter import filedialog, simpledialog
def load_and_prepare_data(df, class_column, class_a, class_b, feature_columns=None):
# Filter to two-class subset
df_sub = df[df[class_column].isin([class_a, class_b])].copy()
df_sub['Label'] = df_sub[class_column].apply(lambda x: 'ClassA' if x == class_a else 'ClassB')
# Determine feature columns
if feature_columns is None:
feature_columns = df_sub.select_dtypes(include=[np.number]).columns.tolist()
feature_columns = [col for col in feature_columns if col != class_column]
# Normalize features to [0,1]
df_sub[feature_columns] = (df_sub[feature_columns] - df_sub[feature_columns].min()) / \
(df_sub[feature_columns].max() - df_sub[feature_columns].min())
return df_sub, feature_columns
def create_split_data(df_sub, feature_columns, split_fraction=0.5):
num_features = len(feature_columns)
split_count = int(num_features * split_fraction)
overlap_indices = sorted(np.random.choice(range(num_features), size=split_count, replace=False))
non_overlap_indices = sorted(set(range(num_features)) - set(overlap_indices))
df_classA = df_sub[df_sub['Label'] == 'ClassA'].copy()
df_classB = df_sub[df_sub['Label'] == 'ClassB'].copy()
for i in non_overlap_indices:
col = feature_columns[i]
margin = 0.02
gap_size = np.random.uniform(0.05, 0.15)
available = 1.0 - 2 * margin - gap_size
classA_frac = np.random.uniform(0.45, 0.55)
classB_frac = 1.0 - classA_frac
b_max = margin + available * classB_frac
b_min = margin
a_min = b_max + gap_size
a_max = a_min + available * classA_frac
df_classB[col] = np.interp(df_classB[col], (df_classB[col].min(), df_classB[col].max()), (b_min, b_max))
df_classA[col] = np.interp(df_classA[col], (df_classA[col].min(), df_classA[col].max()), (a_min, a_max))
return df_classA, df_classB, overlap_indices
def shift_split_axes(df_classA, df_classB, feature_columns, overlap_indices):
df_a_shifted = df_classA.copy()
df_b_shifted = df_classB.copy()
for i in overlap_indices:
col = feature_columns[i]
df_a_shifted[col] = np.interp(df_a_shifted[col], (0, 1), (0.5, 1.5))
df_b_shifted[col] = np.interp(df_b_shifted[col], (0, 1), (-0.5, 0.5))
return df_a_shifted, df_b_shifted
def manual_parallel_coordinates_split_with_axis_ranges(
df_blue, df_red,
top_blue, bottom_blue, top_red, bottom_red,
feature_columns, xtick_labels, overlap_indices, title, blue_on_top=True
):
plt.figure(figsize=(14, 6))
x = np.arange(len(feature_columns))
# Plot all data lines - only use feature columns, not Label
if blue_on_top:
for _, row in df_red[feature_columns].iterrows():
plt.plot(x, row.values, color='red', alpha=0.2)
for _, row in df_blue[feature_columns].iterrows():
plt.plot(x, row.values, color='blue', alpha=0.2)
else:
for _, row in df_blue[feature_columns].iterrows():
plt.plot(x, row.values, color='blue', alpha=0.2)
for _, row in df_red[feature_columns].iterrows():
plt.plot(x, row.values, color='red', alpha=0.2)
# Outlines
blue_outline = top_blue + bottom_blue + [top_blue[0]]
red_outline = top_red + bottom_red + [top_red[0]]
blue_x, blue_y = zip(*blue_outline)
red_x, red_y = zip(*red_outline)
# Outline drawing
if blue_on_top:
plt.plot(red_x, red_y, linestyle='--', linewidth=3, color='red')
plt.plot(blue_x, blue_y, linestyle='--', linewidth=3, color='blue')
else:
plt.plot(blue_x, blue_y, linestyle='--', linewidth=3, color='blue')
plt.plot(red_x, red_y, linestyle='--', linewidth=3, color='red')
# Draw vertical bars for axis ranges
for i in range(len(feature_columns)):
# Default: full axis
if i not in overlap_indices:
plt.plot([i, i], [0, 1], color='black', linewidth=1.2)
else:
# Split axes: blue on top, red on bottom
plt.plot([i, i], [0.5, 1.5], color='blue', linewidth=1.2)
plt.plot([i, i], [-0.5, 0.5], color='red', linewidth=1.2)
plt.xticks(x, xtick_labels, rotation=45)
plt.xlim(-0.5, len(feature_columns) - 0.5)
plt.ylim(-0.6, 1.6)
plt.grid(True)
plt.title(title)
plt.tight_layout()
plt.show()
def manual_parallel_coordinates_no_split(
df_blue, df_red, top_blue, bottom_blue, top_red, bottom_red,
feature_columns, xtick_labels, title, blue_on_top=True
):
plt.figure(figsize=(14, 6))
x = np.arange(len(feature_columns))
if blue_on_top:
for _, row in df_red[feature_columns].iterrows():
plt.plot(x, row.values, color='red', alpha=0.2)
for _, row in df_blue[feature_columns].iterrows():
plt.plot(x, row.values, color='blue', alpha=0.2)
else:
for _, row in df_blue[feature_columns].iterrows():
plt.plot(x, row.values, color='blue', alpha=0.2)
for _, row in df_red[feature_columns].iterrows():
plt.plot(x, row.values, color='red', alpha=0.2)
# Outlines
blue_outline = top_blue + bottom_blue + [top_blue[0]]
red_outline = top_red + bottom_red + [top_red[0]]
blue_x, blue_y = zip(*blue_outline)
red_x, red_y = zip(*red_outline)
if blue_on_top:
plt.plot(red_x, red_y, linestyle='--', linewidth=3, color='red')
plt.plot(blue_x, blue_y, linestyle='--', linewidth=3, color='blue')
else:
plt.plot(blue_x, blue_y, linestyle='--', linewidth=3, color='blue')
plt.plot(red_x, red_y, linestyle='--', linewidth=3, color='red')
plt.xticks(x, xtick_labels, rotation=45)
plt.xlim(-0.5, len(feature_columns) - 0.5)
plt.ylim(0, 1)
plt.grid(True)
plt.title(title)
plt.tight_layout()
plt.show()
def select_classes(df, class_column):
"""Prompt user to select two classes to compare."""
root = tk.Tk()
root.withdraw()
# Get unique classes
unique_classes = df[class_column].unique().tolist()
# Create dialogs to select two classes
class_a = simpledialog.askstring("Select First Class",
f"Enter the name of the first class from: {', '.join(unique_classes)}")
# Validate first selection
while class_a not in unique_classes:
class_a = simpledialog.askstring("Invalid Selection",
f"Please enter a valid class name from: {', '.join(unique_classes)}")
# Get remaining classes
remaining_classes = [c for c in unique_classes if c != class_a]
# Select second class
class_b = simpledialog.askstring("Select Second Class",
f"Enter the name of the second class from: {', '.join(remaining_classes)}")
# Validate second selection
while class_b not in remaining_classes:
class_b = simpledialog.askstring("Invalid Selection",
f"Please enter a valid class name from: {', '.join(remaining_classes)}")
return class_a, class_b
if __name__ == "__main__":
# Create root window and withdraw it
root = tk.Tk()
root.withdraw()
# Load data with file dialog
file_path = filedialog.askopenfilename(title="Select Dataset File",
filetypes=[("CSV files", "*.csv"), ("All files", "*.*")])
if not file_path:
print("No file selected. Exiting.")
exit()
# Load the dataset
try:
df = pd.read_csv(file_path)
print(f"Loaded dataset with {len(df)} rows and {len(df.columns)} columns")
print(f"Columns: {', '.join(df.columns)}")
except Exception as e:
print(f"Error loading dataset: {e}")
exit()
# Find the class column (case-insensitive)
class_column = None
for col in df.columns:
if col.lower() == 'class':
class_column = col
break
if class_column is None:
print("Error: No column named 'class' (case-insensitive) found in the dataset.")
exit()
print(f"Using '{class_column}' as the class column")
# Select classes to compare
class_a, class_b = select_classes(df, class_column)
print(f"Selected classes to compare: {class_a} and {class_b}")
# Process data with proper normalization
df_sub, feature_columns = load_and_prepare_data(df, class_column, class_a, class_b)
print(f"Processed data with {len(feature_columns)} features")
# Split dataframe by class
df_blue = df_sub[df_sub['Label'] == 'ClassA']
df_red = df_sub[df_sub['Label'] == 'ClassB']
# Create split data
df_blue_split, df_red_split, overlap_indices = create_split_data(df_sub, feature_columns)
# Shift split axes
df_blue_split_axes, df_red_split_axes = shift_split_axes(df_blue_split, df_red_split, feature_columns, overlap_indices)
# Compute outlines for split axes plot
blue_top_split = df_blue_split_axes[feature_columns].max()
blue_bottom_split = df_blue_split_axes[feature_columns].min()
red_top_split = df_red_split_axes[feature_columns].max()
red_bottom_split = df_red_split_axes[feature_columns].min()
x = np.arange(len(feature_columns))
blue_top_line_split = list(zip(x, blue_top_split.values))
blue_bottom_line_split = list(zip(x[::-1], blue_bottom_split.values[::-1]))
red_top_line_split = list(zip(x, red_top_split.values))
red_bottom_line_split = list(zip(x[::-1], red_bottom_split.values[::-1]))
# Compute outlines for normal plot
blue_top_full = df_blue[feature_columns].max()
blue_bottom_full = df_blue[feature_columns].min()
red_top_full = df_red[feature_columns].max()
red_bottom_full = df_red[feature_columns].min()
blue_top_line_full = list(zip(x, blue_top_full.values))
blue_bottom_line_full = list(zip(x[::-1], blue_bottom_full.values[::-1]))
red_top_line_full = list(zip(x, red_top_full.values))
red_bottom_line_full = list(zip(x[::-1], red_bottom_full.values[::-1]))
# Create xtick labels with asterisk for overlap features
xtick_labels = [f'{col}*' if i in overlap_indices else col for i, col in enumerate(feature_columns)]
# Plot with split axes
manual_parallel_coordinates_split_with_axis_ranges(
df_blue=df_blue_split_axes,
df_red=df_red_split_axes,
top_blue=blue_top_line_split,
bottom_blue=blue_bottom_line_split,
top_red=red_top_line_split,
bottom_red=red_bottom_line_split,
feature_columns=feature_columns,
xtick_labels=xtick_labels,
overlap_indices=overlap_indices,
title=f"Split Axis Parallel Coordinates: {class_a} vs {class_b}",
blue_on_top=True
)
# Plot without axis split
manual_parallel_coordinates_no_split(
df_blue=df_blue,
df_red=df_red,
top_blue=blue_top_line_full,
bottom_blue=blue_bottom_line_full,
top_red=red_top_line_full,
bottom_red=red_bottom_line_full,
feature_columns=feature_columns,
xtick_labels=xtick_labels,
title=f"Normal Parallel Coordinates: {class_a} vs {class_b}",
blue_on_top=True
)