1
1
from __future__ import division , print_function , absolute_import
2
2
import os
3
- from os .path import join as pjoin
3
+ from os .path import join as pjoin , isdir
4
4
import getpass
5
5
import time
6
+ import hashlib
7
+
6
8
7
9
from ...tmpdirs import InTemporaryDirectory
8
10
9
11
from nose .tools import assert_true
10
12
import numpy as np
11
- from numpy .testing import assert_equal
13
+ from numpy .testing import assert_equal , dec
14
+
15
+ from .. import (read_geometry , read_morph_data , read_annot , read_label ,
16
+ write_geometry , write_annot )
17
+
18
+ from ...tests .nibabel_data import get_nibabel_data
12
19
13
- from .. import read_geometry , read_morph_data , read_annot , read_label , \
14
- write_geometry , write_annot
15
20
21
+ DATA_SDIR = 'fsaverage'
16
22
17
- have_freesurfer = True
18
- if 'SUBJECTS_DIR' not in os .environ :
19
- # Test suite relies on the definition of SUBJECTS_DIR
20
- have_freesurfer = False
23
+ have_freesurfer = False
24
+ if 'SUBJECTS_DIR' in os .environ :
25
+ # May have Freesurfer installed with data
26
+ data_path = pjoin (os .environ ["SUBJECTS_DIR" ], DATA_SDIR )
27
+ have_freesurfer = isdir (data_path )
28
+ else :
29
+ # May have nibabel test data submodule checked out
30
+ nib_data = get_nibabel_data ()
31
+ if nib_data != '' :
32
+ data_path = pjoin (nib_data , 'nitest-freesurfer' , DATA_SDIR )
33
+ have_freesurfer = isdir (data_path )
21
34
22
- freesurfer_test = np .testing .dec .skipif (not have_freesurfer ,
23
- 'SUBJECTS_DIR not set' )
35
+ freesurfer_test = dec .skipif (
36
+ not have_freesurfer ,
37
+ 'cannot find freesurfer {0} directory' .format (DATA_SDIR ))
24
38
25
- if have_freesurfer :
26
- subj_dir = os .environ ["SUBJECTS_DIR" ]
27
- subject_id = 'fsaverage'
28
- data_path = pjoin (subj_dir , subject_id )
39
+
40
+ def _hash_file_content (fname ):
41
+ hasher = hashlib .md5 ()
42
+ with open (fname , 'rb' ) as afile :
43
+ buf = afile .read ()
44
+ hasher .update (buf )
45
+ return hasher .hexdigest ()
29
46
30
47
31
48
@freesurfer_test
@@ -83,6 +100,8 @@ def test_annot():
83
100
annots = ['aparc' , 'aparc.a2005s' ]
84
101
for a in annots :
85
102
annot_path = pjoin (data_path , "label" , "%s.%s.annot" % ("lh" , a ))
103
+ hash_ = _hash_file_content (annot_path )
104
+
86
105
labels , ctab , names = read_annot (annot_path )
87
106
assert_true (labels .shape == (163842 , ))
88
107
assert_true (ctab .shape == (len (names ), 5 ))
@@ -91,7 +110,14 @@ def test_annot():
91
110
if a == 'aparc' :
92
111
labels_orig , _ , _ = read_annot (annot_path , orig_ids = True )
93
112
np .testing .assert_array_equal (labels == - 1 , labels_orig == 0 )
94
- assert_true (np .sum (labels_orig == 0 ) > 0 )
113
+ # Handle different version of fsaverage
114
+ if hash_ == 'bf0b488994657435cdddac5f107d21e8' :
115
+ assert_true (np .sum (labels_orig == 0 ) == 13887 )
116
+ elif hash_ == 'd4f5b7cbc2ed363ac6fcf89e19353504' :
117
+ assert_true (np .sum (labels_orig == 1639705 ) == 13327 )
118
+ else :
119
+ raise RuntimeError ("Unknown freesurfer file. Please report "
120
+ "the problem to the maintainer of nibabel." )
95
121
96
122
# Test equivalence of freesurfer- and nibabel-generated annot files
97
123
# with respect to read_annot()
0 commit comments