test_gifti.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. function tests = test_gifti
  2. % Unit Tests for gifti
  3. %__________________________________________________________________________
  4. % Copyright (C) 2015 Wellcome Trust Centre for Neuroimaging
  5. % $Id: test_gifti.m 6516 2015-08-07 17:28:33Z guillaume $
  6. tests = functiontests(localfunctions);
  7. function test_gifti_constructor(testCase)
  8. import matlab.unittest.constraints.*
  9. g1 = gifti;
  10. g2 = gifti(fullfile(spm('Dir'),'canonical','cortex_20484.surf.gii'));
  11. g3 = gifti(g2);
  12. s = struct(g3);
  13. g4 = gifti(s);
  14. g5 = gifti(rand(16,1));
  15. testCase.verifyThat(g1, IsOfClass('gifti'));
  16. testCase.verifyThat(fieldnames(g1), IsEmpty);
  17. testCase.verifyThat(g2, IsOfClass('gifti'));
  18. testCase.verifyThat(s, HasField('faces'));
  19. testCase.verifyThat(s, HasField('vertices'));
  20. testCase.verifyThat(s, HasField('mat'));
  21. testCase.verifyThat(g3, IsOfClass('gifti'));
  22. testCase.verifyThat(g3, IsEqualTo(g2));
  23. testCase.verifyThat(s, IsOfClass('struct'));
  24. testCase.verifyThat(s, HasField('faces'));
  25. testCase.verifyThat(s, HasField('vertices'));
  26. testCase.verifyThat(s, HasField('mat'));
  27. testCase.verifyThat(g4, IsOfClass('gifti'));
  28. testCase.verifyThat(struct(g4), IsEqualTo(s));
  29. testCase.verifyThat(g5, IsOfClass('gifti'));
  30. testCase.verifyThat(g5, HasField('cdata'));
  31. function test_gifti_accessor(testCase)
  32. import matlab.unittest.constraints.*
  33. g1 = gifti(fullfile(spm('Dir'),'canonical','cortex_8196.surf.gii'));
  34. s.faces = g1.faces;
  35. s.vertices = g1.vertices;
  36. s.mat = g1.mat;
  37. testCase.verifyEqual(s.faces(1000,:), g1.faces(1000,:));
  38. testCase.verifyEqual(s.vertices(1000,:), g1.vertices(1000,:));
  39. testCase.verifyEqual(s.mat(:,4), g1.mat(:,4));
  40. cdata = single(rand(16,1));
  41. g2 = gifti(cdata);
  42. testCase.verifyEqual(cdata,g2.cdata);
  43. testCase.verifyEqual(cdata(8),g2.cdata(8));
  44. testCase.verifyEqual(cdata(8,1),g2.cdata(8,1));
  45. function test_gifti_mutator(testCase)
  46. import matlab.unittest.constraints.*
  47. g = gifti(fullfile(spm('Dir'),'canonical','cortex_5124.surf.gii'));
  48. g.mat = eye(4);
  49. g.mat(logical(eye(4))) = 2;
  50. g.mat(4,4) = 1;
  51. testCase.verifyEqual(g.mat, diag([2 2 2 1]));
  52. faces = g.faces;
  53. faces = faces(:,[2 1 3]);
  54. g.faces = faces;
  55. g.faces(64,:) = [1 2 3];
  56. g.faces(:,1) = faces(:,1);
  57. testCase.verifyEqual(g.faces(64,:), [faces(64,1) 2 3]);
  58. vertices = g.vertices;
  59. vertices = vertices(:,[2 1 3]);
  60. g.vertices = vertices;
  61. g.vertices(64,:) = zeros(1,3);
  62. g.vertices(:,2:3) = ones(size(vertices,1),2);
  63. testCase.verifyEqual(g.vertices(64,:), single([0 1 1]));
  64. g.cdata = rand(size(g.vertices,1),1);
  65. g.cdata(1) = pi;
  66. g.cdata(2:end) = exp(1);
  67. testCase.verifyEqual(g.cdata(1), single(pi));
  68. function test_gifti_export(testCase)
  69. import matlab.unittest.constraints.*
  70. mri = load('mri');
  71. g = gifti(isosurface(smooth3(squeeze(mri.D)),5));
  72. s = export(g);
  73. testCase.verifyThat(s, HasField('vertices'));
  74. testCase.verifyThat(s, HasField('faces'));
  75. s = export(g,'matlab');
  76. testCase.verifyThat(s, HasField('vertices'));
  77. testCase.verifyThat(s, HasField('faces'));
  78. s = export(g,'patch');
  79. testCase.verifyThat(s, HasField('vertices'));
  80. testCase.verifyThat(s, HasField('faces'));
  81. testCase.verifyThat(s, ~HasField('mat'));
  82. s = export(g,'fieldtrip');
  83. testCase.verifyThat(s, HasField('pnt'));
  84. testCase.verifyThat(s, HasField('tri'));
  85. s = export(g,'spm');
  86. testCase.verifyThat(s, HasField('vert'));
  87. testCase.verifyThat(s, HasField('face'));
  88. function test_gifti_load(testCase)
  89. import matlab.unittest.constraints.*
  90. d = fullfile(spm('Dir'),'canonical');
  91. files = dir(fullfile(d,'*.gii'));
  92. for i=1:numel(files)
  93. g = gifti(fullfile(d,files(i).name));
  94. testCase.verifyThat(evalc('g'), ~IsEmpty); % check display()
  95. end
  96. function test_gifti_save(testCase)
  97. mri = load('mri');
  98. g = gifti(isosurface(smooth3(squeeze(mri.D)),5));
  99. g.cdata = rand(size(g.vertices,1),1);
  100. basename = tempname;
  101. file = [basename '.gii'];
  102. save(g,file,'ASCII');
  103. g = gifti(file);
  104. delete(file);
  105. save(g,file,'Base64Binary');
  106. g = gifti(file);
  107. delete(file);
  108. save(g,file,'GZipBase64Binary');
  109. g = gifti(file);
  110. delete(file);
  111. save(g,file,'ExternalFileBinary');
  112. g = gifti(file);
  113. delete(file);
  114. delete([basename '.dat']);