Esempio n. 1
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
        image_type = None
        try:
            if os.path.isfile(file_path):
                ndims = misc.infer_ndims_from_file(file_path)
                image_type = cls.INSTANCE_DICT.get(ndims, None)
        except TypeError:
            pass
        if image_type is None:
            try:
                assert all([os.path.isfile(path) for path in file_path])
                ndims = misc.infer_ndims_from_file(file_path[0])
                ndims = ndims + (1 if len(file_path) > 1 else 0)
                image_type = cls.INSTANCE_DICT.get(ndims, None)
            except AssertionError:
                tf.logging.fatal('Could not load file: %s', file_path)
                raise IOError
        if image_type is None:
            tf.logging.fatal('Not supported image type: %s', file_path)
            raise NotImplementedError
        return image_type(file_path, **kwargs)
    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. 3
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)