|
1 | 1 | ''' Testing trackvis module '''
|
2 | 2 | from functools import partial
|
3 |
| -import warnings |
4 | 3 |
|
5 | 4 | import numpy as np
|
6 | 5 |
|
@@ -503,3 +502,73 @@ def test_tv_class():
|
503 | 502 | assert_raises(tv.TrackvisFileError,
|
504 | 503 | tv.TrackvisFile,
|
505 | 504 | iter([]))
|
| 505 | + |
| 506 | + |
| 507 | +def test_tvfile_io(): |
| 508 | + # Test reading and writing tracks with file class |
| 509 | + out_f = BytesIO() |
| 510 | + ijk0 = np.arange(15).reshape((5,3)) / 2.0 |
| 511 | + ijk1 = ijk0 + 20 |
| 512 | + vx_streams = [(ijk0, None, None), (ijk1, None, None)] |
| 513 | + vxmm_streams = [(ijk0 * [[2,3,4]], None, None), |
| 514 | + (ijk1 * [[2,3,4]], None, None)] |
| 515 | + # Roundtrip basic |
| 516 | + tvf = tv.TrackvisFile(vxmm_streams) |
| 517 | + tvf.to_file(out_f) |
| 518 | + out_f.seek(0) |
| 519 | + tvf2 = tv.TrackvisFile.from_file(out_f) |
| 520 | + assert_equal(tvf2.filename, None) |
| 521 | + assert_true(streamlist_equal(vxmm_streams, tvf2.streamlines)) |
| 522 | + assert_equal(tvf2.points_space, None) |
| 523 | + # Voxel points_space |
| 524 | + tvf = tv.TrackvisFile(vx_streams, points_space='voxel') |
| 525 | + out_f.seek(0) |
| 526 | + # No voxel size - error |
| 527 | + assert_raises(tv.HeaderError, tvf.to_file, out_f) |
| 528 | + out_f.seek(0) |
| 529 | + # With voxel size, no error, roundtrip works |
| 530 | + tvf.header['voxel_size'] = [2,3,4] |
| 531 | + tvf.to_file(out_f) |
| 532 | + out_f.seek(0) |
| 533 | + tvf2 = tv.TrackvisFile.from_file(out_f, points_space='voxel') |
| 534 | + assert_true(streamlist_equal(vx_streams, tvf2.streamlines)) |
| 535 | + assert_equal(tvf2.points_space, 'voxel') |
| 536 | + out_f.seek(0) |
| 537 | + # Also with affine specified |
| 538 | + tvf = tv.TrackvisFile(vx_streams, points_space='voxel', |
| 539 | + affine=np.diag([2,3,4,1])) |
| 540 | + tvf.to_file(out_f) |
| 541 | + out_f.seek(0) |
| 542 | + tvf2 = tv.TrackvisFile.from_file(out_f, points_space='voxel') |
| 543 | + assert_true(streamlist_equal(vx_streams, tvf2.streamlines)) |
| 544 | + # Fancy affine test |
| 545 | + fancy_affine = np.array([[0., -2, 0, 10], |
| 546 | + [3, 0, 0, 20], |
| 547 | + [0, 0, 4, 30], |
| 548 | + [0, 0, 0, 1]]) |
| 549 | + def f(pts): # from vx to mm |
| 550 | + pts = pts[:,[1,0,2]] * [[-2,3,4]] # apply zooms / reorder |
| 551 | + return pts + [[10,20,30]] # apply translations |
| 552 | + xyz0, xyz1 = f(ijk0), f(ijk1) |
| 553 | + fancy_rasmm_streams = [(xyz0, None, None), (xyz1, None, None)] |
| 554 | + # Roundtrip |
| 555 | + tvf = tv.TrackvisFile(fancy_rasmm_streams, points_space='rasmm') |
| 556 | + out_f.seek(0) |
| 557 | + # No affine |
| 558 | + assert_raises(tv.HeaderError, tvf.to_file, out_f) |
| 559 | + out_f.seek(0) |
| 560 | + # With affine set, no error, roundtrip works |
| 561 | + tvf.set_affine(fancy_affine, pos_vox=True, set_order=True) |
| 562 | + tvf.to_file(out_f) |
| 563 | + out_f.seek(0) |
| 564 | + tvf2 = tv.TrackvisFile.from_file(out_f, points_space='rasmm') |
| 565 | + assert_true(streamlist_equal(fancy_rasmm_streams, tvf2.streamlines)) |
| 566 | + assert_equal(tvf2.points_space, 'rasmm') |
| 567 | + out_f.seek(0) |
| 568 | + # Also when affine given in init |
| 569 | + tvf = tv.TrackvisFile(fancy_rasmm_streams, points_space='rasmm', |
| 570 | + affine=fancy_affine) |
| 571 | + tvf.to_file(out_f) |
| 572 | + out_f.seek(0) |
| 573 | + tvf2 = tv.TrackvisFile.from_file(out_f, points_space='rasmm') |
| 574 | + assert_true(streamlist_equal(fancy_rasmm_streams, tvf2.streamlines)) |
0 commit comments