# Path: deepcpg/utils.py
# def get_from_module(identifier, module_params, ignore_case=True):
#     """Return object from module.
# 
#     Return object with name `identifier` from module with items `module_params`.
# 
#     Parameters
#     ----------
#     identifier: str
#         Name of object, e.g. a function, in module.
#     module_params: dict
#         `dict` of items in module, e.g. `globals()`
#     ignore_case: bool
#         If `True`, ignore case of `identifier`.
# 
#     Returns
#     -------
#     object
#         Object with name `identifier` in module, e.g. a function or class.
#     """
#     if ignore_case:
#         _module_params = dict()
#         for key, value in six.iteritems(module_params):
#             _module_params[key.lower()] = value
#         _identifier = identifier.lower()
#     else:
#         _module_params = module_params
#         _identifier = identifier
#     item = _module_params.get(_identifier)
#     if not item:
#         raise ValueError('Invalid identifier "%s"!' % identifier)
#     return item


from keras import backend as K
from .utils import get_from_module
from .data import CPG_NAN
def _cat_sample_weights(y, mask=None):
    return 1 - K.cast(K.equal(K.sum(y, axis=-1), 0), K.floatx())


def cat_acc(y, z):
    """Compute categorical accuracy given one-hot matrices."""
    weights = _cat_sample_weights(y)
    _acc = K.cast(K.equal(K.argmax(y, axis=-1),
                          K.argmax(z, axis=-1)),
                  K.floatx())
    _acc = K.sum(_acc * weights) / K.sum(weights)
    return _acc


def mse(y, z, mask=CPG_NAN):
    """Compute mean squared error."""
    weights = _sample_weights(y, mask)
    _mse = K.sum(K.square(y - z) * weights) / K.sum(weights)
    return _mse


def mae(y, z, mask=CPG_NAN):
    """Compute mean absolute deviation."""
    weights = _sample_weights(y, mask)
    _mae = K.sum(K.abs(y - z) * weights) / K.sum(weights)
    return _mae


def get(name):
    """Return object from module by its name."""

# Path: deepcpg/utils.py
# def get_from_module(identifier, module_params, ignore_case=True):
#     """Return object from module.
# 
#     Return object with name `identifier` from module with items `module_params`.
# 
#     Parameters
#     ----------
#     identifier: str
#         Name of object, e.g. a function, in module.
#     module_params: dict
#         `dict` of items in module, e.g. `globals()`
#     ignore_case: bool
#         If `True`, ignore case of `identifier`.
# 
#     Returns
#     -------
#     object
#         Object with name `identifier` in module, e.g. a function or class.
#     """
#     if ignore_case:
#         _module_params = dict()
#         for key, value in six.iteritems(module_params):
#             _module_params[key.lower()] = value
#         _identifier = identifier.lower()
#     else:
#         _module_params = module_params
#         _identifier = identifier
#     item = _module_params.get(_identifier)
#     if not item:
#         raise ValueError('Invalid identifier "%s"!' % identifier)
#     return item


# Path: deepcpg/metrics.py
from keras import backend as K
from .utils import get_from_module
from .data import CPG_NAN

def _cat_sample_weights(y, mask=None):
    return 1 - K.cast(K.equal(K.sum(y, axis=-1), 0), K.floatx())


def cat_acc(y, z):
    """Compute categorical accuracy given one-hot matrices."""
    weights = _cat_sample_weights(y)
    _acc = K.cast(K.equal(K.argmax(y, axis=-1),
                          K.argmax(z, axis=-1)),
                  K.floatx())
    _acc = K.sum(_acc * weights) / K.sum(weights)
    return _acc


def mse(y, z, mask=CPG_NAN):
    """Compute mean squared error."""
    weights = _sample_weights(y, mask)
    _mse = K.sum(K.square(y - z) * weights) / K.sum(weights)
    return _mse


def mae(y, z, mask=CPG_NAN):
    """Compute mean absolute deviation."""
    weights = _sample_weights(y, mask)
    _mae = K.sum(K.abs(y - z) * weights) / K.sum(weights)
    return _mae


def get(name):
    """Return object from module by its name."""

    return get_from_module(name, globals())