Пример #1
0
def test_get_affine():
    hdr = tv.empty_header()
    # default header gives useless affine
    yield assert_array_equal(tv.aff_from_hdr(hdr), np.diag([0, 0, 0, 1]))
    hdr['voxel_size'] = 1
    yield assert_array_equal(tv.aff_from_hdr(hdr), np.diag([0, 0, 0, 1]))
    # DICOM direction cosines
    hdr['image_orientation_patient'] = [1, 0, 0, 0, 1, 0]
    yield assert_array_equal(tv.aff_from_hdr(hdr), np.diag([-1, -1, 1, 1]))
    # RAS direction cosines
    hdr['image_orientation_patient'] = [-1, 0, 0, 0, -1, 0]
    yield assert_array_equal(tv.aff_from_hdr(hdr), np.eye(4))
    # translations
    hdr['origin'] = [1, 2, 3]
    exp_aff = np.eye(4)
    exp_aff[:3, 3] = [-1, -2, 3]
    yield assert_array_equal(tv.aff_from_hdr(hdr), exp_aff)
    # now use the easier vox_to_ras field
    hdr = tv.empty_header()
    aff = np.eye(4)
    aff[:3, :] = np.arange(12).reshape(3, 4)
    hdr['vox_to_ras'] = aff
    yield assert_array_equal(tv.aff_from_hdr(hdr), aff)
    # mappings work too
    d = {
        'version': 1,
        'voxel_size': np.array([1, 2, 3]),
        'image_orientation_patient': np.array([1, 0, 0, 0, 1, 0]),
        'origin': np.array([10, 11, 12])
    }
    aff = tv.aff_from_hdr(d)
Пример #2
0
def test_get_affine():
    hdr = tv.empty_header()
    # default header gives useless affine
    yield assert_array_equal(tv.aff_from_hdr(hdr),
                             np.diag([0,0,0,1]))
    hdr['voxel_size'] = 1
    yield assert_array_equal(tv.aff_from_hdr(hdr),
                             np.diag([0,0,0,1]))
    # DICOM direction cosines
    hdr['image_orientation_patient'] = [1,0,0,0,1,0]
    yield assert_array_equal(tv.aff_from_hdr(hdr),
                             np.diag([-1,-1,1,1]))
    # RAS direction cosines
    hdr['image_orientation_patient'] = [-1,0,0,0,-1,0]
    yield assert_array_equal(tv.aff_from_hdr(hdr),
                             np.eye(4))
    # translations
    hdr['origin'] = [1,2,3]
    exp_aff = np.eye(4)
    exp_aff[:3,3] = [-1,-2,3]
    yield assert_array_equal(tv.aff_from_hdr(hdr),
                             exp_aff)
    # now use the easier vox_to_ras field
    hdr = tv.empty_header()
    aff = np.eye(4)
    aff[:3,:] = np.arange(12).reshape(3,4)
    hdr['vox_to_ras'] = aff
    yield assert_array_equal(tv.aff_from_hdr(hdr), aff)
    # mappings work too
    d = {'version': 1,
         'voxel_size': np.array([1,2,3]),
         'image_orientation_patient': np.array([1,0,0,0,1,0]),
         'origin': np.array([10,11,12])}
    aff = tv.aff_from_hdr(d)
Пример #3
0
def test_empty_header():
    for endian in '<>':
        hdr = tv.empty_header(endian)
        yield assert_equal, hdr['id_string'], 'TRACK'
        yield assert_equal, hdr['version'], 1
        yield assert_equal, hdr['hdr_size'], 1000
    hdr_endian = tv.endian_codes[tv.empty_header().dtype.byteorder]
    yield assert_equal, hdr_endian, tv.native_code
Пример #4
0
def test_empty_header():
    for endian in '<>':
        hdr = tv.empty_header(endian)
        yield assert_equal(hdr['id_string'], 'TRACK')
        yield assert_equal(hdr['version'], 1)
        yield assert_equal(hdr['hdr_size'], 1000)
        yield assert_array_equal(
            hdr['image_orientation_patient'],
            [0,0,0,0,0,0])
    hdr_endian = tv.endian_codes[tv.empty_header().dtype.byteorder]
    yield assert_equal(hdr_endian, tv.native_code)
Пример #5
0
def test_empty_header():
    for endian in '<>':
        for version in (1, 2):
            hdr = tv.empty_header(endian, version)
            yield assert_equal(hdr['id_string'], 'TRACK')
            yield assert_equal(hdr['version'], version)
            yield assert_equal(hdr['hdr_size'], 1000)
            yield assert_array_equal(hdr['image_orientation_patient'],
                                     [0, 0, 0, 0, 0, 0])
    hdr = tv.empty_header(version=2)
    yield assert_array_equal(hdr['vox_to_ras'], np.zeros((4, 4)))
    hdr_endian = tv.endian_codes[tv.empty_header().dtype.byteorder]
    yield assert_equal(hdr_endian, tv.native_code)
