# Path: cotede/qctests/fuzzylogic.py
# def fuzzylogic(features, cfg, require="all"):
#     """
# 
#         FIXME: Think about, should I return 0, or have an assert, and at qc.py
#           all qc tests are applied with a try, and in case it fails it flag
#           0s.
# 
#     """
#     require = cfg.get("require", require)
# 
#     if (require == "all") and not np.all([f in features for f in cfg["features"]]):
#         module_logger.warning(
#             "Not all features (%s) required by fuzzy logic are available".format(
#                 cfg["features"].keys()
#             )
#         )
#         raise KeyError
# 
#     uncertainty = fuzzy_uncertainty(
#         data=features, features=cfg["features"], output=cfg["output"], require=require
#     )
# 
#     return uncertainty
# 
# class FuzzyLogic(QCCheckVar):
#     def set_features(self):
#         self.features = {}
#         for v in [f for f in self.cfg["features"] if f not in self.features]:
#             if v == "woa_bias":
#                 woa_comparison = woa_normbias(self.data, self.varname, self.attrs)
#                 self.features[v] = woa_comparison["woa_bias"]
#             elif v == "woa_normbias":
#                 woa_comparison = woa_normbias(self.data, self.varname, self.attrs)
#                 self.features[v] = woa_comparison["woa_normbias"]
#             elif v == "spike":
#                 self.features[v] = spike(self.data[self.varname])
#             elif v == "gradient":
#                 self.features[v] = gradient(self.data[self.varname])
# 
#         self.features["fuzzylogic"] = fuzzylogic(self.features, self.cfg)
# 
# 
#     def test(self):
#         self.flags = {}
#         cfg = self.cfg
#         flag = np.zeros(np.shape(self.data[self.varname]), dtype="i1")
# 
#         uncertainty = self.features["fuzzylogic"]
#         # FIXME: As it is now, it will have no zero flag value. Think about cases
#         #   where some values in a profile would not be estimated, hence flag=0
#         # I needed to use np.nonzeros because now uncertainty is a masked array,
#         #   to accept when a feature is masked.
#         flag[np.nonzero(uncertainty <= 0.29)] = 1
#         flag[np.nonzero((uncertainty > 0.29) & (uncertainty <= 0.34))] = 2
#         flag[np.nonzero((uncertainty > 0.34) & (uncertainty <= 0.72))] = 3
#         flag[np.nonzero(uncertainty > 0.72)] = 4
# 
#         self.flags["fuzzylogic"] = flag
# 
# Path: tests/qctests/compare.py
# def compare_input_types(Procedure, cfg):
#     compare_tuple(Procedure, cfg)
#     compare_pandas(Procedure, cfg)
#     compare_xarray(Procedure, cfg)
# 
# def compare_compound_feature_input_types(feature, *args, **kwargs):
#     compare_compound_feature_tuple(feature, *args, **kwargs)
#     compare_compound_feature_dataframe(feature, *args, **kwargs)
#     compare_compound_feature_dataset(feature, *args, **kwargs)
# 
# Path: tests/data.py
# class DummyData(object):
#     def __init__(self):
#         self.attrs = {
#                 'datetime': datetime(2016,6,4),
#                 'LATITUDE': 15, 'LONGITUDE': -38}
#         self.data = {
#                 'PRES': ma.fix_invalid(
#                     [2, 6, 10, 21, 44, 79, 100, 150,
#                         200, 400, 410, 650, 1000, 2000, 5000]),
#                 'TEMP': ma.fix_invalid(
#                     [25.32, 25.34, 25.34, 25.31, 24.99, 23.46, 21.85, 17.95,
#                         15.39, 11.08, 6.93, 7.93, 5.71, 3.58, np.nan]),
#                 'PSAL': ma.fix_invalid(
#                     [36.49, 36.51, 36.52, 36.53, 36.59, 36.76, 36.81, 36.39,
#                         35.98, 35.30, 35.28, 34.93, 34.86, np.nan, np.nan])}
# 
#     def __getitem__(self, key):
#         return self.data[key]
# 
#     def keys(self):
#         return self.data.keys()
# 
#     @property
#     def attributes(self):
#         print('attributes will be removed. Use attrs instead!')
#         return self.attrs


from datetime import timedelta
from hypothesis import given, settings, strategies as st
from hypothesis.extra.numpy import arrays, array_shapes
from cotede.qctests import FuzzyLogic, fuzzylogic
from .compare import compare_input_types, compare_compound_feature_input_types
from ..data import DummyData
import numpy as np
import pytest
                        "high": {'type': 'smf', 'params': [2, 6]}
                    },
                    "woa_normbias": {
                        "weight": 1,
                        "low": {'type': 'zmf', 'params': [3, 4]},
                        "medium": {'type': 'trapmf', 'params': [3, 4, 5, 6]},
                        "high": {'type': 'smf', 'params': [5, 6]}
                    },
                    "gradient": {
                        "weight": 1,
                        "low": {'type': 'zmf', 'params': [0.5, 1.5]},
                        "medium": {'type': 'trapmf', 'params': [0.5, 1.5, 3, 4]},
                        "high": {'type': 'smf', 'params': [3, 4]}
                    }
                }
            }


def test_standard_dataset():
    features = {
        "fuzzylogic": np.array([np.nan, 0.22222222, 0.22222222, 0.22222222, 0.23232323, 0.22222222, 0.26262626, 0.22222222, 0.24242424, 0.22222222, 0.29292929, 0.43434343, 0.22222222, np.nan, np.nan])
    }
    flags = {
        "fuzzylogic": np.array(
            [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 1, 0, 0], dtype="i1"
        )
    }

    profile = DummyData()


