Ejemplo n.º 1
0
def test_nifti_mapper(filename):
    """Basic testing of map2Nifti
    """
    skip_if_no_external('scipy')

    import nibabel
    data = fmri_dataset(samples=os.path.join(pymvpa_dataroot,
                                             'example4d.nii.gz'),
                        targets=[1, 2])

    # test mapping of ndarray
    vol = map2nifti(data, np.ones((294912, ), dtype='int16'))
    if externals.versions['nibabel'] >= '1.2':
        vol_shape = vol.shape
    else:
        vol_shape = vol.get_shape()
    assert_equal(vol_shape, (128, 96, 24))
    assert_true((vol.get_data() == 1).all())
    # test mapping of the dataset
    vol = map2nifti(data)
    if externals.versions['nibabel'] >= '1.2':
        vol_shape = vol.shape
    else:
        vol_shape = vol.get_shape()
    assert_equal(vol_shape, (128, 96, 24, 2))
    ok_(isinstance(vol, data.a.imgtype))

    # test providing custom imgtypes
    vol = map2nifti(data, imgtype=nibabel.Nifti1Pair)
    if externals.versions['nibabel'] >= '1.2':
        vol_shape = vol.shape
    else:
        vol_shape = vol.get_shape()
    ok_(isinstance(vol, nibabel.Nifti1Pair))

    # Lets generate a dataset using an alternative format (MINC)
    # and see if type persists
    volminc = nibabel.MincImage(vol.get_data(), vol.get_affine(),
                                vol.get_header())
    ok_(isinstance(volminc, nibabel.MincImage))
    dsminc = fmri_dataset(volminc, targets=1)
    ok_(dsminc.a.imgtype is nibabel.MincImage)
    ok_(isinstance(dsminc.a.imghdr, nibabel.minc.MincImage.header_class))

    # Lets test if we could save/load now into Analyze volume/dataset
    if externals.versions['nibabel'] < '1.1.0':
        raise SkipTest(
            'nibabel prior 1.1.0 had an issue with types comprehension')
    volanal = map2nifti(
        dsminc, imgtype=nibabel.AnalyzeImage)  # MINC has no 'save' capability
    ok_(isinstance(volanal, nibabel.AnalyzeImage))
    volanal.to_filename(filename)
    dsanal = fmri_dataset(filename, targets=1)
    # this one is tricky since it might become Spm2AnalyzeImage
    ok_('AnalyzeImage' in str(dsanal.a.imgtype))
    ok_('AnalyzeHeader' in str(dsanal.a.imghdr.__class__))
    volanal_ = map2nifti(dsanal)
    ok_(isinstance(volanal_, dsanal.a.imgtype))  # type got preserved
Ejemplo n.º 2
0
def run(args):
    try:
        nii = nibabel.load(args.input)
        img = numpy.squeeze(nii.get_data())
        img_min = numpy.amin(img)
        img_max = numpy.amax(img)
        print 'Image type: {} {}-{}'.format(img.dtype,img_min,img_max)
        hdr = nii.get_header()
        q = hdr.get_best_affine();
        ornt = nibabel.io_orientation(q)
        print 'Orientation: {}'.format(ornt)

        from StringIO import StringIO
        file_map = nibabel.MincImage.make_file_map()
        file_map['image'].fileobj = StringIO()
        
        print sorted(file_map)
        mnc = nibabel.MincImage(img,q)
        mnc.file_map = file_map
        mnc.to_file_map()
    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise