-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest-read_nisurface.R
120 lines (83 loc) · 4.37 KB
/
test-read_nisurface.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
test_that("A surface file in FreeSurfer binary format can be read using read_nisurfacefile", {
fsbin_surface_file = system.file("extdata", "lh.tinysurface", package = "freesurferformats", mustWork = TRUE);
surf = read_nisurfacefile(fsbin_surface_file);
expect_true(is.fs.surface(surf));
known_vertex_count = 5L;
known_face_count = 3L;
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("A surface file in FreeSurfer ASCII format can be read using read_nisurfacefile", {
fsasc_surface_file = system.file("extdata", "lh.tinysurface.asc", package = "freesurferformats", mustWork = TRUE);
surf = read_nisurfacefile(fsasc_surface_file);
surf2 = read_nisurfacefile.fsascii(fsasc_surface_file);
expect_true(is.fs.surface(surf));
expect_true(is.fs.surface(surf2));
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);
# The final default method should fail, it is reached if none of the readers were successfull
expect_error(read_nisurfacefile.default(fsasc_surface_file));
})
test_that("A surface file in GIFTI format can be read using read_nisurfacefile", {
gii_surface_file = system.file("extdata", "lh.tinysurface.gii", package = "freesurferformats", mustWork = TRUE);
surf = read_nisurfacefile(gii_surface_file);
surf2 = read_nisurfacefile.gifti(gii_surface_file);
expect_true(is.fs.surface(surf));
expect_true(is.fs.surface(surf2));
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);
})
test_that("A surface files is discovered and read from its basename by read_nisurface", {
fsasc_surface_file = system.file("extdata", "lh.testsurface.asc", package = "freesurferformats", mustWork = TRUE);
path_including_basename = file.path(dirname(fsasc_surface_file), 'lh.testsurface');
# Note: a file named like 'path_including_basename' does not exits. The read_nisurface function will check for
# a number of surface file extensions (indlucing '.asc', the one that exists) and still discover and read the file:
expect_false(file.exists(path_including_basename));
surf = read_nisurface(path_including_basename);
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);
})
test_that("Erros are thrown if something goes wrong", {
nofile = tempfile();
expect_error(read_nisurfacefile(nofile));
file.create(nofile); # empty file
expect_error(read_nisurfacefile.gifti(nofile));
expect_error(read_nisurfacefile.fsascii(nofile));
expect_error(read_nisurfacefile.fsnative(nofile));
})