-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from jhlegarreta/RefactorArgParsingToOwnMethods
STYLE: Refactor argparsing to own methods in scripts
- Loading branch information
Showing
40 changed files
with
610 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
|
@@ -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): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]") | ||
|
@@ -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.") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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).') | ||
|
@@ -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): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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].") | ||
|
@@ -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, '*'))) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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].") | ||
|
@@ -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=',') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.\"") | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.\"") | ||
|
@@ -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" | ||
|
Oops, something went wrong.