-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pca.py
181 lines (159 loc) · 7.46 KB
/
test_pca.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
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
import unittest
import numpy as np
from python_shape_stats import helpers, procrustes,statistical_shape_models
import pathlib
import copy
from sklearn import decomposition
import matplotlib.pyplot as plt
class TestSSM(unittest.TestCase):
def setUp(self) -> None:
path = helpers.get_path_to_simulated_faces()
obj_paths = pathlib.Path(path).glob('*.obj')
r,_ = helpers.load_shapes_to_array(obj_paths, n_jobs=1)
r = [procrustes.apply_procrustes_transform(r[:,:,i],helpers._random_transformation()['matrix']) for i in range(r.shape[2])]
r = np.stack(r,axis=2)
self.original_shapes = copy.copy(r)
r,mu = procrustes.do_generalized_procrustes_analysis(r,scale=False,max_iter=2)
self.shapes = r
self.shape_mean = mu
def test_animate_pc(self):
mod = statistical_shape_models.ShapePCA()
pd,_,_ = helpers.load_shape(helpers.get_path_to_demo_face())
mod.reference_polydata = pd
mod.fit(self.shapes, center=True, center_config=self.shape_mean)
mod.animate_pc(1,3)
self.assertTrue(True)
def test_pca_fit_runs(self):
shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
mod = statistical_shape_models.PCA()
mod.fit(shapes_vec)
self.assertTrue(True)
def test_shape_pca_fit_runs(self):
mod = statistical_shape_models.ShapePCA()
mod.fit(self.shapes,center=True,center_config=self.shape_mean)
self.assertTrue(True)
def test_agreement_with_scipy(self):
mod = statistical_shape_models.ShapePCA()
mod.fit(self.shapes, center=True)
PCA2 = decomposition.PCA(svd_solver='full')
PCA2.fit_transform(helpers.landmark_3d_to_2d(self.shapes))
self.assertTrue(np.allclose(PCA2.explained_variance_,mod.eig_val))
v1 = mod.eig_vec
v2 = PCA2.components_
self.assertTrue(np.allclose(np.abs(np.diag(v1 @ v2.T)[:-2]),1))
# k = kkk
def test_pca_transform_predict(self):
shapes_vec = helpers.landmark_3d_to_2d(self.shapes['landmarks'])
mod = statistical_shape_models.PCA()
mod.fit_transform(shapes_vec)
p = mod.predict(mod.transformed_training_data)
self.assertTrue(np.allclose(shapes_vec,p))
def test_shape_pca_transform_predict_runs(self):
mod = statistical_shape_models.ShapePCA()
mod.fit_transform(self.shapes['landmarks'],center=True,center_config=self.shapes['mean'])
self.assertTrue(True)
def test_shape_pca_transform_runs(self):
mod = statistical_shape_models.ShapePCA()
mod.fit_transform(self.shapes['landmarks'],center=True,center_config=self.shapes['mean'])
#assert(np.array_equal(self.shapes['mean'],helpers.landmark_2d_to_3d(mod.center_vec)))
scores,_ = mod.transform(self.original_shapes,apply_procrustes_transform=True,procrustes_scale=True)
self.assertTrue(np.array_equal(scores,mod.transformed_training_data))
def test_agreement_between_eig_val_and_score_variance(self):
shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
mod = statistical_shape_models.PCA()
mod.fit_transform(shapes_vec)
self.assertTrue(np.allclose(mod.eig_val,np.var(mod.transformed_training_data,axis=0,ddof=1)))
# def test_log_lik(self):
# shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
# shapes_vec = shapes_vec[:,1:10]
#
# mod = statistical_shape_models.PCA()
# mod.fit_transform(shapes_vec)
# n_comp = np.argmax(mod.dim_log_likelihood)
# # compare to scikit
# mod2 = sklearn.decomposition.PCA(n_components='mle',svd_solver='full')
# mod2.fit_transform(shapes_vec)
def test_trim_n_pcs(self):
shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
mod = statistical_shape_models.PCA()
mod.fit_transform(shapes_vec)
mod.trim_no_pcs(15)
self.assertTrue(mod.n_dim==15)
self.assertTrue(len(mod.eig_val)==15)
self.assertTrue(mod.transformed_training_data.shape[1]==15)
self.assertTrue(np.allclose(mod.eig_val, np.var(mod.transformed_training_data, axis=0, ddof=0)))
def test_trim_strip_perc_var_removes_pcs(self):
shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
mod = statistical_shape_models.PCA()
mod.fit_transform(shapes_vec)
n_dim = mod.n_dim
mod.trim_perc_var(78)
n_dim2 = mod.n_dim
self.assertTrue(sum(mod.cumulative_perc_var>=78)==1)
def test_parallel_analysis_single_iter(self):
shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
mod = statistical_shape_models.PCA()
mod.fit_transform(shapes_vec)
out = mod._parallel_analysis_one_iter(mod.params['x0'])
self.assertTrue(True)
def test_maha_dist_to_p_value(self):
shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
mod = statistical_shape_models.PCA()
mod.fit_transform(shapes_vec)
mod.trim_no_pcs(1)
p = mod.maha_dist_to_p_value(1.959963984540054)
self.assertTrue(np.isclose(p,0.05))
m = mod.p_value_maha_dist(p)
self.assertTrue(np.isclose(m, 1.959963984540054))
def test_weighted_transform_with_equal_weights_agrees_with_transform(self):
shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
mod = statistical_shape_models.PCA()
mod.fit_transform(shapes_vec)
sc1 = mod.transformed_training_data[0,:]
w = np.ones(mod._n_train_features) / 3
sc2 = mod.weighted_transform_to_model(shapes_vec[0,:],w)
self.assertTrue(np.allclose(sc1,sc2))
def test_scale_vec(self):
shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
mod = statistical_shape_models.PCA()
mod.fit_transform(shapes_vec)
sc1 = mod.transformed_training_data[0, :]
sc2 = mod.scale_vec(sc1,target_dist=1,metric = 'euclidean')
d1 = mod.get_distance(sc2,metric='euclidean')
sc2 = mod.scale_vec(sc1, target_dist=1, metric='mahalanobis')
d2 = mod.get_distance(sc2,metric='mahalanobis')
self.assertTrue(np.allclose([d1,d2],[1.,1.]))
# def test_eigenvalue_plot_runs(self):
# shapes_vec = helpers.landmark_3d_to_2d(self.shapes['landmarks'])
# mod = statistical_shape_models.PCA()
# mod.fit_transform(shapes_vec)
# distr = helpers.broken_stick_empirical(mod.n_dim,10000,seed=1)*mod._initial_var
# statistical_shape_models._eigen_value_plot(mod.eig_val,distr=distr)
# plt.show()
def test_broken_stick_plot_runs(self):
shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
mod = statistical_shape_models.PCA()
mod.fit_transform(shapes_vec)
mod.broken_stick_plot()
plt.show()
def test_parallel_analysis_runs(self):
shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
mod = statistical_shape_models.PCA()
mod.fit_transform(shapes_vec)
ax,np_pcs = mod.parallel_analysis_plot()
# ax.set_ylim(np.array([-0,0.2])*1e-7)
plt.show()
# def test_cross_validation_runs(self):
# shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
# mod = statistical_shape_models.PCA()
# mod.fit_transform(shapes_vec)
# mod.cross_validation()
#
# def test_cv_plot(self):
# shapes_vec = helpers.landmark_3d_to_2d(self.shapes)
# mod = statistical_shape_models.PCA()
# mod.fit_transform(shapes_vec)
# mod.cross_validation_plot(atol=1e-20)
# plt.show()
if __name__ == '__main__':
unittest.main()