# Path: cotede/fuzzy/fuzzy_core.py
# def fuzzyfy(data, features, output, require="all"):
#     """
#     Notes
#     -----
#     In the generalize this once the membership combining rules are defined
#     in the cfg, so I can decide to use mean or max.
#     """
#     features_list = list(features.keys())
# 
#     N = max([len(data[f]) for f in features_list])
# 
#     # The fuzzy set are usually: low, medium, high
#     # The membership of each fuzzy set are each feature scaled.
#     membership = {f: {} for f in output.keys()}
# 
#     mfuncs = {"smf": smf, "trimf": trimf, "trapmf": trapmf, "zmf": zmf}
#     for t in features_list:
#         for m in membership:
#             assert m in features[t], "Missing %s in %s" % (m, features[t])
#             f = mfuncs[features[t][m]["type"]]
#             membership[m][t] = f(np.asanyarray(data[t]), features[t][m]["params"])
# 
#     # Rule Set
#     rules = {}
#     # Low: u_low = mean(S_l(spike), S_l(clim)...)
#     # u_low = np.mean([weights['spike']['low'],
#     #    weights['woa_relbias']['low']], axis=0)
#     # Medium: u_medium = mean(S_l(spike), S_l(clim)...)
#     # u_medium = np.mean([weights['spike']['medium'],
#     #    weights['woa_relbias']['medium']], axis=0)
# 
#     for m in [m for m in membership if m != "high"]:
#         tmp = np.vstack([membership[m][f] for f in membership[m]])
#         if require == "any":
#             rules[m] = np.nanmean(tmp, axis=0)
#         else:
#             rules[m] = np.mean(tmp, axis=0)
# 
#     # High: u_high = max(S_l(spike), S_l(clim)...)
#     # u_high = np.max([weights['spike']['high'],
#     #    weights['woa_relbias']['high']], axis=0)
# 
#     tmp = np.vstack([membership["high"][f] for f in membership["high"]])
#     if require == "any":
#         rules["high"] = np.nanmax(tmp, axis=0)
#     else:
#         rules["high"] = np.max(tmp, axis=0)
# 
#     return rules


import numpy as np
from numpy.testing import assert_allclose
from cotede.fuzzy import fuzzyfy
    assert_allclose(rules["low"], [0.226666666])
    assert_allclose(rules["medium"], [0.733333333])
    assert_allclose(rules["high"], [0.08000000])


def test_fuzzyfy_with_nan():
    features = {
        "f1": np.array([1.0, np.nan, 1.0, 1.0, np.nan]),
        "f2": np.array([5.2, 5.2, np.nan, 5.2, np.nan]),
        "f3": np.array([0.9, 0.9, 0.9, np.nan, np.nan]),
    }

    rules = fuzzyfy(features, **CFG)
    assert_allclose(rules["low"], [0.22666667, np.nan, np.nan, np.nan, np.nan])
    assert_allclose(rules["medium"], [0.733333333, np.nan, np.nan, np.nan, np.nan])
    assert_allclose(rules["high"], [0.08000000, np.nan, np.nan, np.nan, np.nan])

    rules = fuzzyfy(features, **CFG, require="any")
    assert_allclose(rules["low"], [0.22666667, 0.34, 0.34, 0, np.nan])
    assert_allclose(rules["medium"], [0.733333333, 0.6, 0.7, 0.9, np.nan])
    assert_allclose(rules["high"], [0.08, 0.08, 0, 0.08, np.nan])


def test_fuzzyfy_all_nan():
    features = {
        "f1": np.array([np.nan]),
        "f2": np.array([np.nan]),
        "f3": np.array([np.nan]),
    }


# Path: cotede/fuzzy/fuzzy_core.py
# def fuzzyfy(data, features, output, require="all"):
#     """
#     Notes
#     -----
#     In the generalize this once the membership combining rules are defined
#     in the cfg, so I can decide to use mean or max.
#     """
#     features_list = list(features.keys())
# 
#     N = max([len(data[f]) for f in features_list])
# 
#     # The fuzzy set are usually: low, medium, high
#     # The membership of each fuzzy set are each feature scaled.
#     membership = {f: {} for f in output.keys()}
# 
#     mfuncs = {"smf": smf, "trimf": trimf, "trapmf": trapmf, "zmf": zmf}
#     for t in features_list:
#         for m in membership:
#             assert m in features[t], "Missing %s in %s" % (m, features[t])
#             f = mfuncs[features[t][m]["type"]]
#             membership[m][t] = f(np.asanyarray(data[t]), features[t][m]["params"])
# 
#     # Rule Set
#     rules = {}
#     # Low: u_low = mean(S_l(spike), S_l(clim)...)
#     # u_low = np.mean([weights['spike']['low'],
#     #    weights['woa_relbias']['low']], axis=0)
#     # Medium: u_medium = mean(S_l(spike), S_l(clim)...)
#     # u_medium = np.mean([weights['spike']['medium'],
#     #    weights['woa_relbias']['medium']], axis=0)
# 
#     for m in [m for m in membership if m != "high"]:
#         tmp = np.vstack([membership[m][f] for f in membership[m]])
#         if require == "any":
#             rules[m] = np.nanmean(tmp, axis=0)
#         else:
#             rules[m] = np.mean(tmp, axis=0)
# 
#     # High: u_high = max(S_l(spike), S_l(clim)...)
#     # u_high = np.max([weights['spike']['high'],
#     #    weights['woa_relbias']['high']], axis=0)
# 
#     tmp = np.vstack([membership["high"][f] for f in membership["high"]])
#     if require == "any":
#         rules["high"] = np.nanmax(tmp, axis=0)
#     else:
#         rules["high"] = np.max(tmp, axis=0)
# 
#     return rules


# Path: tests/fuzzy/test_fuzzyfy.py
import numpy as np
from numpy.testing import assert_allclose
from cotede.fuzzy import fuzzyfy

    assert_allclose(rules["low"], [0.226666666])
    assert_allclose(rules["medium"], [0.733333333])
    assert_allclose(rules["high"], [0.08000000])


def test_fuzzyfy_with_nan():
    features = {
        "f1": np.array([1.0, np.nan, 1.0, 1.0, np.nan]),
        "f2": np.array([5.2, 5.2, np.nan, 5.2, np.nan]),
        "f3": np.array([0.9, 0.9, 0.9, np.nan, np.nan]),
    }

    rules = fuzzyfy(features, **CFG)
    assert_allclose(rules["low"], [0.22666667, np.nan, np.nan, np.nan, np.nan])
    assert_allclose(rules["medium"], [0.733333333, np.nan, np.nan, np.nan, np.nan])
    assert_allclose(rules["high"], [0.08000000, np.nan, np.nan, np.nan, np.nan])

    rules = fuzzyfy(features, **CFG, require="any")
    assert_allclose(rules["low"], [0.22666667, 0.34, 0.34, 0, np.nan])
    assert_allclose(rules["medium"], [0.733333333, 0.6, 0.7, 0.9, np.nan])
    assert_allclose(rules["high"], [0.08, 0.08, 0, 0.08, np.nan])


def test_fuzzyfy_all_nan():
    features = {
        "f1": np.array([np.nan]),
        "f2": np.array([np.nan]),
        "f3": np.array([np.nan]),
    }


    rules = fuzzyfy(features, **CFG)