Changeset 9301:7610090bd67a in orange


Ignore:
Timestamp:
12/05/11 18:07:43 (2 years ago)
Author:
ales_erjavec <ales.erjavec@…>
Branch:
default
Convert:
bb2f2d1ddbf5d2bee3db84223c74c40957d68fed
Message:

Renamed members, functions in Orange.misc.selection underscore style and fixed documentation.
Added name mappings to fix_changed_names fixer.

Location:
orange
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • orange/Orange/misc/__init__.py

    r9050 r9301  
    1111 
    1212.. automodule:: Orange.misc.selection 
    13   :members: 
    1413 
    1514.. automodule:: Orange.misc.addons 
     
    2423import environ 
    2524import counters 
    26 import selection 
    2725import render 
    2826import serverfiles 
     
    574572progressBarMilestones = deprecated_function_name(progress_bar_milestones) 
    575573printVerbose = deprecated_function_name(print_verbose) 
     574 
     575# Must be imported after deprecation function definitions 
     576import selection 
  • orange/Orange/misc/selection.py

    r8042 r9301  
    88   single: misc; selection 
    99 
    10 Many machine learning techniques generate a set different solutions or have to 
    11 choose, as for instance in classification tree induction, between different 
    12 features. The most trivial solution is to iterate through the candidates, 
    13 compare them and remember the optimal one. The problem occurs, however, when 
    14 there are multiple candidates that are equally good, and the naive approaches 
    15 would select the first or the last one, depending upon the formulation of 
    16 the if-statement. 
     10Many machine learning techniques generate a set of different solutions or 
     11have to choose, as for instance in classification tree induction, between 
     12different features. The most trivial solution is to iterate through the  
     13candidates, compare them and remember the optimal one. The problem occurs, 
     14however, when there are multiple candidates that are equally good, and the 
     15naive approaches would select the first or the last one, depending upon the 
     16formulation of the if-statement. 
    1717 
    1818:class:`Orange.misc.selection` provides a class that makes a random choice 
     
    2323current one) is 1/w, where w is the current number of equal candidates, 
    2424including the present one. One can easily verify that this gives equal 
    25 chances to all candidates, independent of the order in which they are presented. 
     25chances to all candidates, independent of the order in which they are 
     26presented. 
    2627 
    2728Example 
     
    3738 
    3839Our candidates are tuples gain ratios and features, so we set 
    39 :obj:`callCompareOn1st` to make the compare function compare the first element 
    40 (gain ratios). We could achieve the same by initializing the object like this: 
     40:obj:`call_compare_on_1st` to make the compare function compare the first 
     41element (gain ratios). We could achieve the same by initializing the object 
     42like this: 
    4143 
    4244part of `misc-selection-bestonthefly.py`_ (uses `lymphography.tab`_) 
     
    5658.. _lymphography.tab: code/lymphography.tab 
    5759 
    58 Here we only give gain ratios to :obj:`bestOnTheFly`, so we don't have to specify a 
    59 special compare operator. After checking all features we get the index of the  
    60 optimal one by calling :obj:`winnerIndex`. 
     60Here we only give gain ratios to :obj:`BestOnTheFly`, so we don't have 
     61to specify a special compare operator. After checking all features we 
     62get the index of the  optimal one by calling :obj:`winner_index`. 
     63 
     64.. autoclass:: BestOnTheFly 
     65 
     66.. autofunction:: select_best 
     67 
     68.. autofunction:: select_best_index 
     69 
     70.. autofunction:: compare_first_bigger 
     71 
     72.. autofunction:: compare_first_smaller 
     73 
     74.. autofunction:: compare_last_bigger 
     75 
     76.. autofunction:: compare_last_smaller 
     77 
     78.. autofunction:: compare_bigger 
     79 
     80.. autofunction:: compare_smaller 
     81 
    6182""" 
    6283 
    6384import random 
     85 
     86from Orange.misc import deprecated_members, deprecated_function_name, \ 
     87                        deprecated_keywords 
    6488 
    6589class BestOnTheFly: 
     
    7599    pseudo-randomness.random seed. 
    76100    :type seed: int 
    77     :param callCompareOn1st: If set, :obj:`BestOnTheFly` will suppose\ 
     101    :param call_compare_on_1st: If set, :obj:`BestOnTheFly` will suppose\ 
    78102    that the candidates are lists are tuples, and it will call compare\ 
    79103    with the first element of the tuple. 
    80     :type callCompareOn1st: bool 
    81     """ 
    82      
    83     def __init__(self, compare=cmp, seed = 0, callCompareOn1st = False): 
     104    :type call_compare_on_1st: bool 
     105    """ 
     106     
     107    def __init__(self, compare=cmp, seed = 0, call_compare_on_1st = False): 
    84108        self.randomGenerator = random.Random(seed) 
    85109        self.compare=compare 
     
    87111        self.bestIndex, self.index = -1, -1 
    88112        self.best = None 
    89         self.callCompareOn1st = callCompareOn1st 
     113        self.call_compare_on_1st = call_compare_on_1st 
    90114 
    91115    def candidate(self, x): 
     
    93117         
    94118        :param x: new candidate. 
    95         :type x: object""" 
     119        :type x: object 
     120        """ 
    96121        self.index += 1 
    97122        if not self.wins: 
     
    101126            return 1 
    102127        else: 
    103             if self.callCompareOn1st: 
     128            if self.call_compare_on_1st: 
    104129                cmpr=self.compare(x[0], self.best[0]) 
    105130            else: 
     
    122147        any number of times, even when the candidates are still coming. 
    123148         
    124         :rtype: object""" 
     149        :rtype: object 
     150        """ 
    125151        return self.best 
    126152 
    127     def winnerIndex(self): 
     153    def winner_index(self): 
    128154        """Return the index of the optimal object within the sequence of 
    129155        the candidates. 
    130156         
    131         :rtype: int""" 
     157        :rtype: int 
     158        """ 
    132159        if self.best is not None: 
    133160            return self.bestIndex 
     
    135162            return None 
    136163 
    137 def selectBest(x, compare=cmp, seed = 0, callCompareOn1st = False): 
     164BestOnTheFly = deprecated_members({"callCompareOn1st": "call_compare_on_1st", 
     165                                   "winnerIndex": "winner_index", 
     166                                   }, 
     167                                   wrap_methods=["__init__"])(BestOnTheFly) 
     168 
     169 
     170@deprecated_keywords({"callCompareOn1st": "call_compare_on_1st"}) 
     171def select_best(x, compare=cmp, seed = 0, call_compare_on_1st = False): 
    138172    """Return the optimal object from list x. The function is used if the candidates 
    139173    are already in the list, so using the more complicated :obj:`BestOnTheFly` directly is 
     
    143177    :obj:`selectBest`:: 
    144178     
    145       def selectBest(x, compare=cmp, seed = 0, callCompareOn1st = False): 
    146           bs=BestOnTheFly(compare, seed, callCompareOn1st) 
     179      def selectBest(x, compare=cmp, seed = 0, call_compare_on_1st = False): 
     180          bs=BestOnTheFly(compare, seed, call_compare_on_1st) 
    147181          for i in x: 
    148182              bs.candidate(i) 
     
    152186    :type x: list 
    153187    :param compare: compare function. 
    154     :param seed: If not given, a random seed of 0 is used to ensure that\ 
    155     the same experiment always gives the same results, despite\ 
    156     pseudo-randomness.random seed. 
     188    :param seed: If not given, a random seed of 0 is used to ensure that 
     189        the same experiment always gives the same results, despite 
     190        pseudo-randomness.random seed. 
    157191    :type seed: int 
    158     :param callCompareOn1st: If set, :obj:`BestOnTheFly` will suppose\ 
    159     that the candidates are lists are tuples, and it will call compare\ 
    160     with the first element of the tuple. 
    161     :type callCompareOn1st: bool 
    162     :rtype: object""" 
    163     bs=BestOnTheFly(compare, seed, callCompareOn1st) 
     192    :param call_compare_on_1st: If set, :obj:`BestOnTheFly` will suppose 
     193        that the candidates are lists are tuples, and it will call compare 
     194        with the first element of the tuple. 
     195    :type call_compare_on_1st: bool 
     196    :rtype: object 
     197     
     198    """ 
     199    bs=BestOnTheFly(compare, seed, call_compare_on_1st) 
    164200    for i in x: 
    165201        bs.candidate(i) 
    166202    return bs.winner() 
    167203 
    168 def selectBestIndex(x, compare=cmp, seed = 0, callCompareOn1st = False): 
     204selectBest = deprecated_function_name(select_best) 
     205 
     206 
     207@deprecated_keywords({"callCompareOn1st": "call_compare_on_1st"}) 
     208def select_best_index(x, compare=cmp, seed = 0, call_compare_on_1st = False): 
    169209    """Similar to :obj:`selectBest` except that it doesn't return the best object 
    170     but its index in the list x.""" 
    171     bs=BestOnTheFly(compare, seed, callCompareOn1st) 
     210    but its index in the list x. 
     211    """ 
     212    bs=BestOnTheFly(compare, seed, call_compare_on_1st) 
    172213    for i in x: 
    173214        bs.candidate(i) 
    174     return bs.winnerIndex() 
    175  
    176 # def compare2_firstBigger(x, y): 
    177 def compareFirstBigger(x, y): 
     215    return bs.winner_index() 
     216 
     217selectBestIndex = deprecated_function_name(select_best_index) 
     218 
     219 
     220def compare_first_bigger(x, y): 
    178221    """Function takes two lists and compares first elements. 
    179222     
     
    182225    :param y: list of values. 
    183226    :type y: list 
    184     :rtype:  cmp(x[0], y[0])""" 
     227    :rtype:  cmp(x[0], y[0]) 
     228    """ 
    185229    return cmp(x[0], y[0]) 
    186230 
    187 #def compare2_firstSmaller(x, y): 
    188 def compareFirstSmaller(x, y): 
     231def compare_first_smaller(x, y): 
    189232    """Function takes two lists and compares first elements. 
    190233     
     
    193236    :param y: list of values. 
    194237    :type y: list 
    195     :rtype:  -cmp(x[0], y[0])""" 
     238    :rtype:  -cmp(x[0], y[0]) 
     239    """ 
    196240    return -cmp(x[0], y[0]) 
    197241 
    198 #     def compare2_lastBigger(x, y): 
    199 def compareLastBigger(x, y): 
     242def compare_last_bigger(x, y): 
    200243    """Function takes two lists and compares last elements. 
    201244     
     
    204247    :param y: list of values. 
    205248    :type y: list 
    206     :rtype:  cmp(x[0], y[0])""" 
     249    :rtype:  cmp(x[0], y[0]) 
     250    """ 
    207251    return cmp(x[-1], y[-1]) 
    208252 
    209 #    def compare2_lastSmaller(x, y): 
    210 def compareLastSmaller(x, y): 
     253def compare_last_smaller(x, y): 
    211254    """Function takes two lists and compares last elements. 
    212255     
     
    215258    :param y: list of values. 
    216259    :type y: list 
    217     :rtype:  -cmp(x[0], y[0])""" 
     260    :rtype:  -cmp(x[0], y[0]) 
     261    """ 
    218262    return -cmp(x[-1], y[-1]) 
    219263     
    220 #     def compare2_bigger(x, y): 
    221 def compareBigger(x, y): 
     264def compare_bigger(x, y): 
    222265    """Function takes and compares two numbers. 
    223266     
     
    226269    :param y: value. 
    227270    :type y: int 
    228     :rtype:  cmp(x, y)""" 
     271    :rtype:  cmp(x, y) 
     272    """ 
    229273    return cmp(x, y) 
    230 #     def compare2_smaller(x, y): 
    231 def compareSmaller(x, y): 
     274 
     275def compare_smaller(x, y): 
    232276    """Function takes and compares two numbers. 
    233277     
     
    236280    :param y: value. 
    237281    :type y: int 
    238     :rtype: cmp(x, y)""" 
     282    :rtype: cmp(x, y) 
     283    """ 
    239284    return -cmp(x, y) 
  • orange/doc/Orange/rst/code/misc-selection-bestonthefly.py

    r7560 r9301  
    99table = Orange.data.Table("lymphography") 
    1010 
    11 findBest = Orange.misc.selection.BestOnTheFly(callCompareOn1st = True) 
     11find_best = Orange.misc.selection.BestOnTheFly(call_compare_on_1st = True) 
    1212 
    1313for attr in table.domain.attributes: 
    14     findBest.candidate((Orange.feature.scoring.GainRatio(attr, table), attr)) 
     14    find_best.candidate((Orange.feature.scoring.GainRatio(attr, table), attr)) 
    1515 
    16 print "%5.3f: %s" % findBest.winner() 
     16print "%5.3f: %s" % find_best.winner() 
    1717 
    18 findBest = Orange.misc.selection.BestOnTheFly(Orange.misc.selection.compareFirstBigger) 
     18find_best = Orange.misc.selection.BestOnTheFly(Orange.misc.selection.compare_first_bigger) 
    1919 
    2020for attr in table.domain.attributes: 
    21     findBest.candidate((Orange.feature.scoring.GainRatio(attr, table), attr)) 
     21    find_best.candidate((Orange.feature.scoring.GainRatio(attr, table), attr)) 
    2222 
    23 print "%5.3f: %s" % findBest.winner() 
     23print "%5.3f: %s" % find_best.winner() 
    2424 
    25 findBest = Orange.misc.selection.BestOnTheFly() 
     25find_best = Orange.misc.selection.BestOnTheFly() 
    2626 
    2727for attr in table.domain.attributes: 
    28     findBest.candidate(Orange.feature.scoring.GainRatio(attr, table)) 
     28    find_best.candidate(Orange.feature.scoring.GainRatio(attr, table)) 
    2929 
    30 bestIndex = findBest.winnerIndex() 
    31 print "%5.3f: %s" % (findBest.winner(), table.domain[bestIndex]) 
     30best_index = find_best.winner_index() 
     31print "%5.3f: %s" % (find_best.winner(), table.domain[best_index]) 
  • orange/fixes/fix_changed_names.py

    r9298 r9301  
    388388#           "orngStat.x" : "Orange.evaluation.scoring.x", 
    389389            
    390            # Now use old orngMisc 
    391390           # Orange.selection 
    392            #"orngMisc.BestOnTheFly":"Orange.misc.selection.BestOnTheFly", 
    393            #"orngMisc.selectBest":"Orange.misc.selection.selectBest", 
    394            #"orngMisc.selectBestIndex":"Orange.misc.selection.selectBestIndex", 
    395            #"orngMisc.compare2_firstBigger":"Orange.misc.selection.compareFirstBigger", 
    396            #"orngMisc.compare2_firstBigger":"Orange.misc.selection.compareFirstBigger", 
    397            #"orngMisc.compare2_firstSmaller":"Orange.misc.selection.compareFirstSmaller", 
    398            #"orngMisc.compare2_lastBigger":"Orange.misc.selection.compareLastBigger", 
    399            #"orngMisc.compare2_lastSmaller":"Orange.misc.selection.compareLastSmaller", 
    400            #"orngMisc.compare2_bigger":"Orange.misc.selection.compareBigger", 
    401            #"orngMisc.compare2_smaller":"Orange.misc.selection.compareSmaller", 
     391           "orngMisc.BestOnTheFly":"Orange.misc.selection.BestOnTheFly", 
     392           "orngMisc.selectBest":"Orange.misc.selection.select_best", 
     393           "orngMisc.selectBestIndex":"Orange.misc.selection.select_best_index", 
     394           "orngMisc.compare2_firstBigger":"Orange.misc.selection.compare_first_bigger", 
     395           "orngMisc.compare2_firstBigger":"Orange.misc.selection.compare_first_bigger", 
     396           "orngMisc.compare2_firstSmaller":"Orange.misc.selection.compare_first_smaller", 
     397           "orngMisc.compare2_lastBigger":"Orange.misc.selection.compare_last_bigger", 
     398           "orngMisc.compare2_lastSmaller":"Orange.misc.selection.compare_last_smaller", 
     399           "orngMisc.compare2_bigger":"Orange.misc.selection.compare_bigger", 
     400           "orngMisc.compare2_smaller":"Orange.misc.selection.compare_smaller", 
    402401            
    403402           "orngMisc.Renderer": "Orange.misc.render.Renderer", 
Note: See TracChangeset for help on using the changeset viewer.