# Path: third_party/xiuminglib/xiuminglib/img.py
# def rgb2lum(im):
#     """Converts RGB to relative luminance (if input is linear RGB) or luma
#     (if input is gamma-corrected RGB).
# 
#     Args:
#         im (numpy.ndarray): RGB array of shape ``(..., 3)``.
# 
#     Returns:
#         numpy.ndarray: Relative luminance or luma array.
#     """
#     assert im.shape[-1] == 3, "Input's last dimension must hold RGB"
# 
#     lum = 0.2126 * im[..., 0] + 0.7152 * im[..., 1] + 0.0722 * im[..., 2]
# 
#     return lum
# 
# Path: third_party/xiuminglib/xiuminglib/os.py
# def open_file(path, mode):
#     """Opens a file.
# 
#     Supports Google Colossus if ``gfile`` can be imported.
# 
#     Args:
#         path (str): Path to open.
#         mode (str): ``'r'``, ``'rb'``, ``'w'``, or ``'wb'``.
# 
#     Returns:
#         File handle that can be used as a context.
#     """
#     gfile = preset_import('gfile')
# 
#     open_func = open if gfile is None else gfile.Open
#     handle = open_func(path, mode)
# 
#     return handle
# 
# Path: third_party/xiuminglib/xiuminglib/imprt.py
# def preset_import(name, assert_success=False):
#     """A unified importer for both regular and ``google3`` modules, according
#     to specified presets/profiles (e.g., ignoring ``ModuleNotFoundError``).
#     """
#     if name in ('cv2', 'opencv'):
#         try:
#             # BUILD dep:
#             # "//third_party/py/cvx2",
#             from cvx2 import latest as mod
#             # Or
#             # BUILD dep:
#             # "//third_party/OpenCVX:cvx2",
#             # from google3.third_party.OpenCVX import cvx2 as cv2
#         except ModuleNotFoundError:
#             mod = import_module_404ok('cv2')
# 
#     elif name in ('tf', 'tensorflow'):
#         mod = import_module_404ok('tensorflow')
# 
#     elif name == 'gfile':
#         # BUILD deps:
#         # "//pyglib:gfile",
#         # "//file/colossus/cns",
#         mod = import_module_404ok('google3.pyglib.gfile')
# 
#     elif name == 'video_api':
#         # BUILD deps:
#         # "//learning/deepmind/video/python:video_api",
#         mod = import_module_404ok(
#             'google3.learning.deepmind.video.python.video_api')
# 
#     elif name in ('bpy', 'bmesh', 'OpenEXR', 'Imath'):
#         # BUILD deps:
#         # "//third_party/py/Imath",
#         # "//third_party/py/OpenEXR",
#         mod = import_module_404ok(name)
# 
#     elif name in ('Vector', 'Matrix', 'Quaternion'):
#         mod = import_module_404ok('mathutils')
#         mod = _get_module_class(mod, name)
# 
#     elif name == 'BVHTree':
#         mod = import_module_404ok('mathutils.bvhtree')
#         mod = _get_module_class(mod, name)
# 
#     else:
#         raise NotImplementedError(name)
# 
#     if assert_success:
#         assert mod is not None, "Failed in importing '%s'" % name
# 
#     return mod
# 
# Path: third_party/xiuminglib/xiuminglib/log.py
# def get_logger(level=None):
#     """Creates a logger for functions in the library.
# 
#     Args:
#         level (str, optional): Logging level. Defaults to ``logging.INFO``.
# 
#     Returns:
#         logging.Logger: Logger created.
#     """
#     if level is None:
#         level = logging.INFO
#     logging.basicConfig(level=level)
#     logger = logging.getLogger()
#     return logger


import numpy as np
from .img import rgb2lum
from .const import Path
from .os import open_file
from .imprt import preset_import
from .log import get_logger
    from scipy import stats
class SSIM(Base):
    r"""The (multi-scale) Structural Similarity Index (SSIM) :math:`\in [0,1]`
    (higher is better).

    If the inputs are RGB, they are first converted to luma (or relative
    luminance, if the inputs are not gamma-corrected). SSIM is computed
    on the luma.
    """
    def __call__(self, im1, im2, multiscale=False):
        """
        Args:
            im1
            im2
            multiscale (bool, optional): Whether to compute MS-SSIM.

        Returns:
            float: SSIM computed (higher is better).
        """
        tf = preset_import('tf', assert_success=True)
        self._assert_type(im1)
        self._assert_type(im2)
        im1 = im1.astype(float) # must be cast to an unbounded type
        im2 = im2.astype(float)
        im1 = self._ensure_3d(im1)
        im2 = self._ensure_3d(im2)
        self._assert_same_shape(im1, im2)
        self._assert_drange(im1)
        self._assert_drange(im2)
        # To luma
        if im1.shape[2] == 3:

