From 298a51b3c0c2bb92f1ce705742dd6b4dacac4c35 Mon Sep 17 00:00:00 2001 From: 36000 Date: Tue, 5 Jul 2022 14:40:52 -0700 Subject: [PATCH 1/4] [ENH] add median bundle len function --- AFQ/api/utils.py | 1 + AFQ/tasks/segmentation.py | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/AFQ/api/utils.py b/AFQ/api/utils.py index 4771dfebf..5fd28b749 100644 --- a/AFQ/api/utils.py +++ b/AFQ/api/utils.py @@ -125,6 +125,7 @@ def export_all_helper(api_afq_object, seg_algo, xforms, indiv, viz): if seg_algo == "AFQ": api_afq_object.export("rois") api_afq_object.export("sl_counts") + api_afq_object.export("sl_lengths") api_afq_object.export("profiles") if viz: diff --git a/AFQ/tasks/segmentation.py b/AFQ/tasks/segmentation.py index e49103446..741780fbe 100644 --- a/AFQ/tasks/segmentation.py +++ b/AFQ/tasks/segmentation.py @@ -214,6 +214,46 @@ def export_sl_counts(data_imap, return counts_df, dict(sources=bundles_files) +@pimms.calc("sl_lengths") +@as_file('_sl_lengths.csv', include_track=True, include_seg=True) +def export_sl_counts(data_imap, + clean_bundles, bundles): + """ + full path to a JSON file containing streamline counts + """ + bundle_dict = data_imap["bundle_dict"] + med_len_clean_counts = [] + med_len_counts = [] + bundle_names = list(bundle_dict.keys()) + if "whole_brain" not in bundle_names: + bundle_names.append("whole_brain") + bundles_files = [clean_bundles, bundles] + lists = [med_len_clean_counts, med_len_counts] + + for bundles_file, lens in zip(bundles_files, lists): + seg_sft = aus.SegmentedSFT.fromfile(bundles_file) + + for bundle in bundle_names: + if bundle == "whole_brain": + lens.append(np.median( + seg_sft.sft._tractogram._streamlines._lengths)) + else: + lens.append(np.median( + seg_sft.get_bundle( + bundle)._tractogram._streamlines._lengths)) + if np.isnan(lens[-1]): + lens[-1] = 0 # cannot be NaN to be int + else: + lens[-1] = int(lens[-1]) + + counts_df = pd.DataFrame( + data=dict( + median_len=med_len_counts, + median_len_clean=med_len_clean_counts), + index=bundle_names) + return counts_df, dict(sources=bundles_files) + + @pimms.calc("profiles") @as_file('_profiles.csv', include_track=True, include_seg=True) def tract_profiles(clean_bundles, data_imap, From b8f95a493343457c2984f8d7fc8a85b1d0ef9965 Mon Sep 17 00:00:00 2001 From: 36000 Date: Tue, 5 Jul 2022 15:21:45 -0700 Subject: [PATCH 2/4] typo --- AFQ/tasks/segmentation.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/AFQ/tasks/segmentation.py b/AFQ/tasks/segmentation.py index 741780fbe..14d2eb5bc 100644 --- a/AFQ/tasks/segmentation.py +++ b/AFQ/tasks/segmentation.py @@ -216,8 +216,8 @@ def export_sl_counts(data_imap, @pimms.calc("sl_lengths") @as_file('_sl_lengths.csv', include_track=True, include_seg=True) -def export_sl_counts(data_imap, - clean_bundles, bundles): +def export_sl_lengths(data_imap, + clean_bundles, bundles): """ full path to a JSON file containing streamline counts """ @@ -381,6 +381,7 @@ def get_segmentation_plan(kwargs): segmentation_tasks = with_name([ get_scalar_dict, export_sl_counts, + export_sl_lengths, export_bundles, clean_bundles, segment, From 42f69efea3204741f18af7561610264e0657cce0 Mon Sep 17 00:00:00 2001 From: 36000 Date: Wed, 6 Jul 2022 07:31:18 -0700 Subject: [PATCH 3/4] explicitly catch empty bundles --- AFQ/tasks/segmentation.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/AFQ/tasks/segmentation.py b/AFQ/tasks/segmentation.py index 14d2eb5bc..20ca5b8e3 100644 --- a/AFQ/tasks/segmentation.py +++ b/AFQ/tasks/segmentation.py @@ -238,13 +238,13 @@ def export_sl_lengths(data_imap, lens.append(np.median( seg_sft.sft._tractogram._streamlines._lengths)) else: - lens.append(np.median( - seg_sft.get_bundle( - bundle)._tractogram._streamlines._lengths)) - if np.isnan(lens[-1]): - lens[-1] = 0 # cannot be NaN to be int - else: - lens[-1] = int(lens[-1]) + these_lengths = seg_sft.get_bundle( + bundle)._tractogram._streamlines._lengths + if len(these_lengths) > 0: + lens.append(np.median( + these_lengths)) + else: + lens.append(0) counts_df = pd.DataFrame( data=dict( From a4f995b5f2f76fb4e6f4cfdb56b136beddd91bd0 Mon Sep 17 00:00:00 2001 From: 36000 Date: Wed, 6 Jul 2022 14:08:38 -0700 Subject: [PATCH 4/4] update name --- AFQ/api/utils.py | 2 +- AFQ/tasks/segmentation.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/AFQ/api/utils.py b/AFQ/api/utils.py index 5fd28b749..b22cb6620 100644 --- a/AFQ/api/utils.py +++ b/AFQ/api/utils.py @@ -125,7 +125,7 @@ def export_all_helper(api_afq_object, seg_algo, xforms, indiv, viz): if seg_algo == "AFQ": api_afq_object.export("rois") api_afq_object.export("sl_counts") - api_afq_object.export("sl_lengths") + api_afq_object.export("median_bundle_lengths") api_afq_object.export("profiles") if viz: diff --git a/AFQ/tasks/segmentation.py b/AFQ/tasks/segmentation.py index 20ca5b8e3..be7a359bd 100644 --- a/AFQ/tasks/segmentation.py +++ b/AFQ/tasks/segmentation.py @@ -214,12 +214,12 @@ def export_sl_counts(data_imap, return counts_df, dict(sources=bundles_files) -@pimms.calc("sl_lengths") -@as_file('_sl_lengths.csv', include_track=True, include_seg=True) -def export_sl_lengths(data_imap, - clean_bundles, bundles): +@pimms.calc("median_bundle_lengths") +@as_file('_medianBundleLengths.csv', include_track=True, include_seg=True) +def export_bundle_lengths(data_imap, + clean_bundles, bundles): """ - full path to a JSON file containing streamline counts + full path to a JSON file containing median bundle lengths """ bundle_dict = data_imap["bundle_dict"] med_len_clean_counts = [] @@ -381,7 +381,7 @@ def get_segmentation_plan(kwargs): segmentation_tasks = with_name([ get_scalar_dict, export_sl_counts, - export_sl_lengths, + export_bundle_lengths, export_bundles, clean_bundles, segment,