From b8bead9f3a26b981b735c831e0852c3c362cdb65 Mon Sep 17 00:00:00 2001 From: Howard Chiu <137316255+chiuhoward@users.noreply.github.com> Date: Fri, 8 Nov 2024 22:56:11 -0800 Subject: [PATCH 1/4] Update segmentation.py 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? --- AFQ/tasks/segmentation.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/AFQ/tasks/segmentation.py b/AFQ/tasks/segmentation.py index 65be67ee..e8f4697a 100644 --- a/AFQ/tasks/segmentation.py +++ b/AFQ/tasks/segmentation.py @@ -30,6 +30,8 @@ from dipy.stats.analysis import afq_profile from dipy.tracking.streamline import set_number_of_points, values_from_volume +import gzip +import shutil logger = logging.getLogger('AFQ') @@ -61,6 +63,18 @@ def segment(data_imap, mapping_imap, trx = load_trx(streamlines, data_imap["dwi"]) trx.streamlines._data = trx.streamlines._data.astype(np.float32) tg = trx.to_sft() + elif streamlines.endswith(".tck.gz"): + # uncompress tck.gz to a temporary tck: + temp_tck = streamlines.replace(".gz", "") + 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 + indices_to_remove, _ = tg.remove_invalid_streamlines() if len(indices_to_remove) > 0: From 4b04c5dcfaf840bd1186de9ace88f81aceddfbc3 Mon Sep 17 00:00:00 2001 From: Howard Chiu <137316255+chiuhoward@users.noreply.github.com> Date: Tue, 12 Nov 2024 00:57:00 -0800 Subject: [PATCH 2/4] Update segmentation.py Added mkdtemp --- AFQ/tasks/segmentation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/AFQ/tasks/segmentation.py b/AFQ/tasks/segmentation.py index e8f4697a..db6a0783 100644 --- a/AFQ/tasks/segmentation.py +++ b/AFQ/tasks/segmentation.py @@ -32,6 +32,8 @@ import gzip import shutil +import os.path as op +from tempfile import mkdtemp logger = logging.getLogger('AFQ') @@ -65,7 +67,7 @@ def segment(data_imap, mapping_imap, tg = trx.to_sft() elif streamlines.endswith(".tck.gz"): # uncompress tck.gz to a temporary tck: - temp_tck = streamlines.replace(".gz", "") + temp_tck = op.join(mkdtemp(), streamlines.replace(".gz", "")) with gzip.open(streamlines, 'rb') as f_in: with open(temp_tck, 'wb') as f_out: shutil.copyfileobj(f_in, f_out) From 39c9396faf75c89f9854ae24911cbd071a4e8087 Mon Sep 17 00:00:00 2001 From: Howard Chiu <137316255+chiuhoward@users.noreply.github.com> Date: Tue, 12 Nov 2024 13:47:56 -0800 Subject: [PATCH 3/4] Update segmentation.py Print temp_tck for debugging --- AFQ/tasks/segmentation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/AFQ/tasks/segmentation.py b/AFQ/tasks/segmentation.py index db6a0783..d49342da 100644 --- a/AFQ/tasks/segmentation.py +++ b/AFQ/tasks/segmentation.py @@ -68,6 +68,7 @@ def segment(data_imap, mapping_imap, elif streamlines.endswith(".tck.gz"): # uncompress tck.gz to a temporary tck: temp_tck = op.join(mkdtemp(), streamlines.replace(".gz", "")) + print(temp_tck) with gzip.open(streamlines, 'rb') as f_in: with open(temp_tck, 'wb') as f_out: shutil.copyfileobj(f_in, f_out) From ef436f1d3005deeda8596e86b73ca3c3e7b4d2b2 Mon Sep 17 00:00:00 2001 From: Howard Chiu <137316255+chiuhoward@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:45:58 -0800 Subject: [PATCH 4/4] Update segmentation.py split temp_tck path to generate relative path instead of absolute path --- AFQ/tasks/segmentation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AFQ/tasks/segmentation.py b/AFQ/tasks/segmentation.py index d49342da..3130a02c 100644 --- a/AFQ/tasks/segmentation.py +++ b/AFQ/tasks/segmentation.py @@ -67,8 +67,8 @@ def segment(data_imap, mapping_imap, tg = trx.to_sft() elif streamlines.endswith(".tck.gz"): # uncompress tck.gz to a temporary tck: - temp_tck = op.join(mkdtemp(), streamlines.replace(".gz", "")) - print(temp_tck) + temp_tck = op.join(mkdtemp(), op.split(streamlines.replace(".gz", ""))[1]) + logger.info(f"Temporary tck file created at: {temp_tck}") with gzip.open(streamlines, 'rb') as f_in: with open(temp_tck, 'wb') as f_out: shutil.copyfileobj(f_in, f_out)