Пример #1
0
 def test_header_read_restore(self):
     # Test that reading a header restores the file position
     trk_fname = DATA['simple_trk_fname']
     bio = BytesIO()
     bio.write(b'Along my very merry way')
     hdr_pos = bio.tell()
     hdr_from_fname = TrkFile._read_header(trk_fname)
     with open(trk_fname, 'rb') as fobj:
         bio.write(fobj.read())
     bio.seek(hdr_pos)
     # Check header is as expected
     hdr_from_fname['_offset_data'] += hdr_pos  # Correct for start position
     assert_arr_dict_equal(TrkFile._read_header(bio), hdr_from_fname)
     # Check fileobject file position has not changed
     assert_equal(bio.tell(), hdr_pos)
Пример #2
0
    def test_load_write_LPS_file(self):
        # Load the RAS and LPS version of the standard.
        trk_RAS = TrkFile.load(DATA['standard_trk_fname'], lazy_load=False)
        trk_LPS = TrkFile.load(DATA['standard_LPS_trk_fname'], lazy_load=False)
        assert_tractogram_equal(trk_LPS.tractogram, trk_RAS.tractogram)

        # Write back the standard.
        trk_file = BytesIO()
        trk = TrkFile(trk_LPS.tractogram, trk_LPS.header)
        trk.save(trk_file)
        trk_file.seek(0, os.SEEK_SET)

        new_trk = TrkFile.load(trk_file)

        assert_arr_dict_equal(new_trk.header, trk.header)
        assert_tractogram_equal(new_trk.tractogram, trk.tractogram)

        new_trk_orig = TrkFile.load(DATA['standard_LPS_trk_fname'])
        assert_tractogram_equal(new_trk.tractogram, new_trk_orig.tractogram)

        trk_file.seek(0, os.SEEK_SET)
        assert_equal(trk_file.read(),
                     open(DATA['standard_LPS_trk_fname'], 'rb').read())

        # Test writing a file where the header is missing the
        # Field.VOXEL_ORDER.
        trk_file = BytesIO()

        # For TRK file format, the default voxel order is LPS.
        header = copy.deepcopy(trk_LPS.header)
        header[Field.VOXEL_ORDER] = b""

        trk = TrkFile(trk_LPS.tractogram, header)
        trk.save(trk_file)
        trk_file.seek(0, os.SEEK_SET)

        new_trk = TrkFile.load(trk_file)

        assert_arr_dict_equal(new_trk.header, trk_LPS.header)
        assert_tractogram_equal(new_trk.tractogram, trk.tractogram)

        new_trk_orig = TrkFile.load(DATA['standard_LPS_trk_fname'])
        assert_tractogram_equal(new_trk.tractogram, new_trk_orig.tractogram)

        trk_file.seek(0, os.SEEK_SET)
        assert_equal(trk_file.read(),
                     open(DATA['standard_LPS_trk_fname'], 'rb').read())