Skip to content

Commit

Permalink
Merge pull request #174 from jhlegarreta/RefactorArgParsingToOwnMethods
Browse files Browse the repository at this point in the history
STYLE: Refactor argparsing to own methods in scripts
  • Loading branch information
ljod committed Oct 12, 2023
2 parents ac09099 + 617a578 commit 42d9c86
Show file tree
Hide file tree
Showing 40 changed files with 610 additions and 315 deletions.
22 changes: 17 additions & 5 deletions bin/harden_transform_with_slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [email protected]")

parser.add_argument("-v", "--version",
action="version", default=argparse.SUPPRESS,
version='1.0',
Expand All @@ -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):
Expand Down
24 changes: 17 additions & 7 deletions bin/wm_append_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [email protected]")
Expand All @@ -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.")
Expand Down
20 changes: 14 additions & 6 deletions bin/wm_append_clusters_to_anatomical_tracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [email protected]")

parser.add_argument(
'inputDirectory',
help='Contains fiber clusters as vtkPolyData file(s).')
Expand All @@ -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):
Expand Down
19 changes: 14 additions & 5 deletions bin/wm_append_diffusion_measures_across_subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [email protected].")
Expand All @@ -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, '*')))

Expand Down
26 changes: 18 additions & 8 deletions bin/wm_assess_cluster_location_by_hemisphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. ",
Expand All @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
19 changes: 14 additions & 5 deletions bin/wm_average_tract_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [email protected].")
Expand All @@ -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=',')

Expand Down
19 changes: 13 additions & 6 deletions bin/wm_change_nrrd_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [email protected]")

parser.add_argument(
'inputnrrd',
help='Nrrd header in .nhdr format')
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions bin/wm_cluster_atlas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 17 additions & 12 deletions bin/wm_cluster_from_atlas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [email protected]. 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.\"")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 15 additions & 9 deletions bin/wm_cluster_remove_outliers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, [email protected]. 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.\"")
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 42d9c86

Please sign in to comment.