Exemple #1
0
def parse(args):
    """Parse the command-line arguments of the `view` command.

    Parameters
    ----------
    args : list of str
        List of arguments, without the command name.

    Returns
    -------
    View
        Filled structure

    """

    struct = View()

    struct.files = []
    while cli.next_isvalue(args):
        val, *args = args
        struct.files.append(val)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct
Exemple #2
0
def parse(args):
    """

    Parameters
    ----------
    args : list of str
        Command line arguments (without the command name)

    Returns
    -------
    Convert
        Filled structure

    """

    struct = Convert()

    struct.files = []
    while cli.next_isvalue(args):
        val, *args = args
        struct.files.append(val)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-m', '--meta'):
            struct.meta = {}
            while cli.next_isvalue(args):
                val, *args = args
                if '=' not in val:
                    raise ValueError(f'Metadata should have format KEY=VAL '
                                     f'without whitespaces. Found {val} '
                                     f'alone.')
                key, val = val.split('=')
                struct.meta[key] = val
        elif tag in ('-c', '--casting'):
            cli.check_next_isvalue(args, tag)
            struct.cast, *args = args
        elif tag in ('-dt', '--dtype'):
            cli.check_next_isvalue(args, tag)
            struct.dtype, *args = args
        elif tag in ('-f', '--format'):
            cli.check_next_isvalue(args, tag)
            struct.format, *args = args
        elif tag in ('-o', '--output'):
            struct.output = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.output.append(val)
        elif tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct
Exemple #3
0
def parse(args):
    """

    Parameters
    ----------
    args

    Returns
    -------

    """

    struct = Orient()

    struct.files = []
    while cli.next_isvalue(args):
        val, *args = args
        struct.files.append(val)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-l', '--layout'):
            cli.check_next_isvalue(args, tag)
            struct.layout, *args = args
        elif tag in ('-v', '--voxel-size'):
            struct.voxel_size = []
            while cli.next_isvalue(args):
                val, *args = args
                if val.lower() in ('self', 'like'):
                    struct.voxel_size = val
                    break
                struct.voxel_size.append(float(val))
        elif tag in ('-c', '--center'):
            struct.center = []
            while cli.next_isvalue(args):
                val, *args = args
                if val.lower() in ('self', 'like'):
                    struct.center = val
                    break
                struct.center.append(float(val))
        elif tag in ('-k', '--like'):
            struct.like, *args = args
        elif tag in ('-o', '--output'):
            struct.output = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.output.append(val)
        elif tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct
Exemple #4
0
def parse(args):
    struct = Vexp()

    struct.files = []
    while cli.next_isvalue(args):
        val, *args = args
        struct.files.append(val)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-t', '--type'):
            cli.check_next_isvalue(args, tag)
            struct.type, *args = args
        elif tag in ('-u', '--unit'):
            cli.check_next_isvalue(args, tag)
            struct.unit, *args = args
        elif tag in ('-b', '--bound'):
            cli.check_next_isvalue(args, tag)
            struct.bound, *args = args
        elif tag in ('-n', '--nb-steps'):
            cli.check_next_isvalue(args, tag)
            struct.nb_steps, *args = args
            struct.nb_steps = int(struct.nb_steps)
        elif tag in ('-inv', '--inverse'):
            val = False
            if cli.next_isvalue(args):
                val, *args = args
            if val[0] == 'f':
                val = False
            elif val[0] == 't':
                val = True
            else:
                val = bool(int(val))
            struct.inv = val
        elif tag in ('-o', '--output'):
            struct.output = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.output.append(val)
        elif tag in ('-cpu', '--cpu'):
            struct.device = 'cpu'
        elif tag in ('-gpu', '--gpu'):
            struct.device = 'cuda'
            if cli.next_isvalue(args):
                gpu, *args = args
                struct.device = 'cuda:{:d}'.format(int(gpu))
        elif tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct
Exemple #5
0
def parse(args):
    """

    Parameters
    ----------
    args

    Returns
    -------

    """

    struct = Chunk()

    struct.files = []
    while cli.next_isvalue(args):
        val, *args = args
        struct.files.append(val)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-d', '--dim', '--dimension'):
            cli.check_next_isvalue(args, tag)
            struct.dim, *args = args
            struct.dim = int(struct.dim)
        elif tag in ('-n', '--chunk'):
            cli.check_next_isvalue(args, tag)
            struct.chunk, *args = args
            struct.chunk = int(struct.chunk)
        elif tag in ('-o', '--output'):
            struct.output = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.output.append(val)
        elif tag in ('t', '--transform'):
            if not cli.next_isvalue(args):
                struct.transform = ['{dir}{sep}{base}_2_{i}.lta']
            else:
                struct.transform = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.transform.append(val)
        elif tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct
