|
18 | 18 | from ..spatialimages import HeaderDataError
|
19 | 19 | from .. import nifti1 as nifti1
|
20 | 20 | from ..nifti1 import (load, Nifti1Header, Nifti1Image,
|
21 |
| - Nifti1Pair, Nifti1Extension, data_type_codes, |
22 |
| - extension_codes, slice_order_codes) |
| 21 | + Nifti1Pair, Nifti1Extension, Nifti1Extensions, |
| 22 | + data_type_codes, extension_codes, slice_order_codes) |
23 | 23 |
|
24 | 24 | from numpy.testing import assert_array_equal, assert_array_almost_equal
|
25 | 25 | from nose.tools import (assert_true, assert_false, assert_equal,
|
@@ -387,73 +387,85 @@ def test_extension_codes():
|
387 | 387 | ext = Nifti1Extension(k, 'somevalue')
|
388 | 388 |
|
389 | 389 |
|
390 |
| -@parametric |
| 390 | +def test_extension_list(): |
| 391 | + ext_c0 = Nifti1Extensions() |
| 392 | + ext_c1 = Nifti1Extensions() |
| 393 | + assert_equal(ext_c0, ext_c1) |
| 394 | + ext = Nifti1Extension('comment', '123') |
| 395 | + ext_c1.append(ext) |
| 396 | + assert_false(ext_c0 == ext_c1) |
| 397 | + ext_c0.append(ext) |
| 398 | + assert_true(ext_c0 == ext_c1) |
| 399 | + |
| 400 | + |
391 | 401 | def test_nifti_extensions():
|
392 | 402 | nim = load(image_file)
|
393 | 403 | # basic checks of the available extensions
|
394 |
| - ext = nim.extra['extensions'] |
395 |
| - yield assert_true(len(ext) == 2) |
396 |
| - yield assert_true(ext.count('comment') == 2) |
397 |
| - yield assert_true(ext.count('afni') == 0) |
398 |
| - yield assert_true(ext.get_codes() == [6, 6]) |
399 |
| - yield assert_true((ext.get_sizeondisk() - 4) % 16 == 0) |
| 404 | + hdr = nim.get_header() |
| 405 | + exts_container = hdr.extensions |
| 406 | + assert_true(len(exts_container) == 2) |
| 407 | + assert_true(exts_container.count('comment') == 2) |
| 408 | + assert_true(exts_container.count('afni') == 0) |
| 409 | + assert_true(exts_container.get_codes() == [6, 6]) |
| 410 | + assert_true((exts_container.get_sizeondisk() - 4) % 16 == 0) |
400 | 411 | # first extension should be short one
|
401 |
| - yield assert_true(ext[0].get_content() == 'extcomment1') |
| 412 | + assert_true(exts_container[0].get_content() == 'extcomment1') |
402 | 413 | # add one
|
403 | 414 | afniext = Nifti1Extension('afni', '<xml></xml>')
|
404 |
| - ext.append(afniext) |
405 |
| - yield assert_true(ext.get_codes() == [6, 6, 4]) |
406 |
| - yield assert_true(ext.count('comment') == 2) |
407 |
| - yield assert_true(ext.count('afni') == 1) |
408 |
| - yield assert_true((ext.get_sizeondisk() - 4) % 16 == 0) |
| 415 | + exts_container.append(afniext) |
| 416 | + assert_true(exts_container.get_codes() == [6, 6, 4]) |
| 417 | + assert_true(exts_container.count('comment') == 2) |
| 418 | + assert_true(exts_container.count('afni') == 1) |
| 419 | + assert_true((exts_container.get_sizeondisk() - 4) % 16 == 0) |
409 | 420 | # delete one
|
410 |
| - del ext[1] |
411 |
| - yield assert_true(ext.get_codes() == [6, 4]) |
412 |
| - yield assert_true(ext.count('comment') == 1) |
413 |
| - yield assert_true(ext.count('afni') == 1) |
| 421 | + del exts_container[1] |
| 422 | + assert_true(exts_container.get_codes() == [6, 4]) |
| 423 | + assert_true(exts_container.count('comment') == 1) |
| 424 | + assert_true(exts_container.count('afni') == 1) |
414 | 425 |
|
415 | 426 |
|
416 |
| -@parametric |
417 | 427 | def test_loadsave_cycle():
|
418 | 428 | nim = load(image_file)
|
419 | 429 | # ensure we have extensions
|
420 |
| - yield assert_true(nim.extra.has_key('extensions')) |
421 |
| - yield assert_true(len(nim.extra['extensions'])) |
| 430 | + hdr = nim.get_header() |
| 431 | + exts_container = hdr.extensions |
| 432 | + assert_true(len(exts_container) > 0) |
422 | 433 | # write into the air ;-)
|
423 | 434 | stio = StringIO()
|
424 | 435 | nim.file_map['image'].fileobj = stio
|
425 | 436 | nim.to_file_map()
|
426 | 437 | stio.seek(0)
|
427 | 438 | # reload
|
428 | 439 | lnim = Nifti1Image.from_file_map(nim.file_map)
|
429 |
| - yield assert_true(lnim.extra.has_key('extensions')) |
430 |
| - yield assert_equal(nim.extra['extensions'], |
431 |
| - lnim.extra['extensions']) |
| 440 | + hdr = lnim.get_header() |
| 441 | + lexts_container = hdr.extensions |
| 442 | + assert_equal(exts_container, |
| 443 | + lexts_container) |
432 | 444 | # build int16 image
|
433 | 445 | data = np.ones((2,3,4,5), dtype='int16')
|
434 | 446 | img = Nifti1Image(data, np.eye(4))
|
435 | 447 | hdr = img.get_header()
|
436 |
| - yield assert_equal(hdr.get_data_dtype(), np.int16) |
| 448 | + assert_equal(hdr.get_data_dtype(), np.int16) |
437 | 449 | # default should have no scaling
|
438 |
| - yield assert_equal(hdr.get_slope_inter(), (1.0, 0.0)) |
| 450 | + assert_equal(hdr.get_slope_inter(), (1.0, 0.0)) |
439 | 451 | # set scaling
|
440 | 452 | hdr.set_slope_inter(2, 8)
|
441 |
| - yield assert_equal(hdr.get_slope_inter(), (2, 8)) |
| 453 | + assert_equal(hdr.get_slope_inter(), (2, 8)) |
442 | 454 | # now build new image with updated header
|
443 | 455 | wnim = Nifti1Image(data, np.eye(4), header=hdr)
|
444 |
| - yield assert_equal(wnim.get_data_dtype(), np.int16) |
445 |
| - yield assert_equal(wnim.get_header().get_slope_inter(), (2, 8)) |
| 456 | + assert_equal(wnim.get_data_dtype(), np.int16) |
| 457 | + assert_equal(wnim.get_header().get_slope_inter(), (2, 8)) |
446 | 458 | # write into the air again ;-)
|
447 | 459 | stio = StringIO()
|
448 | 460 | wnim.file_map['image'].fileobj = stio
|
449 | 461 | wnim.to_file_map()
|
450 | 462 | stio.seek(0)
|
451 | 463 | lnim = Nifti1Image.from_file_map(wnim.file_map)
|
452 |
| - yield assert_equal(lnim.get_data_dtype(), np.int16) |
| 464 | + assert_equal(lnim.get_data_dtype(), np.int16) |
453 | 465 | # the test below does not pass, because the slope and inter are
|
454 | 466 | # always reset from the data, by the image write
|
455 | 467 | raise SkipTest
|
456 |
| - yield assert_equal(lnim.get_header().get_slope_inter(), (2, 8)) |
| 468 | + assert_equal(lnim.get_header().get_slope_inter(), (2, 8)) |
457 | 469 |
|
458 | 470 |
|
459 | 471 | @parametric
|
|
0 commit comments