Esempio n. 1
0
def test_save_load():
    shape = (2, 4, 6)
    npt = np.float32
    data = np.arange(np.prod(shape), dtype=npt).reshape(shape)
    affine = np.diag([1, 2, 3, 1])
    affine[:3,3] = [3,2,1]
    img = ni1.Nifti1Image(data, affine)
    img.set_data_dtype(npt)
    with InTemporaryDirectory() as pth:
        pth = mkdtemp()
        nifn = pjoin(pth, 'an_image.nii')
        sifn = pjoin(pth, 'another_image.img')
        ni1.save(img, nifn)
        re_img = nils.load(nifn)
        yield assert_true(isinstance(re_img, ni1.Nifti1Image))
        yield assert_array_equal(re_img.get_data(), data)
        yield assert_array_equal(re_img.get_affine(), affine)
        # These and subsequent del statements are to prevent confusing
        # windows errors when trying to open files or delete the
        # temporary directory. 
        del re_img
        try:
            import scipy.io
        except ImportError:
            # ignore if there is no matfile reader, and restart
            pass
        else:
            spm2.save(img, sifn)
            re_img2 = nils.load(sifn)
            yield assert_true(isinstance(re_img2, spm2.Spm2AnalyzeImage))
            yield assert_array_equal(re_img2.get_data(), data)
            yield assert_array_equal(re_img2.get_affine(), affine)
            del re_img2
            spm99.save(img, sifn)
            re_img3 = nils.load(sifn)
            yield assert_true(isinstance(re_img3,
                                         spm99.Spm99AnalyzeImage))
            yield assert_array_equal(re_img3.get_data(), data)
            yield assert_array_equal(re_img3.get_affine(), affine)
            ni1.save(re_img3, nifn)
            del re_img3
        re_img = nils.load(nifn)
        yield assert_true(isinstance(re_img, ni1.Nifti1Image))
        yield assert_array_equal(re_img.get_data(), data)
        yield assert_array_equal(re_img.get_affine(), affine)
        del re_img
Esempio n. 2
0
def test_save_load():
    shape = (2, 4, 6)
    npt = np.float32
    data = np.arange(np.prod(shape), dtype=npt).reshape(shape)
    affine = np.diag([1, 2, 3, 1])
    affine[:3, 3] = [3, 2, 1]
    img = ni1.Nifti1Image(data, affine)
    img.set_data_dtype(npt)
    with InTemporaryDirectory() as pth:
        pth = mkdtemp()
        nifn = pjoin(pth, 'an_image.nii')
        sifn = pjoin(pth, 'another_image.img')
        ni1.save(img, nifn)
        re_img = nils.load(nifn)
        yield assert_true(isinstance(re_img, ni1.Nifti1Image))
        yield assert_array_equal(re_img.get_data(), data)
        yield assert_array_equal(re_img.get_affine(), affine)
        # These and subsequent del statements are to prevent confusing
        # windows errors when trying to open files or delete the
        # temporary directory.
        del re_img
        try:
            import scipy.io
        except ImportError:
            # ignore if there is no matfile reader, and restart
            pass
        else:
            spm2.save(img, sifn)
            re_img2 = nils.load(sifn)
            yield assert_true(isinstance(re_img2, spm2.Spm2AnalyzeImage))
            yield assert_array_equal(re_img2.get_data(), data)
            yield assert_array_equal(re_img2.get_affine(), affine)
            del re_img2
            spm99.save(img, sifn)
            re_img3 = nils.load(sifn)
            yield assert_true(isinstance(re_img3, spm99.Spm99AnalyzeImage))
            yield assert_array_equal(re_img3.get_data(), data)
            yield assert_array_equal(re_img3.get_affine(), affine)
            ni1.save(re_img3, nifn)
            del re_img3
        re_img = nils.load(nifn)
        yield assert_true(isinstance(re_img, ni1.Nifti1Image))
        yield assert_array_equal(re_img.get_data(), data)
        yield assert_array_equal(re_img.get_affine(), affine)
        del re_img