Exemple #6
0
def parse(args):
    """

    Parameters
    ----------
    args : list of str
        Command line arguments (without the command name)

    Returns
    -------
    Info
        Filled structure

    """

    struct = Info()

    struct.files = []
    while cli.next_isvalue(args):
        val, *args = args
        struct.files.append(val)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-m', '--meta'):
            struct.meta = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.meta.append(val)
        elif tag in ('-s', '--stat'):
            struct.stat = True
            if cli.next_isvalue(args):
                val, *args = args
                if val.lower().startswith('f'):
                    val = False
                elif val.lower().startswith('t'):
                    val = True
                struct.stat = bool(int(val))
        elif tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct
Exemple #7
0
def parse(args):
    struct = Crop()

    struct.files = []
    while cli.next_isvalue(args):
        val, *args = args
        struct.files.append(val)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-s', '--size'):
            struct.size = []
            while cli.next_isvalue(args):
                val, *args = args
                if val.lower() in ('vox', 'ras'):
                    struct.size_space = val
                else:
                    struct.size.append(float(val))
        elif tag in ('-c', '--center'):
            struct.center = []
            while cli.next_isvalue(args):
                val, *args = args
                if val.lower() in ('vox', 'ras'):
                    struct.center_space = val
                else:
                    struct.center.append(float(val))
        elif tag in ('-b', '--bounding-box'):
            struct.bbox = True
            if cli.next_isvalue(args):
                struct.bbox = float(args.pop(0))
        elif tag in ('-k', '--like'):
            cli.check_next_isvalue(args, tag)
            struct.like = args.pop(0)
        elif tag in ('-o', '--output'):
            struct.output = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.output.append(val)
        elif tag in ('-t', '--transform'):
            if not cli.next_isvalue(args):
                struct.transform = ['{dir}{sep}{base}.crop.lta']
            else:
                struct.transform = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.transform.append(val)
        elif tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct
Exemple #8
0
def parse(args):
    struct = Pad()

    struct.files = []
    while cli.next_isvalue(args):
        val, *args = args
        struct.files.append(val)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-s', '--size'):
            struct.size = []
            while cli.next_isvalue(args):
                val, *args = args
                if val.lower() in ('vox', 'ras'):
                    struct.size_space = val
                elif '(' in val or '[' in val:
                    bracket = ')' if '(' in val else ']'
                    while cli.next_isvalue(args):
                        tmp, *args = args
                        val += tmp
                        if bracket in tmp:
                            val = ast.literal_eval(val)
                            struct.size.append(val)
                            break
                else:
                    struct.size.append(float(val))
        elif tag in ('-k', '--like'):
            cli.check_next_isvalue(args, tag)
            struct.like = args.pop(0)
        elif tag in ('-b', '--bound'):
            cli.check_next_isvalue(args, tag)
            struct.bound = args.pop(0)
            if struct.bound not in ('zero', 'dft', 'dct', 'dct2', 'replicate'):
                struct.bound = float(struct.bound)
            if struct.bound == 'zero':
                struct.bound = 0.
        elif tag in ('-o', '--output'):
            struct.output = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.output.append(val)
        elif tag in ('-t', '--transform'):
            if not cli.next_isvalue(args):
                struct.transform = ['{dir}{sep}{base}.crop.lta']
            else:
                struct.transform = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.transform.append(val)
        elif tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct
Exemple #9
0
def parse(args):
    """Parse AutoGrad's command-line arguments"""

    # This is the object that we will populate
    options = Composer()

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args

        # Parse transforms
        if tag in (*trf, *itrf):
            args = parse_transform([tag, *args], options)

        # Help -> return empty option
        elif tag in ('-h', '--help'):
            print(help)
            return {}

        # Parse remaining top-level tags
        elif tag in ('-t', '--target'):
            cli.check_next_isvalue(args, tag)
            options.target, *args = args
        elif tag in ('-o', '--output'):
            cli.check_next_isvalue(args, tag)
            options.output, *args = args
        elif tag in ('-ou', '--output-unit'):
            cli.check_next_isvalue(args, tag)
            options.output_unit, *args = args
        elif tag in ('-cpu', '--cpu'):
            options.device = 'cpu'
        elif tag in ('-gpu', '--gpu'):
            options.device = 'cuda'
            if cli.next_isvalue(args):
                gpu, *args = args
                options.device = 'cuda:{:d}'.format(int(gpu))

        # Something went wrong
        else:
            raise ParseError(f'Unknown tag {tag}')

    return options
