Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Allow tck.gz files to be used as input #15

Merged
merged 4 commits into from
Nov 13, 2024

Conversation

chiuhoward
Copy link
Contributor

@chiuhoward chiuhoward commented Nov 9, 2024

Enhancement for #14 to accommodate .tck.gz files by first unzipping them.

This is important because qsirecon pipeline produces tck.gz outputs.

Before this change, these files would not be recognized, leading to a crash later on.

Enhancement for #14 to accommodate .tck.gz files by first unzipping them.

mapping.py also uses tractography_imap but does not check for extensions, would it matter there?
@arokem
Copy link
Contributor

arokem commented Nov 11, 2024

Looks good to me! Does it work for your use-case?

@chiuhoward
Copy link
Contributor Author

I haven’t actually tested oops, let me test this in a conda environment and get back!

AFQ/tasks/segmentation.py Outdated Show resolved Hide resolved
AFQ/tasks/segmentation.py Outdated Show resolved Hide resolved
Added mkdtemp
@chiuhoward
Copy link
Contributor Author

@arokem sorry I tested this last night - it still writes a .tck file to the qsirecon folder and doesn’t delete it after pyAFQ completes...

Do I need something like a os.remove(temp_tck) line on line 79 after is_trx = false?

@arokem
Copy link
Contributor

arokem commented Nov 12, 2024

That's weird. Can you print the value of temp_tck? I would expect this to be a file-system location entirely different from your qsirecon folder, so it's odd that anything ends up there.

Print temp_tck for debugging
@chiuhoward
Copy link
Contributor Author

I also tried this but the .tck file remains in the qsirecon folder even after the job completes

    elif streamlines.endswith(".tck.gz"):
        # uncompress tck.gz to a temporary tck:
        temp_dir = mkdtemp()
        temp_tck = op.join(temp_dir, streamlines.replace(".gz", ""))
        logger.info(f"Temporary tck file created at: {temp_tck}")
        try:
            with gzip.open(streamlines, 'rb') as f_in:
                with open(temp_tck, 'wb') as f_out:
                    shutil.copyfileobj(f_in, f_out)
            # initialize stateful tractogram from tck file:
            tg = load_tractogram(
                temp_tck, data_imap["dwi"], Space.VOX, 
                bbox_valid_check=False)
            is_trx = False
        finally:
            shutil.rmtree(temp_dir)

The logger line prints:

2024-11-12 19:41:44,218 [    INFO] Saving /scratch/groups/jyeatman/howard/DBPFullwT1derivOct24-pyAFQ/sub-16004/sub-16004_acq-b700_dir-PA_space-subject_desc-template_dwi.nii.gz
2024-11-12 19:41:44,232 [    INFO] Temporary tck file created at: /scratch/groups/jyeatman/howard/DBPFullwT1derivOct24-recon/derivatives/qsirecon-MRtrix3_fork-SS3T_act-None/sub-16004/dwi/sub-16004_acq-b700_dir-PA_space-T1w_model-ifod2_streamlines.tck
2024-11-12 19:42:25,474 [    INFO] Preprocessing Streamlines

@arokem
Copy link
Contributor

arokem commented Nov 13, 2024

That's very odd. I'd expect mkdtemp to give you a completely different file-system location:

>>> from tempfile import mkdtemp
>>> mkdtemp()
'/var/folders/px/t5ym05pn5k16nz97mzcpqqkr0000gn/T/tmp8p0d9rda'

Is streamlines a full path at that point or a relative path?

@chiuhoward
Copy link
Contributor Author

streamlines is an absolute path here, was this because I passed in import_tract as an absolute path?

2024-11-13 10:58:39,780 [    INFO] Streamlines path: /scratch/groups/jyeatman/howard/DBPFullwT1derivOct24-recon/derivatives/qsirecon-MRtrix3_fork-SS3T_act-None/sub-16004/dwi/sub-16004_acq-b700_dir-PA_space-T1w_model-ifod2_streamlines.tck.gz
2024-11-13 10:58:39,780 [    INFO] Temporary tck file created at: /scratch/groups/jyeatman/howard/DBPFullwT1derivOct24-recon/derivatives/qsirecon-MRtrix3_fork-SS3T_act-None/sub-16004/dwi/sub-16004_acq-b700_dir-PA_space-T1w_model-ifod2_streamlines.tck

AFQ/tasks/segmentation.py Outdated Show resolved Hide resolved
split temp_tck path to generate relative path instead of absolute path
@chiuhoward
Copy link
Contributor Author

@arokem that worked! the temporary folder was used and the qsirecon no longer has a .tck file after pyAFQ completes!

2024-11-13 13:53:56,163 [    INFO] Temporary tck file created at: /tmp/tmpj0pshudg/sub-16004_acq-b700_dir-PA_space-T1w_model-ifod2_streamlines.tck

@arokem
Copy link
Contributor

arokem commented Nov 13, 2024

Great! Sorry that it took so many iterations to get to a working solution...

@36000 : WDYT? Should we leave that line of logging in or is that just noise?

@36000
Copy link
Collaborator

36000 commented Nov 13, 2024

I think its fine! Should we merge this?

@chiuhoward chiuhoward changed the title Update segmentation.py [ENH] Update segmentation.py Nov 13, 2024
@arokem arokem merged commit bb622a6 into tractometry:main Nov 13, 2024
7 checks passed
@arokem arokem changed the title [ENH] Update segmentation.py [ENH] Allow tck.gz files to be used as input Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants