def test_loadsavecycle(): nim = load(image_file) # ensure we have extensions ok_(nim.extra.has_key('extensions')) ok_(len(nim.extra['extensions'])) # write into the air ;-) stio = StringIO() files = {'header': stio, 'image': stio} nim.to_files(files) stio.seek(0) # reload lnim = Nifti1Image.from_files(files) ok_(lnim.extra.has_key('extensions')) ok_(nim.extra['extensions'] == lnim.extra['extensions'])
def test_nifti1_images(): shape = (2, 4, 6) npt = np.float32 data = np.arange(np.prod(shape), dtype=npt).reshape(shape) affine = np.diag([1, 2, 3, 1]) img = Nifti1Image(data, affine) yield assert_equal, img.get_shape(), shape img.set_data_dtype(npt) stio = StringIO() files = {'header': stio, 'image': stio} img.to_files(files) stio.seek(0) img2 = Nifti1Image.from_files(files) yield assert_array_equal, img2.get_data(), data for ext in ('.gz', '.bz2'): try: _, fname = tempfile.mkstemp('.nii' + ext) img.to_filename(fname) img3 = Nifti1Image.load(fname) yield assert_true, isinstance(img3, img.__class__) yield assert_array_equal, img3.get_data(), data yield assert_equal, img3.get_header(), img.get_header() finally: os.unlink(fname)