Exemple #10
0
def parse(args):
    """

    Parameters
    ----------
    args

    Returns
    -------

    """

    struct = Patch()

    cli.check_next_isvalue(args)
    struct.file = args.pop(0)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-n', '--size'):
            struct.size = []
            while cli.next_isvalue(args):
                struct.size.append(int(args.pop(0)))
        elif tag in ('-s', '--stride'):
            struct.stride = []
            while cli.next_isvalue(args):
                struct.stride.append(int(args.pop(0)))
        elif tag in ('-o', '--output'):
            struct.output = []
            while cli.next_isvalue(args):
                struct.output.append(args.pop(0))
        elif tag in ('t', '--transform'):
            if not cli.next_isvalue(args):
                struct.transform = ['{dir}{sep}{base}_to_{i}_{j}_{k}.lta']
            else:
                struct.transform = []
            while cli.next_isvalue(args):
                struct.transform.append(args.pop(0))
        elif tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct
Exemple #11
0
def parse_transform(args, options):

    tag, *args = args

    # is it an inversed transform?
    inv = False
    if tag in itrf:
        inv = True
        if tag.startswith('--'):
            tag = tag.split('--inverse')[0]
        else:
            tag = '-' + tag[2:]

    opt = (struct.Linear if tag in lin else struct.Displacement
           if tag in disp else struct.Velocity if tag in vel else None)
    opt = opt()
    opt.inv = inv

    cli.check_next_isvalue(args, tag)
    opt.file, *args = args
    if isinstance(opt, struct.Velocity) and cli.next_isvalue(args):
        opt.json, *args = args

    while cli.next_istag(args):
        tag, *args = args
        if isinstance(opt, struct.Displacement) and tag in ('-n', '--order'):
            cli.check_next_isvalue(args, tag)
            opt.order, *args = args
            opt.order = int(opt.order)
        if isinstance(opt, struct.Displacement) and tag in ('-u', '--unit'):
            cli.check_next_isvalue(args, tag)
            opt.unit, *args = args
        else:
            args = [tag, *args]
            break

    options.transformations.append(opt)

    return args
Exemple #12
0
def parse(args):
    """Parse the command-line arguments of the `nipool` command.

    Parameters
    ----------
    args : list of str
        List of arguments, without the command name.

    Returns
    -------
    Pool
        Filled structure

    """

    struct = Pool()

    struct.files = []
    while cli.next_isvalue(args):
        val, *args = args
        struct.files.append(val)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-w', '--window'):
            struct.window = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.window.append(int(val))
        elif tag in ('-s', '--stride'):
            struct.stride = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.stride.append(int(val))
        elif tag in ('-m', '--method'):
            cli.check_next_isvalue(args, tag)
            struct.method, *args = args
        elif tag in ('-cpu', '--cpu'):
            struct.device = 'cpu'
        elif tag in ('-gpu', '--gpu'):
            struct.device = 'cuda'
            if cli.next_isvalue(args):
                gpu, *args = args
                struct.device = 'cuda:{:d}'.format(int(gpu))
        elif tag in ('-d', '--dim', '--dimension'):
            cli.check_next_isvalue(args, tag)
            struct.dim, *args = args
        elif tag in ('-o', '--output'):
            struct.output = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.output.append(val)
        elif tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct
Exemple #13
0
def parse(args):
    """Parse AutoGrad's command-line arguments"""

    # This is the object that we will populate
    options = struct.Reslicer()

    # Get input files
    while cli.next_isvalue(args):
        val, *args = args
        options.files.append(val)

    while args:
        if not cli.next_istag(args):
            break
        tag, *args = args

        # Parse transforms
        if tag in (*trf, *itrf):
            args = parse_transform([tag, *args], options)

        # Help -> return empty option
        elif tag in ('-h', '--help'):
            print(help)
            return {}

        # Parse remaining top-level tags
        elif tag in ('-t', '--target'):
            cli.check_next_isvalue(args, tag)
            options.target, *args = args
        elif tag in ('-o', '--output'):
            options.output = []
            while cli.next_isvalue(args):
                val, *args = args
                options.output.append(val)
        elif tag in ('-i', '-inter', '--interpolation'):
            cli.check_next_isvalue(args, tag)
            options.interpolation, *args = args
            options.interpolation = int(options.interpolation)
        elif tag in ('-b', '-bnd', '--bound'):
            cli.check_next_isvalue(args, tag)
            options.bound, *args = args
        elif tag in ('-p', '--prefilter'):
            cli.check_next_isvalue(args, tag)
            options.prefilter, *args = args
            options.prefilter = (True if options.prefilter.lower()[0] in 'ty'
                                 else False if options.prefilter.lower()[0]
                                 in 'fn' else bool(int(options.prefilter)))
        elif tag in ('-x', '-ex', '--extrapolate'):
            options.extrapolate = False
        elif tag in ('-v', '-vx', '--voxel-size'):
            options.voxel_size = []
            while cli.next_isvalue(args):
                val, *args = args
                options.voxel_size.append(float(val))
        elif tag in ('-cpu', '--cpu'):
            options.device = 'cpu'
        elif tag in ('-gpu', '--gpu'):
            options.device = 'cuda'
            if cli.next_isvalue(args):
                gpu, *args = args
                options.device = 'cuda:{:d}'.format(int(gpu))

        # Something went wrong
        else:
            print(help)
            raise cli.ParseError(f'Argument {tag} does not seem to '
                                 f'belong to a group')

    return options
