-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest-read_fs_surface.R
430 lines (303 loc) · 17.3 KB
/
test-read_fs_surface.R
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
test_that("Our demo surface file can be read using read.fs.surface", {
testthat::skip_on_cran(); # cannot download testdata on CRAN.
skip_if(tests_running_on_cran_under_macos(), message = "Skipping on CRAN under MacOS, required test data cannot be downloaded.");
freesurferformats::download_opt_data();
subjects_dir = freesurferformats::get_opt_data_filepath("subjects_dir");
surface_file = file.path(subjects_dir, "subject1", "surf", "lh.white");
skip_if_not(file.exists(surface_file), message="Test data missing.") ;
surf = read.fs.surface(surface_file);
expect_true(is.fs.surface(surf));
known_vertex_count = 149244;
known_face_count = 298484;
expect_equal(surf$mesh_face_type, "tris");
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices
expect_equal(typeof(surf$faces), "integer");
# Check whether vertex indices were incremented properly
num_faces_with_index_zero = sum(surf$faces==0);
expect_equal(num_faces_with_index_zero, 0);
})
test_that("Vertex connectivity in the demo surface file is as expected from reference implementation", {
# Checks for bug in issue #8: freesurfer surface import - row-major/column-major order
# The vertex connectivities used in this test are known from running the FreeSurfer Matlab function $FREESURFER_HOME/matlab/read_surf.m on the data
testthat::skip_on_cran(); # cannot download testdata on CRAN.
skip_if(tests_running_on_cran_under_macos(), message = "Skipping on CRAN under MacOS, required test data cannot be downloaded.");
freesurferformats::download_opt_data();
subjects_dir = freesurferformats::get_opt_data_filepath("subjects_dir");
surface_file = file.path(subjects_dir, "subject1", "surf", "lh.white");
skip_if_not(file.exists(surface_file), message="Test data missing.");
surf = read.fs.surface(surface_file);
expect_true(is.fs.surface(surf));
# This test assures that the vertices are connected as defined in the reference implementation.
expect_equal(surf$faces[1,], c(0, 1, 5) + 1); # the +1 is because R using 1-based indexing
expect_equal(surf$faces[100,], c(137, 130, 41) + 1);
expect_equal(surf$faces[1000,], c(770, 444, 757) + 1);
expect_equal(surf$vertices[1,], c(-1.8522, -107.9827, 22.7697), tolerance=1e-2);
expect_equal(surf$vertices[100,], c(-4.3814, -107.6495, 24.0106), tolerance=1e-2);
expect_equal(surf$vertices[1000,], c(-5.2788, -103.5765, 19.6616), tolerance=1e-2);
})
test_that("The vertices of a face are close to each other", {
# Checks for bug in issue #8: freesurfer surface import - row-major/column-major order
testthat::skip_on_cran(); # cannot download testdata on CRAN.
skip_if(tests_running_on_cran_under_macos(), message = "Skipping on CRAN under MacOS, required test data cannot be downloaded.");
freesurferformats::download_opt_data();
subjects_dir = freesurferformats::get_opt_data_filepath("subjects_dir");
surface_file = file.path(subjects_dir, "subject1", "surf", "lh.white");
skip_if_not(file.exists(surface_file), message="Test data missing.");
surf = read.fs.surface(surface_file);
expect_true(is.fs.surface(surf));
# Test that the distance between the vertices of a face is small. For brain surface meshes, the
# coords are given in mm and the resolution is quite high. It is definitely sane to request that
# the vertices should not be more than 2 mm apart:
test_faces = c(1, 50, 100, 1000, 10000, 42);
for(face_idx in test_faces) {
vertex_indices_of_face = surf$faces[face_idx,];
vertex_coords_of_face = surf$vertices[vertex_indices_of_face,];
dist_matrix_face_verts = stats::dist(vertex_coords_of_face);
expect_true(max(dist_matrix_face_verts) < 2.0);
}
})
test_that("The lh.white of Bert can be read using read.fs.surface", {
surface_file = system.file("extdata", "bert.lh.white", package = "freesurferformats", mustWork = FALSE)
skip_if_not(file.exists(surface_file), message="Test data missing.");
surface_file = system.file("extdata", "bert.lh.white", package = "freesurferformats", mustWork = TRUE)
surf = read.fs.surface(surface_file);
expect_true(is.fs.surface(surf));
known_vertex_count_bert = 133176;
known_face_count_bert = 266348;
expect_equal(surf$mesh_face_type, "tris");
expect_true(is.fs.surface(surf));
expect_false(is.fs.surface(list("whatever"=6)));
expect_equal(nrow(surf$vertices), known_vertex_count_bert);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count_bert);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices
expect_equal(typeof(surf$faces), "integer");
# Check whether vertex indices were incremented properly
num_faces_with_index_zero = sum(surf$faces==0);
expect_equal(num_faces_with_index_zero, 0);
# This test assures that the vertices are connected as defined in the reference implementation.
expect_equal(surf$faces[1,], c(0, 1, 3) + 1); # the +1 is because R using 1-based indexing
expect_equal(surf$faces[100,], c(140, 36, 139) + 1);
expect_equal(surf$faces[1000,], c(440, 454, 441) + 1);
expect_equal(surf$vertices[1,], c(-12.6998, -102.2399, -10.0076), tolerance=1e-2);
expect_equal(surf$vertices[100,], c(-12.234, -100.672, 0.129), tolerance=1e-2);
expect_equal(surf$vertices[1000,], c(-18.8, -97.6, -16.8), tolerance=1e-2);
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("A surface file in FreeSurfer ASCII format can be read using read.fs.surface", {
fsasc_surface_file = system.file("extdata", "lh.tinysurface.asc", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(fsasc_surface_file);
expect_true(is.fs.surface(surf));
known_vertex_count = 5L;
known_face_count = 3L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices
expect_equal(typeof(surf$faces), "integer");
# Check whether vertex indices were incremented properly
num_faces_with_index_zero = sum(surf$faces==0);
expect_equal(num_faces_with_index_zero, 0);
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("The binary version of our FreeSurfer tiny surface file can be read using read.fs.surface", {
surface_file = system.file("extdata", "lh.tinysurface", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file);
expect_true(is.fs.surface(surf));
known_vertex_count = 5L;
known_face_count = 3L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices
expect_equal(typeof(surf$faces), "integer");
# Check whether vertex indices were incremented properly
num_faces_with_index_zero = sum(surf$faces==0);
expect_equal(num_faces_with_index_zero, 0);
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("A surface file in STL binary format can be read using read.fs.surface", {
stlbin_surface_file = system.file("extdata", "cube_bin.stl", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(stlbin_surface_file);
known_vertex_count = 8L;
known_face_count = 12L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("A surface file in BYU ASCII mesh format can be read using read.fs.surface", {
byu_surface_file = system.file("extdata", "cube_quads.byu", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(byu_surface_file);
known_vertex_count = 8L;
known_face_count = 12L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices of a triangle
# The file actually contains quadrangular polygons, not triangles. The function
# remeshes automatically, but we can access the original quads as well:
expect_equal(ncol(surf$metadata$faces_quads), 4); # the 4 vertex indices of a quad
expect_equal(nrow(surf$metadata$faces_quads), known_face_count / 2L);
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
# expected errors
expect_error(read.fs.surface.byu(byu_surface_file, part = 3L));
})
test_that("Surface files in GEO format can be read using read.fs.surface", {
surface_file = system.file("extdata", "cube.geo", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file);
known_vertex_count = 8L;
known_face_count = 12L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices of a triangle
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("Surface files in STL format can be read using read.fs.surface", {
surface_file = system.file("extdata", "cube.stl", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file);
known_vertex_count = 8L;
known_face_count = 12L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices of a triangle
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("Surface files in TRI format can be read using read.fs.surface", {
surface_file = system.file("extdata", "cube.tri", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file);
known_vertex_count = 8L;
known_face_count = 12L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices of a triangle
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("Surface files in VTK format can be read using read.fs.surface", {
surface_file = system.file("extdata", "cube.vtk", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file);
known_vertex_count = 8L;
known_face_count = 12L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices of a triangle
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("Surface files in Wavefront OBJ format can be read using read.fs.surface", {
# I had to change the file extension of demo .obj files that come with this package to
# '.wobj', because the file extension '.obj' triggered false positive warnings on CRAN. ~
surface_file = system.file("extdata", "cube.wobj", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file, format = 'obj');
known_vertex_count = 8L;
known_face_count = 12L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices of a triangle
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("Surface files in Wavefront OBJ format with non-standard vertex colors can be read and written.", {
# I had to change the file extension of demo .obj files that come with this package to
# '.wobj', because the file extension '.obj' triggered false positive warnings on CRAN. ~
surface_file = system.file("extdata", "cube.wobj", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file, format = 'obj');
# Test writing vertex colors as strings and re-reading. (Will be converted to floats before writing.)
known_vertex_count = 8L;
known_face_count = 12L;
vertex_colors_string = rep("green", known_vertex_count);
vertex_colors_string[3:5] = 'blue';
tfile = tempfile(fileext = '.wobj');
write.fs.surface.obj(tfile, surf$vertices, surf$faces, vertex_colors = vertex_colors_string);
surf_col_string = read.fs.surface.obj(tfile);
expect_false(is.null(surf_col_string$vertex_colors));
expect_equal(nrow(surf_col_string$vertex_colors), known_vertex_count);
expect_equal(ncol(surf_col_string$vertex_colors), 3L); # RGB
expect_equal(nrow(surf_col_string$vertices), known_vertex_count);
expect_equal(ncol(surf_col_string$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf_col_string$vertices), "double");
expect_equal(nrow(surf_col_string$faces), known_face_count);
expect_equal(ncol(surf_col_string$faces), 3); # the 3 vertex indices of a triangle
expect_equal(min(surf_col_string$faces), 1L); # vertex indices must start at 1
})
test_that("Surface files in Object File Format (OFF) can be read using read.fs.surface", {
surface_file = system.file("extdata", "cube.off", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file);
known_vertex_count = 8L;
known_face_count = 12L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices of a triangle
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("Surface files in MZ3 Format can be read using read.fs.surface", {
surface_file = system.file("extdata", "cube.mz3", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file);
known_vertex_count = 8L;
known_face_count = 12L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices of a triangle
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("Surface files in PLY Format can be read using read.fs.surface", {
surface_file = system.file("extdata", "cube.ply", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file);
known_vertex_count = 8L;
known_face_count = 12L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices of a triangle
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})
test_that("The read.fs.surface function errors on invalid format.", {
expect_error(read.fs.surface(tempfile(), format = 'invalid_format')); # invalid format
})
test_that("An even number of triangular faces can be converted to quad faces", {
surface_file = system.file("extdata", "cube.mz3", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file);
tris_faces = surf$faces;
expect_equal(nrow(tris_faces), 12L);
quad_faces = faces.tris.to.quad(tris_faces);
expect_equal(nrow(quad_faces), 6L);
expect_equal(faces.quad.to.tris(quad_faces), tris_faces);
# It des not work with uneven tris face count:
brk_tris_faces = rbind(tris_faces, c(3, 3, 3));
expect_error(faces.tris.to.quad(brk_tris_faces));
})
test_that("Surface files in GIFTI Format can be read using read.fs.surface", {
surface_file = system.file("extdata", "cube.gii", package = "freesurferformats", mustWork = TRUE);
surf = read.fs.surface(surface_file);
known_vertex_count = 8L;
known_face_count = 12L;
expect_equal(nrow(surf$vertices), known_vertex_count);
expect_equal(ncol(surf$vertices), 3); # the 3 coords (x,y,z)
expect_equal(typeof(surf$vertices), "double");
expect_equal(nrow(surf$faces), known_face_count);
expect_equal(ncol(surf$faces), 3); # the 3 vertex indices of a triangle
expect_equal(min(surf$faces), 1L); # vertex indices must start at 1
})