-
Notifications
You must be signed in to change notification settings - Fork 2
/
analysis_plot.py
108 lines (99 loc) · 3.49 KB
/
analysis_plot.py
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
import matplotlib as mpl
# disable display
mpl.use('Agg')
import numpy as np
import glob
import pandas as pd
from sklearn.decomposition import PCA
from mpl_toolkits.mplot3d import Axes3D
import mpl_toolkits.mplot3d as mp3d
import matplotlib.pyplot as plt
from matplotlib import cm
import argparse
import os
from matplotlib import colors
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--folder', help='the sub folder under analysis_test_accuracy')
parser.add_argument('-d', '--dimension', help='the dimension of data')
parser.add_argument('-o', '--output', help='output file name')
args = parser.parse_args()
SUB_FOLDER = args.folder
if SUB_FOLDER is None:
LOG_PATHS = os.path.join('log', 'analysis_test_accuracy', 'landscape', )
else:
LOG_PATHS = os.path.join('log', 'analysis_test_accuracy', 'landscape', SUB_FOLDER)
print('log path: {}'.format(LOG_PATHS))
csv_path = os.path.join(LOG_PATHS, '*.csv')
x_dimension = 16 if args.dimension is None else args.dimension
output_file_name = 'ipec_pca_plot' if args.output is None else args.output
def load_data(csv_path, x_dimension):
files = glob.glob(csv_path)
data = None
for file in files:
loaded_data = pd.read_csv(file, header=None)
data = loaded_data if data is None else pd.concat([data, loaded_data], ignore_index=True)
df_x = data.iloc[:, 1:x_dimension + 1]
df_y = data.iloc[:, x_dimension + 1]
# get the first 2 pca components
pca = PCA(n_components=2)
pca_result = pca.fit_transform(df_x.values)
df_pca_x = pd.DataFrame(pca_result)
# prepare x, y, z data for 3D plots
x = df_pca_x.iloc[:, 0].values
y = df_pca_x.iloc[:, 1].values
z = df_y.values
return (x, y, z)
def plot_1d(x, z, output_file_path):
# 1D plot
fig = plt.figure(1, figsize=(20, 40))
# scatter plot
plt.subplot(211)
plt.scatter(x, z, c=z, alpha=0.3)
plt.xlabel('PCA One')
plt.ylabel('Accuracy')
plt.title('Scatter Plot')
# line plot
plt.subplot(212)
plt.plot(x, z)
plt.xlabel('PCA One')
plt.ylabel('Accuracy')
plt.title('Line Plot')
fig.savefig(output_file_path)
def plot_2d(x, y, z, output_file_path):
# 3D plot
fig = plt.figure(2, figsize=(20, 40))
# scatter plot
ax1 = fig.add_subplot(211, projection='3d')
ax1.scatter(x, y, z, c=z, marker='o', alpha=0.2)
ax1.set_xlabel('PCA One')
ax1.set_ylabel('PCA Two')
ax1.set_zlabel('Accuracy')
ax1.set_title('Scatter Plot')
# line plot
ax2 = fig.add_subplot(212, projection='3d')
ax2.plot(x, y, z, alpha=0.2)
ax2.set_xlabel('PCA One')
ax2.set_ylabel('PCA Two')
ax2.set_zlabel('Accuracy')
ax2.set_title('Line Plot')
fig.savefig(output_file_path)
def plot_2d_surface(x, y, z, output_file_path):
# mesh 3D plot
fig = plt.figure(3, figsize=(20, 20))
ax = Axes3D(fig)
ax.plot_trisurf(x, y, z, cmap=plt.cm.CMRmap)
ax.set_xlabel('PCA One')
ax.set_ylabel('PCA Two')
ax.set_zlabel('Accuracy')
ax.set_title('Tri surface')
fig.savefig(output_file_path)
x, y, z = load_data(csv_path, x_dimension)
output_file_path_1d = os.path.join(LOG_PATHS, output_file_name+'_1d.png')
output_file_path_2d = os.path.join(LOG_PATHS, output_file_name+'_2d.png')
output_file_path_2d_surface = os.path.join(LOG_PATHS, output_file_name+'_2d_surface.png')
print('plotting 1d figure')
plot_1d(x, z, output_file_path_1d)
print('plotting 2d figure')
plot_2d(x, y, z, output_file_path_2d)
print('plotting 2d surface figure')
plot_2d_surface(x, y, z, output_file_path_2d_surface)