# Path: deepcpg/utils.py
# EPS = 10e-8
# 
# 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


import numpy as np
from ..utils import EPS, get_from_module
def var(x, *args, **kwargs):
    """Variance between cells."""
    if x.ndim > 2:
        x = x.mean(axis=2)
    return x.var(axis=1)


def cat_var(x, nb_bin=3, *args, **kwargs):
    """Categorical variance between cells.

    Discretizes variance from :func:`var` into `nb_bin` equally-spaced bins.
    """
    v = var(x, *args, **kwargs)
    bins = np.linspace(-EPS, 0.25, nb_bin + 1)
    cv = np.digitize(v, bins, right=True) - 1
    return np.ma.masked_array(cv, v.mask)


def cat2_var(*args, **kwargs):
    """Binary variance between cells."""
    cv = cat_var(*args, **kwargs)
    cv[cv > 0] = 1
    return cv


def entropy(x):
    """Entropy of single CpG sites between cells."""
    if x.ndim > 2:
        x = x.mean(axis=2)
    p1 = x.mean(axis=1)

# Path: deepcpg/utils.py
# EPS = 10e-8
# 
# 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/data/stats.py
import numpy as np
from ..utils import EPS, get_from_module

def var(x, *args, **kwargs):
    """Variance between cells."""
    if x.ndim > 2:
        x = x.mean(axis=2)
    return x.var(axis=1)


def cat_var(x, nb_bin=3, *args, **kwargs):
    """Categorical variance between cells.

    Discretizes variance from :func:`var` into `nb_bin` equally-spaced bins.
    """
    v = var(x, *args, **kwargs)
    bins = np.linspace(-EPS, 0.25, nb_bin + 1)
    cv = np.digitize(v, bins, right=True) - 1
    return np.ma.masked_array(cv, v.mask)


def cat2_var(*args, **kwargs):
    """Binary variance between cells."""
    cv = cat_var(*args, **kwargs)
    cv[cv > 0] = 1
    return cv


def entropy(x):
    """Entropy of single CpG sites between cells."""
    if x.ndim > 2:
        x = x.mean(axis=2)
    p1 = x.mean(axis=1)

    p1 = np.minimum(1 - EPS, np.maximum(EPS, p1))