def test_fuzzyfy(): features = {"f1": np.array([1.0]), "f2": np.array([5.2]), "f3": np.array([0.9])} rules = fuzzyfy(features, **CFG) 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) assert_allclose(rules["low"], [np.nan]) assert_allclose(rules["medium"], [np.nan]) assert_allclose(rules["high"], [np.nan])
def morello2014(features, cfg): """ 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. """ #for f in cfg['features']: # assert f in features, \ # "morello2014 requires feature %s, which is not available" \ # % f if not np.all([f in features for f in cfg['features']]): print("Not all features (%s) required by morello2014 are available" % cfg['features'].keys()) try: return np.zeros(features[features.keys()[0]].shape, dtype='i1') except: return 0 f = fuzzyfy(features, cfg) ## This is how Timms and Morello defined the Fuzzy Logic approach #flag = np.zeros(N, dtype='i1') # Flag must be np.array, not a ma.array. flag = np.zeros(features[features.keys()[0]].shape, dtype='i1') flag[(f['low'] > 0.5) & (f['high'] < 0.3)] = 2 flag[(f['low'] > 0.9)] = 1 # Everything else is flagged 3 flag[(f['low'] <= 0.5) | (f['high'] >= 0.3)] = 3 # Missing check if threshold was crossed, to flag as 4 # The thresholds coincide with the end of the ramp for the fuzzy set high, # hence we can simply flag[(f['high'] == 1.)] = 4 return flag
def morello2014(features, cfg): """ 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. """ #for f in cfg['features']: # assert f in features, \ # "morello2014 requires feature %s, which is not available" \ # % f if not np.all([f in features for f in cfg['features']]): print("Not all features (%s) required by morello2014 are available" % cfg['features'].keys()) try: return np.zeros(features[features.keys()[0]].shape, dtype='i1') except: return 0 f = fuzzyfy(features, cfg) ## This is how Timms and Morello defined the Fuzzy Logic approach #flag = np.zeros(N, dtype='i1') # Flag must be np.array, not a ma.array. flag = np.zeros(features[list(features.keys())[0]].shape, dtype='i1') flag[(f['low'] > 0.5) & (f['high'] < 0.3)] = 2 flag[(f['low'] > 0.9)] = 1 # Everything else is flagged 3 flag[(f['low'] <= 0.5) | (f['high'] >= 0.3)] = 3 # Missing check if threshold was crossed, to flag as 4 # The thresholds coincide with the end of the ramp for the fuzzy set high, # hence we can simply flag[(f['high'] == 1.)] = 4 return flag