11import argparse
2+ import sys
23from pathlib import Path
4+ from typing import Literal
35
46import numpy as np
57
911from CorpusCallosum .shape .cc_mesh import CC_Mesh
1012
1113
12- def options_parse () -> argparse .Namespace :
13- """Parse command line arguments for the visualization pipeline."""
14+ def make_parser () -> argparse .ArgumentParser :
15+ """Create a command line parser for the visualization pipeline."""
1416 parser = argparse .ArgumentParser (description = "Visualize corpus callosum from template files." )
1517 parser .add_argument ("--contours" , type = str , required = False , help = "Path to contours.txt file" , default = None )
1618 parser .add_argument ("--thickness" , type = str , required = True , help = "Path to thickness_values.txt file" )
@@ -38,12 +40,17 @@ def options_parse() -> argparse.Namespace:
3840 nargs = 2 ,
3941 default = None ,
4042 metavar = ("MIN" , "MAX" ),
41- help = "Optional fixed range for the colorbar (min max)" ,
43+ required = False ,
44+ help = "Specify the range for the colorbar (2 values: min max). Defaults to automatic choice." ,
4245 )
4346 parser .add_argument ("--legend" , type = str , default = "Thickness (mm)" , help = "Legend for the colorbar" )
4447 parser .add_argument ("--twoD" , action = "store_true" , help = "Generate 2D visualization instead of 3D mesh" )
4548
46- args = parser .parse_args ()
49+ return parser
50+
51+ def options_parse () -> argparse .Namespace :
52+ """Parse command line arguments for the pipeline."""
53+ args = make_parser ().parse_args ()
4754
4855 # Create output directory if it doesn't exist
4956 Path (args .output_dir ).mkdir (parents = True , exist_ok = True )
@@ -62,7 +69,7 @@ def main(
6269 color_range : tuple [float , float ] | None = None ,
6370 legend : str | None = None ,
6471 twoD : bool = False ,
65- ) -> None :
72+ ) -> Literal [ 0 ] | str :
6673 """Main function to visualize corpus callosum from template files.
6774
6875 This function loads contours and thickness values from template files,
@@ -146,21 +153,24 @@ def main(
146153 cc_mesh .write_vtk (str (output_dir / "cc_mesh.vtk" ))
147154 cc_mesh .write_fssurf (str (output_dir / "cc_mesh.fssurf" ))
148155 cc_mesh .write_overlay (str (output_dir / "cc_mesh_overlay.curv" ))
149- cc_mesh .snap_cc_picture (str (output_dir / "cc_mesh_snap.png" ))
150-
156+ try :
157+ cc_mesh .snap_cc_picture (str (output_dir / "cc_mesh_snap.png" ))
158+ except RuntimeError :
159+ return ("The cc_visualization script requires whippersnappy>=1.3.1 to makes screenshots, install with "
160+ "`pip install whippersnappy>=1.3.1` !" )
161+ return 0
151162
152163if __name__ == "__main__" :
153- options = options_parse ()
154- main_args = {
155- "contours_path" : options .contours ,
156- "thickness_path" : options .thickness ,
157- "measurement_points_path" : options .measurement_points ,
158- "output_dir" : options .output_dir ,
159- "resolution" : options .resolution ,
160- "smoothing_window" : options .smoothing_window ,
161- "colormap" : options .colormap ,
162- "color_range" : options .color_range ,
163- "legend" : options .legend ,
164- "twoD" : options .twoD ,
165- }
166- main (** main_args )
164+ options = make_parser ().parse_args ()
165+ sys .exit (main (
166+ contours_path = options .contours ,
167+ thickness_path = options .thickness ,
168+ measurement_points_path = options .measurement_points ,
169+ output_dir = options .output_dir ,
170+ resolution = options .resolution ,
171+ smooth_iterations = options .smooth_iterations ,
172+ colormap = options .colormap ,
173+ color_range = options .color_range ,
174+ legend = options .legend ,
175+ twoD = options .twoD ,
176+ ))
0 commit comments