Exemple #14
0
def parse(args):
    """Parse the command-line arguments of the `inpaint` command.

    Parameters
    ----------
    args : list of str
        List of arguments, without the command name.

    Returns
    -------
    InPaint
        Filled structure

    """

    struct = InPaint()

    struct.files = []
    while cli.next_isvalue(args):
        val, *args = args
        struct.files.append(val)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-m', '--missing'):
            struct.missing = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.missing.append(float(val))
        elif tag in ('-nrls', '--max-rls'):
            cli.check_next_isvalue(args, tag)
            struct.max_rls, *args = args
            struct.max_rls = int(struct.max_rls)
        elif tag in ('-trls', '--tol-rls'):
            cli.check_next_isvalue(args, tag)
            struct.tol_rls, *args = args
            struct.tol_rls = float(struct.tol_rls)
        elif tag in ('-ncg', '--max-cg'):
            cli.check_next_isvalue(args, tag)
            struct.max_cg, *args = args
            struct.max_cg = int(struct.max_cg)
        elif tag in ('-tcg', '--tol-cg'):
            cli.check_next_isvalue(args, tag)
            struct.tol_cg, *args = args
            struct.tol_cg = float(struct.tol_cg)
        elif tag in ('-cpu', '--cpu'):
            struct.device = 'cpu'
        elif tag in ('-gpu', '--gpu'):
            struct.device = 'cuda'
            if cli.next_isvalue(args):
                gpu, *args = args
                struct.device = 'cuda:{:d}'.format(int(gpu))
        elif tag in ('-o', '--output'):
            struct.output = []
            while cli.next_isvalue(args):
                val, *args = args
                struct.output.append(val)
        elif tag in ('-v', '--verbose'):
            struct.verbose = 1
            if cli.next_isvalue(args):
                struct.verbose, *args = args
                struct.verbose = int(struct.verbose)
        elif tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct
Exemple #15
0
def parse(args):
    """Parse the command-line arguments of the `denoise_mri` command.

    Parameters
    ----------
    args : list of str
        List of arguments, without the command name.

    Returns
    -------
    DenoiseMRI
        Filled structure

    """

    struct = DenoiseMRI()

    struct.files = []
    while cli.next_isvalue(args):
        val, *args = args
        struct.files.append(val)

    while args:
        if cli.next_isvalue(args):
            raise ParseError(f'Value {args[0]} does not seem to belong '
                             f'to a tag.')
        tag, *args = args
        if tag in ('-s', '--lam_scl'):
            cli.check_next_isvalue(args, tag)
            struct.lam_scl, *args = args
            struct.lam_scl = float(struct.lam_scl)
        elif tag in ('-lr', '--learning_rate'):
            cli.check_next_isvalue(args, tag)
            struct.learning_rate, *args = args
            struct.learning_rate = float(struct.learning_rate)
        elif tag in ('-n', '--max_iter'):
            cli.check_next_isvalue(args, tag)
            struct.max_iter, *args = args
            struct.max_iter = int(struct.max_iter)
        elif tag in ('-t', '--tolerance'):
            cli.check_next_isvalue(args, tag)
            struct.tolerance, *args = args
            struct.tolerance = float(struct.tolerance)
        elif tag in ('-cpu', '--cpu'):
            struct.device = 'cpu'
        elif tag in ('-gpu', '--gpu'):
            struct.device = 'cuda'
            if cli.next_isvalue(args):
                gpu, *args = args
                struct.device = 'cuda:{:d}'.format(int(gpu))
        elif tag in ('-o', '--dir_out'):
            cli.check_next_isvalue(args, tag)
            struct.dir_out, *args = args
            struct.dir_out = str(struct.dir_out)
        elif tag in ('-v', '--verbose'):
            cli.check_next_isvalue(args, tag)
            struct.verbose, *args = args
            struct.verbose = bool(struct.verbose)
        elif tag in ('-h', '--help'):
            print(help)
            return None
        else:
            raise ParseError(f'Unknown tag {tag}')

    return struct