Esempio n. 3
0
def test_save_load():
    shape = (2, 4, 6)
    npt = np.float32
    data = np.arange(np.prod(shape), dtype=npt).reshape(shape)
    affine = np.diag([1, 2, 3, 1])
    affine[:3,3] = [3,2,1]
    img = ni1.Nifti1Image(data, affine)
    img.set_data_dtype(npt)
    with InTemporaryDirectory() as pth:
        nifn = 'an_image.nii'
        sifn = 'another_image.img'
        ni1.save(img, nifn)
        re_img = nils.load(nifn)
        yield assert_true(isinstance(re_img, ni1.Nifti1Image))
        yield assert_array_equal(re_img.get_data(), data)
        yield assert_array_equal(re_img.get_affine(), affine)
        # These and subsequent del statements are to prevent confusing
        # windows errors when trying to open files or delete the
        # temporary directory. 
        del re_img
        if have_scipy: # skip we we cannot read .mat files
            spm2.save(img, sifn)
            re_img2 = nils.load(sifn)
            yield assert_true(isinstance(re_img2, spm2.Spm2AnalyzeImage))
            yield assert_array_equal(re_img2.get_data(), data)
            yield assert_array_equal(re_img2.get_affine(), affine)
            del re_img2
            spm99.save(img, sifn)
            re_img3 = nils.load(sifn)
            yield assert_true(isinstance(re_img3,
                                         spm99.Spm99AnalyzeImage))
            yield assert_array_equal(re_img3.get_data(), data)
            yield assert_array_equal(re_img3.get_affine(), affine)
            ni1.save(re_img3, nifn)
            del re_img3
        re_img = nils.load(nifn)
        yield assert_true(isinstance(re_img, ni1.Nifti1Image))
        yield assert_array_equal(re_img.get_data(), data)
        yield assert_array_equal(re_img.get_affine(), affine)
        del re_img
Esempio n. 4
0
def test_save_load():
    shape = (2, 4, 6)
    npt = np.float32
    data = np.arange(np.prod(shape), dtype=npt).reshape(shape)
    affine = np.diag([1, 2, 3, 1])
    affine[:3, 3] = [3, 2, 1]
    img = ni1.Nifti1Image(data, affine)
    img.set_data_dtype(npt)
    with InTemporaryDirectory() as pth:
        nifn = 'an_image.nii'
        sifn = 'another_image.img'
        ni1.save(img, nifn)
        re_img = nils.load(nifn)
        yield assert_true(isinstance(re_img, ni1.Nifti1Image))
        yield assert_array_equal(re_img.get_data(), data)
        yield assert_array_equal(re_img.get_affine(), affine)
        # These and subsequent del statements are to prevent confusing
        # windows errors when trying to open files or delete the
        # temporary directory.
        del re_img
        if have_scipy:  # skip we we cannot read .mat files
            spm2.save(img, sifn)
            re_img2 = nils.load(sifn)
            yield assert_true(isinstance(re_img2, spm2.Spm2AnalyzeImage))
            yield assert_array_equal(re_img2.get_data(), data)
            yield assert_array_equal(re_img2.get_affine(), affine)
            del re_img2
            spm99.save(img, sifn)
            re_img3 = nils.load(sifn)
            yield assert_true(isinstance(re_img3, spm99.Spm99AnalyzeImage))
            yield assert_array_equal(re_img3.get_data(), data)
            yield assert_array_equal(re_img3.get_affine(), affine)
            ni1.save(re_img3, nifn)
            del re_img3
        re_img = nils.load(nifn)
        yield assert_true(isinstance(re_img, ni1.Nifti1Image))
        yield assert_array_equal(re_img.get_data(), data)
        yield assert_array_equal(re_img.get_affine(), affine)
        del re_img