16
16
17
17
from numpy .testing import (assert_array_almost_equal ,
18
18
assert_array_equal )
19
- from nose . tools import ( assert_true , assert_false , assert_equal , assert_raises )
19
+ import pytest
20
20
from nibabel .testing import clear_and_catch_warnings , test_data
21
21
from .test_parse_gifti_fast import (DATA_FILE1 , DATA_FILE2 , DATA_FILE3 ,
22
22
DATA_FILE4 , DATA_FILE5 , DATA_FILE6 )
@@ -35,7 +35,7 @@ def test_agg_data():
35
35
func_data = np .column_stack (tuple (da .data for da in func_da ))
36
36
shape_data = shape_gii_img .get_arrays_from_intent ('shape' )[0 ].data
37
37
38
- assert_equal ( surf_gii_img .agg_data (), (point_data , triangle_data ) )
38
+ assert surf_gii_img .agg_data () == (point_data , triangle_data )
39
39
assert_array_equal (func_gii_img .agg_data (), func_data )
40
40
assert_array_equal (shape_gii_img .agg_data (), shape_data )
41
41
@@ -44,219 +44,218 @@ def test_agg_data():
44
44
assert_array_equal (func_gii_img .agg_data ('time series' ), func_data )
45
45
assert_array_equal (shape_gii_img .agg_data ('shape' ), shape_data )
46
46
47
- assert_equal ( surf_gii_img .agg_data ('time series' ), () )
48
- assert_equal ( func_gii_img .agg_data ('triangle' ), () )
49
- assert_equal ( shape_gii_img .agg_data ('pointset' ), () )
47
+ assert surf_gii_img .agg_data ('time series' ) == ( )
48
+ assert func_gii_img .agg_data ('triangle' ) == ( )
49
+ assert shape_gii_img .agg_data ('pointset' ) == ( )
50
50
51
- assert_equal ( surf_gii_img .agg_data (('pointset' , 'triangle' )), (point_data , triangle_data ) )
52
- assert_equal ( surf_gii_img .agg_data (('triangle' , 'pointset' )), (triangle_data , point_data ) )
51
+ assert surf_gii_img .agg_data (('pointset' , 'triangle' )) == (point_data , triangle_data )
52
+ assert surf_gii_img .agg_data (('triangle' , 'pointset' )) == (triangle_data , point_data )
53
53
54
54
def test_gifti_image ():
55
55
# Check that we're not modifying the default empty list in the default
56
56
# arguments.
57
57
gi = GiftiImage ()
58
- assert_equal ( gi .darrays , [])
59
- assert_equal ( gi .meta .metadata , {})
60
- assert_equal ( gi .labeltable .labels , [])
58
+ assert gi .darrays == []
59
+ assert gi .meta .metadata == {}
60
+ assert gi .labeltable .labels == []
61
61
arr = np .zeros ((2 , 3 ))
62
62
gi .darrays .append (arr )
63
63
# Now check we didn't overwrite the default arg
64
64
gi = GiftiImage ()
65
- assert_equal ( gi .darrays , [])
65
+ assert gi .darrays == []
66
66
67
67
# Test darrays / numDA
68
68
gi = GiftiImage ()
69
- assert_equal ( gi .numDA , 0 )
69
+ assert gi .numDA == 0
70
70
71
71
# Test from numpy numeric array
72
72
data = np .random .random ((5 ,))
73
73
da = GiftiDataArray (data )
74
74
gi .add_gifti_data_array (da )
75
- assert_equal ( gi .numDA , 1 )
75
+ assert gi .numDA == 1
76
76
assert_array_equal (gi .darrays [0 ].data , data )
77
77
78
78
# Test removing
79
79
gi .remove_gifti_data_array (0 )
80
- assert_equal ( gi .numDA , 0 )
80
+ assert gi .numDA == 0
81
81
82
82
# Remove from empty
83
83
gi = GiftiImage ()
84
84
gi .remove_gifti_data_array_by_intent (0 )
85
- assert_equal ( gi .numDA , 0 )
85
+ assert gi .numDA == 0
86
86
87
87
# Remove one
88
88
gi = GiftiImage ()
89
89
da = GiftiDataArray (np .zeros ((5 ,)), intent = 0 )
90
90
gi .add_gifti_data_array (da )
91
91
92
92
gi .remove_gifti_data_array_by_intent (3 )
93
- assert_equal ( gi .numDA , 1 , "data array should exist on 'missed' remove" )
93
+ assert gi .numDA == 1 , "data array should exist on 'missed' remove"
94
94
95
95
gi .remove_gifti_data_array_by_intent (da .intent )
96
- assert_equal ( gi .numDA , 0 )
96
+ assert gi .numDA == 0
97
97
98
98
99
99
def test_gifti_image_bad_inputs ():
100
100
img = GiftiImage ()
101
101
# Try to set a non-data-array
102
- assert_raises (TypeError , img .add_gifti_data_array , 'not-a-data-array' )
102
+ pytest . raises (TypeError , img .add_gifti_data_array , 'not-a-data-array' )
103
103
104
104
# Try to set to non-table
105
105
def assign_labeltable (val ):
106
106
img .labeltable = val
107
- assert_raises (TypeError , assign_labeltable , 'not-a-table' )
107
+ pytest . raises (TypeError , assign_labeltable , 'not-a-table' )
108
108
109
109
# Try to set to non-table
110
110
def assign_metadata (val ):
111
111
img .meta = val
112
- assert_raises (TypeError , assign_metadata , 'not-a-meta' )
112
+ pytest . raises (TypeError , assign_metadata , 'not-a-meta' )
113
113
114
114
115
115
def test_dataarray_empty ():
116
116
# Test default initialization of DataArray
117
117
null_da = GiftiDataArray ()
118
- assert_equal ( null_da .data , None )
119
- assert_equal ( null_da .intent , 0 )
120
- assert_equal ( null_da .datatype , 0 )
121
- assert_equal ( null_da .encoding , 3 )
122
- assert_equal ( null_da .endian , 2 if sys .byteorder == 'little' else 1 )
123
- assert_equal ( null_da .coordsys .dataspace , 0 )
124
- assert_equal ( null_da .coordsys .xformspace , 0 )
118
+ assert null_da .data is None
119
+ assert null_da .intent == 0
120
+ assert null_da .datatype == 0
121
+ assert null_da .encoding == 3
122
+ assert null_da .endian == ( 2 if sys .byteorder == 'little' else 1 )
123
+ assert null_da .coordsys .dataspace == 0
124
+ assert null_da .coordsys .xformspace == 0
125
125
assert_array_equal (null_da .coordsys .xform , np .eye (4 ))
126
- assert_equal ( null_da .ind_ord , 1 )
127
- assert_equal ( null_da .meta .metadata , {})
128
- assert_equal ( null_da .ext_fname , '' )
129
- assert_equal ( null_da .ext_offset , 0 )
126
+ assert null_da .ind_ord == 1
127
+ assert null_da .meta .metadata == {}
128
+ assert null_da .ext_fname == ''
129
+ assert null_da .ext_offset == 0
130
130
131
131
132
132
def test_dataarray_init ():
133
133
# Test non-default dataarray initialization
134
134
gda = GiftiDataArray # shortcut
135
- assert_equal ( gda (None ).data , None )
135
+ assert gda (None ).data is None
136
136
arr = np .arange (12 , dtype = np .float32 ).reshape ((3 , 4 ))
137
137
assert_array_equal (gda (arr ).data , arr )
138
138
# Intents
139
- assert_raises (KeyError , gda , intent = 1 ) # Invalid code
140
- assert_raises (KeyError , gda , intent = 'not an intent' ) # Invalid string
141
- assert_equal ( gda (intent = 2 ).intent , 2 )
142
- assert_equal ( gda (intent = 'correlation' ).intent , 2 )
143
- assert_equal ( gda (intent = 'NIFTI_INTENT_CORREL' ).intent , 2 )
139
+ pytest . raises (KeyError , gda , intent = 1 ) # Invalid code
140
+ pytest . raises (KeyError , gda , intent = 'not an intent' ) # Invalid string
141
+ assert gda (intent = 2 ).intent == 2
142
+ assert gda (intent = 'correlation' ).intent == 2
143
+ assert gda (intent = 'NIFTI_INTENT_CORREL' ).intent == 2
144
144
# Datatype
145
- assert_equal ( gda (datatype = 2 ).datatype , 2 )
146
- assert_equal ( gda (datatype = 'uint8' ).datatype , 2 )
147
- assert_raises (KeyError , gda , datatype = 'not_datatype' )
145
+ assert gda (datatype = 2 ).datatype == 2
146
+ assert gda (datatype = 'uint8' ).datatype == 2
147
+ pytest . raises (KeyError , gda , datatype = 'not_datatype' )
148
148
# Float32 datatype comes from array if datatype not set
149
- assert_equal ( gda (arr ).datatype , 16 )
149
+ assert gda (arr ).datatype == 16
150
150
# Can be overriden by init
151
- assert_equal ( gda (arr , datatype = 'uint8' ).datatype , 2 )
151
+ assert gda (arr , datatype = 'uint8' ).datatype == 2
152
152
# Encoding
153
- assert_equal ( gda (encoding = 1 ).encoding , 1 )
154
- assert_equal ( gda (encoding = 'ASCII' ).encoding , 1 )
155
- assert_equal ( gda (encoding = 'GIFTI_ENCODING_ASCII' ).encoding , 1 )
156
- assert_raises (KeyError , gda , encoding = 'not an encoding' )
153
+ assert gda (encoding = 1 ).encoding == 1
154
+ assert gda (encoding = 'ASCII' ).encoding == 1
155
+ assert gda (encoding = 'GIFTI_ENCODING_ASCII' ).encoding == 1
156
+ pytest . raises (KeyError , gda , encoding = 'not an encoding' )
157
157
# Endian
158
- assert_equal ( gda (endian = 1 ).endian , 1 )
159
- assert_equal ( gda (endian = 'big' ).endian , 1 )
160
- assert_equal ( gda (endian = 'GIFTI_ENDIAN_BIG' ).endian , 1 )
161
- assert_raises (KeyError , gda , endian = 'not endian code' )
158
+ assert gda (endian = 1 ).endian == 1
159
+ assert gda (endian = 'big' ).endian == 1
160
+ assert gda (endian = 'GIFTI_ENDIAN_BIG' ).endian == 1
161
+ pytest . raises (KeyError , gda , endian = 'not endian code' )
162
162
# CoordSys
163
163
aff = np .diag ([2 , 3 , 4 , 1 ])
164
164
cs = GiftiCoordSystem (1 , 2 , aff )
165
165
da = gda (coordsys = cs )
166
- assert_equal ( da .coordsys .dataspace , 1 )
167
- assert_equal ( da .coordsys .xformspace , 2 )
166
+ assert da .coordsys .dataspace == 1
167
+ assert da .coordsys .xformspace == 2
168
168
assert_array_equal (da .coordsys .xform , aff )
169
169
# Ordering
170
- assert_equal ( gda (ordering = 2 ).ind_ord , 2 )
171
- assert_equal ( gda (ordering = 'F' ).ind_ord , 2 )
172
- assert_equal ( gda (ordering = 'ColumnMajorOrder' ).ind_ord , 2 )
173
- assert_raises (KeyError , gda , ordering = 'not an ordering' )
170
+ assert gda (ordering = 2 ).ind_ord == 2
171
+ assert gda (ordering = 'F' ).ind_ord == 2
172
+ assert gda (ordering = 'ColumnMajorOrder' ).ind_ord == 2
173
+ pytest . raises (KeyError , gda , ordering = 'not an ordering' )
174
174
# metadata
175
175
meta_dict = dict (one = 1 , two = 2 )
176
- assert_equal (gda (meta = GiftiMetaData .from_dict (meta_dict )).meta .metadata ,
177
- meta_dict )
178
- assert_equal (gda (meta = meta_dict ).meta .metadata , meta_dict )
179
- assert_equal (gda (meta = None ).meta .metadata , {})
176
+ assert gda (meta = GiftiMetaData .from_dict (meta_dict )).meta .metadata == meta_dict
177
+ assert gda (meta = meta_dict ).meta .metadata == meta_dict
178
+ assert gda (meta = None ).meta .metadata == {}
180
179
# ext_fname and ext_offset
181
- assert_equal ( gda (ext_fname = 'foo' ).ext_fname , 'foo' )
182
- assert_equal ( gda (ext_offset = 12 ).ext_offset , 12 )
180
+ assert gda (ext_fname = 'foo' ).ext_fname == 'foo'
181
+ assert gda (ext_offset = 12 ).ext_offset == 12
183
182
184
183
185
184
def test_dataarray_from_array ():
186
185
with clear_and_catch_warnings () as w :
187
186
warnings .filterwarnings ('always' , category = DeprecationWarning )
188
187
da = GiftiDataArray .from_array (np .ones ((3 , 4 )))
189
- assert_equal ( len (w ), 1 )
188
+ assert len (w ) == 1
190
189
for dt_code in data_type_codes .value_set ():
191
190
data_type = data_type_codes .type [dt_code ]
192
191
if data_type is np .void : # not supported
193
192
continue
194
193
arr = np .zeros ((10 , 3 ), dtype = data_type )
195
194
da = GiftiDataArray .from_array (arr , 'triangle' )
196
- assert_equal ( da .datatype , data_type_codes [arr .dtype ])
195
+ assert da .datatype == data_type_codes [arr .dtype ]
197
196
bs_arr = arr .byteswap ().newbyteorder ()
198
197
da = GiftiDataArray .from_array (bs_arr , 'triangle' )
199
- assert_equal ( da .datatype , data_type_codes [arr .dtype ])
198
+ assert da .datatype == data_type_codes [arr .dtype ]
200
199
201
200
202
201
def test_to_xml_open_close_deprecations ():
203
202
# Smoke test on deprecated functions
204
203
da = GiftiDataArray (np .ones ((1 ,)), 'triangle' )
205
204
with clear_and_catch_warnings () as w :
206
205
warnings .filterwarnings ('always' , category = DeprecationWarning )
207
- assert_true ( isinstance (da .to_xml_open (), str ) )
208
- assert_equal ( len (w ), 1 )
206
+ assert isinstance (da .to_xml_open (), str )
207
+ assert len (w ) == 1
209
208
with clear_and_catch_warnings () as w :
210
209
warnings .filterwarnings ('once' , category = DeprecationWarning )
211
- assert_true ( isinstance (da .to_xml_close (), str ) )
212
- assert_equal ( len (w ), 1 )
210
+ assert isinstance (da .to_xml_close (), str )
211
+ assert len (w ) == 1
213
212
214
213
215
214
def test_num_dim_deprecation ():
216
215
da = GiftiDataArray (np .ones ((2 , 3 , 4 )))
217
216
# num_dim is property, set automatically from len(da.dims)
218
- assert_equal ( da .num_dim , 3 )
217
+ assert da .num_dim == 3
219
218
with clear_and_catch_warnings () as w :
220
219
warnings .filterwarnings ('always' , category = DeprecationWarning )
221
220
# OK setting num_dim to correct value, but raises DeprecationWarning
222
221
da .num_dim = 3
223
- assert_equal ( len (w ), 1 )
222
+ assert len (w ) == 1
224
223
# Any other value gives a ValueError
225
- assert_raises (ValueError , setattr , da , 'num_dim' , 4 )
224
+ pytest . raises (ValueError , setattr , da , 'num_dim' , 4 )
226
225
227
226
228
227
def test_labeltable ():
229
228
img = GiftiImage ()
230
- assert_equal ( len (img .labeltable .labels ), 0 )
229
+ assert len (img .labeltable .labels ) == 0
231
230
232
231
new_table = GiftiLabelTable ()
233
232
new_table .labels += ['test' , 'me' ]
234
233
img .labeltable = new_table
235
- assert_equal ( len (img .labeltable .labels ), 2 )
234
+ assert len (img .labeltable .labels ) == 2
236
235
237
236
# Test deprecations
238
237
with clear_and_catch_warnings () as w :
239
238
warnings .filterwarnings ('always' , category = DeprecationWarning )
240
239
newer_table = GiftiLabelTable ()
241
240
newer_table .labels += ['test' , 'me' , 'again' ]
242
241
img .set_labeltable (newer_table )
243
- assert_equal ( len (w ), 1 )
244
- assert_equal ( len (img .get_labeltable ().labels ), 3 )
245
- assert_equal ( len (w ), 2 )
242
+ assert len (w ) == 1
243
+ assert len (img .get_labeltable ().labels ) == 3
244
+ assert len (w ) == 2
246
245
247
246
248
247
def test_metadata ():
249
248
nvpair = GiftiNVPairs ('key' , 'value' )
250
249
md = GiftiMetaData (nvpair = nvpair )
251
- assert_equal ( md .data [0 ].name , 'key' )
252
- assert_equal ( md .data [0 ].value , 'value' )
250
+ assert md .data [0 ].name == 'key'
251
+ assert md .data [0 ].value == 'value'
253
252
# Test deprecation
254
253
with clear_and_catch_warnings () as w :
255
254
warnings .filterwarnings ('always' , category = DeprecationWarning )
256
- assert_equal ( md .get_metadata (), dict (key = 'value' ) )
257
- assert_equal ( len (w ), 1 )
258
- assert_equal ( len (GiftiDataArray ().get_metadata ()), 0 )
259
- assert_equal ( len (w ), 2 )
255
+ assert md .get_metadata () == dict (key = 'value' )
256
+ assert len (w ) == 1
257
+ assert len (GiftiDataArray ().get_metadata ()) == 0
258
+ assert len (w ) == 2
260
259
261
260
262
261
def test_gifti_label_rgba ():
@@ -267,31 +266,31 @@ def test_gifti_label_rgba():
267
266
assert_array_equal (rgba , gl1 .rgba )
268
267
269
268
gl1 .red = 2 * gl1 .red
270
- assert_false ( np .allclose (rgba , gl1 .rgba ) ) # don't just store the list!
269
+ assert not np .allclose (rgba , gl1 .rgba ) # don't just store the list!
271
270
272
271
gl2 = GiftiLabel ()
273
272
gl2 .rgba = rgba
274
273
assert_array_equal (rgba , gl2 .rgba )
275
274
276
275
gl2 .blue = 2 * gl2 .blue
277
- assert_false ( np .allclose (rgba , gl2 .rgba ) ) # don't just store the list!
276
+ assert not np .allclose (rgba , gl2 .rgba ) # don't just store the list!
278
277
279
278
def assign_rgba (gl , val ):
280
279
gl .rgba = val
281
280
gl3 = GiftiLabel (** kwargs )
282
- assert_raises (ValueError , assign_rgba , gl3 , rgba [:2 ])
283
- assert_raises (ValueError , assign_rgba , gl3 , rgba .tolist () + rgba .tolist ())
281
+ pytest . raises (ValueError , assign_rgba , gl3 , rgba [:2 ])
282
+ pytest . raises (ValueError , assign_rgba , gl3 , rgba .tolist () + rgba .tolist ())
284
283
285
284
# Test deprecation
286
285
with clear_and_catch_warnings () as w :
287
286
warnings .filterwarnings ('once' , category = DeprecationWarning )
288
- assert_equal ( kwargs ['red' ], gl3 .get_rgba ()[0 ])
289
- assert_equal ( len (w ), 1 )
287
+ assert kwargs ['red' ] == gl3 .get_rgba ()[0 ]
288
+ assert len (w ) == 1
290
289
291
290
# Test default value
292
291
gl4 = GiftiLabel ()
293
- assert_equal ( len (gl4 .rgba ), 4 )
294
- assert_true ( np .all ([elem is None for elem in gl4 .rgba ]) )
292
+ assert len (gl4 .rgba ) == 4
293
+ assert np .all ([elem is None for elem in gl4 .rgba ])
295
294
296
295
297
296
def test_print_summary ():
@@ -304,7 +303,7 @@ def test_print_summary():
304
303
def test_gifti_coord ():
305
304
from ..gifti import GiftiCoordSystem
306
305
gcs = GiftiCoordSystem ()
307
- assert_true ( gcs .xform is not None )
306
+ assert gcs .xform is not None
308
307
309
308
# Smoke test
310
309
gcs .xform = None
@@ -316,7 +315,7 @@ def test_data_tag_deprecated():
316
315
with clear_and_catch_warnings () as w :
317
316
warnings .filterwarnings ('once' , category = DeprecationWarning )
318
317
data_tag (np .array ([]), 'ASCII' , '%i' , 1 )
319
- assert_equal ( len (w ), 1 )
318
+ assert len (w ) == 1
320
319
321
320
322
321
def test_gifti_round_trip ():
@@ -443,5 +442,5 @@ def test_darray_dtype_coercion_failures():
443
442
gii = GiftiImage (darrays = [da ])
444
443
gii_copy = GiftiImage .from_bytes (gii .to_bytes ())
445
444
da_copy = gii_copy .darrays [0 ]
446
- assert_equal ( np .dtype (da_copy .data .dtype ), np .dtype (darray_dtype ) )
445
+ assert np .dtype (da_copy .data .dtype ) == np .dtype (darray_dtype )
447
446
assert_array_equal (da_copy .data , da .data )
0 commit comments