Esempio n. 1
0
def test_nifti_extensions():
    nim = load(image_file)
    # basic checks of the available extensions
    ext = nim.extra['extensions']
    ok_(len(ext) == 2)
    ok_(ext.count('comment') == 2)
    ok_(ext.count('afni') == 0)
    ok_(ext.get_codes() == [6, 6])
    ok_((ext.get_sizeondisk() - 4) % 16 == 0)
    # first extension should be short one
    ok_(ext[0].get_content() == 'extcomment1')

    # add one
    afniext = Nifti1Extension('afni', '<xml></xml>')
    ext.append(afniext)
    ok_(ext.get_codes() == [6, 6, 4])
    ok_(ext.count('comment') == 2)
    ok_(ext.count('afni') == 1)
    ok_((ext.get_sizeondisk() - 4) % 16 == 0)

    # delete one
    del ext[1]
    ok_(ext.get_codes() == [6, 4])
    ok_(ext.count('comment') == 1)
    ok_(ext.count('afni') == 1)
Esempio n. 2
0
def load(filename, *args, **kwargs):
    ''' Load file given filename, guessing at file type

    Parameters
    ----------
    filename : string or file-like
       specification of filename or file to load
    *args
    **kwargs
       arguments to pass to image load function

    Returns
    -------
    img : ``SpatialImage``
       Image of guessed type

    '''
    # Try and guess file type from filename
    if isinstance(filename, basestring):
        fname = filename
        for ending in ('.gz', '.bz2'):
            if filename.endswith(ending):
                fname = fname[:-len(ending)]
                break
        if fname.endswith('.nii'):
            return nifti1.load(filename, *args, **kwargs)
        if fname.endswith('.mnc'):
            return minc.load(filename, *args, **kwargs)
    # Not a string, or not recognized as nii or mnc
    try:
        files = nifti1.Nifti1Image.filespec_to_files(filename)
    except ValueError:
        raise RuntimeError('Cannot work out file type of "%s"' %
                           filename)
    hdr = nifti1.Nifti1Header.from_fileobj(
        vu.allopen(files['header']),
        check=False)
    magic = hdr['magic']
    if magic in ('ni1', 'n+1'):
        return nifti1.load(filename, *args, **kwargs)
    return spm2.load(filename, *args, **kwargs)
Esempio n. 3
0
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'])