Debian Codegraph @

dwtest/test_PCA.praat

raw
1 # test_PCA.praat
2 # djmw 20110525, 20211128, 20260322
3
4 appendInfoLine: "test_PCA.praat"
5
6 @testOlderFormats
7
8 @test_pca_simple
9
10 @testConvertsFromXToPCA
11
12 @test_projections
13
14 appendInfoLine: "test_PCA.praat OK"
15
16 procedure testOlderFormats
17 .test$ = tab$ + "test older PCA formats:"
18 appendInfoLine: .test$
19 appendInfo: tab$, tab$, "format 0:"
20 .pca = Read from file: "pols1973_format0.PCA"
21 .n = Get number of eigenvectors
22 .evals# = {239588.24623428631, 65532.75420378206, 18892.8604411748}
23 assert .n = 3
24 for .i to .n
25 .val = Get eigenvalue: .i
26 .r = abs (.val - .evals# [.i]) / .evals# [.i]
27 assert .r < 1e-10
28 endfor
29 removeObject: .pca
30 appendInfoLine: " OK"
31 appendInfoLine: .test$ + " OK"
32 endproc
33
34 procedure testConvertsFromXToPCA
35 .test$ = tab$ + "test converts from X to PCA:"
36 appendInfoLine: .test$
37 .pols = Create TableOfReal (Pols 1973): "yes"
38 .covar = To Covariance
39 appendInfo: tab$, tab$, "Covariance to PCA:"
40 .pcacovar = To PCA
41 appendInfoLine: " OK"
42 selectObject: .covar
43 .cor = To Correlation
44 appendInfo: tab$, tab$, "Correlation to PCA:"
45 .pcacor = To PCA
46 appendInfoLine: " OK"
47 selectObject: .pols
48 .sscp = To SSCP: 0,0,0,0
49 appendInfo: tab$, tab$, "SSCP to PCA:"
50 .pcasscp = To PCA
51 appendInfoLine: " OK"
52 selectObject: .pols
53 .gm = To GaussianMixture (row labels): "Diagonals"
54 appendInfo: tab$, tab$, "GaussianMixture to PCA:"
55 .pcagm = To PCA
56 appendInfoLine: " OK"
57
58 removeObject: .pols, .covar, .pcacovar, .cor, .pcacor, .sscp, .pcasscp
59 removeObject: .gm, .pcagm
60 appendInfoLine: .test$, " OK"
61 endproc
62
63 procedure create_reference_TableOfReal
64 # 5 points,
65 # p1, p2, p3 on a line through the origin with an angle of pi/6 with variance 6
66 # p4, p5 orthogonal with variance 2
67 #
68 .npoints = 5
69 m## = {{ -sqrt(2), -1},
70 ... { 0, 0},
71 ... { sqrt(2), 1},
72 ... {-1/sqrt(3), sqrt(2/3)},
73 ... { 1/sqrt(3), -sqrt(2/3)}}
74 .t = Create TableOfReal: "p5", .npoints, 2
75 Formula: ~ m## [row, col]
76 endproc
77
78 procedure test_pca_simple
79 appendInfoLine: tab$, "test_pca_simple"
80 .tol = 1e-12
81 @create_reference_TableOfReal
82 .tor = selected ("TableOfReal")
83 .nrows = Get number of rows
84 .ncols = Get number of columns
85 .pca = To PCA
86 .numberOfEigenvectors = Get number of eigenvectors
87 assert .numberOfEigenvectors = .ncols
88 appendInfo: tab$, tab$, "Eigenvalues:"
89 .eigenvalue1 = Get eigenvalue... 1
90 .eigenvalue2 = Get eigenvalue... 2
91 assert abs (.eigenvalue1 - 6 / (.nrows - 1)) < .tol
92 assert abs (.eigenvalue2 - 2 / (.nrows - 1)) < .tol
93 appendInfoLine: " OK"
94
95 appendInfo: tab$, tab$, "Fraction variance accounted for:"
96 .fvaf = Get fraction variance accounted for... 1 1
97 assert abs(.fvaf - 6/8) < .tol
98 appendInfoLine: " OK"
99
100 appendInfo: tab$, tab$, "Eigenvectors should be orthogonal:"
101 .ev11 = Get eigenvector element... 1 1
102 .ev12 = Get eigenvector element... 1 2
103 .ev21 = Get eigenvector element... 2 1
104 .ev22 = Get eigenvector element... 2 2
105 .inprod = .ev11*.ev21+.ev12*.ev22
106
107 assert abs (.inprod) < .tol
108 appendInfoLine: " OK"
109
110 removeObject: .pca, .tor
111 appendInfoLine: tab$, "test_pca_simple: OK"
112 endproc
113
114 procedure test_projections
115 appendInfoLine: tab$, "test PCA & TableOfReal projections:"
116 .tol = 1e-12
117 @create_reference_TableOfReal
118 .tor = selected ("TableOfReal")
119 .nrows = Get number of rows
120 .ncols = Get number of columns
121 .pca = To PCA
122 .numberOfEigenvectors = Get number of eigenvectors
123 .evectorLength = Get eigenvector dimension
124
125 appendInfo: tab$, tab$, "PCA & TableOfReal: Project rows:"
126 selectObject: .tor, .pca
127 .projection1 = To TableOfReal (project rows): 0
128 .ncols_p1 = Get number of columns
129 assert .ncols_p1 = 2
130 selectObject: .tor, .pca
131 .projection2 = To TableOfReal (project rows): 1
132 .ncols_p2 = Get number of columns
133 assert .ncols_p2 = 1
134
135 selectObject: .tor
136 .minuscolumn = Copy: "1"
137 Remove column (index): 2
138 .ncolminus = Get number of columns
139 selectObject: .minuscolumn, .pca
140 asserterror The number of columns in the TableOfReal ('.ncolminus') should match the length of the eigenvectors of the PCA ('.evectorLength').
141 .projection3 = To TableOfReal (project rows): 0
142 appendInfoLine: " OK"
143
144 appendInfo: tab$, tab$, "PCA & TableOfReal: To Configuration:"
145 selectObject: .tor, .pca
146 .configuration1 = To Configuration: 0
147 .ncols_c1 = Get number of columns
148 assert .ncols_c1 = 2
149 selectObject: .tor, .pca
150 .configuration2 = To Configuration: 1
151 .ncols_c2 = Get number of columns
152 assert .ncols_c2 = 1
153 selectObject: .minuscolumn, .pca
154 asserterror The number of columns in the TableOfReal ('.ncolminus') should match the length of the eigenvectors of the PCA ('.evectorLength').
155 .configuration3 = To Configuration: 0
156 appendInfoLine: " OK"
157
158 appendInfo: tab$, tab$, "PCA & TableOfReal: Z-scores:"
159 selectObject: .tor, .pca
160 .zscore1 = To TableOfReal (z-scores): 0
161 .ncols_z1 = Get number of columns
162 assert .ncols_z1 = 2
163 selectObject: .tor, .pca
164 .zscore2 = To TableOfReal (z-scores): 1
165 .ncols_z2 = Get number of columns
166 assert .ncols_z2 = 1
167 selectObject: .minuscolumn, .pca
168 asserterror The number of columns in the TableOfReal ('.ncolminus') should match the length of the eigenvectors of the PCA ('.evectorLength').
169 .zscore3 = To TableOfReal (z-scores): 0
170 appendInfoLine: " OK"
171
172 appendInfo: tab$, tab$, "TableOfReal reconstruction from PCA+ Configuration:"
173 selectObject: .configuration2, .pca
174 .reconstruction = To TableOfReal (reconstruct)
175 .columns3 = Create TableOfReal: "3", 5, 3
176 .toomanyColumns = To Configuration
177 selectObject: .pca, .toomanyColumns
178 asserterror The number of columns in the Configuration should not exceed the number of eigenvectors in the PCA.
179 .tor2 = To TableOfReal (reconstruct)
180 appendInfoLine: " OK"
181
182 removeObject: .projection2, .projection1, .configuration1,
183 ... .configuration2, .minuscolumn, .zscore1, .zscore2, .pca, .tor,
184 ... .reconstruction, .columns3, .toomanyColumns
185
186 appendInfoLine: tab$, "test PCA & TableOfReal projections OK"
187 endproc
188
189
190