Ignore:
Timestamp:
06/04/12 16:52:41 (23 months ago)
Author:
Matija Polajnar <matija.polajnar@…>
Branch:
default
Message:

Add probability conversion decorator to more scoring functions as needed, fix decorator usage.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/evaluation/scoring.py

    r10901 r10902  
    4343        raise ValueError, "Cannot compute the score: no examples." 
    4444 
     45# Backward compatibility 
     46def replace_use_weights(fun): 
     47    if environ.orange_no_deprecated_members: 
     48        return fun 
     49 
     50    @functools.wraps(fun) 
     51    def wrapped(*args, **kwargs): 
     52        use_weights = kwargs.pop("useWeights", None) 
     53        if use_weights is not None: 
     54            deprecation_warning("useWeights", "ignore_weights") 
     55            kwargs["ignore_weights"] = not use_weights 
     56        return fun(*args, **kwargs) 
     57    return wrapped 
     58 
     59def replace_discrete_probabilities_with_list(method): 
     60    if environ.orange_no_deprecated_members: 
     61        return lambda fun: fun 
     62 
     63    def decorator(fun): 
     64        @functools.wraps(fun) 
     65        def wrapped(*args, **kwargs): 
     66            res = args[method] if len(args)>method else kwargs.get("res", kwargs.get("test_results", None)) 
     67            convert = res is not None 
     68 
     69            if convert: 
     70                old_probs = [] 
     71                for r in res.results: 
     72                    old_probs.append(r.probabilities) 
     73                    r.probabilities = [list(p) if type(p) is Orange.statistics.distribution.Discrete 
     74                                       else p for p in r.probabilities] 
     75            result = fun(*args, **kwargs) 
     76            if convert: 
     77                for r, old in zip(res.results, old_probs): 
     78                    r.probabilities = old 
     79            return result 
     80        return wrapped 
     81    return decorator 
    4582 
    4683def split_by_iterations(res): 
     
    9981035@deprecated_keywords({"classIndex": "class_index", 
    9991036                      "unweighted": "ignore_weights"}) 
     1037@replace_discrete_probabilities_with_list(False) 
    10001038def AUCWilcoxon(res, class_index= -1, ignore_weights=False, **argkw): 
    10011039    """Compute the area under ROC (AUC) and its standard error using 
     
    10411079@deprecated_keywords({"classIndex": "class_index", 
    10421080                      "unweighted": "ignore_weights"}) 
     1081@replace_discrete_probabilities_with_list(False) 
    10431082def compare_2_AUCs(res, lrn1, lrn2, class_index= -1, 
    10441083                   ignore_weights=False, **argkw): 
     
    10511090 
    10521091@deprecated_keywords({"classIndex": "class_index"}) 
     1092@replace_discrete_probabilities_with_list(False) 
    10531093def compute_ROC(res, class_index= -1): 
    10541094    """Compute a ROC curve as a list of (x, y) tuples, where x is  
     
    11101150@deprecated_keywords({"classIndex": "class_index", 
    11111151                      "keepConcavities": "keep_concavities"}) 
     1152@replace_discrete_probabilities_with_list(False) 
    11121153def TC_compute_ROC(res, class_index= -1, keep_concavities=1): 
    11131154    problists, tots = corn.computeROCCumulative(res, class_index) 
     
    13381379##  - noClassRugPoints is an array of (x, 0) points 
    13391380@deprecated_keywords({"classIndex": "class_index"}) 
     1381@replace_discrete_probabilities_with_list(False) 
    13401382def compute_calibration_curve(res, class_index= -1): 
    13411383    ## merge multiple iterations into one 
     
    14041446##    on the Lift Curve 
    14051447@deprecated_keywords({"classIndex": "class_index"}) 
     1448@replace_discrete_probabilities_with_list(False) 
    14061449def compute_lift_curve(res, class_index= -1): 
    14071450    ## merge multiple iterations into one 
     
    14421485@deprecated_keywords({"classIndex": "class_index", 
    14431486                      "unweighted": "ignore_weights"}) 
     1487@replace_discrete_probabilities_with_list(False) 
    14441488def compute_CDT(res, class_index= -1, ignore_weights=False, **argkw): 
    14451489    """Obsolete, don't use.""" 
     
    14691513    else: 
    14701514        return corn.computeCDT(res, class_index, useweights) 
    1471  
    1472 # Backward compatibility 
    1473 def replace_use_weights(fun): 
    1474     if environ.orange_no_deprecated_members: 
    1475         return fun 
    1476  
    1477     @functools.wraps(fun) 
    1478     def wrapped(*args, **kwargs): 
    1479         use_weights = kwargs.pop("useWeights", None) 
    1480         if use_weights is not None: 
    1481             deprecation_warning("useWeights", "ignore_weights") 
    1482             kwargs["ignore_weights"] = not use_weights 
    1483         return fun(*args, **kwargs) 
    1484     return wrapped 
    1485  
    1486 def replace_discrete_probabilities_with_list(method=False): 
    1487     if environ.orange_no_deprecated_members: 
    1488         return lambda fun: fun 
    1489  
    1490     def decorator(fun): 
    1491         @functools.wraps(fun) 
    1492         def wrapped(*args, **kwargs): 
    1493             res = args[method] if len(args)>method else kwargs.get("res", kwargs.get("test_results", None)) 
    1494             convert = res is not None 
    1495  
    1496             if convert: 
    1497                 old_probs = [] 
    1498                 for r in res.results: 
    1499                     old_probs.append(r.probabilities) 
    1500                     r.probabilities = [list(p) if type(p) is Orange.statistics.distribution.Discrete 
    1501                                        else p for p in r.probabilities] 
    1502             result = fun(*args, **kwargs) 
    1503             if convert: 
    1504                 for r, old in zip(res.results, old_probs): 
    1505                     r.probabilities = old 
    1506             return result 
    1507         return wrapped 
    1508     return decorator 
    15091515 
    15101516class AUC(list): 
     
    18011807#Backward compatibility 
    18021808@replace_use_weights 
    1803 @replace_discrete_probabilities_with_list 
     1809@replace_discrete_probabilities_with_list(False) 
    18041810def AUC_binary(res, ignore_weights=False): 
    18051811    auc = deprecated_function_name(AUC)(ignore_weights=ignore_weights) 
     
    18081814 
    18091815@replace_use_weights 
    1810 @replace_discrete_probabilities_with_list 
     1816@replace_discrete_probabilities_with_list(False) 
    18111817def AUC_multi(res, ignore_weights=False, method=0): 
    18121818    auc = deprecated_function_name(AUC)(ignore_weights=ignore_weights, 
     
    18571863 
    18581864@deprecated_keywords({"unweighted": "ignore_weights"}) 
    1859 @replace_discrete_probabilities_with_list 
     1865@replace_discrete_probabilities_with_list(False) 
    18601866def McNemar(res, ignore_weights=False, **argkw): 
    18611867    """ 
     
    19051911    return mcm 
    19061912 
    1907 @replace_discrete_probabilities_with_list 
     1913@replace_discrete_probabilities_with_list(False) 
    19081914def McNemar_of_two(res, lrn1, lrn2, ignore_weights=False): 
    19091915    """ 
     
    19351941        return 0 
    19361942 
    1937 @replace_discrete_probabilities_with_list 
     1943@replace_discrete_probabilities_with_list(False) 
    19381944def Friedman(res, stat=CA): 
    19391945    """ 
     
    19601966    return F, statc.chisqprob(F, k - 1), sums 
    19611967 
    1962 @replace_discrete_probabilities_with_list 
     1968@replace_discrete_probabilities_with_list(False) 
    19631969def Wilcoxon_pairs(res, avgranks, stat=CA): 
    19641970    """ 
Note: See TracChangeset for help on using the changeset viewer.