# Path: cotede/qctests/fuzzylogic.py
# def fuzzylogic(features, cfg, require="all"):
#     """
# 
#         FIXME: Think about, should I return 0, or have an assert, and at qc.py
#           all qc tests are applied with a try, and in case it fails it flag
#           0s.
# 
#     """
#     require = cfg.get("require", require)
# 
#     if (require == "all") and not np.all([f in features for f in cfg["features"]]):
#         module_logger.warning(
#             "Not all features (%s) required by fuzzy logic are available".format(
#                 cfg["features"].keys()
#             )
#         )
#         raise KeyError
# 
#     uncertainty = fuzzy_uncertainty(
#         data=features, features=cfg["features"], output=cfg["output"], require=require
#     )
# 
#     return uncertainty
# 
# class FuzzyLogic(QCCheckVar):
#     def set_features(self):
#         self.features = {}
#         for v in [f for f in self.cfg["features"] if f not in self.features]:
#             if v == "woa_bias":
#                 woa_comparison = woa_normbias(self.data, self.varname, self.attrs)
#                 self.features[v] = woa_comparison["woa_bias"]
#             elif v == "woa_normbias":
#                 woa_comparison = woa_normbias(self.data, self.varname, self.attrs)
#                 self.features[v] = woa_comparison["woa_normbias"]
#             elif v == "spike":
#                 self.features[v] = spike(self.data[self.varname])
#             elif v == "gradient":
#                 self.features[v] = gradient(self.data[self.varname])
# 
#         self.features["fuzzylogic"] = fuzzylogic(self.features, self.cfg)
# 
# 
#     def test(self):
#         self.flags = {}
#         cfg = self.cfg
#         flag = np.zeros(np.shape(self.data[self.varname]), dtype="i1")
# 
#         uncertainty = self.features["fuzzylogic"]
#         # FIXME: As it is now, it will have no zero flag value. Think about cases
#         #   where some values in a profile would not be estimated, hence flag=0
#         # I needed to use np.nonzeros because now uncertainty is a masked array,
#         #   to accept when a feature is masked.
#         flag[np.nonzero(uncertainty <= 0.29)] = 1
#         flag[np.nonzero((uncertainty > 0.29) & (uncertainty <= 0.34))] = 2
#         flag[np.nonzero((uncertainty > 0.34) & (uncertainty <= 0.72))] = 3
#         flag[np.nonzero(uncertainty > 0.72)] = 4
# 
#         self.flags["fuzzylogic"] = flag
# 
# Path: tests/qctests/compare.py
# def compare_input_types(Procedure, cfg):
#     compare_tuple(Procedure, cfg)
#     compare_pandas(Procedure, cfg)
#     compare_xarray(Procedure, cfg)
# 
# def compare_compound_feature_input_types(feature, *args, **kwargs):
#     compare_compound_feature_tuple(feature, *args, **kwargs)
#     compare_compound_feature_dataframe(feature, *args, **kwargs)
#     compare_compound_feature_dataset(feature, *args, **kwargs)
# 
# Path: tests/data.py
# class DummyData(object):
#     def __init__(self):
#         self.attrs = {
#                 'datetime': datetime(2016,6,4),
#                 'LATITUDE': 15, 'LONGITUDE': -38}
#         self.data = {
#                 'PRES': ma.fix_invalid(
#                     [2, 6, 10, 21, 44, 79, 100, 150,
#                         200, 400, 410, 650, 1000, 2000, 5000]),
#                 'TEMP': ma.fix_invalid(
#                     [25.32, 25.34, 25.34, 25.31, 24.99, 23.46, 21.85, 17.95,
#                         15.39, 11.08, 6.93, 7.93, 5.71, 3.58, np.nan]),
#                 'PSAL': ma.fix_invalid(
#                     [36.49, 36.51, 36.52, 36.53, 36.59, 36.76, 36.81, 36.39,
#                         35.98, 35.30, 35.28, 34.93, 34.86, np.nan, np.nan])}
# 
#     def __getitem__(self, key):
#         return self.data[key]
# 
#     def keys(self):
#         return self.data.keys()
# 
#     @property
#     def attributes(self):
#         print('attributes will be removed. Use attrs instead!')
#         return self.attrs


# Path: tests/qctests/test_qc_fuzzylogic.py
from datetime import timedelta
from hypothesis import given, settings, strategies as st
from hypothesis.extra.numpy import arrays, array_shapes
from cotede.qctests import FuzzyLogic, fuzzylogic
from .compare import compare_input_types, compare_compound_feature_input_types
from ..data import DummyData
import numpy as np
import pytest

                        "high": {'type': 'smf', 'params': [2, 6]}
                    },
                    "woa_normbias": {
                        "weight": 1,
                        "low": {'type': 'zmf', 'params': [3, 4]},
                        "medium": {'type': 'trapmf', 'params': [3, 4, 5, 6]},
                        "high": {'type': 'smf', 'params': [5, 6]}
                    },
                    "gradient": {
                        "weight": 1,
                        "low": {'type': 'zmf', 'params': [0.5, 1.5]},
                        "medium": {'type': 'trapmf', 'params': [0.5, 1.5, 3, 4]},
                        "high": {'type': 'smf', 'params': [3, 4]}
                    }
                }
            }


def test_standard_dataset():
    features = {
        "fuzzylogic": np.array([np.nan, 0.22222222, 0.22222222, 0.22222222, 0.23232323, 0.22222222, 0.26262626, 0.22222222, 0.24242424, 0.22222222, 0.29292929, 0.43434343, 0.22222222, np.nan, np.nan])
    }
    flags = {
        "fuzzylogic": np.array(
            [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 1, 0, 0], dtype="i1"
        )
    }

    profile = DummyData()


    y = FuzzyLogic(profile, "TEMP", cfg=CFG)