Ignore:
Timestamp:
03/15/11 17:36:41 (3 years ago)
Author:
jzbontar <jure.zbontar@…>
Branch:
default
Convert:
7653aa8dea372a82b8c95c24d99eb605e3af013e
Message:

Rename camel case identifiers in Orange.evaluation.testing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/Orange/evaluation/testing.py

    r7616 r7751  
    8686      first imported in the current Python session. :: 
    8787 
    88           res = orngTest.proportionTest(learners, data, 0.7, 
     88          res = Orange.evaluation.testing.proportion_test(learners, data, 0.7, 
    8989              randomGenerator=orange.globalRandom)  
    9090 
     
    132132 
    133133        classnoise = orange.Preprocessor_addClassNoise(proportion=1.0)  
    134         res = orngTest.proportionTest(learners, data, 0.7, 100, pps = [("L", classnoise)])  
     134        res = Orange.evaluation.testing.proportion_test(learners, data, 0.7, 100, pps = [("L", classnoise)])  
    135135 
    136136*proportions* 
     
    148148    classifiers it induces. :: 
    149149 
    150         res = orngTest.proportionTest(learners, data, 0.7, 100, storeClassifier=1) 
     150        res = Orange.evaluation.testing.proportion_test(learners, data, 0.7, 100, storeClassifier=1) 
    151151 
    152152*verbose (keyword argument)* 
     
    157157============================== 
    158158 
    159 .. autofunction:: proportionTest 
    160 .. autofunction:: leaveOneOut 
    161 .. autofunction:: crossValidation 
    162 .. autofunction:: testWithIndices 
    163 .. autofunction:: learningCurve 
    164 .. autofunction:: learningCurveN 
    165 .. autofunction:: learningCurveWithTestData 
    166 .. autofunction:: learnAndTestOnTestData 
    167 .. autofunction:: learnAndTestOnLearnData 
    168 .. autofunction:: testOnData 
     159.. autofunction:: proportion_test 
     160.. autofunction:: leave_one_out 
     161.. autofunction:: cross_validation 
     162.. autofunction:: test_with_indices 
     163.. autofunction:: learning_curve 
     164.. autofunction:: learning_curve_n 
     165.. autofunction:: learning_curve_with_test_data 
     166.. autofunction:: learn_and_test_on_test_data 
     167.. autofunction:: learn_and_test_on_learn_data 
     168.. autofunction:: test_on_data 
    169169 
    170170Classes 
     
    199199#### Some private stuff 
    200200 
    201 def encodePP(pps): 
     201def encode_PP(pps): 
    202202    pps="" 
    203203    for pp in pps: 
     
    254254        self.weight = weight 
    255255     
    256     def addResult(self, aclass, aprob): 
     256    def add_result(self, aclass, aprob): 
    257257        """Appends a new result (class and probability prediction by a single classifier) to the classes and probabilities field.""" 
    258258     
     
    264264            self.probabilities.append(list(aprob)) 
    265265 
    266     def setResult(self, i, aclass, aprob): 
     266    def set_result(self, i, aclass, aprob): 
    267267        """Sets the result of the i-th classifier to the given values.""" 
    268268        if type(aclass.value)==float: 
     
    332332        self.__dict__.update(argkw) 
    333333 
    334     def loadFromFiles(self, learners, filename): 
     334    def load_from_files(self, learners, filename): 
    335335        self.loaded = [] 
    336336       
     
    344344                    if (tre.actualClass, tre.iterationNumber) != d[ex][0]: 
    345345                        raise SystemError, "mismatching example tables or sampling" 
    346                     self.results[ex].setResult(i, d[ex][1][0], d[ex][1][1]) 
     346                    self.results[ex].set_result(i, d[ex][1][0], d[ex][1][1]) 
    347347                self.loaded.append(1) 
    348348            except exceptions.Exception: 
     
    353353        return not 0 in self.loaded                 
    354354                 
    355     def saveToFiles(self, learners, filename): 
     355    def save_to_files(self, learners, filename): 
    356356        """ 
    357357        Saves and load testing results. ``learners`` is a list of learners and 
     
    377377        Filename should include enough data to make sure that it 
    378378        indeed contains the right experimental results. The function 
    379         :obj:`learningCurve`, for example, forms the name of the file 
    380         from a string ``"{learningCurve}"``, the proportion of learning 
     379        :obj:`learning_curve`, for example, forms the name of the file 
     380        from a string ``"{learning_curve}"``, the proportion of learning 
    381381        examples, random seeds for cross-validation and learning set 
    382382        selection, a list of preprocessors' names and a checksum for 
     
    437437#### Experimental procedures 
    438438 
    439 def leaveOneOut(learners, examples, pps=[], indicesrandseed="*", **argkw): 
     439def leave_one_out(learners, examples, pps=[], indicesrandseed="*", **argkw): 
    440440 
    441441    """leave-one-out evaluation of learners on a data set 
     
    449449 
    450450    (examples, weight) = demangleExamples(examples) 
    451     return testWithIndices(learners, examples, range(len(examples)), indicesrandseed, pps, **argkw) 
    452     # return testWithIndices(learners, examples, range(len(examples)), pps=pps, argkw) 
    453  
    454 # apply(testWithIndices, (learners, (examples, weight), indices, indicesrandseed, pps), argkw) 
    455  
    456  
    457 def proportionTest(learners, examples, learnProp, times=10, 
     451    return test_with_indices(learners, examples, range(len(examples)), indicesrandseed, pps, **argkw) 
     452    # return test_with_indices(learners, examples, range(len(examples)), pps=pps, argkw) 
     453 
     454# apply(test_with_indices, (learners, (examples, weight), indices, indicesrandseed, pps), argkw) 
     455 
     456 
     457def proportion_test(learners, examples, learnProp, times=10, 
    458458                   strat=Orange.core.MakeRandomIndices.StratifiedIfPossible, 
    459459                   pps=[], callback=None, **argkw): 
     
    469469    are used for training and 30% for testing is done by:: 
    470470 
    471         res = orngTest.proportionTest(learners, data, 0.7, 100)  
     471        res = Orange.evaluation.testing.proportion_test(learners, data, 0.7, 100)  
    472472 
    473473    Note that Python allows naming the arguments; instead of "100" you 
     
    496496        learnset = examples.selectref(indices, 0) 
    497497        testset = examples.selectref(indices, 1) 
    498         learnAndTestOnTestData(learners, (learnset, weight), (testset, weight), testResults, time, pps, **argkw) 
     498        learn_and_test_on_test_data(learners, (learnset, weight), (testset, weight), testResults, time, pps, **argkw) 
    499499        if callback: callback() 
    500500    return testResults 
    501501 
    502 def crossValidation(learners, examples, folds=10, 
     502def cross_validation(learners, examples, folds=10, 
    503503                    strat=Orange.core.MakeRandomIndices.StratifiedIfPossible, 
    504504                    pps=[], indicesrandseed="*", **argkw): 
     
    514514        randomGenerator = argkw.get("randseed", 0) or argkw.get("randomGenerator", 0) 
    515515        indices = Orange.core.MakeRandomIndicesCV(examples, folds, stratified = strat, randomGenerator = randomGenerator) 
    516     return testWithIndices(learners, (examples, weight), indices, indicesrandseed, pps, **argkw) 
    517  
    518  
    519 def learningCurveN(learners, examples, folds=10, 
     516    return test_with_indices(learners, (examples, weight), indices, indicesrandseed, pps, **argkw) 
     517 
     518 
     519def learning_curve_n(learners, examples, folds=10, 
    520520                   strat=Orange.core.MakeRandomIndices.StratifiedIfPossible, 
    521521                   proportions=Orange.core.frange(0.1), pps=[], **argkw): 
    522522    """Construct a learning curve for learners. 
    523523 
    524     A simpler interface for the function :obj:`learningCurve`. Instead 
     524    A simpler interface for the function :obj:`learning_curve`. Instead 
    525525    of methods for preparing indices, it simply takes the number of folds 
    526526    and a flag telling whether we want a stratified cross-validation or 
     
    529529 
    530530        prop = [0.2, 0.4, 0.6, 0.8, 1.0] 
    531         res = orngTest.learningCurveN(learners, data, folds = 5, proportions = prop) 
     531        res = Orange.evaluation.testing.learning_curve_n(learners, data, folds = 5, proportions = prop) 
    532532        for i, p in enumerate(prop): 
    533533            print "%5.3f:" % p, 
     
    535535 
    536536    This function basically prepares a random generator and example selectors 
    537     (``cv`` and ``pick``) and calls :obj:`learningCurve`. 
     537    (``cv`` and ``pick``) and calls :obj:`learning_curve`. 
    538538 
    539539    """ 
     
    551551        cv=Orange.core.RandomIndicesCV(folds = folds, stratified = strat, randomGenerator = randomGenerator) 
    552552        pick=Orange.core.RandomIndices2(stratified = strat, randomGenerator = randomGenerator) 
    553     return apply(learningCurve, (learners, examples, cv, pick, proportions, pps), argkw) 
    554  
    555  
    556 def learningCurve(learners, examples, cv=None, pick=None, proportions=Orange.core.frange(0.1), pps=[], **argkw): 
     553    return apply(learning_curve, (learners, examples, cv, pick, proportions, pps), argkw) 
     554 
     555 
     556def learning_curve(learners, examples, cv=None, pick=None, proportions=Orange.core.frange(0.1), pps=[], **argkw): 
    557557    """ 
    558558    Computes learning curves using a procedure recommended by Salzberg 
     
    598598    folds = cv(examples) 
    599599    ccsum = hex(examples.checksum())[2:] 
    600     ppsp = encodePP(pps) 
     600    ppsp = encode_PP(pps) 
    601601    nLrn = len(learners) 
    602602 
     
    608608            cache = 0 
    609609        else: 
    610             fnstr = "{learningCurve}_%s_%s_%s_%s%s-%s" % ("%s", p, cv.randseed, pick.randseed, ppsp, ccsum) 
     610            fnstr = "{learning_curve}_%s_%s_%s_%s%s-%s" % ("%s", p, cv.randseed, pick.randseed, ppsp, ccsum) 
    611611            if "*" in fnstr: 
    612612                cache = 0 
     
    617617                               for i in range(len(examples))] 
    618618 
    619         if cache and testResults.loadFromFiles(learners, fnstr): 
     619        if cache and testResults.load_from_files(learners, fnstr): 
    620620            printVerbose("  loaded from cache", verb) 
    621621        else: 
     
    646646                            if not cache or not testResults.loaded[cl]: 
    647647                                cls, pro = classifiers[cl](ex, Orange.core.GetBoth) 
    648                                 testResults.results[i].setResult(cl, cls, pro) 
     648                                testResults.results[i].set_result(cl, cls, pro) 
    649649                if callback: callback() 
    650650            if cache: 
    651                 testResults.saveToFiles(learners, fnstr) 
     651                testResults.save_to_files(learners, fnstr) 
    652652 
    653653        allResults.append(testResults) 
     
    656656 
    657657 
    658 def learningCurveWithTestData(learners, learnset, testset, times=10, 
     658def learning_curve_with_test_data(learners, learnset, testset, times=10, 
    659659                              proportions=Orange.core.frange(0.1), 
    660660                              strat=Orange.core.MakeRandomIndices.StratifiedIfPossible, pps=[], **argkw): 
     
    697697        for t in range(times): 
    698698            printVerbose("  repetition %d" % t, verb) 
    699             learnAndTestOnTestData(learners, (learnset.selectref(pick(learnset, p), 0), learnweight), 
     699            learn_and_test_on_test_data(learners, (learnset.selectref(pick(learnset, p), 0), learnweight), 
    700700                                   testset, testResults, t) 
    701701 
     
    705705 
    706706    
    707 def testWithIndices(learners, examples, indices, indicesrandseed="*", pps=[], callback=None, **argkw): 
     707def test_with_indices(learners, examples, indices, indicesrandseed="*", pps=[], callback=None, **argkw): 
    708708    """ 
    709709    Performs a cross-validation-like test. The difference is that the 
    710710    caller provides indices (each index gives a fold of an example) which 
    711711    do not necessarily divide the examples into folds of (approximately) 
    712     same sizes. In fact, the function :obj:`crossValidation` is actually written 
    713     as a single call to ``testWithIndices``. 
    714  
    715     ``testWithIndices`` takes care the ``TestedExamples`` are in the same order 
     712    same sizes. In fact, the function :obj:`cross_validation` is actually written 
     713    as a single call to ``test_with_indices``. 
     714 
     715    ``test_with_indices`` takes care the ``TestedExamples`` are in the same order 
    716716    as the corresponding examples in the original set. Preprocessing of 
    717717    testing examples is thus not allowed. The computed results can be 
     
    756756         
    757757    ccsum = hex(examples.checksum())[2:] 
    758     ppsp = encodePP(pps) 
     758    ppsp = encode_PP(pps) 
    759759    fnstr = "{TestWithIndices}_%s_%s%s-%s" % ("%s", indicesrandseed, ppsp, ccsum) 
    760760    if "*" in fnstr: 
    761761        cache = 0 
    762762 
    763     if cache and testResults.loadFromFiles(learners, fnstr): 
     763    if cache and testResults.load_from_files(learners, fnstr): 
    764764        printVerbose("  loaded from cache", verb) 
    765765    else: 
     
    812812                            if cr[0].isSpecial(): 
    813813                                raise "Classifier %s returned unknown value" % (classifiers[cl].name or ("#%i" % cl)) 
    814                             testResults.results[i].setResult(cl, cr[0], cr[1]) 
     814                            testResults.results[i].set_result(cl, cr[0], cr[1]) 
    815815            if callback: 
    816816                callback() 
    817817        if cache: 
    818             testResults.saveToFiles(learners, fnstr) 
     818            testResults.save_to_files(learners, fnstr) 
    819819         
    820820    return testResults 
    821821 
    822822 
    823 def learnAndTestOnTestData(learners, learnset, testset, testResults=None, iterationNumber=0, pps=[], callback=None, **argkw): 
     823def learn_and_test_on_test_data(learners, learnset, testset, testResults=None, iterationNumber=0, pps=[], callback=None, **argkw): 
    824824    """ 
    825825    This function performs no sampling on its own: two separate datasets 
     
    835835    of the test will be appended with the given iteration 
    836836    number. This is because :obj:`learnAndTestWithTestData` 
    837     gets called by other functions, like :obj:`proportionTest` and 
    838     :obj:`learningCurveWithTestData`. If you omit the parameters, a new 
     837    gets called by other functions, like :obj:`proportion_test` and 
     838    :obj:`learning_curve_with_test_data`. If you omit the parameters, a new 
    839839    :obj:`ExperimentResults` will be created. 
    840840 
     
    867867    classifiers = [learner(learnset, learnweight) for learner in learners] 
    868868    for i in range(len(learners)): classifiers[i].name = getattr(learners[i], 'name', 'noname') 
    869     testResults = testOnData(classifiers, (testset, testweight), testResults, iterationNumber, storeExamples) 
     869    testResults = test_on_data(classifiers, (testset, testweight), testResults, iterationNumber, storeExamples) 
    870870    if storeclassifiers: 
    871871        testResults.classifiers.append(classifiers) 
     
    873873 
    874874 
    875 def learnAndTestOnLearnData(learners, learnset, testResults=None, iterationNumber=0, pps=[], callback=None, **argkw): 
     875def learn_and_test_on_learn_data(learners, learnset, testResults=None, iterationNumber=0, pps=[], callback=None, **argkw): 
    876876    """ 
    877877    This function is similar to the above, except that it learns and 
     
    881881    the learning set and tests it on the testing set. 
    882882 
    883     As with :obj:`learnAndTestOnTestData`, you can pass an already initialized 
     883    As with :obj:`learn_and_test_on_test_data`, you can pass an already initialized 
    884884    :obj:`ExperimentResults` (argument ``results``) and an iteration number to the 
    885885    function. In this case, results of the test will be appended with 
     
    918918            callback() 
    919919    for i in range(len(learners)): classifiers[i].name = getattr(learners[i], "name", "noname") 
    920     testResults = testOnData(classifiers, (testset, learnweight), testResults, iterationNumber, storeExamples) 
     920    testResults = test_on_data(classifiers, (testset, learnweight), testResults, iterationNumber, storeExamples) 
    921921    if storeclassifiers: 
    922922        testResults.classifiers.append(classifiers) 
     
    924924 
    925925 
    926 def testOnData(classifiers, testset, testResults=None, iterationNumber=0, storeExamples = False, **argkw): 
     926def test_on_data(classifiers, testset, testResults=None, iterationNumber=0, storeExamples = False, **argkw): 
    927927    """ 
    928928    This function gets a list of classifiers, not learners like the other 
     
    968968            ex2.setclass("?") 
    969969            cr = classifier(ex2, Orange.core.GetBoth) 
    970             te.addResult(cr[0], cr[1]) 
     970            te.add_result(cr[0], cr[1]) 
    971971        testResults.results.append(te) 
    972972         
Note: See TracChangeset for help on using the changeset viewer.