Skip to content

First point of LazyTractogram is not saved #586

@nilgoyette

Description

@nilgoyette

This test should pass

def test_save_from_generator(self):
    tractogram = Tractogram(DATA['streamlines'],
                            affine_to_rasmm=np.eye(4))

    # Just to create a generator
    filtered = (s for s in tractogram.streamlines if True)
    lazy_tractogram = LazyTractogram(lambda: filtered,
                                     affine_to_rasmm=np.eye(4))

    for ext, _ in FORMATS.items():
        with InTemporaryDirectory():
            filename = 'streamlines' + ext
            nib.streamlines.save(lazy_tractogram, filename)
            tfile = nib.streamlines.load(filename, lazy_load=False)
            assert_tractogram_equal(tfile.tractogram, tractogram)

The problem comes from

first_item = next(iter(self.tractogram))
...
for t in tractogram:

When self.tractogram is a generator, first_item is not in the generator anymore.

I took 10 minutes to fix it but I couldn't find a clean way to do it. A simple itertools.chain([first_item], tractogram) is not enough :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions