Esempio n. 1
0
def __resolve_config_file_path(cmdline_arg):
    """
    Search for the absolute file name of the configuration file.
    starting from `-c` value provided by the user.

    :param cmdline_arg:
    :return:
    """
    if not cmdline_arg:
        raise IOError("\nNo configuration file has been provided, did you "
                      "forget '-c' command argument?{}".format(EPILOG_STRING))
    # Resolve relative configuration file location
    config_file_path = os.path.expanduser(cmdline_arg)
    try:
        config_file_path = resolve_file_name(
            config_file_path, ('.', NIFTYNET_HOME))
        if os.path.isfile(config_file_path):
            return config_file_path
    except (IOError, TypeError):
        config_file_path = os.path.expanduser(cmdline_arg)

    config_file_path = os.path.join(
        NiftyNetGlobalConfig().get_default_examples_folder(),
        config_file_path, config_file_path + "_config.ini")
    if os.path.isfile(config_file_path):
        return config_file_path

    # could not proceed without a configuration file
    raise IOError("\nConfiguration file not found: {}.{}".format(
        os.path.expanduser(cmdline_arg), EPILOG_STRING))
def __resolve_config_file_path(cmdline_arg):
    """
    Search for the absolute file name of the configuration file.
    starting from `-c` value provided by the user.

    :param cmdline_arg:
    :return:
    """
    if not cmdline_arg:
        raise IOError("\nNo configuration file has been provided, did you "
                      "forget '-c' command argument?{}".format(EPILOG_STRING))
    # Resolve relative configuration file location
    config_file_path = os.path.expanduser(cmdline_arg)
    try:
        config_file_path = resolve_file_name(config_file_path,
                                             ('.', NIFTYNET_HOME))
        if os.path.isfile(config_file_path):
            return config_file_path
    except (IOError, TypeError):
        config_file_path = os.path.expanduser(cmdline_arg)

    config_file_path = os.path.join(
        NiftyNetGlobalConfig().get_default_examples_folder(), config_file_path,
        config_file_path + "_config.ini")
    if os.path.isfile(config_file_path):
        return config_file_path

    # could not proceed without a configuration file
    raise IOError("\nConfiguration file not found: {}.{}".format(
        os.path.expanduser(cmdline_arg), EPILOG_STRING))
    def create_instance(cls, file_path, **kwargs):
        """
        Read image headers and create image instance.

        :param file_path: a file path or a sequence of file paths
        :param kwargs: output properties for transforming the image data
            array into a desired format
        :return: an image instance
        """
        if file_path is None:
            tf.logging.fatal('No file_path provided, '
                             'please check input sources in config file')
            raise ValueError

        ndims = 0
        image_type = None
        home_folder = NiftyNetGlobalConfig().get_niftynet_home_folder()
        try:
            file_path = resolve_file_name(file_path, ('.', home_folder))
            if os.path.isfile(file_path):
                loader = kwargs.get('loader', None) or None
                ndims = misc.infer_ndims_from_file(file_path, loader)
                image_type = cls.INSTANCE_DICT.get(ndims, None)
        except (TypeError, IOError, AttributeError):
            pass

        if image_type is None:
            try:
                file_path = [
                    resolve_file_name(path, ('.', home_folder))
                    for path in file_path
                ]
                loader = kwargs.get('loader', None) or (None, )
                ndims = misc.infer_ndims_from_file(file_path[0], loader[0])
                ndims = ndims + (1 if len(file_path) > 1 else 0)
                image_type = cls.INSTANCE_DICT.get(ndims, None)
            except (AssertionError, TypeError, IOError, AttributeError):
                tf.logging.fatal('Could not load file: %s', file_path)
                raise IOError
        if image_type is None:
            tf.logging.fatal('Not supported image type from:\n%s', file_path)
            raise NotImplementedError(
                "unrecognised spatial rank {}".format(ndims))
        return image_type(file_path, **kwargs)
Esempio n. 4
0
    def create_instance(cls, file_path, **kwargs):
        """
        Read image headers and create image instance.

        :param file_path: a file path or a sequence of file paths
        :param kwargs: output properties for transforming the image data
            array into a desired format
        :return: an image instance
        """
        if file_path is None:
            tf.logging.fatal('No file_path provided, '
                             'please check input sources in config file')
            raise ValueError

        ndims = 0
        image_type = None
        home_folder = NiftyNetGlobalConfig().get_niftynet_home_folder()
        try:
            file_path = resolve_file_name(file_path, ('.', home_folder))
            if os.path.isfile(file_path):
                loader = kwargs.get('loader', None) or None
                ndims = misc.infer_ndims_from_file(file_path, loader)
                image_type = cls.INSTANCE_DICT.get(ndims, None)
        except (TypeError, IOError, AttributeError):
            pass

        if image_type is None:
            try:
                file_path = [resolve_file_name(path, ('.', home_folder))
                             for path in file_path]
                loader = kwargs.get('loader', None) or (None,)
                ndims = misc.infer_ndims_from_file(file_path[0], loader[0])
                ndims = ndims + (1 if len(file_path) > 1 else 0)
                image_type = cls.INSTANCE_DICT.get(ndims, None)
            except (AssertionError, TypeError, IOError, AttributeError):
                tf.logging.fatal('Could not load file: %s', file_path)
                raise IOError
        if image_type is None:
            tf.logging.fatal('Not supported image type from:\n%s', file_path)
            raise NotImplementedError(
                "unrecognised spatial rank {}".format(ndims))
        return image_type(file_path, **kwargs)
Esempio n. 5
0
 def file_path(self, path_array):
     if isinstance(path_array, string_types):
         path_array = (path_array,)
     home_folder = NiftyNetGlobalConfig().get_niftynet_home_folder()
     try:
         self._file_path = tuple(resolve_file_name(path, ('.', home_folder))
                                 for path in path_array)
     except (TypeError, AssertionError, AttributeError, IOError):
         tf.logging.fatal(
             "unrecognised file path format, should be a valid filename,"
             "or a sequence of filenames %s", path_array)
         raise IOError
Esempio n. 6
0
 def file_path(self, path_array):
     if isinstance(path_array, string_types):
         path_array = (path_array,)
     home_folder = NiftyNetGlobalConfig().get_niftynet_home_folder()
     try:
         self._file_path = tuple(resolve_file_name(path, ('.', home_folder))
                                 for path in path_array)
     except (TypeError, AssertionError, AttributeError, IOError):
         tf.logging.fatal(
             "unrecognised file path format, should be a valid filename,"
             "or a sequence of filenames %s", path_array)
         raise IOError
def run():
    """
    meta_parser is first used to find out location
    of the configuration file. Based on the application_name
    or meta_parser.prog name, the section parsers are organised
    to find system parameters and application specific
    parameters.

    :return: system parameters is a group of parameters including
        SYSTEM_SECTIONS and app_module.REQUIRED_CONFIG_SECTION
        input_data_args is a group of input data sources to be
        used by niftynet.io.ImageReader
    """
    meta_parser = argparse.ArgumentParser(
        description="Launch a NiftyNet application.",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=textwrap.dedent(EPILOG_STRING))
    version_string = get_niftynet_version_string()
    meta_parser.add_argument("action",
                             help="train networks, run inferences "
                             "or evaluate inferences",
                             metavar='ACTION',
                             choices=list(ACTIONS))
    meta_parser.add_argument("-v",
                             "--version",
                             action='version',
                             version=version_string)
    meta_parser.add_argument("-c",
                             "--conf",
                             help="specify configurations from a file",
                             metavar="CONFIG_FILE")
    meta_parser.add_argument("-a",
                             "--application_name",
                             help="specify an application module name",
                             metavar='APPLICATION_NAME',
                             default="")

    meta_args, args_from_cmdline = meta_parser.parse_known_args()
    print(version_string)

    # read configurations, to be parsed by sections
    config_file_name = __resolve_config_file_path(meta_args.conf)
    config = NiftyNetLaunchConfig()
    config.read([config_file_name])

    # infer application name from command
    app_name = None
    try:
        parser_prog = meta_parser.prog.replace('.py', '')
        app_name = parser_prog if parser_prog in SUPPORTED_APP \
            else meta_args.application_name
        assert app_name
    except (AttributeError, AssertionError):
        raise ValueError("\nUnknown application {}, or did you forget '-a' "
                         "command argument?{}".format(app_name, EPILOG_STRING))

    # load application by name
    app_module = ApplicationFactory.create(app_name)
    try:
        assert app_module.REQUIRED_CONFIG_SECTION, \
            "\nREQUIRED_CONFIG_SECTION should be static variable " \
            "in {}".format(app_module)
        has_section_in_config(config, app_module.REQUIRED_CONFIG_SECTION)
    except AttributeError:
        raise AttributeError(
            "Application code doesn't have REQUIRED_CONFIG_SECTION property. "
            "{} should be an instance of "
            "niftynet.application.base_application".format(app_module))
    except ValueError:
        raise ValueError(
            "\n{} requires [{}] section in the config file.{}".format(
                app_name, app_module.REQUIRED_CONFIG_SECTION, EPILOG_STRING))

    # check keywords in configuration file
    _check_config_file_keywords(config)

    # using configuration as default, and parsing all command line arguments
    # command line args override the configure file options
    all_args = {}
    for section in config.sections():
        # try to rename user-specified sections for consistency
        section = standardise_section_name(config, section)
        section_defaults = dict(config.items(section))
        section_args, args_from_cmdline = \
            _parse_arguments_by_section([],
                                        section,
                                        section_defaults,
                                        args_from_cmdline,
                                        app_module.REQUIRED_CONFIG_SECTION)
        all_args[section] = section_args

    # check if any args from command line not recognised
    _check_cmd_remaining_keywords(list(args_from_cmdline))

    # split parsed results in all_args
    # into dictionaries of system_args and input_data_args
    system_args, input_data_args = {}, {}
    for section in all_args:

        # copy system default sections to ``system_args``
        if section in SYSTEM_SECTIONS:
            system_args[section] = all_args[section]
            continue

        # copy application specific sections to ``system_args``
        if section == app_module.REQUIRED_CONFIG_SECTION:
            system_args['CUSTOM'] = all_args[section]
            vars(system_args['CUSTOM'])['name'] = app_name
            continue

        # copy non-default sections to ``input_data_args``
        input_data_args[section] = all_args[section]

        # set the output path of csv list if not exists
        try:
            csv_path = resolve_file_name(
                input_data_args[section].csv_file,
                (os.path.dirname(config_file_name), NIFTYNET_HOME))
            input_data_args[section].csv_file = csv_path
            # don't search files if csv specified in config
            try:
                delattr(input_data_args[section], 'path_to_search')
            except AttributeError:
                pass
        except (IOError, TypeError):
            input_data_args[section].csv_file = ''

    # preserve ``config_file`` and ``action parameter`` from the meta_args
    system_args['CONFIG_FILE'] = argparse.Namespace(path=config_file_name)
    # mapping the captured action argument to a string in ACTIONS
    system_args['SYSTEM'].action = \
        look_up_operations(meta_args.action, ACTIONS)
    if not system_args['SYSTEM'].model_dir:
        system_args['SYSTEM'].model_dir = os.path.join(
            os.path.dirname(config_file_name), 'model')
    return system_args, input_data_args
