diff --git a/bin/harden_transform_with_slicer.py b/bin/harden_transform_with_slicer.py index 484319ca..06a3b09e 100644 --- a/bin/harden_transform_with_slicer.py +++ b/bin/harden_transform_with_slicer.py @@ -42,11 +42,12 @@ def harden_transform(polydata, transform, inverse, outdir): slicer.util.saveNode(polydata_node, output_name) -def main(): + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Harden transform with Slicer.", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu") - parser.add_argument("-v", "--version", action="version", default=argparse.SUPPRESS, version='1.0', @@ -63,9 +64,20 @@ def main(): parser.add_argument( 'outdir', help='') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if os.path.isfile(args.polydata): harden_transform(args.polydata, args.transform, args.inverse, args.outdir) elif os.path.isdir(args.polydata): diff --git a/bin/wm_append_clusters.py b/bin/wm_append_clusters.py index fe180970..6452632e 100755 --- a/bin/wm_append_clusters.py +++ b/bin/wm_append_clusters.py @@ -7,10 +7,9 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Append multiple fiber clusters into one fiber tract.", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu") @@ -30,9 +29,20 @@ def main(): parser.add_argument( '-tractMRML', action="store", type=str, help='A MRML file that contains the fiber clusters to be appended. If neither -clusterList nor -tractMRML are provided, all vtk/vtp files in the input folder will be appended.') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + inputdir = os.path.abspath(args.inputDirectory) if not os.path.isdir(args.inputDirectory): print(f"<{os.path.basename(__file__)}> Error: Input directory", args.inputDirectory, "does not exist.") diff --git a/bin/wm_append_clusters_to_anatomical_tracts.py b/bin/wm_append_clusters_to_anatomical_tracts.py index df1e7bda..c4bae12b 100755 --- a/bin/wm_append_clusters_to_anatomical_tracts.py +++ b/bin/wm_append_clusters_to_anatomical_tracts.py @@ -9,14 +9,11 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Append multiple fiber clusters into anatomical tracts based on the ORG atlas.", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu") - parser.add_argument( 'inputDirectory', help='Contains fiber clusters as vtkPolyData file(s).') @@ -27,7 +24,18 @@ def main(): 'outputDirectory', help='The output directory should be a new empty directory. It will be created if needed.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) inputdir = os.path.abspath(args.inputDirectory) if not os.path.isdir(args.inputDirectory): diff --git a/bin/wm_append_diffusion_measures_across_subjects.py b/bin/wm_append_diffusion_measures_across_subjects.py index c3e78e27..d78e1c2b 100644 --- a/bin/wm_append_diffusion_measures_across_subjects.py +++ b/bin/wm_append_diffusion_measures_across_subjects.py @@ -8,10 +8,8 @@ import glob -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Append diffusion measure files from multiple subjects", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu.") @@ -22,7 +20,18 @@ def main(): 'appenedmeasurefile', help='Output csv file.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) subject_folders = sorted(glob.glob(os.path.join(args.inputfolder, '*'))) diff --git a/bin/wm_assess_cluster_location_by_hemisphere.py b/bin/wm_assess_cluster_location_by_hemisphere.py index c2775dc1..c64ad971 100755 --- a/bin/wm_assess_cluster_location_by_hemisphere.py +++ b/bin/wm_assess_cluster_location_by_hemisphere.py @@ -21,10 +21,9 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Assess if a fiber within the clusters belongs to left hemispheric, right hemispheric, or commissural tracts. " "This code needs to be run in the ATLAS space, where the brain is clustered at the RAS origin. ", @@ -51,7 +50,17 @@ def main(): parser.add_argument( '-outputDirectory', help='If this is given, separated clusters will be output under this folder. The output directory will be created if it does not exist.') - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + def list_cluster_files(input_dir): # Find input files input_mask = f"{input_dir}/cluster_*.vtk" @@ -109,9 +118,10 @@ def read_mask_location_from_vtk(inpd): break return flag_location, mask_location - - args = parser.parse_args() - + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.isdir(args.inputDirectory): print(f"<{os.path.basename(__file__)}> Error: Input directory", args.inputDirectory, "does not exist or is not a directory.") exit() diff --git a/bin/wm_average_tract_measures.py b/bin/wm_average_tract_measures.py index 579655d8..11b4e878 100644 --- a/bin/wm_average_tract_measures.py +++ b/bin/wm_average_tract_measures.py @@ -8,10 +8,8 @@ import re -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Compute averaged statistics of the anatomical tracts. For diffusion measure with multiple statistics, only the Mean will be outputted.", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu.") @@ -28,7 +26,18 @@ def main(): '-tractList', action="store", type=str, nargs='+', help='A list of tracts to be appended, e.g., AF_left AF_right.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) stats = pandas.read_table(args.inputmeasure, delimiter=',') diff --git a/bin/wm_change_nrrd_dir.py b/bin/wm_change_nrrd_dir.py index d5fb5329..00033aaa 100644 --- a/bin/wm_change_nrrd_dir.py +++ b/bin/wm_change_nrrd_dir.py @@ -13,14 +13,11 @@ from joblib import Parallel, delayed -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Make sign change of the gradient direction", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu") - parser.add_argument( 'inputnrrd', help='Nrrd header in .nhdr format') @@ -31,8 +28,18 @@ def main(): '-d', action="store", dest="dim", type=str, help='The dimension to change: x, y, or z.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + parser = _build_arg_parser() + args = _parse_args(parser) inputnrrd = args.inputnrrd outputnrrd = args.outputnrrd diff --git a/bin/wm_cluster_atlas.py b/bin/wm_cluster_atlas.py index 6d3230e5..2f25a75d 100755 --- a/bin/wm_cluster_atlas.py +++ b/bin/wm_cluster_atlas.py @@ -261,10 +261,6 @@ def main(): # contain almost as much information and reduce noise for single subject clustering #number_of_eigenvectors = 20 - # ======================================================================= - # Above this line is argument parsing. Below this line is the pipeline. - # ======================================================================= - input_polydatas = wma.io.list_vtk_files(args.inputDirectory) number_of_subjects = len(input_polydatas) diff --git a/bin/wm_cluster_from_atlas.py b/bin/wm_cluster_from_atlas.py index 2c4a39ab..cb5cd324 100644 --- a/bin/wm_cluster_from_atlas.py +++ b/bin/wm_cluster_from_atlas.py @@ -10,10 +10,9 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Clusters tractography (propagates clusters) from a cluster atlas (a multi-subject/multi-atlas cluster representation).", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"O'Donnell, Lauren J., and C-F. Westin. Automatic tractography segmentation using a high-dimensional white matter atlas. Medical Imaging, IEEE Transactions on 26.11 (2007): 1562-1575.\"") @@ -51,10 +50,20 @@ def main(): parser.add_argument( '-norender', action='store_true', dest="flag_norender", help='No Render. Prevents rendering of images that would require an X connection.') - - args = parser.parse_args() - - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.exists(args.inputFile): print(f"<{os.path.basename(__file__)}> Error: Input file", args.inputFile, "does not exist.") exit() @@ -118,10 +127,6 @@ def main(): render = not args.flag_norender print("==========================\n") - - # ======================================================================= - # Above this line is argument parsing. Below this line is the pipeline. - # ======================================================================= # read atlas print(f"<{os.path.basename(__file__)}> Loading input atlas:", args.atlasDirectory) diff --git a/bin/wm_cluster_remove_outliers.py b/bin/wm_cluster_remove_outliers.py index f4281612..18b85a01 100644 --- a/bin/wm_cluster_remove_outliers.py +++ b/bin/wm_cluster_remove_outliers.py @@ -19,10 +19,9 @@ print(f"<{os.path.basename(__file__)}> Failed to import joblib, cannot multiprocess.") print(f"<{os.path.basename(__file__)}> Please install joblib for this functionality.") -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Removes outliers in a subject dataset that was clustered from a cluster atlas. This script uses the atlas to identifies and remove outliers in each cluster of the subject. The atlas must be the same one used to cluster the subject dataset", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"O'Donnell, Lauren J., and C-F. Westin. Automatic tractography segmentation using a high-dimensional white matter atlas. Medical Imaging, IEEE Transactions on 26.11 (2007): 1562-1575.\"") @@ -52,7 +51,18 @@ def main(): '-j', action="store", dest="numberOfJobs", type=str, help='Number of processors to use.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputDirectory): print(f"<{os.path.basename(__file__)}> Error: Input subject directory", args.inputDirectory, "does not exist.") @@ -100,10 +110,6 @@ def main(): verbose = args.flag_verbose print("==========================\n") - - # ======================================================================= - # Above this line is argument parsing. Below this line is the pipeline. - # ======================================================================= # Copy all MRML files to the new subject directory input_mask = f"{args.inputDirectory}/clustered_tracts*.mrml" diff --git a/bin/wm_cluster_volumetric_measurements.py b/bin/wm_cluster_volumetric_measurements.py index 0d142457..84523c67 100644 --- a/bin/wm_cluster_volumetric_measurements.py +++ b/bin/wm_cluster_volumetric_measurements.py @@ -10,45 +10,48 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Compute measurements of a cluster based on a provided volumetric scalar map (e.g. an FA image).", epilog="Written by Fan Zhang") - parser.add_argument("-v", "--version", action="version", default=argparse.SUPPRESS, version='1.0', help="Show program's version number and exit") - parser.add_argument( 'inputDirectory', help='Directory of input VTK/VTP files.') - parser.add_argument( 'inputVolumetricMap', help='Path to input volumetric map, e.g. an FA image. Note: this input image needs to be a nifti (nii or nii.gz) file. To read and write this format, NiBabel package is needed, by running: pip install nibabel ') - parser.add_argument( 'outputDirectory', help='Directory of output statistics.') - parser.add_argument( 'outputStatFile', action="store", type=str, help="File name of the output statistics.") - parser.add_argument( '-sampleSize', action="store", type=float, help='Fiber sample size') - parser.add_argument( '-outputLabelmap', action='store_true', help='Generate a label map of each input cluster if given.') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + inputdir = os.path.abspath(args.inputDirectory) if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") diff --git a/bin/wm_compare_vtks.py b/bin/wm_compare_vtks.py index 15757e79..f2fecda8 100755 --- a/bin/wm_compare_vtks.py +++ b/bin/wm_compare_vtks.py @@ -10,24 +10,33 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Compare two tractography files to see how much registration has changed the points. The files must have the exact same size and lines and points, e.g. the original file and the file that results from applying a transform to a file.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"O'Donnell, Lauren J., and C-F. Westin. Automatic tractography segmentation using a high-dimensional white matter atlas. Medical Imaging, IEEE Transactions on 26.11 (2007): 1562-1575.\"", version='1.0') - parser.add_argument( 'inputFile1', help='A file of whole-brain tractography as vtkPolyData (.vtk or .vtp).') parser.add_argument( 'inputFile2', help='A file of whole-brain tractography as vtkPolyData (.vtk or .vtp).') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + pd1 = wma.io.read_polydata(args.inputFile1) pd2 = wma.io.read_polydata(args.inputFile2) diff --git a/bin/wm_create_mrml_file.py b/bin/wm_create_mrml_file.py index 2f07e6ad..219a7f80 100644 --- a/bin/wm_create_mrml_file.py +++ b/bin/wm_create_mrml_file.py @@ -10,20 +10,29 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Create a MRML file that includes all vtk and vtp files in the input directory. Color tracts with different colors in the scene.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"O'Donnell, Lauren J., and C-F. Westin. Automatic tractography segmentation using a high-dimensional white matter atlas. Medical Imaging, IEEE Transactions on 26.11 (2007): 1562-1575.\"") - parser.add_argument( 'inputDirectory', help='A directory of whole-brain tractography as vtkPolyData (.vtk or .vtp). Output MRML scene file scene.mrml will be stored here and will point to all tractography files.') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.isdir(args.inputDirectory): print(f"<{os.path.basename(__file__)}> Error: Input directory", args.inputDirectory, "does not exist.") exit() diff --git a/bin/wm_diffusion_measurements.py b/bin/wm_diffusion_measurements.py index 92c0ba04..2d254612 100644 --- a/bin/wm_diffusion_measurements.py +++ b/bin/wm_diffusion_measurements.py @@ -8,14 +8,12 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Compute diffusion scalar measurements (such as FA, MD, etc). This script reports the mean statistics of each fiber cluster (or fiber tract) within the input folder.", epilog="Written by Fan Zhang (fzhang@bwh.harvard.edu)") - parser.add_argument("-v", "--version", action="version", default=argparse.SUPPRESS, version='1.0', @@ -29,9 +27,20 @@ def main(): parser.add_argument( 'Slicer', help='Path of 3D Slicer.') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") exit() diff --git a/bin/wm_download_anatomically_curated_atlas.py b/bin/wm_download_anatomically_curated_atlas.py index 56032fda..09d78318 100755 --- a/bin/wm_download_anatomically_curated_atlas.py +++ b/bin/wm_download_anatomically_curated_atlas.py @@ -8,13 +8,9 @@ import sys import ssl -def main(): - if (not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None)): - ssl._create_default_https_context = ssl._create_unverified_context - - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Download a pre-provided anatomically curated fiber clustering white matter atlas.", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu") @@ -25,9 +21,23 @@ def main(): parser.add_argument( '-atlas', action="store", dest="requested_atlas", type=str, help='Name of the atlas. Currently, \'ORG-800FC-100HCP\' and \'ORG-2000FC-100HCP\' are available to download.') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + + if (not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None)): + ssl._create_default_https_context = ssl._create_unverified_context + def download_file(url, output_file): try: diff --git a/bin/wm_harden_transform.py b/bin/wm_harden_transform.py index 9badb648..6fbcb9bf 100644 --- a/bin/wm_harden_transform.py +++ b/bin/wm_harden_transform.py @@ -8,14 +8,12 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Harden transform with Slicer.", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu") - parser.add_argument("-v", "--version", action="version", default=argparse.SUPPRESS, version='1.0', @@ -41,9 +39,20 @@ def main(): parser.add_argument( '-j', action="store", dest="numberOfJobs", type=int, help='Number of processors to use.') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + inputdir = os.path.abspath(args.inputDirectory) if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") diff --git a/bin/wm_preprocess_all.py b/bin/wm_preprocess_all.py index edc1e818..10ca3336 100755 --- a/bin/wm_preprocess_all.py +++ b/bin/wm_preprocess_all.py @@ -11,14 +11,12 @@ from joblib import Parallel, delayed -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Applies preprocessing to input directory. Downsamples, removes short fibers. Preserves tensors and scalar point data along retained fibers.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu") - parser.add_argument( 'inputDirectory', help='Contains whole-brain tractography as vtkPolyData file(s).') @@ -44,7 +42,18 @@ def main(): '--nonidentical', action='store_true', help='Obtain nonidentical results across runs for downsampling.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") @@ -90,10 +99,6 @@ def main(): print("==========================") - # ======================================================================= - # Above this line is argument parsing. Below this line is the pipeline. - # ======================================================================= - # Loop over input DWIs inputPolyDatas = wma.io.list_vtk_files(args.inputDirectory) diff --git a/bin/wm_quality_control_after_clustering.py b/bin/wm_quality_control_after_clustering.py index 5ae44b94..5c018307 100644 --- a/bin/wm_quality_control_after_clustering.py +++ b/bin/wm_quality_control_after_clustering.py @@ -18,14 +18,12 @@ print(f"<{os.path.basename(__file__)}> Error importing matplotlib.pyplot package, can't plot quality control data.\n") HAVE_PLT = 0 -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Quality control of fiber clustering results across multiple subjects.", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu") - parser.add_argument("-v", "--version", action="version", default=argparse.SUPPRESS, version='1.0', @@ -36,9 +34,20 @@ def main(): parser.add_argument( 'outputDirectory', help='Quality control information will be stored in the output directory, which will be created if it does not exist.') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") exit() diff --git a/bin/wm_quality_control_cluster_measurements.py b/bin/wm_quality_control_cluster_measurements.py index dbc2a907..af149969 100755 --- a/bin/wm_quality_control_cluster_measurements.py +++ b/bin/wm_quality_control_cluster_measurements.py @@ -8,14 +8,12 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Perform quality control steps (check if all subjects have matching data) for .csv or txt measurement files in the input directory.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"O'Donnell, Lauren J., and C-F. Westin. Automatic tractography segmentation using a high-dimensional white matter atlas. Medical Imaging, IEEE Transactions on 26.11 (2007): 1562-1575.\"") - parser.add_argument( 'inputDirectory', help='A directory of .csv or txt measurement files from Slicer tractography measurements.') @@ -26,9 +24,20 @@ def main(): parser.add_argument( '-outlier_std', action="store", dest="OutlierStandardDeviation", type=float, default=2.0, help='Standard deviation for outlier detection of subjects.') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") exit() diff --git a/bin/wm_quality_control_tract_overlap.py b/bin/wm_quality_control_tract_overlap.py index 480d2c22..e4f1f0aa 100755 --- a/bin/wm_quality_control_tract_overlap.py +++ b/bin/wm_quality_control_tract_overlap.py @@ -19,14 +19,12 @@ print(f"<{os.path.basename(__file__)}> Error importing matplotlib.pyplot package, can't plot quality control data.\n") HAVE_PLT = 0 -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Perform quality control to render the overlap of two tracts.", epilog="Written by Fan Zhang (fzhang@bwh.harvard.edu).") - parser.add_argument( 'inputTract1', help='Input tract 1 as vtkPolyData (.vtk or .vtp).') @@ -36,9 +34,20 @@ def main(): parser.add_argument( 'outputDirectory', help='Quality control information will be stored in the output directory, which will be created if it does not exist.') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + print(f"<{os.path.basename(__file__)}> Starting...") if not os.path.exists(args.inputTract1): diff --git a/bin/wm_quality_control_tractography.py b/bin/wm_quality_control_tractography.py index 08680f46..359c9d08 100755 --- a/bin/wm_quality_control_tractography.py +++ b/bin/wm_quality_control_tractography.py @@ -16,16 +16,15 @@ import matplotlib.pyplot as plt except: print(f"<{os.path.basename(__file__)}> Error importing matplotlib.pyplot package, can't plot quality control data.\n") - HAVE_PLT = 0 + HAVE_PLT = 0 + + +def _build_arg_parser(): -def main(): - #----------------- - # Parse arguments - #----------------- parser = argparse.ArgumentParser( description="Perform quality control steps (rendering images and fiber length testing) for all vtk and vtp files in the input directory.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"O'Donnell, Lauren J., and C-F. Westin. Automatic tractography segmentation using a high-dimensional white matter atlas. Medical Imaging, IEEE Transactions on 26.11 (2007): 1562-1575.\"") - + parser.add_argument( 'inputDirectory', help='A directory of whole-brain tractography as vtkPolyData (.vtk or .vtp).') @@ -33,11 +32,22 @@ def main(): 'outputDirectory', help='Quality control information will be stored in the output directory, which will be created if it does not exist.') # for now this is not parallelized. that would complicate summary info about group. - #parser.add_argument( + # parser.add_argument( # '-j', action="store", dest="numberOfJobs", type=int, # help='Number of processors to use.') - - args = parser.parse_args() + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) print(f"<{os.path.basename(__file__)}> Starting...") diff --git a/bin/wm_register_multisubject_faster.py b/bin/wm_register_multisubject_faster.py index 9e01075c..7806db0e 100755 --- a/bin/wm_register_multisubject_faster.py +++ b/bin/wm_register_multisubject_faster.py @@ -14,14 +14,12 @@ import scipy.optimize -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Runs multisubject unbiased group registration of tractography.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"Unbiased Groupwise Registration of White Matter Tractography. LJ O'Donnell, WM Wells III, Golby AJ, CF Westin. Med Image Comput Comput Assist Interv. 2012;15(Pt 3):123-30.\"") - parser.add_argument( 'inputDirectory', help='A directory of whole-brain tractography as vtkPolyData (.vtk or .vtp).') @@ -58,10 +56,20 @@ def main(): parser.add_argument( '-advanced_only_random_seed', action='store', dest="randomSeed", type=int, help='(Advanced parameter for testing only.) Set random seed for reproducible sampling in software tests.') - - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + print("\n\n =========GROUP REGISTRATION============") print(f"<{os.path.basename(__file__)}> Performing unbiased group registration.") print(f"<{os.path.basename(__file__)}> Input directory: ", args.inputDirectory) diff --git a/bin/wm_register_to_atlas_new.py b/bin/wm_register_to_atlas_new.py index bca490fa..fa81f25a 100755 --- a/bin/wm_register_to_atlas_new.py +++ b/bin/wm_register_to_atlas_new.py @@ -13,15 +13,12 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Registers a whole-brain vtk tractography file to another vtk tractography file (an atlas).", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"Unbiased Groupwise Registration of White Matter Tractography. LJ O'Donnell, WM Wells III, Golby AJ, CF Westin. Med Image Comput Comput Assist Interv. 2012;15(Pt 3):123-30.\"") - - parser.add_argument( 'inputSubject', help='One subject data: whole-brain tractography as vtkPolyData (.vtk or .vtp).') @@ -46,9 +43,20 @@ def main(): #parser.add_argument( # '-pf', action="store", dest="pointsPerFiber", type=int, default=15, # help='Number of points for fiber representation during registration. The default of 15 is reasonable.') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + print("\n\n =========GROUP REGISTRATION============") print(f"<{os.path.basename(__file__)}> Registering to atlas.") print(f"<{os.path.basename(__file__)}> Input subject file: ", args.inputSubject) diff --git a/bin/wm_remove_data_along_tracts.py b/bin/wm_remove_data_along_tracts.py index a6a7129b..85b8376c 100644 --- a/bin/wm_remove_data_along_tracts.py +++ b/bin/wm_remove_data_along_tracts.py @@ -12,14 +12,12 @@ from joblib import Parallel, delayed -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Applies preprocessing to input directory. Downsamples, removes short fibers. Preserves tensors and scalar point data along retained fibers.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu") - parser.add_argument( 'inputDirectory', help='Contains whole-brain tractography as vtkPolyData file(s).') @@ -35,10 +33,20 @@ def main(): parser.add_argument( '-j', action="store", dest="numberOfJobs", type=int, help='Number of processors to use.') - - args = parser.parse_args() - - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") exit() @@ -63,10 +71,6 @@ def main(): print("==========================") - # ======================================================================= - # Above this line is argument parsing. Below this line is the pipeline. - # ======================================================================= - # Loop over input DWIs inputPolyDatas = wma.io.list_vtk_files(args.inputDirectory) diff --git a/bin/wm_separate_clusters_by_hemisphere.py b/bin/wm_separate_clusters_by_hemisphere.py index 3bc58b21..91c898e4 100755 --- a/bin/wm_separate_clusters_by_hemisphere.py +++ b/bin/wm_separate_clusters_by_hemisphere.py @@ -21,10 +21,9 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Separate each cluster into left/right/commissural tracts based on the fiber location information computed by . " "The output is three directories of fiber bundles according to left hemisphere, right hemisphere, and commissural tracts. ", @@ -39,9 +38,20 @@ def main(): parser.add_argument( 'outputDirectory', help='The output directory will be created if it does not exist.') - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.isdir(args.inputDirectory): print(f"<{os.path.basename(__file__)}> Error: Input directory", args.inputDirectory, "does not exist or is not a directory.") exit() diff --git a/bin/wm_tract_to_volume.py b/bin/wm_tract_to_volume.py index a308b906..81679195 100644 --- a/bin/wm_tract_to_volume.py +++ b/bin/wm_tract_to_volume.py @@ -11,14 +11,12 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Convert a fiber tract or cluster (vtk) to a voxel-wise fiber density image (nii.gz). ", epilog="Written by Fan Zhang") - parser.add_argument("-v", "--version", action="version", default=argparse.SUPPRESS, version='1.0', @@ -36,13 +34,24 @@ def main(): parser.add_argument( '-m', action="store", dest="measure", type=str, help="diffusion measure; if not provided, the output will be density map") - - args = parser.parse_args() - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.exists(args.inputVTK): print("Error: Input directory", args.inputVTK, "does not exist.") exit() - + def convert_cluster_to_volume_with_sz(inpd, volume, sampling_size=0.5): volume_shape = volume.get_fdata().shape diff --git a/bin/wm_vtp2vtk.py b/bin/wm_vtp2vtk.py index 9db2fca3..57a22f86 100644 --- a/bin/wm_vtp2vtk.py +++ b/bin/wm_vtp2vtk.py @@ -9,24 +9,32 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- + +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Converts all vtp files in input directory to vtk files, which are saved in output directory.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu") - parser.add_argument( 'inputDirectory', help='Contains input tractography as vtkPolyData vtp format file(s).') parser.add_argument( 'outputDirectory', help='The output directory should be a new empty directory. It will be created if needed. The actual output will be placed into a directory within this output directory, to preserve the informative name of the input directory that may contain subject ID, tract name, etc.') - - args = parser.parse_args() - - + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") exit() @@ -50,11 +58,7 @@ def main(): print("=====top-level output directory requested by user=====\n", args.outputDirectory) print("=====final output directory=====\n", outdir_subject) print("==========================") - - # ======================================================================= - # Above this line is argument parsing. Below this line is the pipeline. - # ======================================================================= - + def list_vtp_files(input_dir): # Find input files (JUST vtp) #input_mask = "{0}/*.vtk".format(input_dir) diff --git a/utilities/wm_assess_cluster_location.py b/utilities/wm_assess_cluster_location.py index f8935121..f81028d2 100644 --- a/utilities/wm_assess_cluster_location.py +++ b/utilities/wm_assess_cluster_location.py @@ -10,14 +10,11 @@ -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Decide a cluster belonging to commissural or hemispheric in the atlas.", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu") - parser.add_argument("-v", "--version", action="version", default=argparse.SUPPRESS, version='1.0', @@ -32,7 +29,18 @@ def main(): '-advanced_times_threshold', action="store", dest="advanced_times_threshold", type=float, default=3, help='(Advanced parameter should be used according to applications.) For one cluster, if it has the hemispheric (left or right) fibers three times more than the commissural part, this cluster will be considered as a hemispheric cluster. In a similar way, one cluster needs to have the commissural fibers three times more than the hemispheric (left and right) fibers to be considered as a commissural cluster. Users can change -advanced_num_threshold to have more strict classification. For example, if advanced_num_threshold = 5, the cluster needs to have the commissural fibers five times more than the hemispheric (left and right) fibers to be considered as a commissural cluster.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputAtlasDirectory): print("Error: Input directory", args.inputTractDirectory, "does not exist.") diff --git a/utilities/wm_compute_FA_from_DWIs.py b/utilities/wm_compute_FA_from_DWIs.py index 7f4f0362..5b6924ed 100644 --- a/utilities/wm_compute_FA_from_DWIs.py +++ b/utilities/wm_compute_FA_from_DWIs.py @@ -18,10 +18,8 @@ def list_nhdr_files(input_dir): return(input_pd_fnames) -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Measures mean FA, etc. in tractography clusters in a directory.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"O'Donnell, Lauren J., and C-F. Westin. Automatic tractography segmentation using a high-dimensional white matter atlas. Medical Imaging, IEEE Transactions on 26.11 (2007): 1562-1575.\"") @@ -39,7 +37,18 @@ def main(): 'outputDirectory', help='Output data will be saved here.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputDirectoryDWI): print("Error: Input directory", args.inputDirectory, "does not exist.") diff --git a/utilities/wm_compute_TAP.py b/utilities/wm_compute_TAP.py index e4ce051c..e0013c9b 100755 --- a/utilities/wm_compute_TAP.py +++ b/utilities/wm_compute_TAP.py @@ -9,7 +9,8 @@ import whitematteranalysis as wma -def main(): +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Compute Tract Anatomical Profile", epilog="Written by Fan Zhang, zhangfanmark@gmail.com.") @@ -21,7 +22,18 @@ def main(): 'inputDirectory', help='A directory containing subdirectories for all clustered subjects.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist or is not a directory.") diff --git a/utilities/wm_extract_cluster.py b/utilities/wm_extract_cluster.py index 90538a71..f36c5389 100755 --- a/utilities/wm_extract_cluster.py +++ b/utilities/wm_extract_cluster.py @@ -9,7 +9,8 @@ import whitematteranalysis as wma -def main(): +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Grab one cluster from within all subject directories and rename to include subject ID. Output all clusters into the output directory", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"O'Donnell, Lauren J., and C-F. Westin. Automatic tractography segmentation using a high-dimensional white matter atlas. Medical Imaging, IEEE Transactions on 26.11 (2007): 1562-1575.\"") @@ -27,10 +28,18 @@ def main(): 'outputDirectory', help='The output directory will be created if it does not exist.') + return parser + +def _parse_args(parser): - args = parser.parse_args() + return parser.parse_args() + + +def main(): + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputDirectory): print(f"<{os.path.basename(__file__)}> Error: Input directory", args.inputDirectory, "does not exist or is not a directory.") diff --git a/utilities/wm_extract_clusters_by_endpoints.py b/utilities/wm_extract_clusters_by_endpoints.py index d77bc730..1c6dad37 100755 --- a/utilities/wm_extract_clusters_by_endpoints.py +++ b/utilities/wm_extract_clusters_by_endpoints.py @@ -9,14 +9,11 @@ -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Extract fiber clusters that connect to one particular region, such as one cortical parcel or one fMRI functional area. This is based on the results from .", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu") - parser.add_argument("-v", "--version", action="version", default=argparse.SUPPRESS, version='1.0', @@ -37,7 +34,18 @@ def main(): '-fc_folder', type=str, dest="fiber_cluster_folder", help='Contains the fiber clustering result, in which each sub folder represents one subject. If specified, a mrml file that displays all output clusters will be generated. ') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") diff --git a/utilities/wm_fix_UKF_trace.py b/utilities/wm_fix_UKF_trace.py index c4bd4c98..66a0315a 100644 --- a/utilities/wm_fix_UKF_trace.py +++ b/utilities/wm_fix_UKF_trace.py @@ -52,19 +52,27 @@ def fix_trace(inpd): return inpd -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Fix the incorrect trace value stored by UKF.", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu") - parser.add_argument( 'inputDirectory', help='Contains fiber clusters as vtkPolyData file(s).') - - args = parser.parse_args() + + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) fixed_log = os.path.join(args.inputDirectory, 'fixed.log') if os.path.exists(fixed_log): diff --git a/utilities/wm_flatten_length_distribution.py b/utilities/wm_flatten_length_distribution.py index 4d382912..2b9ed24a 100644 --- a/utilities/wm_flatten_length_distribution.py +++ b/utilities/wm_flatten_length_distribution.py @@ -12,15 +12,12 @@ from joblib import Parallel, delayed -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Samples fibers such that the output distribution of fiber lengths is approximately flat, within the input length range.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu", version='1.0') - parser.add_argument( 'inputDirectory', help='Contains whole-brain tractography as vtkPolyData file(s).') @@ -46,10 +43,19 @@ def main(): '-j', action="store", dest="numberOfJobs", type=int, help='Number of processors to use.') + return parser + + +def _parse_args(parser): - args = parser.parse_args() + return parser.parse_args() +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") exit() @@ -91,10 +97,6 @@ def main(): print("==========================") - # ======================================================================= - # Above this line is argument parsing. Below this line is the pipeline. - # ======================================================================= - # Loop over input DWIs inputMask1 = f"{args.inputDirectory}/*.vtk" inputMask2 = f"{args.inputDirectory}/*.vtp" diff --git a/utilities/wm_laterality_all.py b/utilities/wm_laterality_all.py index 7ba778c9..25047285 100755 --- a/utilities/wm_laterality_all.py +++ b/utilities/wm_laterality_all.py @@ -9,14 +9,11 @@ -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Applies white matter laterality pipeline to input directory.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu") - parser.add_argument("-v", "--version", action="version", default=argparse.SUPPRESS, version='1.0', @@ -48,7 +45,18 @@ def main(): '-fibers_per_hem', action="store", dest="numberOfFibersPerHem", type=int, help='Number of fibers to analyze from each hemisphere.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") @@ -96,10 +104,6 @@ def main(): print("==========================") - # ======================================================================= - # Above this line is argument parsing. Below this line is the pipeline. - # ======================================================================= - inputPolyDatas = wma.io.list_vtk_files(args.inputDirectory) print(f"<{os.path.basename(__file__)}> Input number of files: ", len(inputPolyDatas)) diff --git a/utilities/wm_measure_all_clusters.py b/utilities/wm_measure_all_clusters.py index 31891ae2..84e7032b 100755 --- a/utilities/wm_measure_all_clusters.py +++ b/utilities/wm_measure_all_clusters.py @@ -9,10 +9,8 @@ -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Measures mean FA, etc. in tractography clusters in a directory.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"O'Donnell, Lauren J., and C-F. Westin. Automatic tractography segmentation using a high-dimensional white matter atlas. Medical Imaging, IEEE Transactions on 26.11 (2007): 1562-1575.\"") @@ -27,7 +25,18 @@ def main(): 'outputFile', help='Output measurements will be recorded here.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") @@ -40,11 +49,6 @@ def main(): print("==========================") print("") - - # ======================================================================= - # Above this line is argument parsing. Below this line is the pipeline. - # ======================================================================= - def compute_point_data_stats(pd, array_name): point_array = pd.GetPointData().GetArray(array_name) if point_array is None: diff --git a/utilities/wm_measure_endpoint_overlap.py b/utilities/wm_measure_endpoint_overlap.py index df4e9e96..06e030e0 100755 --- a/utilities/wm_measure_endpoint_overlap.py +++ b/utilities/wm_measure_endpoint_overlap.py @@ -10,14 +10,11 @@ -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Measure overlaps of fiber clusters with cortical parcellation or fMRI functional areas. This is based on the 3D Slicer module FiberEndPointFromLabelMap.", epilog="Written by Fan Zhang, fzhang@bwh.harvard.edu") - parser.add_argument("-v", "--version", action="version", default=argparse.SUPPRESS, version='1.0', @@ -38,7 +35,18 @@ def main(): '-j', action="store", dest="numberOfJobs", type=int, help='Number of processors to use.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputTractDirectory): print("Error: Input directory", args.inputTractDirectory, "does not exist.") diff --git a/utilities/wm_statistics.py b/utilities/wm_statistics.py index af83bfee..2378aa5d 100644 --- a/utilities/wm_statistics.py +++ b/utilities/wm_statistics.py @@ -12,10 +12,8 @@ import matplotlib.pyplot as plt -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Compute requested statistics comparing groups in the input groups file. Please verify your input by running wm_quality_control_cluster measurements before running this program.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"O'Donnell, Lauren J., and C-F. Westin. Automatic tractography segmentation using a high-dimensional white matter atlas. Medical Imaging, IEEE Transactions on 26.11 (2007): 1562-1575.\"", @@ -39,12 +37,21 @@ def main(): parser.add_argument('--no-hierarchy', dest='hierarchy', action='store_false') parser.set_defaults(hierarchy=True) parser.add_argument('-mode', dest='mode', action='store', default='all', help='all, clusters, toplevel, sublevel') - - parser.add_argument('--onetail', dest='one_tailed', action='store_true') parser.set_defaults(one_tailed=False) - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputDirectory): print(f"<{os.path.basename(__file__)}> Error: Input directory", args.inputDirectory, "does not exist.") diff --git a/utilities/wm_statistics_export_data.py b/utilities/wm_statistics_export_data.py index ac7db4f4..f5edce43 100644 --- a/utilities/wm_statistics_export_data.py +++ b/utilities/wm_statistics_export_data.py @@ -9,15 +9,12 @@ import whitematteranalysis as wma -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Export all subjects selected measures into one excel-readable file for use in statistics programs. Please verify your input by running wm_quality_control_cluster measurements before running this program.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu. Please reference \"O'Donnell, Lauren J., and C-F. Westin. Automatic tractography segmentation using a high-dimensional white matter atlas. Medical Imaging, IEEE Transactions on 26.11 (2007): 1562-1575.\"", version='1.0') - parser.add_argument( 'inputDirectory', help='A directory of .csv or txt measurement files from Slicer tractography measurements.') @@ -31,7 +28,18 @@ def main(): 'outputFileName', help='A file name for output (ending in .txt or .csv).') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() + + +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) if not os.path.isdir(args.inputDirectory): print(f"<{os.path.basename(__file__)}> Error: Input directory", args.inputDirectory, "does not exist.") diff --git a/utilities/wm_transform_polydata.py b/utilities/wm_transform_polydata.py index 7db31002..1e2ee09e 100644 --- a/utilities/wm_transform_polydata.py +++ b/utilities/wm_transform_polydata.py @@ -13,15 +13,12 @@ from joblib import Parallel, delayed -def main(): - #----------------- - # Parse arguments - #----------------- +def _build_arg_parser(): + parser = argparse.ArgumentParser( description="Applies preprocessing to input directory. Downsamples, removes short fibers. Preserves tensors and scalar point data along retained fibers.", epilog="Written by Lauren O\'Donnell, odonnell@bwh.harvard.edu", version='1.0') - parser.add_argument( 'inputDirectory', help='Contains whole-brain tractography as vtkPolyData file(s).') @@ -44,9 +41,19 @@ def main(): '-invert', action='store_true', dest="invert_flag", help='Apply the inverse of the transform.') - args = parser.parse_args() + return parser + + +def _parse_args(parser): + + return parser.parse_args() +def main(): + + parser = _build_arg_parser() + args = _parse_args(parser) + if not os.path.isdir(args.inputDirectory): print("Error: Input directory", args.inputDirectory, "does not exist.") exit() @@ -71,11 +78,6 @@ def main(): print("==========================") - # ======================================================================= - # Above this line is argument parsing. Below this line is the pipeline. - # ======================================================================= - - # Loop over input DWIs inputPolyDatas = wma.io.list_vtk_files(args.inputDirectory)