本文整理汇总了Python中msct_parser.Parser.parse方法的典型用法代码示例。如果您正苦于以下问题:Python Parser.parse方法的具体用法?Python Parser.parse怎么用?Python Parser.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msct_parser.Parser
的用法示例。
在下文中一共展示了Parser.parse方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_data_or_scalar
# 需要导入模块: from msct_parser import Parser [as 别名]
# 或者: from msct_parser.Parser import parse [as 别名]
def get_data_or_scalar(argument, data_in):
"""
Get data from list of file names (scenario 1) or scalar (scenario 2)
:param argument: list of file names of scalar
:param data_in: if argument is scalar, use data to get shape
:return: 3d or 4d numpy array
"""
if argument.replace('.', '').isdigit(): # so that it recognize float as digits too
# build data2 with same shape as data
data_out = data_in[:, :, :] * 0 + float(argument)
else:
# parse file name and check integrity
parser2 = Parser(__file__)
parser2.add_option(name='-i', type_value=[[','], 'file'])
list_fname = parser2.parse(['-i', argument]).get('-i')
data_out = get_data(list_fname)
return data_out
示例2: mm
# 需要导入模块: from msct_parser import Parser [as 别名]
# 或者: from msct_parser.Parser import parse [as 别名]
description="reference volume for label removing.",
mandatory=False)
parser.add_option(name="-c",
type_value="int",
description="cross radius in mm (default=5mm).",
mandatory=False)
parser.add_option(name="-d",
type_value=None,
description="dilatation bool for cross generation ('-c' option).",
mandatory=False)
parser.add_option(name="-v",
type_value="multiple_choice",
description="verbose. Default=" + str(param_default.verbose),
mandatory=False,
example=['0', '1'])
arguments = parser.parse(sys.argv[1:])
input_filename = arguments["-i"]
process_type = arguments["-t"]
input_fname_output = None
input_fname_ref = None
input_cross_radius = 5
input_dilate = False
input_coordinates = None
input_verbose = '1'
input_fname_output = arguments["-o"]
if "-r" in arguments:
input_fname_ref = arguments["-r"]
if "-x" in arguments:
input_coordinates = arguments["-x"]
if "-c" in arguments:
示例3: main
# 需要导入模块: from msct_parser import Parser [as 别名]
# 或者: from msct_parser.Parser import parse [as 别名]
def main():
# get default parameters
step1 = Paramreg(step='1', type='seg', algo='slicereg', metric='MeanSquares', iter='10')
step2 = Paramreg(step='2', type='im', algo='syn', metric='MI', iter='3')
# step1 = Paramreg()
paramreg = ParamregMultiStep([step1, step2])
# step1 = Paramreg_step(step='1', type='seg', algo='bsplinesyn', metric='MeanSquares', iter='10', shrink='1', smooth='0', gradStep='0.5')
# step2 = Paramreg_step(step='2', type='im', algo='syn', metric='MI', iter='10', shrink='1', smooth='0', gradStep='0.5')
# paramreg = ParamregMultiStep([step1, step2])
# Initialize the parser
parser = Parser(__file__)
parser.usage.set_description('Register anatomical image to the template.')
parser.add_option(name="-i",
type_value="file",
description="Anatomical image.",
mandatory=True,
example="anat.nii.gz")
parser.add_option(name="-s",
type_value="file",
description="Spinal cord segmentation.",
mandatory=True,
example="anat_seg.nii.gz")
parser.add_option(name="-l",
type_value="file",
description="Labels. See: http://sourceforge.net/p/spinalcordtoolbox/wiki/create_labels/",
mandatory=True,
default_value='',
example="anat_labels.nii.gz")
parser.add_option(name="-t",
type_value="folder",
description="Path to MNI-Poly-AMU template.",
mandatory=False,
default_value=param.path_template)
parser.add_option(name="-p",
type_value=[[':'], 'str'],
description="""Parameters for registration (see sct_register_multimodal). Default:\n--\nstep=1\ntype="""+paramreg.steps['1'].type+"""\nalgo="""+paramreg.steps['1'].algo+"""\nmetric="""+paramreg.steps['1'].metric+"""\npoly="""+paramreg.steps['1'].poly+"""\n--\nstep=2\ntype="""+paramreg.steps['2'].type+"""\nalgo="""+paramreg.steps['2'].algo+"""\nmetric="""+paramreg.steps['2'].metric+"""\niter="""+paramreg.steps['2'].iter+"""\nshrink="""+paramreg.steps['2'].shrink+"""\nsmooth="""+paramreg.steps['2'].smooth+"""\ngradStep="""+paramreg.steps['2'].gradStep+"""\n--""",
mandatory=False,
example="step=2,type=seg,algo=bsplinesyn,metric=MeanSquares,iter=5,shrink=2:step=3,type=im,algo=syn,metric=MI,iter=5,shrink=1,gradStep=0.3")
parser.add_option(name="-r",
type_value="multiple_choice",
description="""Remove temporary files.""",
mandatory=False,
default_value='1',
example=['0', '1'])
parser.add_option(name="-v",
type_value="multiple_choice",
description="""Verbose. 0: nothing. 1: basic. 2: extended.""",
mandatory=False,
default_value=param.verbose,
example=['0', '1', '2'])
if param.debug:
print '\n*** WARNING: DEBUG MODE ON ***\n'
fname_data = '/Users/julien/data/temp/sct_example_data/t2/t2.nii.gz'
fname_landmarks = '/Users/julien/data/temp/sct_example_data/t2/labels.nii.gz'
fname_seg = '/Users/julien/data/temp/sct_example_data/t2/t2_seg.nii.gz'
path_template = param.path_template
remove_temp_files = 0
verbose = 2
# speed = 'superfast'
#param_reg = '2,BSplineSyN,0.6,MeanSquares'
else:
arguments = parser.parse(sys.argv[1:])
# get arguments
fname_data = arguments['-i']
fname_seg = arguments['-s']
fname_landmarks = arguments['-l']
path_template = arguments['-t']
remove_temp_files = int(arguments['-r'])
verbose = int(arguments['-v'])
if '-p' in arguments:
paramreg_user = arguments['-p']
# update registration parameters
for paramStep in paramreg_user:
paramreg.addStep(paramStep)
# initialize other parameters
file_template = param.file_template
file_template_label = param.file_template_label
file_template_seg = param.file_template_seg
output_type = param.output_type
zsubsample = param.zsubsample
# smoothing_sigma = param.smoothing_sigma
# start timer
start_time = time.time()
# get absolute path - TO DO: remove! NEVER USE ABSOLUTE PATH...
path_template = os.path.abspath(path_template)
# get fname of the template + template objects
fname_template = sct.slash_at_the_end(path_template, 1)+file_template
fname_template_label = sct.slash_at_the_end(path_template, 1)+file_template_label
fname_template_seg = sct.slash_at_the_end(path_template, 1)+file_template_seg
# check file existence
sct.printv('\nCheck template files...')
#.........这里部分代码省略.........
示例4: main
# 需要导入模块: from msct_parser import Parser [as 别名]
# 或者: from msct_parser.Parser import parse [as 别名]
def main():
# Initialization
fname_output = ''
fname_mask = param.fname_mask
fname_src_seg = ''
fsloutput = 'export FSLOUTPUTTYPE=NIFTI; ' # for faster processing, all outputs are in NIFTI'
start_time = time.time()
# get path of the toolbox
status, path_sct = commands.getstatusoutput('echo $SCT_DIR')
# get default registration parameters
# step1 = Paramreg(step='1', type='im', algo='syn', metric='MI', iter='5', shrink='1', smooth='0', gradStep='0.5')
step0 = Paramreg(step='0', type='im', algo='syn', metric='MI', iter='0', shrink='1', smooth='0', gradStep='0.5') # only used to put src into dest space
step1 = Paramreg()
paramreg = ParamregMultiStep([step0, step1])
# Initialize the parser
parser = Parser(__file__)
parser.usage.set_description('This program co-registers two 3D volumes. The deformation is non-rigid and is '
'constrained along Z direction (i.e., axial plane). Hence, this function assumes '
'that orientation of the destination image is axial (RPI). If you need to register '
'two volumes with large deformations and/or different contrasts, it is recommended to '
'input spinal cord segmentations (binary mask) in order to achieve maximum robustness.'
' The program outputs a warping field that can be used to register other images to the'
' destination image. To apply the warping field to another image, use '
'sct_apply_transfo')
parser.add_option(name="-i",
type_value="file",
description="Image source.",
mandatory=True,
example="src.nii.gz")
parser.add_option(name="-d",
type_value="file",
description="Image destination.",
mandatory=True,
example="dest.nii.gz")
parser.add_option(name="-iseg",
type_value="file",
description="Segmentation source.",
mandatory=False,
example="src_seg.nii.gz")
parser.add_option(name="-dseg",
type_value="file",
description="Segmentation destination.",
mandatory=False,
example="dest_seg.nii.gz")
parser.add_option(name="-m",
type_value="file",
description="Binary mask to improve accuracy over region of interest.",
mandatory=False,
example="mask.nii.gz")
parser.add_option(name="-o",
type_value="file_output",
description="Name of output file.",
mandatory=False,
example="src_reg.nii.gz")
parser.add_option(name="-p",
type_value=[[':'],'str'],
description="""Parameters for registration. Separate arguments with ",". Separate steps with ":".\nstep: <int> Step number (starts at 1).\ntype: {im,seg} type of data used for registration.\nalgo: {slicereg,rigid,affine,syn,bsplinesyn}. Default="""+paramreg.steps['1'].algo+"""\n For info about slicereg, see here: goo.gl/Sj3ZeU\nmetric: {CC,MI,MeanSquares}. Default="""+paramreg.steps['1'].metric+"""\niter: <int> Number of iterations. Default="""+paramreg.steps['1'].iter+"""\nshrink: <int> Shrink factor (only for SyN). Default="""+paramreg.steps['1'].shrink+"""\nsmooth: <int> Smooth factor (only for SyN). Default="""+paramreg.steps['1'].smooth+"""\ngradStep: <float> Gradient step (only for SyN). Default="""+paramreg.steps['1'].gradStep+"""\npoly: <int> Polynomial degree (only for slicereg). Default="""+paramreg.steps['1'].poly,
mandatory=False,
example="step=1,type=seg,algo=slicereg,metric=MeanSquares:step=2,type=im,algo=syn,metric=MI,iter=5,shrink=2")
parser.add_option(name="-z",
type_value="int",
description="""size of z-padding to enable deformation at edges when using SyN.""",
mandatory=False,
default_value=param.padding)
parser.add_option(name="-x",
type_value="multiple_choice",
description="""Final interpolation.""",
mandatory=False,
default_value='linear',
example=['nn', 'linear', 'spline'])
parser.add_option(name="-r",
type_value="multiple_choice",
description="""Remove temporary files.""",
mandatory=False,
default_value='1',
example=['0', '1'])
parser.add_option(name="-v",
type_value="multiple_choice",
description="""Verbose.""",
mandatory=False,
default_value='1',
example=['0', '1', '2'])
arguments = parser.parse(sys.argv[1:])
# get arguments
fname_src = arguments['-i']
fname_dest = arguments['-d']
if '-iseg' in arguments:
fname_src_seg = arguments['-iseg']
if '-dseg' in arguments:
fname_dest_seg = arguments['-dseg']
if '-o' in arguments:
fname_output = arguments['-o']
if "-m" in arguments:
fname_mask = arguments['-m']
padding = arguments['-z']
#.........这里部分代码省略.........
示例5: main
# 需要导入模块: from msct_parser import Parser [as 别名]
# 或者: from msct_parser.Parser import parse [as 别名]
def main():
# get default parameters
step1 = Paramreg(step='1', type='seg', algo='slicereg', metric='MeanSquares', iter='10')
step2 = Paramreg(step='2', type='im', algo='syn', metric='MI', iter='3')
# step1 = Paramreg()
paramreg = ParamregMultiStep([step1, step2])
# step1 = Paramreg_step(step='1', type='seg', algo='bsplinesyn', metric='MeanSquares', iter='10', shrink='1', smooth='0', gradStep='0.5')
# step2 = Paramreg_step(step='2', type='im', algo='syn', metric='MI', iter='10', shrink='1', smooth='0', gradStep='0.5')
# paramreg = ParamregMultiStep([step1, step2])
# Initialize the parser
parser = Parser(__file__)
parser.usage.set_description('Register anatomical image to the template.')
parser.add_option(name="-i",
type_value="file",
description="Anatomical image.",
mandatory=True,
example="anat.nii.gz")
parser.add_option(name="-s",
type_value="file",
description="Spinal cord segmentation.",
mandatory=True,
example="anat_seg.nii.gz")
parser.add_option(name="-l",
type_value="file",
description="Labels. See: http://sourceforge.net/p/spinalcordtoolbox/wiki/create_labels/",
mandatory=True,
default_value='',
example="anat_labels.nii.gz")
parser.add_option(name="-t",
type_value="folder",
description="Path to MNI-Poly-AMU template.",
mandatory=False,
default_value=param.path_template)
parser.add_option(name="-p",
type_value=[[':'], 'str'],
description="""Parameters for registration (see sct_register_multimodal). Default:\n--\nstep=1\ntype="""+paramreg.steps['1'].type+"""\nalgo="""+paramreg.steps['1'].algo+"""\nmetric="""+paramreg.steps['1'].metric+"""\npoly="""+paramreg.steps['1'].poly+"""\n--\nstep=2\ntype="""+paramreg.steps['2'].type+"""\nalgo="""+paramreg.steps['2'].algo+"""\nmetric="""+paramreg.steps['2'].metric+"""\niter="""+paramreg.steps['2'].iter+"""\nshrink="""+paramreg.steps['2'].shrink+"""\nsmooth="""+paramreg.steps['2'].smooth+"""\ngradStep="""+paramreg.steps['2'].gradStep+"""\n--""",
mandatory=False,
example="step=2,type=seg,algo=bsplinesyn,metric=MeanSquares,iter=5,shrink=2:step=3,type=im,algo=syn,metric=MI,iter=5,shrink=1,gradStep=0.3")
parser.add_option(name="-r",
type_value="multiple_choice",
description="""Remove temporary files.""",
mandatory=False,
default_value='1',
example=['0', '1'])
parser.add_option(name="-v",
type_value="multiple_choice",
description="""Verbose. 0: nothing. 1: basic. 2: extended.""",
mandatory=False,
default_value=param.verbose,
example=['0', '1', '2'])
if param.debug:
print '\n*** WARNING: DEBUG MODE ON ***\n'
fname_data = '/Users/julien/data/temp/sct_example_data/t2/t2.nii.gz'
fname_landmarks = '/Users/julien/data/temp/sct_example_data/t2/labels.nii.gz'
fname_seg = '/Users/julien/data/temp/sct_example_data/t2/t2_seg.nii.gz'
path_template = param.path_template
remove_temp_files = 0
verbose = 2
# speed = 'superfast'
#param_reg = '2,BSplineSyN,0.6,MeanSquares'
else:
arguments = parser.parse(sys.argv[1:])
# get arguments
fname_data = arguments['-i']
fname_seg = arguments['-s']
fname_landmarks = arguments['-l']
path_template = arguments['-t']
remove_temp_files = int(arguments['-r'])
verbose = int(arguments['-v'])
if '-p' in arguments:
paramreg_user = arguments['-p']
# update registration parameters
for paramStep in paramreg_user:
paramreg.addStep(paramStep)
# initialize other parameters
file_template = param.file_template
file_template_label = param.file_template_label
file_template_seg = param.file_template_seg
output_type = param.output_type
zsubsample = param.zsubsample
# smoothing_sigma = param.smoothing_sigma
# start timer
start_time = time.time()
# get absolute path - TO DO: remove! NEVER USE ABSOLUTE PATH...
path_template = os.path.abspath(path_template)
# get fname of the template + template objects
fname_template = sct.slash_at_the_end(path_template, 1)+file_template
fname_template_label = sct.slash_at_the_end(path_template, 1)+file_template_label
fname_template_seg = sct.slash_at_the_end(path_template, 1)+file_template_seg
# check file existence
sct.printv('\nCheck template files...')
#.........这里部分代码省略.........
示例6: main
# 需要导入模块: from msct_parser import Parser [as 别名]
# 或者: from msct_parser.Parser import parse [as 别名]
def main():
# Initialization
fsloutputdir = 'export FSLOUTPUTTYPE=NIFTI_GZ; '
file_ordering = 'alternate'
start_time = time.time()
# Initialize the parser
parser = Parser(__file__)
parser.usage.set_description('Convert dcm2nii using nin sequence from J. Finsterbusch. '
'Requires the software dcm2nii (from mricron).')
parser.add_option(name="-i",
type_value="folder",
description="Path to dicom data.",
mandatory=True,
example="data/my_data")
parser.add_option(name="-ord",
type_value="multiple_choice",
description="""File ordering: \nalternate: spine,brain,spine,brain... (with custom coil)\nbloc: spine,spine... brain,brain... (with head-neck coil)\n""",
mandatory=False,
default_value='bloc',
example=['alternate', 'bloc'])
parser.add_option(name="-r",
type_value="multiple_choice",
description="""Remove temporary files.""",
mandatory=False,
default_value='1',
example=['0', '1'])
parser.add_option(name="-v",
type_value="multiple_choice",
description="""Verbose.""",
mandatory=False,
default_value='1',
example=['0', '1', '2'])
arguments = parser.parse(sys.argv[1:])
# get arguments
path_data = arguments['-i']
if '-ord' in arguments:
file_ordering = arguments['-ord']
remove_temp_files = int(arguments['-r'])
verbose = int(arguments['-v'])
path_tmp = sct.tmp_create(basename="nin_convert_dcm2nii", verbose=verbose)
# go to temporary folder
curdir = os.getcwd()
os.chdir(path_tmp)
# list DICOM files
file_data_list = os.listdir(path_data)
# create another temp folder for conversion
sct.run('mkdir tmp', verbose)
# loop across files
file_nii = []
i = 0
for i_file in file_data_list:
# convert dicom to nifti and put in temporary folder
status, output = sct.run('dcm2nii -o tmp/ -v n '+os.path.join(path_data, i_file), verbose)
# change file name
file_nii.append('data_'+str(i).zfill(4)+'.nii.gz')
sct.run('mv tmp/*.nii.gz '+file_nii[i])
# increment file index
i = i+1
# Merge data
nb_files = len(file_data_list)
if file_ordering == 'alternate':
sct.run(fsloutputdir+'fslmerge -t data_spine '+' '.join([file_nii[i] for i in range(0, nb_files, 2)]))
sct.run(fsloutputdir+'fslmerge -t data_brain '+' '.join([file_nii[i] for i in range(1, nb_files, 2)]))
if file_ordering == 'bloc':
sct.run(fsloutputdir+'fslmerge -t data_spine '+' '.join([file_nii[i] for i in range(0, nb_files/2)]))
sct.run(fsloutputdir+'fslmerge -t data_brain '+' '.join([file_nii[i] for i in range(nb_files/2+1, nb_files)]))
# come back
os.chdir(curdir)
# Generate output files
sct.printv('\nGenerate output files...', verbose)
fname_data_spine = sct.generate_output_file(os.path.join(path_tmp, 'data_spine.nii.gz'), 'data_spine.nii.gz', verbose)
fname_data_brain = sct.generate_output_file(os.path.join(path_tmp, 'data_brain.nii.gz'), 'data_brain.nii.gz', verbose)
# Delete temporary files
if remove_temp_files:
sct.printv('\nRemove temporary files...', verbose)
sct.run('rm -rf '+path_tmp, verbose)
elapsed_time = time.time() - start_time
sct.printv('\nFinished! Elapsed time: '+str(int(round(elapsed_time)))+'s', verbose)
sct.printv('\nTo view results, type:', verbose)
sct.printv('fslview data_spine &', verbose, 'info')
sct.printv('fslview data_brain &\n', verbose, 'info')