# Path: cotede/anomaly_detection/anomaly_detection.py
# def estimate_anomaly(features, params, method='produtorium'):
#     """ Estimate probability from PDF defined by params
# 
#         The output is the natural logarithm of the estimated probability.
# 
#         params are the parameters that define the PDF for each feature
#           in features. This function estimate the combined probability of
#           each row in features as the produtorium between the probabilities
#           of the different features on the same row.
# 
#         ATENTION!! I should think more about what would I like from this
#           function. What should happens in case of a masked feature? And
#           if all features for one measurement are masked? Right now it
#           simply don't add for the estimate, so that all features masked
#           would lead to an expectation of 100% it's good.
#     """
#     assert hasattr(params, 'keys')
#     assert hasattr(features, 'keys')
# 
#     features_names = list(features.keys())
#     for k in params.keys():
#         assert k in features_names, "features doesn't have: %s" % k
# 
#     prob = ma.masked_all(np.shape(features[features_names[0]]), dtype='f8')
# 
#     for t in params.keys():
#         param = params[t]['param']
#         valid = ~ma.fix_invalid(features[t]).mask
# 
#         tmp = exponweib.sf(np.asanyarray(features[t]),
#                 *param[:-2], loc=param[-2], scale=param[-1])
#         # Arbitrary solution. No value can have a probability of 0.
#         tmp[tmp == 0] = 1e-25
#         p = ma.log(tmp)
# 
#         # If both are valid, operate as choosed method.
#         ind = ~prob.mask & valid
#         if method == 'produtorium':
#             prob[ind] = prob[ind] + p[ind]
#         elif method == 'min':
#             prob[ind] = min(prob[ind], p[ind])
#         else:
#             assert "Invalid method: %s" % method
# 
#         # Update prob if new value is valid and prob is masked
#         # Operate twice the first feature if moved above.
#         ind = prob.mask & valid
#         prob[ind] = p[ind]
# 
#     return prob


import logging
import numpy as np
from cotede.anomaly_detection import estimate_anomaly
# -*- coding: utf-8 -*-

"""
    Quality Control based on anomaly detection
"""


#from numpy import ma


module_logger = logging.getLogger(__name__)

def anomaly_detection(features, cfg):
    """

        Must decide where to set the flags.
    """


# Path: cotede/anomaly_detection/anomaly_detection.py
# def estimate_anomaly(features, params, method='produtorium'):
#     """ Estimate probability from PDF defined by params
# 
#         The output is the natural logarithm of the estimated probability.
# 
#         params are the parameters that define the PDF for each feature
#           in features. This function estimate the combined probability of
#           each row in features as the produtorium between the probabilities
#           of the different features on the same row.
# 
#         ATENTION!! I should think more about what would I like from this
#           function. What should happens in case of a masked feature? And
#           if all features for one measurement are masked? Right now it
#           simply don't add for the estimate, so that all features masked
#           would lead to an expectation of 100% it's good.
#     """
#     assert hasattr(params, 'keys')
#     assert hasattr(features, 'keys')
# 
#     features_names = list(features.keys())
#     for k in params.keys():
#         assert k in features_names, "features doesn't have: %s" % k
# 
#     prob = ma.masked_all(np.shape(features[features_names[0]]), dtype='f8')
# 
#     for t in params.keys():
#         param = params[t]['param']
#         valid = ~ma.fix_invalid(features[t]).mask
# 
#         tmp = exponweib.sf(np.asanyarray(features[t]),
#                 *param[:-2], loc=param[-2], scale=param[-1])
#         # Arbitrary solution. No value can have a probability of 0.
#         tmp[tmp == 0] = 1e-25
#         p = ma.log(tmp)
# 
#         # If both are valid, operate as choosed method.
#         ind = ~prob.mask & valid
#         if method == 'produtorium':
#             prob[ind] = prob[ind] + p[ind]
#         elif method == 'min':
#             prob[ind] = min(prob[ind], p[ind])
#         else:
#             assert "Invalid method: %s" % method
# 
#         # Update prob if new value is valid and prob is masked
#         # Operate twice the first feature if moved above.
#         ind = prob.mask & valid
#         prob[ind] = p[ind]
# 
#     return prob


# Path: cotede/qctests/anomaly_detection.py
import logging
import numpy as np
from cotede.anomaly_detection import estimate_anomaly

# -*- coding: utf-8 -*-

"""
    Quality Control based on anomaly detection
"""


#from numpy import ma


module_logger = logging.getLogger(__name__)

def anomaly_detection(features, cfg):
    """

        Must decide where to set the flags.
    """


    prob = estimate_anomaly(features, params = cfg['features'])