<commit_before>from nose.plugins.attrib import attr

from numpy.testing import assert_array_less
from numpy import array, hstack
from numpy.random import rand
from hopkinsTesting import runOnManyExtraNoise, findBalancedSets, manyNames, rest
import alternatingAlgorithms
import utility
import weightedModelTypes


@attr('difficult') # Requires the hopkins dataset to be present.
def testBeatLloydsOnRobustMeasureHopkins():
    "Our method vs Lloyd at high noise on two Hopkins datasets"
    imbalanceRatios, names = findBalancedSets(manyNames + rest)
    numberToRun = 2
    imbalanceRatios = imbalanceRatios[0:numberToRun]
    names = names[0:numberToRun]

    noiseLevel = 0.2
    multipleSummary = runOnManyExtraNoise(names,
                                          lambda sSet: utility.medianAbsLoss(sSet),
                                          iters=10, noiseLevel=noiseLevel)

    lloydsMedianAbsLoss, lloydsR, ourMedianAbsLoss, ourR, toplineQ, toplineR = zip(
        *[(allLloydQualities.min(),
           lloydRunMissRates[allLloydQualities.argmin()],
           allQualities.min(),
           missRates[allQualities.argmin()],
           toplineQuality, toplineMissRate)

          for (name, size, allQualities, allLloydQualities, toplineQuality,
               missRates, lloydRunMissRates, toplineMissRate)
          in multipleSummary])
    assert_array_less(array(ourMedianAbsLoss), array(lloydsMedianAbsLoss))


@attr('difficult') # Requires the hopkins dataset to be present.
def testBeatLloydsOnRobustMeasureQuadratic():
    "Our method vs Lloyds on quadratic regression, should have more eps correct answers."
    coeffs = [array([-0.04780819, -0.3844407, -1.73699185]),
              array([-1.44054051, -0.63069834, -0.9437272])]
    q1 = 40
    x1 = rand(1, 40)
    x2 = rand(1, 40)
    y1 = weightedModelTypes.quadraticKernelMapping(x1.T).dot(coeffs[0]) + 0.05 / rand(q1)
    y2 = weightedModelTypes.quadraticKernelMapping(x2.T).dot(coeffs[1]) + 0.05 / rand(q1)

    # some convenience
    x = hstack((x1, x2))
    y = hstack((y1, y2))
    y = y[x.argsort()].flatten()
    x.sort()
    _, q = x.shape

    alpha = 100

    ourStateSets = []
    lloydsStateSets = []
    for _ in range(20):
        ourInitStates = alternatingAlgorithms.jointlyPenalizedInitialStates((x, y),
                                                                            weightedModelTypes.MultiQuadraticRegressionState,
                                                                            alpha,
                                                                            k=2)

        ourStateSets.append(alternatingAlgorithms.learnJointlyPenalizedMultipleModels(ourInitStates,
                                                                                      alpha,
                                                                                      maxSteps=10))

        lloydsInitStates = alternatingAlgorithms.lloydsInitialStates((x, y),
                                                                     weightedModelTypes.MultiQuadraticRegressionState,
                                                                     k=2)

        lloydsStateSets.append(alternatingAlgorithms.lloydsAlgorithm(lloydsInitStates, maxSteps=10))

    ourBestSSet = ourStateSets[array([utility.medianAbsLoss(sSet)
                                      for sSet
                                      in ourStateSets]).argmin()]

    lloydsBestSSet = lloydsStateSets[array([utility.medianAbsLoss(sSet)
                                            for sSet
                                            in lloydsStateSets]).argmin()]

    def epsCorrect(s, eps):
        return ((s.squaredLosses() < eps) * 1.).sum()

    eps = 0.25
    assert (sum([epsCorrect(s, eps) for s in ourBestSSet]) >=
            sum([epsCorrect(s, eps) for s in lloydsBestSSet]))
<commit_msg>Add a test for single model use.<commit_after>from nose.plugins.attrib import attr
from numpy.testing import assert_array_less
from numpy import array, hstack
from numpy.random import rand

import singleModelSpike as sms
from hopkinsTesting import runOnManyExtraNoise, findBalancedSets, manyNames, rest
import alternatingAlgorithms
import utility
import weightedModelTypes