Пример #6
0
def test_empty_header():
    for endian in '<>':
        for version in (1, 2):
            hdr = tv.empty_header(endian, version)
            yield assert_equal(hdr['id_string'], 'TRACK')
            yield assert_equal(hdr['version'], version)
            yield assert_equal(hdr['hdr_size'], 1000)
            yield assert_array_equal(
                hdr['image_orientation_patient'],
                [0,0,0,0,0,0])
    hdr = tv.empty_header(version=2)
    yield assert_array_equal(hdr['vox_to_ras'], np.zeros((4,4)))
    hdr_endian = tv.endian_codes[tv.empty_header().dtype.byteorder]
    yield assert_equal(hdr_endian, tv.native_code)
Пример #7
0
def test_tv_class():
    tvf = tv.TrackvisFile([])
    yield assert_equal(tvf.streamlines, [])
    yield assert_true(isinstance(tvf.header, np.ndarray))
    yield assert_equal(tvf.endianness, tv.native_code)
    yield assert_equal(tvf.filename, None)
    out_f = StringIO()
    tvf.to_file(out_f)
    yield assert_equal(out_f.getvalue(), tv.empty_header().tostring())
    out_f.truncate(0)
    # Write something not-default
    tvf = tv.TrackvisFile([], {'id_string': 'TRACKb'})
    tvf.to_file(out_f)
    # read it back
    out_f.seek(0)
    tvf_back = tv.TrackvisFile.from_file(out_f)
    yield assert_equal(tvf_back.header['id_string'], 'TRACKb')
    # check that we check input values
    out_f.truncate(0)
    yield assert_raises(tv.HeaderError, tv.TrackvisFile, [],
                        {'id_string': 'not OK'})
    yield assert_raises(tv.HeaderError, tv.TrackvisFile, [], {'version': 3})
    yield assert_raises(tv.HeaderError, tv.TrackvisFile, [], {'hdr_size': 0})
    affine = np.diag([1, 2, 3, 1])
    affine[:3, 3] = [10, 11, 12]
    tvf.set_affine(affine)
    yield assert_true(np.all(tvf.get_affine() == affine))
Пример #8
0
def test_write():
    streams = []
    out_f = StringIO()
    tv.write(out_f, [], {})
    yield assert_equal, out_f.getvalue(), tv.empty_header().tostring()
    out_f.truncate(0)
    # Write something not-default
    tv.write(out_f, [], {'id_string':'TRACKb'})
    # read it back
    out_f.seek(0)
    streams, hdr = tv.read(out_f)
    yield assert_equal, hdr['id_string'], 'TRACKb'
    # check that we can pass none for the header
    out_f.truncate(0)
    tv.write(out_f, [])
    out_f.truncate(0)
    tv.write(out_f, [], None)
    # check that we check input values
    out_f.truncate(0)
    yield (assert_raises, tv.HeaderError,
           tv.write, out_f, [],{'id_string':'not OK'})
    yield (assert_raises, tv.HeaderError,
           tv.write, out_f, [],{'version':2})
    yield (assert_raises, tv.HeaderError,
           tv.write, out_f, [],{'hdr_size':0})
Пример #9
0
def test_tv_class():
    tvf = tv.TrackvisFile([])
    yield assert_equal(tvf.streamlines, [])
    yield assert_true(isinstance(tvf.header, np.ndarray))
    yield assert_equal(tvf.endianness, tv.native_code)
    yield assert_equal(tvf.filename, None)
    out_f = StringIO()
    tvf.to_file(out_f)
    yield assert_equal(out_f.getvalue(), tv.empty_header().tostring())
    out_f.truncate(0)
    # Write something not-default
    tvf = tv.TrackvisFile([], {'id_string':'TRACKb'})
    tvf.to_file(out_f)
    # read it back
    out_f.seek(0)
    tvf_back = tv.TrackvisFile.from_file(out_f)
    yield assert_equal(tvf_back.header['id_string'], 'TRACKb')
    # check that we check input values
    out_f.truncate(0)
    yield assert_raises(tv.HeaderError,
                        tv.TrackvisFile,
                        [],{'id_string':'not OK'})
    yield assert_raises(tv.HeaderError,
                        tv.TrackvisFile,
                        [],{'version': 2})
    yield assert_raises(tv.HeaderError,
                        tv.TrackvisFile,
                        [],{'hdr_size':0})
    affine = np.diag([1,2,3,1])
    affine[:3,3] = [10,11,12]
    tvf.set_affine(affine)
    yield assert_true(np.all(tvf.get_affine() == affine))
Пример #10
0
def test_write():
    streams = []
    out_f = StringIO()
    tv.write(out_f, [], {})
    yield assert_equal(out_f.getvalue(), tv.empty_header().tostring())
    out_f.truncate(0)
    # Write something not-default
    tv.write(out_f, [], {'id_string': 'TRACKb'})
    # read it back
    out_f.seek(0)
    streams, hdr = tv.read(out_f)
    yield assert_equal(hdr['id_string'], 'TRACKb')
    # check that we can pass none for the header
    out_f.truncate(0)
    tv.write(out_f, [])
    out_f.truncate(0)
    tv.write(out_f, [], None)
    # check that we check input values
    out_f.truncate(0)
    yield assert_raises(tv.HeaderError, tv.write, out_f, [],
                        {'id_string': 'not OK'})
    yield assert_raises(tv.HeaderError, tv.write, out_f, [], {'version': 3})
    yield assert_raises(tv.HeaderError, tv.write, out_f, [], {'hdr_size': 0})