Skip to content

Commit

Permalink
ENH: Use comma separator for lists in python command arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel Garcia committed Mar 5, 2025
1 parent 0b1c648 commit afb684f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
9 changes: 5 additions & 4 deletions applications/rtk3Doutputimage_group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itk
from itk import RTK as rtk

__all__ = [
'add_rtk3Doutputimage_group',
Expand All @@ -8,10 +9,10 @@
# Mimicks rtk3Doutputimage_section.ggo
def add_rtk3Doutputimage_group(parser):
rtk3Doutputimage_group = parser.add_argument_group('Output 3D image properties')
rtk3Doutputimage_group.add_argument('--origin', help='Origin (default=centered)', type=float, nargs='+')
rtk3Doutputimage_group.add_argument('--dimension', help='Dimension', type=int, nargs='+', default=[256])
rtk3Doutputimage_group.add_argument('--spacing', help='Spacing', type=float, nargs='+', default=[1])
rtk3Doutputimage_group.add_argument('--direction', help='Direction', type=float, nargs='+')
rtk3Doutputimage_group.add_argument('--origin', help='Origin (default=centered)', type=rtk.comma_separated_args(float))
rtk3Doutputimage_group.add_argument('--dimension', help='Dimension', type=rtk.comma_separated_args(int), default=[256])
rtk3Doutputimage_group.add_argument('--spacing', help='Spacing', type=rtk.comma_separated_args(float), default=[1])
rtk3Doutputimage_group.add_argument('--direction', help='Direction', type=rtk.comma_separated_args(float))
rtk3Doutputimage_group.add_argument('--like', help='Copy information from this image (origin, dimension, spacing, direction)')

# Mimicks SetConstantImageSourceFromGgo
Expand Down
17 changes: 9 additions & 8 deletions applications/rtkinputprojections_group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itk
from itk import RTK as rtk

__all__ = [
'add_rtkinputprojections_group',
Expand All @@ -13,20 +14,20 @@ def add_rtkinputprojections_group(parser):
rtkinputprojections_group.add_argument('--nsort', help='Numeric sort for regular expression matches', action='store_true')
rtkinputprojections_group.add_argument('--submatch', help='Index of the submatch that will be used to sort matches', type=int, default=0)
rtkinputprojections_group.add_argument('--nolineint', help='Disable raw to line integral conversion, just casts to float', type=bool, default=False)
rtkinputprojections_group.add_argument('--newdirection', help='New value of input projections (before pre-processing)', type=float, nargs='+')
rtkinputprojections_group.add_argument('--neworigin', help='New origin of input projections (before pre-processing)', type=float, nargs='+')
rtkinputprojections_group.add_argument('--newspacing', help='New spacing of input projections (before pre-processing)', type=float, nargs='+')
rtkinputprojections_group.add_argument('--lowercrop', help='Lower boundary crop size', type=int, nargs='+', default=[0])
rtkinputprojections_group.add_argument('--uppercrop', help='Upper boundary crop size', type=int, nargs='+', default=[0])
rtkinputprojections_group.add_argument('--binning', help='Shrink / Binning factos in each direction', type=int, nargs='+', default=[1])
rtkinputprojections_group.add_argument('--wpc', help='Water precorrection coefficients (default is no correction)', type=float, nargs='+')
rtkinputprojections_group.add_argument('--newdirection', help='New value of input projections (before pre-processing)', type=rtk.comma_separated_args(float))
rtkinputprojections_group.add_argument('--neworigin', help='New origin of input projections (before pre-processing)', type=rtk.comma_separated_args(float))
rtkinputprojections_group.add_argument('--newspacing', help='New spacing of input projections (before pre-processing)', type=rtk.comma_separated_args(float))
rtkinputprojections_group.add_argument('--lowercrop', help='Lower boundary crop size', type=rtk.comma_separated_args(int))
rtkinputprojections_group.add_argument('--uppercrop', help='Upper boundary crop size', type=rtk.comma_separated_args(int))
rtkinputprojections_group.add_argument('--binning', help='Shrink / Binning factos in each direction', type=rtk.comma_separated_args(int), default=[1])
rtkinputprojections_group.add_argument('--wpc', help='Water precorrection coefficients (default is no correction)', type=rtk.comma_separated_args(float))
rtkinputprojections_group.add_argument('--spr', help='Boellaard scatter correction: scatter-to-primary ratio', type=float, default=0)
rtkinputprojections_group.add_argument('--nonneg', help='Boellaard scatter correction: non-negativity threshold', type=float)
rtkinputprojections_group.add_argument('--airthres', help='Boellaard scatter correction: air threshold', type=float)
rtkinputprojections_group.add_argument('--i0', help='I0 value (when assumed constant per projection), 0 means auto', type=float)
rtkinputprojections_group.add_argument('--idark', help='IDark value, i.e., value when beam is off', type=float, default=0)
rtkinputprojections_group.add_argument('--component', help='Vector component to extract, for multi-material projections', type=int, default=0)
rtkinputprojections_group.add_argument('--radius', help='Radius of neighborhood for conditional median filtering', type=int, nargs='+', default=[0])
rtkinputprojections_group.add_argument('--radius', help='Radius of neighborhood for conditional median filtering', type=rtk.comma_separated_args(int), default=[0])
rtkinputprojections_group.add_argument('--multiplier', help='Threshold multiplier for conditional median filtering', type=float, default=0)

# Mimicks GetProjectionsFileNamesFromGgo
Expand Down
6 changes: 3 additions & 3 deletions applications/rtkorageometry/rtkorageometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def main():
parser.add_argument('--output', '-o', help='Output file name', required=True)
parser.add_argument('--path', '-p', help='Path containing projections', required=True)
parser.add_argument('--regexp', '-r', help='Regular expression to select projection files in path')
parser.add_argument('--margin', '-m', help='Collimation margin (uinf, usup, vinf, vsup)', type=float, nargs='+', default=0.)
parser.add_argument('--margin', '-m', help='Collimation margin (uinf, usup, vinf, vsup)', type=rtk.comma_separated_args(float), default=0.)

args = parser.parse_args()

Expand All @@ -25,8 +25,8 @@ def main():
print(margin)

names = itk.RegularExpressionSeriesFileNames.New()
names.SetDirectory(args.path);
names.SetRegularExpression(args.regexp);
names.SetDirectory(args.path)
names.SetRegularExpression(args.regexp)

reader = rtk.OraGeometryReader.New()
reader.SetProjectionsFileNames(names.GetFileNames())
Expand Down
7 changes: 6 additions & 1 deletion wrapping/rtkExtra.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def CudaImageFromImage(img):
return cuda_img
return img # Return the original image if CUDA is not available

# Returns a lambda function that parses a comma-separated string and converts each element to the specified type.
def comma_separated_args(value_type):
return lambda value: [value_type(s.strip()) for s in value.split(',')]

# Returns the progress percentage
class PercentageProgressCommand:
def __init__(self,caller):
self.percentage = -1
Expand All @@ -45,4 +50,4 @@ def callback(self):
self.percentage = new_percentage

def End(self):
print() # Print newline when execution ends
print() # Print newline when execution ends

0 comments on commit afb684f

Please sign in to comment.