test_MDS.praat 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # test_MDS.praat
  2. appendInfoLine: "test_MDS.praat"
  3. @test_additiveConstant
  4. @testDissimilarityInterface
  5. # side effect: 6 configurations in the list of objects: configuration[1]...configuration[6]
  6. # testINDSCAL uses these 6 configurations
  7. @testINDSCAL
  8. @testProcrustus
  9. for i to 6
  10. removeObject: configuration [i]
  11. endfor
  12. appendInfoLine: "test_MDS.praat OK"
  13. procedure testProcrustus
  14. appendInfoLine: tab$, tab$, "Configuration & Configuration"
  15. for .i from 2 to 6
  16. selectObject: configuration [1]
  17. plusObject: configuration [.i]
  18. .procrustus [1] = To Procrustes: "no"
  19. plusObject: configuration [.i]
  20. .ct [.i] = To Configuration
  21. plusObject: configuration [1]
  22. .procrustus [2] = To Procrustes... no
  23. @check_if_identity_transform: .procrustus [2]
  24. removeObject: .procrustus [1], .procrustus [2]
  25. endfor
  26. for .i from 2 to 6
  27. removeObject: .ct [.i]
  28. endfor
  29. endproc
  30. procedure check_if_identity_transform: .p
  31. selectObject: .p
  32. .scale = Get scale
  33. assert .scale > 1 - 1e-4
  34. for .j to 2
  35. .tj = Get translation element: .j
  36. assert abs(.tj) < 1e-6
  37. for .k to 2
  38. .tjk = Get transformation element: .j, .k
  39. if .j = .k
  40. assert .tjk > 1 - 1e-4
  41. else
  42. assert abs(.tjk) < 1e-6
  43. endif
  44. endfor
  45. endfor
  46. endproc
  47. procedure testDissimilarityInterface
  48. appendInfoLine: tab$, "test interface"
  49. appendInfoLine: tab$, tab$, "Query"
  50. .dissimilarity = Create letter R example: 0
  51. .numberOfRows = Get number of rows
  52. .numberOfColumns = Get number of columns
  53. for .irow to .numberOfRows
  54. .rowLabel$ = Get row label: .irow
  55. .rowIndex = Get row index: .rowLabel$
  56. assert .irow = .rowIndex; '.irow' '.rowIndex'
  57. endfor
  58. for .icol to .numberOfColumns
  59. .columnLabel$ = Get column label: .icol
  60. .columnIndex = Get column index: .columnLabel$
  61. assert .icol = .columnIndex; '.icol' '.columnIndex'
  62. endfor
  63. for .irow to .numberOfRows
  64. for .icol to .numberOfColumns
  65. val = Get value: .irow, .icol
  66. endfor
  67. endfor
  68. .norm = Get table norm
  69. .additiveConstant = Get additive constant
  70. appendInfoLine: tab$, tab$, "Modify: skipped"
  71. appendInfoLine: tab$, tab$, "Synthesize: skipped"
  72. appendInfoLine: tab$, tab$, "Extract part"
  73. selectObject: .dissimilarity
  74. .tmp1 = Extract row ranges: "1 2"
  75. .numberOfRows1 = Get number of rows
  76. assert .numberOfRows1 == 2; '.numberOfRows1' "= 2"
  77. selectObject: .dissimilarity
  78. .tmp2 = Extract rows where: "1"
  79. .numberOfRows2 = Get number of rows
  80. .numberOfColumns2 = Get number of columns
  81. assert .numberOfRows2 == .numberOfRows; '.numberOfRows2' "==" '.numberOfRows'
  82. assert .numberOfColumns2 == .numberOfColumns; '.numberOfColumns2' "==" '.numberOfColumns'
  83. selectObject: .dissimilarity
  84. .tmp3 = Extract column ranges: "1 2"
  85. .numberOfColumns3 = Get number of columns
  86. assert .numberOfColumns3 == 2; '.numberOfColumns3' "= 2"
  87. selectObject: .dissimilarity
  88. .tmp4 = Extract columns where: "1"
  89. .numberOfRows4 = Get number of rows
  90. .numberOfColumns4 = Get number of columns
  91. assert .numberOfRows4 == .numberOfRows; '.numberOfRows4' "==" '.numberOfRows'
  92. assert .numberOfColumns4 == .numberOfColumns; '.numberOfColumns4' "==" '.numberOfColumns'
  93. removeObject: .tmp1, .tmp2, .tmp3, .tmp4
  94. for .irow to .numberOfRows
  95. selectObject: .dissimilarity
  96. .rowLabel$ = Get row label: .irow
  97. .tmpi = Extract rows where label: "is equal to", .rowLabel$
  98. .numberOfRows5 = Get number of rows
  99. assert .numberOfRows5 >= 1
  100. removeObject: .tmpi
  101. endfor
  102. for .icol to .numberOfColumns
  103. selectObject: .dissimilarity
  104. .columnLabel$ = Get column label: .icol
  105. .tmpi = Extract columns where label: "is equal to", .columnLabel$
  106. .numberOfColumns6 = Get number of columns
  107. assert .numberOfColumns6 >= 1
  108. removeObject: .tmpi
  109. endfor
  110. appendInfoLine: tab$, tab$, "Extract"
  111. selectObject: .dissimilarity
  112. .strings1 = Extract row labels as Strings
  113. .numberOfStrings = Get number of strings
  114. assert .numberOfStrings == .numberOfRows
  115. selectObject: .dissimilarity
  116. .strings2 = Extract column labels as Strings
  117. .numberOfStrings = Get number of strings
  118. assert .numberOfStrings == .numberOfColumns
  119. removeObject: .strings1, .strings2
  120. appendInfoLine: tab$, tab$, "Convert"
  121. selectObject: .dissimilarity
  122. .table = To Table: "col1"
  123. .numberOfColumnsT = Get number of columns
  124. assert .numberOfColumnsT = .numberOfColumns + 1
  125. selectObject: .dissimilarity
  126. .matrix = To Matrix
  127. .numberOfRowsM = Get number of rows
  128. .numberOfColumnsM = Get number of columns
  129. assert .numberOfRowsM == .numberOfRows
  130. assert .numberOfColumnsM == .numberOfColumns
  131. selectObject: .dissimilarity
  132. .tableOfReal = To TableOfReal
  133. .numberOfRowsT = Get number of rows
  134. .numberOfColumnsT = Get number of columns
  135. assert .numberOfRowsT == .numberOfRows
  136. assert .numberOfColumnsT == .numberOfColumns
  137. removeObject: .table, .matrix, .tableOfReal
  138. appendInfoLine: tab$, tab$, "To Configuration"
  139. selectObject: .dissimilarity
  140. for .ipar to 6
  141. .numberOfDimensions$ [.ipar] = "2, "
  142. endfor
  143. .numberOfDimensions$[6] = "2, 2, "
  144. .minimizationParameters$ = "1e-05, 10, 1"
  145. .mdsCommand$ [1] = "To Configuration (monotone mds): "
  146. .extraParameters$ [1] = """Primary approach"", "
  147. .mdsCommand$ [2] = "To Configuration (i-spline mds): "
  148. .extraParameters$ [2] = "1, 1, "
  149. .mdsCommand$ [3] = "To Configuration (interval mds): "
  150. .extraParameters$ [3] = ""
  151. .mdsCommand$ [4] = "To Configuration (ratio mds): "
  152. .extraParameters$ [4] = ""
  153. .mdsCommand$ [5] = "To Configuration (absolute mds): "
  154. .extraParameters$ [5] = ""
  155. .mdsCommand$ [6] = "To Configuration (kruskal): "
  156. .extraParameters$ [6] = """Primary approach"", ""Formula1"", "
  157. # Create a random configuration
  158. .command$ = .mdsCommand$ [1] + .numberOfDimensions$ [1] + .extraParameters$ [1] + .minimizationParameters$
  159. .randomConfiguration = '.command$'
  160. Formula: "randomUniform (-1, 1)"
  161. Rename: "random"
  162. # Use the 6 different "To Configuration (..)" commands to get 6 configurations
  163. for .itype to 6
  164. selectObject: .dissimilarity
  165. .command$ = .mdsCommand$ [.itype] + .numberOfDimensions$ [.itype] + .extraParameters$ [.itype] + .minimizationParameters$
  166. configuration [.itype] = '.command$'
  167. endfor
  168. # Use the dissimilarity and the configuration and try to improve the configuration
  169. appendInfoLine: tab$, tab$, "Dissimilarity & Configuration"
  170. .minimizationParameters$ = "1e-08, 50, 1"
  171. for .itype to 6
  172. selectObject: .dissimilarity, configuration [.itype]
  173. .command$ = .mdsCommand$ [.itype] + .extraParameters$ [.itype] + .minimizationParameters$
  174. .configuration [.itype] = '.command$'
  175. endfor
  176. .stressMeasure$ [1] = "Normalized"
  177. .stressMeasure$ [2] = "Kruskal's stress-1"
  178. .stressMeasure$ [3] = "Kruskal's stress-2"
  179. .stressMeasure$ [4] = "Raw"
  180. .tiesHandling$ [1] = "Primary approach"
  181. .tiesHandling$ [2] = "Secondary approach"
  182. .stressCalculation$ [1] = "Formula1"
  183. .stressCalculation$ [2] = "Formula2"
  184. # test kruskal's stress-1 and stress-2
  185. for .ities to 2
  186. selectObject: .dissimilarity, .randomConfiguration
  187. .stress1_random = Get stress (monotone mds): .tiesHandling$ [.ities], .stressMeasure$ [2]
  188. .stress2_random = Get stress (monotone mds): .tiesHandling$ [.ities], .stressMeasure$ [3]
  189. assert .stress1_random <= .stress2_random; '.stress1_random' <= '.stress2_random' ? random
  190. for .i to 6
  191. selectObject: .dissimilarity, .configuration [.i]
  192. .stress1 = Get stress (monotone mds): .tiesHandling$ [.ities], .stressMeasure$ [2]
  193. .stress2 = Get stress (monotone mds): .tiesHandling$ [.ities], .stressMeasure$ [3]
  194. assert .stress1 <= .stress1_random; '.stress1' <= '.stress1_random' ? '.ities' conf['.i']
  195. assert .stress2 <= .stress2_random; '.stress2' <= '.stress2_random' ? '.ities' conf['.i']
  196. assert .stress1 <= .stress2; '.stress1' <= '.stress2' ? '.ities' conf['.i']
  197. endfor
  198. endfor
  199. if 0
  200. for .k to 4
  201. selectObject: .dissimilarity, .randomConfiguration
  202. .stress0 = Get stress (i-spline mds): 1, 3, .stressMeasure$ [.k]
  203. selectObject: .dissimilarity, configuration [2]
  204. .stress1 = Get stress (i-spline mds): 1, 3, .stressMeasure$ [.k]
  205. assert .stress1 <= .stress0
  206. selectObject: .dissimilarity, .configuration [2]
  207. .stress2 = Get stress (i-spline mds): 1, 3, .stressMeasure$ [.k]
  208. assert .stress2 <= .stress1; '.stress2' '.stress1' '.k'
  209. endfor
  210. for .k from 1 to 4
  211. selectObject: .dissimilarity, .randomConfiguration
  212. .stress10 = Get stress (interval mds): .stressMeasure$ [.k]
  213. selectObject: .dissimilarity, configuration [3]
  214. .stress11 = Get stress (interval mds): .stressMeasure$ [.k]
  215. assert .stress11 <= .stress10 ; '.k'
  216. selectObject: .dissimilarity, .configuration [3]
  217. .stress12 = Get stress (interval mds): .stressMeasure$ [.k]
  218. assert .stress12 <= .stress11 ; '.k'
  219. selectObject: .dissimilarity, .randomConfiguration
  220. .stress20 = Get stress (ratio mds): .stressMeasure$ [.k]
  221. selectObject: .dissimilarity, configuration [4]
  222. .stress21 = Get stress (ratio mds): .stressMeasure$ [.k]
  223. assert .stress21 <= .stress20 ; '.k'
  224. selectObject: .dissimilarity, .configuration [4]
  225. .stress22 = Get stress (ratio mds): .stressMeasure$ [.k]
  226. assert .stress22 <= .stress21 ; '.k' '.stress22' < '.stress21' ?
  227. selectObject: .dissimilarity, .randomConfiguration
  228. .stress30 = Get stress (absolute mds): .stressMeasure$ [.k]
  229. selectObject: .dissimilarity, configuration [5]
  230. .stress31 = Get stress (absolute mds): .stressMeasure$ [.k]
  231. assert .stress31 <= .stress30 ; '.k'
  232. selectObject: .dissimilarity, .configuration [5]
  233. .stress32 = Get stress (absolute mds): .stressMeasure$ [.k]
  234. assert .stress32 <= .stress31 ; '.k'
  235. endfor
  236. endif
  237. for .itype to 6
  238. removeObject: .configuration [.itype]
  239. endfor
  240. removeObject: .dissimilarity, .randomConfiguration
  241. endproc
  242. procedure dissimilarity_to_Configurations: .dissimilarity
  243. endproc
  244. procedure testINDSCAL
  245. for .i to 6
  246. selectObject: configuration [.i]
  247. .distance [.i] = To Distance
  248. endfor
  249. selectObject: .distance [1]
  250. for .i from 2 to 6
  251. plusObject: .distance [.i]
  252. endfor
  253. To Configuration (indscal): 2, "no", 1e-5, 10, 1, "yes", "no"
  254. .configuration = selected ("Configuration")
  255. .salience = selected ("Salience")
  256. # test old interface
  257. ;To Configuration (indscal): "no", 1e-5, 10
  258. ;.configuration2 = selected ("Configuration")
  259. ;.salience2 = selected ("Salience")
  260. ;removeObject: .configuration2, .salience2
  261. for .i from 1 to 6
  262. removeObject: .distance[.i]
  263. endfor
  264. removeObject: .configuration, .salience
  265. endproc
  266. procedure test_additiveConstant
  267. # create table 18.1 Borg & Groenen (1997): Modern MDS
  268. # Check with top of table 18.3 where a value of 1.291 is given
  269. .distance = Create TableOfReal: "18.1", 4, 4
  270. .row1# = {0, pi, pi/4, pi/2}
  271. .row2# = {pi, 0, 3*pi/4, pi/2}
  272. .row3# = {pi/4, 3*pi/4, 0, 3*pi/4}
  273. .row4# = {pi/2, pi/2, 3*pi/4, 0}
  274. for .icol to 4
  275. for .irow to 4
  276. Set value: .irow, .icol, .row'.irow'# [.icol]
  277. endfor
  278. endfor
  279. .dissimilarity = To Dissimilarity
  280. .additiveConstant = Get additive constant
  281. .additiveConstant_rounded = number (fixed$ (.additiveConstant, 3))
  282. assert .additiveConstant_rounded = 1.291
  283. removeObject: .dissimilarity, .distance
  284. endproc