Esempio n. 8
0
def run():
    """
    meta_parser is first used to find out location
    of the configuration file. Based on the application_name
    or meta_parser.prog name, the section parsers are organised
    to find system parameters and application specific
    parameters.

    :return: system parameters is a group of parameters including
        SYSTEM_SECTIONS and app_module.REQUIRED_CONFIG_SECTION
        input_data_args is a group of input data sources to be
        used by niftynet.io.ImageReader
    """
    meta_parser = argparse.ArgumentParser(
        description="Launch a NiftyNet application.",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=textwrap.dedent(EPILOG_STRING))
    version_string = get_niftynet_version_string()
    meta_parser.add_argument("action",
                             help="train networks, run inferences "
                                  "or evaluate inferences",
                             metavar='ACTION',
                             choices=['train', 'inference', 'evaluation'])
    meta_parser.add_argument("-v", "--version",
                             action='version',
                             version=version_string)
    meta_parser.add_argument("-c", "--conf",
                             help="specify configurations from a file",
                             metavar="CONFIG_FILE")
    meta_parser.add_argument("-a", "--application_name",
                             help="specify an application module name",
                             metavar='APPLICATION_NAME',
                             default="")

    meta_args, args_from_cmdline = meta_parser.parse_known_args()
    print(version_string)

    # read configurations, to be parsed by sections
    config_file_name = __resolve_config_file_path(meta_args.conf)
    config = configparser.ConfigParser()
    config.read([config_file_name])

    # infer application name from command
    app_name = None
    try:
        parser_prog = meta_parser.prog.replace('.py', '')
        app_name = parser_prog if parser_prog in SUPPORTED_APP \
            else meta_args.application_name
        assert app_name
    except (AttributeError, AssertionError):
        raise ValueError(
            "\nUnknown application {}, or did you forget '-a' "
            "command argument?{}".format(app_name, EPILOG_STRING))

    # load application by name
    app_module = ApplicationFactory.create(app_name)
    try:
        assert app_module.REQUIRED_CONFIG_SECTION, \
            "\nREQUIRED_CONFIG_SECTION should be static variable " \
            "in {}".format(app_module)
        has_section_in_config(config, app_module.REQUIRED_CONFIG_SECTION)
    except AttributeError:
        raise AttributeError(
            "Application code doesn't have REQUIRED_CONFIG_SECTION property. "
            "{} should be an instance of "
            "niftynet.application.base_application".format(app_module))
    except ValueError:
        raise ValueError(
            "\n{} requires [{}] section in the config file.{}".format(
                app_name, app_module.REQUIRED_CONFIG_SECTION, EPILOG_STRING))

    # check keywords in configuration file
    _check_config_file_keywords(config)

    # using configuration as default, and parsing all command line arguments
    # command line args override the configure file options
    all_args = {}
    for section in config.sections():
        # try to rename user-specified sections for consistency
        section = standardise_section_name(config, section)
        section_defaults = dict(config.items(section))
        section_args, args_from_cmdline = \
            _parse_arguments_by_section([],
                                        section,
                                        section_defaults,
                                        args_from_cmdline,
                                        app_module.REQUIRED_CONFIG_SECTION)
        all_args[section] = section_args

    # check if any args from command line not recognised
    _check_cmd_remaining_keywords(list(args_from_cmdline))

    # split parsed results in all_args
    # into dictionaries of system_args and input_data_args
    system_args, input_data_args = {}, {}
    for section in all_args:

        # copy system default sections to ``system_args``
        if section in SYSTEM_SECTIONS:
            system_args[section] = all_args[section]
            continue

        # copy application specific sections to ``system_args``
        if section == app_module.REQUIRED_CONFIG_SECTION:
            system_args['CUSTOM'] = all_args[section]
            vars(system_args['CUSTOM'])['name'] = app_name
            continue

        # copy non-default sections to ``input_data_args``
        input_data_args[section] = all_args[section]

        # set the output path of csv list if not exists
        try:
            csv_path = resolve_file_name(
                input_data_args[section].csv_file,
                (os.path.dirname(config_file_name), NIFTYNET_HOME))
            input_data_args[section].csv_file = csv_path
            # don't search files if csv specified in config
            try:
                delattr(input_data_args[section], 'path_to_search')
            except AttributeError:
                pass
        except (IOError, TypeError):
            input_data_args[section].csv_file = ''

    # preserve ``config_file`` and ``action parameter`` from the meta_args
    system_args['CONFIG_FILE'] = argparse.Namespace(path=config_file_name)
    system_args['SYSTEM'].action = meta_args.action
    if not system_args['SYSTEM'].model_dir:
        system_args['SYSTEM'].model_dir = os.path.join(
            os.path.dirname(config_file_name), 'model')
    return system_args, input_data_args