@attr('difficult') # Requires the hopkins dataset to be present.
def testBeatLloydsOnRobustMeasureHopkins():
    "Our method vs Lloyd at high noise on two Hopkins datasets"
    imbalanceRatios, names = findBalancedSets(manyNames + rest)
    numberToRun = 2
    imbalanceRatios = imbalanceRatios[0:numberToRun]
    names = names[0:numberToRun]

    noiseLevel = 0.2
    multipleSummary = runOnManyExtraNoise(names,
                                          lambda sSet: utility.medianAbsLoss(sSet),
                                          iters=10, noiseLevel=noiseLevel)

    lloydsMedianAbsLoss, lloydsR, ourMedianAbsLoss, ourR, toplineQ, toplineR = zip(
        *[(allLloydQualities.min(),
           lloydRunMissRates[allLloydQualities.argmin()],
           allQualities.min(),
           missRates[allQualities.argmin()],
           toplineQuality, toplineMissRate)

          for (name, size, allQualities, allLloydQualities, toplineQuality,
               missRates, lloydRunMissRates, toplineMissRate)
          in multipleSummary])
    assert_array_less(array(ourMedianAbsLoss), array(lloydsMedianAbsLoss))


@attr('difficult') # Requires the hopkins dataset to be present.
def testBeatLloydsOnRobustMeasureQuadratic():
    "Our method vs Lloyds on quadratic regression, should have more eps correct answers."
    coeffs = [array([-0.04780819, -0.3844407, -1.73699185]),
              array([-1.44054051, -0.63069834, -0.9437272])]
    q1 = 40
    x1 = rand(1, 40)
    x2 = rand(1, 40)
    y1 = weightedModelTypes.quadraticKernelMapping(x1.T).dot(coeffs[0]) + 0.05 / rand(q1)
    y2 = weightedModelTypes.quadraticKernelMapping(x2.T).dot(coeffs[1]) + 0.05 / rand(q1)

    # some convenience
    x = hstack((x1, x2))
    y = hstack((y1, y2))
    y = y[x.argsort()].flatten()
    x.sort()
    _, q = x.shape

    alpha = 100

    ourStateSets = []
    lloydsStateSets = []
    for _ in range(20):
        ourInitStates = alternatingAlgorithms.jointlyPenalizedInitialStates((x, y),
                                                                            weightedModelTypes.MultiQuadraticRegressionState,
                                                                            alpha,
                                                                            k=2)

        ourStateSets.append(alternatingAlgorithms.learnJointlyPenalizedMultipleModels(ourInitStates,
                                                                                      alpha,
                                                                                      maxSteps=10))

        lloydsInitStates = alternatingAlgorithms.lloydsInitialStates((x, y),
                                                                     weightedModelTypes.MultiQuadraticRegressionState,
                                                                     k=2)

        lloydsStateSets.append(alternatingAlgorithms.lloydsAlgorithm(lloydsInitStates, maxSteps=10))

    ourBestSSet = ourStateSets[array([utility.medianAbsLoss(sSet)
                                      for sSet
                                      in ourStateSets]).argmin()]

    lloydsBestSSet = lloydsStateSets[array([utility.medianAbsLoss(sSet)
                                            for sSet
                                            in lloydsStateSets]).argmin()]

    def epsCorrect(s, eps):
        return ((s.squaredLosses() < eps) * 1.).sum()

    eps = 0.25
    assert (sum([epsCorrect(s, eps) for s in ourBestSSet]) >=
            sum([epsCorrect(s, eps) for s in lloydsBestSSet]))


def testSingleModel():
    from numpy import linspace, logspace

    alt_src_strengths = logspace(-1, 1, 2)
    noisy_props = linspace(0, 0.45, 2)
    sms.linear_regression_experiment_matrix_results2(10, 1000, noisy_props, alt_src_strengths)
{'fraction_non_alphanumeric': 0.0831572405220634, 'fraction_numerical': 0.027470478558110627, 'mean_word_length': 1.8431095406360425, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 70, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6776385', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2166, 'n_words': 453}