# Path: third_party/xiuminglib/xiuminglib/img.py
# def rgb2lum(im):
#     """Converts RGB to relative luminance (if input is linear RGB) or luma
#     (if input is gamma-corrected RGB).
# 
#     Args:
#         im (numpy.ndarray): RGB array of shape ``(..., 3)``.
# 
#     Returns:
#         numpy.ndarray: Relative luminance or luma array.
#     """
#     assert im.shape[-1] == 3, "Input's last dimension must hold RGB"
# 
#     lum = 0.2126 * im[..., 0] + 0.7152 * im[..., 1] + 0.0722 * im[..., 2]
# 
#     return lum
# 
# Path: third_party/xiuminglib/xiuminglib/os.py
# def open_file(path, mode):
#     """Opens a file.
# 
#     Supports Google Colossus if ``gfile`` can be imported.
# 
#     Args:
#         path (str): Path to open.
#         mode (str): ``'r'``, ``'rb'``, ``'w'``, or ``'wb'``.
# 
#     Returns:
#         File handle that can be used as a context.
#     """
#     gfile = preset_import('gfile')
# 
#     open_func = open if gfile is None else gfile.Open
#     handle = open_func(path, mode)
# 
#     return handle
# 
# Path: third_party/xiuminglib/xiuminglib/imprt.py
# def preset_import(name, assert_success=False):
#     """A unified importer for both regular and ``google3`` modules, according
#     to specified presets/profiles (e.g., ignoring ``ModuleNotFoundError``).
#     """
#     if name in ('cv2', 'opencv'):
#         try:
#             # BUILD dep:
#             # "//third_party/py/cvx2",
#             from cvx2 import latest as mod
#             # Or
#             # BUILD dep:
#             # "//third_party/OpenCVX:cvx2",
#             # from google3.third_party.OpenCVX import cvx2 as cv2
#         except ModuleNotFoundError:
#             mod = import_module_404ok('cv2')
# 
#     elif name in ('tf', 'tensorflow'):
#         mod = import_module_404ok('tensorflow')
# 
#     elif name == 'gfile':
#         # BUILD deps:
#         # "//pyglib:gfile",
#         # "//file/colossus/cns",
#         mod = import_module_404ok('google3.pyglib.gfile')
# 
#     elif name == 'video_api':
#         # BUILD deps:
#         # "//learning/deepmind/video/python:video_api",
#         mod = import_module_404ok(
#             'google3.learning.deepmind.video.python.video_api')
# 
#     elif name in ('bpy', 'bmesh', 'OpenEXR', 'Imath'):
#         # BUILD deps:
#         # "//third_party/py/Imath",
#         # "//third_party/py/OpenEXR",
#         mod = import_module_404ok(name)
# 
#     elif name in ('Vector', 'Matrix', 'Quaternion'):
#         mod = import_module_404ok('mathutils')
#         mod = _get_module_class(mod, name)
# 
#     elif name == 'BVHTree':
#         mod = import_module_404ok('mathutils.bvhtree')
#         mod = _get_module_class(mod, name)
# 
#     else:
#         raise NotImplementedError(name)
# 
#     if assert_success:
#         assert mod is not None, "Failed in importing '%s'" % name
# 
#     return mod
# 
# Path: third_party/xiuminglib/xiuminglib/log.py
# def get_logger(level=None):
#     """Creates a logger for functions in the library.
# 
#     Args:
#         level (str, optional): Logging level. Defaults to ``logging.INFO``.
# 
#     Returns:
#         logging.Logger: Logger created.
#     """
#     if level is None:
#         level = logging.INFO
#     logging.basicConfig(level=level)
#     logger = logging.getLogger()
#     return logger


# Path: third_party/xiuminglib/xiuminglib/metric.py
import numpy as np
from .img import rgb2lum
from .const import Path
from .os import open_file
from .imprt import preset_import
from .log import get_logger
    from scipy import stats

class SSIM(Base):
    r"""The (multi-scale) Structural Similarity Index (SSIM) :math:`\in [0,1]`
    (higher is better).

    If the inputs are RGB, they are first converted to luma (or relative
    luminance, if the inputs are not gamma-corrected). SSIM is computed
    on the luma.
    """
    def __call__(self, im1, im2, multiscale=False):
        """
        Args:
            im1
            im2
            multiscale (bool, optional): Whether to compute MS-SSIM.

        Returns:
            float: SSIM computed (higher is better).
        """
        tf = preset_import('tf', assert_success=True)
        self._assert_type(im1)
        self._assert_type(im2)
        im1 = im1.astype(float) # must be cast to an unbounded type
        im2 = im2.astype(float)
        im1 = self._ensure_3d(im1)
        im2 = self._ensure_3d(im2)
        self._assert_same_shape(im1, im2)
        self._assert_drange(im1)
        self._assert_drange(im2)
        # To luma
        if im1.shape[2] == 3:

            im1 = np.expand_dims(rgb2lum(im1), -1)