1# test_PCA.praat
2# djmw 20110525, 20211128
3
4appendInfoLine: "test_PCA.praat"
5
6call test_pca_simple
7@test_projections
8
9appendInfoLine: "test_PCA.praat OK"
10
11procedure create_reference_TableOfReal
12	# 5 points,
13	#  	p1, p2,p3 on a line through the origin with an angle of pi/6 with variance 6
14	#	p4, p5 orthogonal with variance 2
15	#
16	.npoints = 5
17	.t = Create TableOfReal... .t .npoints 2
18	Set value... 1 1 -sqrt(2)
19	Set value... 1 2 -1
20	Set value... 2 1  0
21	Set value... 2 2 0
22	Set value... 3 1 sqrt(2)
23	Set value... 3 2  1
24	Set value... 4 1 -1/sqrt(3)
25	Set value... 4 2 sqrt(2/3)
26	Set value... 5 1 1/sqrt(3)
27	Set value... 5 2 -sqrt(2/3)
28endproc
29
30procedure test_pca_simple
31	appendInfoLine: tab$, "test_pca_simple"
32	.tol = 1e-12
33	@create_reference_TableOfReal
34	.tor = selected ("TableOfReal")
35	.nrows = Get number of rows
36	.ncols = Get number of columns
37	.pca = To PCA
38	.numberOfEigenvectors = Get number of eigenvectors
39	assert .numberOfEigenvectors = .ncols
40	appendInfoLine:  tab$, tab$, "Eigenvalues"
41	.eigenvalue1 = Get eigenvalue... 1
42	.eigenvalue2 = Get eigenvalue... 2
43	assert abs (.eigenvalue1 - 6 / (.nrows - 1)) < .tol
44	assert abs (.eigenvalue2 - 2 / (.nrows - 1)) < .tol
45
46	appendInfoLine:  tab$, tab$, "Fraction variance accounted for"
47	.fvaf = Get fraction variance accounted for... 1 1
48	assert abs(.fvaf - 6/8)< .tol
49
50	appendInfoLine:  tab$, tab$, "Eigenvectors should be othogonal"
51	.ev11 = Get eigenvector element... 1 1
52	.ev12 = Get eigenvector element... 1 2
53	.ev21 = Get eigenvector element... 2 1
54	.ev22 = Get eigenvector element... 2 2
55	.inprod = .ev11*.ev21+.ev12*.ev22
56
57	assert abs (.inprod) < .tol
58
59	removeObject: .pca, .tor
60	appendInfoLine:  tab$, "test_pca_simple OK"
61endproc
62
63procedure test_projections
64	appendInfoLine: tab$, "test_PCA_TableOfReal_projections"
65	.tol = 1e-12
66	@create_reference_TableOfReal
67	.tor = selected ("TableOfReal")
68	.nrows = Get number of rows
69	.ncols = Get number of columns
70	.pca = To PCA
71	.numberOfEigenvectors = Get number of eigenvectors
72
73	appendInfoLine:  tab$, tab$, "Project rows"
74	selectObject: .tor, .pca
75	.projection1 = To TableOfReal (project rows): 0
76	.ncols_p1 = Get number of columns
77	assert .ncols_p1 = 2
78	selectObject: .tor, .pca
79	.projection2 = To TableOfReal (project rows): 1
80	.ncols_p2 = Get number of columns
81	assert .ncols_p2 = 1
82
83	selectObject: .tor
84	.minuscolumn = Copy: "1"
85	Remove column (index): 2
86	selectObject: .minuscolumn, .pca
87	asserterror The number of columns in the TableOfReal should match the size of the eigenvector of the PCA.
88	.projection3 = To TableOfReal (project rows): 0
89
90	appendInfoLine:  tab$, tab$, "To Configuration"
91	selectObject: .tor, .pca
92	.configuration1 = To Configuration: 0
93	.ncols_c1 = Get number of columns
94	assert .ncols_c1 = 2
95	selectObject: .tor, .pca
96	.configuration2 = To Configuration: 1
97	.ncols_c2 = Get number of columns
98	assert .ncols_c2 = 1
99	selectObject: .minuscolumn, .pca
100	asserterror The number of columns in the TableOfReal should match the size of the eigenvector of the PCA.
101	.configuration3 = To Configuration: 0
102
103	appendInfoLine:  tab$, tab$, "Z-scores"
104	selectObject: .tor, .pca
105	.zscore1 = To TableOfReal (z-scores): 0
106	.ncols_z1 = Get number of columns
107	assert .ncols_z1 = 2
108	selectObject: .tor, .pca
109	.zscore2 = To TableOfReal (z-scores): 1
110	.ncols_z2 = Get number of columns
111	assert .ncols_z2 = 1
112	selectObject: .minuscolumn, .pca
113	asserterror The number of columns in the TableOfReal should match the size of the eigenvector of the PCA.
114	.zscore3 = To TableOfReal (z-scores): 0
115
116	appendInfoLine:  tab$, tab$, "TableOfReal reconstruction from PCA+ Configuration"
117	selectObject: .configuration2, .pca
118	.reconstruction = To TableOfReal (reconstruct)
119	.columns3 = Create TableOfReal: "3", 5, 3
120	.toomanyColumns = To Configuration
121	selectObject: .pca, .toomanyColumns
122	asserterror The number of columns in the Configuration should not exceed the number of eigenvectors in the PCA.
123	.tor2 = To TableOfReal (reconstruct)
124
125	removeObject: .projection2, .projection1, .configuration1,
126	... .configuration2, .minuscolumn, .zscore1, .zscore2, .pca, .tor,
127	... .reconstruction,  .columns3, .toomanyColumns
128
129	appendInfoLine:  tab$, "test_PCA_TableOfReal_projections OK"
130
131endproc
132
133
134
135