From 0b8d327cbfce4fab1a7528dba127e438b03cca52 Mon Sep 17 00:00:00 2001 From: VincentBeaud Date: Tue, 10 Sep 2024 10:03:37 -0400 Subject: [PATCH] Fix for pep8 --- scripts/scil_ft_fibers_metrics.py | 112 ------------------- scripts/scil_ft_reconstruction_metrics.py | 41 +++---- scripts/scil_ft_tracking.py | 5 +- scripts/scil_ft_visualize_collisions.py | 4 +- scripts/scil_tractogram_filter_collisions.py | 3 +- 5 files changed, 28 insertions(+), 137 deletions(-) delete mode 100644 scripts/scil_ft_fibers_metrics.py diff --git a/scripts/scil_ft_fibers_metrics.py b/scripts/scil_ft_fibers_metrics.py deleted file mode 100644 index d92cebd5a..000000000 --- a/scripts/scil_ft_fibers_metrics.py +++ /dev/null @@ -1,112 +0,0 @@ -#! /usr/bin/env python3 -# -*- coding: utf-8 -*- - -""" -""" - -import os -import argparse -import logging -import numpy as np -import nibabel as nib - -from scilpy.tractanalysis.fibertube_scoring import ( - min_external_distance, - max_voxels, - max_voxel_rotated) -from scilpy.io.streamlines import load_tractogram_with_reference -from scilpy.io.utils import (assert_inputs_exist, - assert_outputs_exist, - add_overwrite_arg, - add_verbose_arg, - add_bbox_arg, - save_dictionary) - - -def _build_arg_parser(): - p = argparse.ArgumentParser( - description=__doc__, - formatter_class=argparse.RawTextHelpFormatter) - - p.add_argument('in_centerlines', - help='Path to the tractogram file containing the \n' - 'fibertube centerlines (must be .trk or .tck).') - - p.add_argument('in_diameters', - help='Path to a text file containing a list of the \n' - 'diameters of each fibertube in mm (.txt). Each line \n' - 'corresponds to the identically numbered centerline.') - - p.add_argument('out_metrics', - help='Output file containing the computed metrics \n' - '(must be .txt).') - - p.add_argument('--save_rotation_matrix', - help='If set, a separate text file will be saved, \n' - 'containing the transformation matrix required to align \n' - 'the max_voxel_anisotropic vector with \n' - 'max_voxel_rotated. The file is derived from the \n' - 'out_metrics parameter with "_max_voxel_rotation"' - 'appended.') - - add_verbose_arg(p) - add_overwrite_arg(p) - add_bbox_arg(p) - - return p - - -def main(): - parser = _build_arg_parser() - args = parser.parse_args() - - if args.verbose: - logging.getLogger().setLevel(logging.DEBUG) - logging.getLogger('numba').setLevel(logging.WARNING) - - if not nib.streamlines.is_supported(args.in_centerlines): - parser.error('Invalid input streamline file format (must be trk ' + - 'or tck): {0}'.format(args.in_centerlines)) - - out_metrics_no_ext, ext = os.path.splitext(args.out_metrics) - - outputs = [args.out_metrics] - if args.save_rotation_matrix: - outputs.append(out_metrics_no_ext + '_max_voxel_rotation' + ext) - - assert_inputs_exist(parser, [args.in_centerlines, args.in_diameters]) - assert_outputs_exist(parser, args, outputs) - - logging.debug('Loading centerline tractogram & diameters') - in_sft = load_tractogram_with_reference(parser, args, args.in_centerlines) - in_sft.to_voxmm() - in_sft.to_center() - # Casting ArraySequence as a list to improve speed - fibers = list(in_sft.get_streamlines_copy()) - diameters = np.loadtxt(args.in_diameters, dtype=np.float64) - if args.single_diameter: - diameter = diameters if np.ndim(diameters) == 0 else diameters[0] - diameters = np.full(len(fibers), diameter) - - min_ext_dist, min_ext_dist_vect = ( - min_external_distance(fibers, diameters, args.verbose)) - max_voxel_ani, max_voxel_iso = max_voxels(min_ext_dist_vect) - mvr_rot, mvr_edge = max_voxel_rotated(min_ext_dist_vect) - - metrics = { - 'min_external_distance': min_ext_dist, - 'max_voxel_anisotropic': max_voxel_ani, - 'max_voxel_isotropic': max_voxel_iso, - 'max_voxel_rotated': [mvr_edge]*3 - } - save_dictionary(metrics, args.out_metrics, args.overwrite) - - if args.save_rotation_matrix: - max_voxel_rotated_transform = np.r_[np.c_[ - mvr_rot, [0, 0, 0]], [[0, 0, 0, 1]]] - np.savetxt(out_metrics_no_ext + '_max_voxel_rotation.txt', - max_voxel_rotated_transform) - - -if __name__ == '__main__': - main() diff --git a/scripts/scil_ft_reconstruction_metrics.py b/scripts/scil_ft_reconstruction_metrics.py index 8dc0abad5..bbf36c219 100644 --- a/scripts/scil_ft_reconstruction_metrics.py +++ b/scripts/scil_ft_reconstruction_metrics.py @@ -72,11 +72,11 @@ def _build_arg_parser(): formatter_class=argparse.RawTextHelpFormatter) p.add_argument('in_fibertubes', - help='Path to the tractogram file containing the \n' - 'fibertubes with their respective diameter saved \n' - 'data_per_streamline (must be .trk). \n' - 'The fibertubes must be void of any collision \n' - '(see scil_filter_intersections.py). \n') + help='Path to the tractogram file containing the \n' + 'fibertubes with their respective diameter saved \n' + 'data_per_streamline (must be .trk). \n' + 'The fibertubes must be void of any collision \n' + '(see scil_filter_intersections.py). \n') p.add_argument('in_tractogram', help='Path to a text file containing the ground-truth \n' @@ -134,22 +134,29 @@ def main(): our_origin = Origin('center') logging.debug('Loading centerline tractogram & diameters') - truth_sft = load_tractogram(args.in_fibertubes, 'same', our_space, our_origin) + truth_sft = load_tractogram(args.in_fibertubes, 'same', our_space, + our_origin) centerlines = truth_sft.get_streamlines_copy() - centerlines, centerlines_length = get_streamlines_as_fixed_array(centerlines) + centerlines, centerlines_length = get_streamlines_as_fixed_array( + centerlines) if "diameters" not in truth_sft.data_per_streamline: - parser.error('No diameters found as data per streamline on ' + args.in_fibertubes) - diameters = np.reshape(truth_sft.data_per_streamline['diameters'], len(centerlines)) + parser.error('No diameters found as data per streamline on ' + + args.in_fibertubes) + diameters = np.reshape(truth_sft.data_per_streamline['diameters'], + len(centerlines)) logging.debug('Loading reconstructed tractogram') - in_sft = load_tractogram(args.in_tractogram, 'same', our_space, our_origin) + in_sft = load_tractogram(args.in_tractogram, 'same', our_space, + our_origin) streamlines = in_sft.get_streamlines_copy() - streamlines, streamlines_length = get_streamlines_as_fixed_array(streamlines) + streamlines, streamlines_length = get_streamlines_as_fixed_array( + streamlines) logging.debug("Loading seeds") if "seeds" not in in_sft.data_per_streamline: - parser.error('No seeds found as data per streamline on ' + args.in_tractogram) + parser.error('No seeds found as data per streamline on ' + + args.in_tractogram) seeds = in_sft.data_per_streamline['seeds'] seeds_fiber = resolve_origin_seeding(seeds, centerlines, diameters) @@ -176,13 +183,9 @@ def main(): seeds_fiber, rand_gen) logging.debug("Computing reconstruction error") - (mean_errors, - error_tractogram) = mean_reconstruction_error(centerlines, centerlines_length, - diameters, - streamlines, - streamlines_length, - seeds_fiber, - args.save_error_tractogram) + (mean_errors, error_tractogram) = mean_reconstruction_error( + centerlines, centerlines_length, diameters, streamlines, + streamlines_length, seeds_fiber, args.save_error_tractogram) metrics = { 'truth_vc_ratio': len(truth_vc)/len(streamlines), diff --git a/scripts/scil_ft_tracking.py b/scripts/scil_ft_tracking.py index e92bf23fc..19057d413 100644 --- a/scripts/scil_ft_tracking.py +++ b/scripts/scil_ft_tracking.py @@ -112,7 +112,7 @@ def _build_arg_parser(): 'Note that points obtained after an invalid direction \n' '(based on the propagator\'s definition of invalid) \n' 'are never added.') - + seed_group = p.add_argument_group( 'Seeding options', 'When no option is provided, uses --nb_seeds_per_fiber 5.') @@ -198,7 +198,8 @@ def main(): logging.debug('Loading tractogram & diameters') in_sft = load_tractogram(args.in_fibertubes, 'same', our_space, our_origin) centerlines = list(in_sft.get_streamlines_copy()) - diameters = np.reshape(in_sft.data_per_streamline['diameters'], len(centerlines)) + diameters = np.reshape(in_sft.data_per_streamline['diameters'], + len(centerlines)) logging.debug("Instantiating datavolumes") fake_mask_data = np.ones(in_sft.dimensions) diff --git a/scripts/scil_ft_visualize_collisions.py b/scripts/scil_ft_visualize_collisions.py index a04a627a6..1f6c1bf1c 100644 --- a/scripts/scil_ft_visualize_collisions.py +++ b/scripts/scil_ft_visualize_collisions.py @@ -72,7 +72,7 @@ def main(): "(must be trk): {0}".format(args.invalid)) invalid_sft = load_tractogram(args.invalid, 'same', - bbox_valid_check=False) + bbox_valid_check=False) invalid_sft.to_voxmm() invalid_sft.to_center() @@ -94,7 +94,7 @@ def main(): # Make display objects and add them to canvas s = window.Scene() invalid_actor = actor.line(invalid_sft.streamlines, - colors=[1., 0., 0.]) + colors=[1., 0., 0.]) s.add(invalid_actor) if (args.obstacle): diff --git a/scripts/scil_tractogram_filter_collisions.py b/scripts/scil_tractogram_filter_collisions.py index 673e99ff4..2d90dca91 100644 --- a/scripts/scil_tractogram_filter_collisions.py +++ b/scripts/scil_tractogram_filter_collisions.py @@ -187,7 +187,7 @@ def main(): streamlines = streamlines[indexes] diameters = diameters[indexes] in_sft = StatefulTractogram.from_sft(streamlines, in_sft) - + # Casting ArraySequence as a list to improve speed streamlines = list(streamlines[indexes]) @@ -256,6 +256,5 @@ def main(): max_voxel_rotated_transform) - if __name__ == "__main__": main()