Пример #1
0
def test_Data_slice(X, Y, i):
    ''' pytest test case
    '''
    Data.init()
    Data_Z = []
    Data_S = []
    z = X[0]
    for z in np.linspace(X[0], X[-1], Data.MAX_SIZE):
        Data_Z.append(z)
        s = CurveT(X, Y, i)
        Data_S.append(s)
        Data.add(s, z)
        for k in range(len(X)):
            Y[k] += 1

    for i in range(len(X) - 1):
        xi = X[i]
        xi1 = X[i + 1]
        for x in (xi, (xi + xi1) / 2):
            Data_Y = []
            for j in range(len(Data_S)):
                s = Data_S[j]
                y = s.eval(x)
                Data_Y.append(y)
            s1 = Data.slice(x, 1)
            s2 = CurveT(Data_Z, Data_Y, 1)
            assert isSame(s1, s2)
Пример #2
0
def test_CurveT_maxD():
    X = [0.4, 1.8, 2.5, 3.1]
    Y = [1, 1, 1, 1]
    assert CurveT(X, Y, 1).maxD() == 3.1
    assert CurveT(X, Y, 2).maxD() == 3.1
    X = [1, 1, 1, 1]
    assert CurveT(X, Y, 1).maxD() == 1
    assert CurveT(X, Y, 2).maxD() == 1
Пример #3
0
def test_CurveT_minD():
    X = [0.4, 1.8, 2.5, 3.1]
    Y = [1, 1, 1, 1]
    assert CurveT(X, Y, 1).minD() == 0.4
    assert CurveT(X, Y, 2).minD() == 0.4
    X = [1, 1, 1, 1]
    assert CurveT(X, Y, 1).minD() == 1
    assert CurveT(X, Y, 2).minD() == 1
Пример #4
0
def test_Data_eval():
    Data.init()
    X = [0.0, 1.0]
    Y = [0.0, 10.0]
    z = 0.0
    for i in range(10):
        X.append(X[-1] + 1.0)
        Y.append(Y[-1] + 10.0)
        z += 0.1
        Data.add(CurveT(list(X), list(Y), 1), z)
    with pytest.raises(OutOfDomain):
        Data.eval(0.0, 0.0)
    with pytest.raises(OutOfDomain):
        Data.eval(0.0, 1.0)
    z = 0.0
    for x in range(9):
        z += 0.1
        assert Data.eval(x, z) == Y[x]
        assert Data.eval(x + 0.5, z) == Y[x] + 5.0
    Data.init()
    X = [0.0, 1.0]
    Y = [0.0, 10.0]
    z = 0.0
    for i in range(10):
        X.append(X[-1] + 1.0)
        Y.append(Y[-1]**.5 + 10.0)
        z += 0.1
        Data.add(CurveT(list(X), list(Y), 2), z)
    with pytest.raises(OutOfDomain):
        Data.eval(0.0, 0.0)
    with pytest.raises(OutOfDomain):
        Data.eval(0.0, 1.0)
    z = 0.0
    delta = 999999
    Y_ = []
    for x in range(9):
        z += 0.1
        assert Data.eval(x, z) == Y[x]
        delta_ = Data.eval(x + 0.5, z) - Y[x]
        assert delta_ < delta
        delta = delta_
        Y_.append(Data.eval(x + 0.5, z))
    assert delta < 1e-5
    Y_expected = [
        5.854715, 12.435854, 13.732203, 13.710058, 13.702791, 13.701730,
        13.701585, 13.701565, 13.701563
    ]
    for y_, y_expected in zip(Y_, Y_expected):
        assert abs(y_ - y_expected) < 1e-6
Пример #5
0
def test_Data_add():
    Data.init()
    Data.add(CurveT([0, 1], [2, 0], 1), -1.0)
    with pytest.raises(IndepVarNotAscending):
        Data.add(CurveT([0, 1], [2, 0], 1), -1.0)
    for z in range(9):
        Data.add(CurveT([-1, 0, 1], [0, 1, 3], 2), z)
    with pytest.raises(Full):
        Data.add(CurveT([-1, 0, 1], [0, 1, 3], 2), z)
    assert (Data.getC(0).minD() == 0.0 and Data.getC(0).maxD() == 1.0
            and Data.getC(0).order() == 1)
    assert (Data.getC(1).minD() == -1.0 and Data.getC(1).maxD() == 1.0
            and Data.getC(1).order() == 2)
    assert (Data.getC(9).minD() == -1.0 and Data.getC(9).maxD() == 1.0
            and Data.getC(9).order() == 2)
Пример #6
0
def Load(s):
    Data.init()
    with open(s, 'r') as infile:
        i = 0
        for line in infile:
            if i == 0:
                Data_Z = line.split(',')
                __toFloat__(Data_Z)
                Xs = []
                Ys = []
                for j in range(len(Data_Z)):
                    Xs.append([])
                    Ys.append([])
                i += 1
            elif i == 1:
                curve_o = line.split(',')
                __toFloat__(curve_o)
                i += 1
            else:
                values = line.split(',')
                __toFloat__(values)
                for j in range(len(values)):
                    value = values[j]
                    if value is not None:
                        k = j // 2
                        if j % 2 == 0:
                            Xs[k].append(value)
                        else:
                            Ys[k].append(value)
        for j in range(len(Data_Z)):
            X = Xs[j]
            Y = Ys[j]
            s = CurveT(X, Y, curve_o[j])
            z = Data_Z[j]
            Data.add(s, z)
Пример #7
0
def test_Data_eval(X, Y, i):
    ''' pytest test case
    '''
    Data.init()
    Data_Z = []
    Data_S = []
    z = X[0]
    for z in np.linspace(X[0], X[-1], Data.MAX_SIZE):
        Data_Z.append(z)
        s = CurveT(X, Y, i)
        Data_S.append(s)
        Data.add(s, z)
        for k in range(len(X)):
            Y[k] += 1

    for i in range(Data.MAX_SIZE - 1):
        xi = Data_Z[i]
        xi1 = Data_Z[i + 1]
        for z in (xi, (xi + xi1) / 2):
            yi = Data_S[i].eval(xi)
            yi1 = Data_S[i + 1].eval(xi)
            y1 = Data.eval(xi, z)
            y2 = interpLin(xi, yi, xi1, yi1, z)
            assert isCloseEnough(y1, y2)
            yi = Data_S[i].eval((xi + xi1) / 2)
            yi1 = Data_S[i + 1].eval((xi + xi1) / 2)
            y1 = Data.eval((xi + xi1) / 2, z)
            y2 = interpLin(xi, yi, xi1, yi1, z)
            assert isCloseEnough(y1, y2)

    with pytest.raises(OutOfDomain):
        Data.eval(xi, Data_Z[0] - 1e-5)
    with pytest.raises(OutOfDomain):
        Data.eval(xi, Data_Z[-1] + 1e-5)
Пример #8
0
def test_CurveT_linVal():
    with open('tmp_file', 'w') as f:
        f.write('\n'.join(
            ['1.0, 10.0', '2.0, 20.0', '3.0, 30.0', '4.0, 40.0', '5.0, 50.0']))
    try:
        curve = CurveT('tmp_file')
        assert (isClose(curve.linVal(1.5), 15.0)
                and isClose(curve.linVal(2.5), 25.0)
                and isClose(curve.linVal(3.5), 35.0))
        assert (isClose(curve.linVal(1.001), 10.01)
                and isClose(curve.linVal(2.001), 20.01)
                and isClose(curve.linVal(3.001), 30.01))
        assert (isClose(curve.linVal(1.999), 19.99)
                and isClose(curve.linVal(2.999), 29.99)
                and isClose(curve.linVal(3.999), 39.99))
        try:
            curve.linVal(0.5)
            raise AssertionError
        except ValueError:
            pass
        try:
            curve.linVal(5.5)
            raise AssertionError
        except ValueError:
            pass
        print('test of CurveT.linVal PASSED.')
    except (AssertionError, ValueError, IndexError):
        print('test of CurveT.linVal FAILED.')
Пример #9
0
def test_CurveT_eval(X, Y, i):
    ''' pytest test case
    '''
    s = CurveT(X, Y, i)

    with pytest.raises(OutOfDomain):
        s.eval(X[0] - 1e-5)
    with pytest.raises(OutOfDomain):
        s.eval(X[-1] + 1e-5)

    o = i

    for i in range(len(X) - 1):
        xi = X[i]
        yi = Y[i]
        if o == 1 or i > 0:
            assert isCloseEnough(s.eval(xi), yi)
        xi1 = X[i + 1]
        if o == 1:
            f = interpolate.interp1d(X[i:i + 2], Y[i:i + 2], kind='linear')
            assert isCloseEnough(s.eval((xi + xi1) / 2), f((xi + xi1) / 2))
        else:
            if i == 0:
                f = interpolate.interp1d(X[i:i + 3],
                                         Y[i:i + 3],
                                         kind='quadratic')
            else:
                f = interpolate.interp1d(X[i - 1:i + 2],
                                         Y[i - 1:i + 2],
                                         kind='quadratic')
            assert isCloseEnough(s.eval((xi + xi1) / 2), f((xi + xi1) / 2))
Пример #10
0
def program():
    testCurve = CurveT()

    testCurve.CurveT('input.txt')
    n = input("Enter the order of polynomial function: ")
    x = input("Enter x value you wish to know y value for: ")

    if (n == 1):
        linTest = testCurve.linVal(x)
        print("linVal: {}".format(linTest))
    elif (n == 2):
        polyTest = testCurve.quadVal(x)
        print("polyVal results : {}".format(polyTest))
    else:
        y = testCurve.npolyval(n, x)
        print("The y value of %f is %f" % (x, y))
Пример #11
0
 def slice(x, i):
     Y = []
     for j in range(len(Data.S)):
         s = Data.S[j]
         y = s.eval(x)
         Y.append(y)
     s = CurveT(Data.Z, Y, i)
     return s
Пример #12
0
def test_dfdx():

    testX = [1, 2, 3, 4, 5, 6, 7]
    testY = [1, 4, 9, 16, 25, 36, 49]

    a = CurveT(testX, testY, 2)

    assert (a.dfdx(2) == 4)  #Fail due to floating point approximation
Пример #13
0
def test_CurveOrder():

    testX = [1, 2, 3, 4, 5, 6, 7]
    testY = [1, 4, 9, 16, 25, 36, 49]

    a = CurveT(testX, testY, 2)

    assert (a.order() == 2)
Пример #14
0
def test_CurveT_init(X, Y, i):
    ''' pytest test case
    '''
    if isAscending(X) is False:
        with pytest.raises(IndepVarNotAscending):
            CurveT(X, Y, i)
    elif len(X) != len(Y):
        with pytest.raises(SeqSizeMismatch):
            CurveT(X, Y, i)
    elif i <= 0:
        with pytest.raises(InvalidInterpOrder):
            CurveT(X, Y, i)
    elif i > CurveT.MAX_ORDER:
        with pytest.raises(InvalidInterpOrder):
            CurveT(X, Y, i)
    else:
        CurveT(X, Y, i)
Пример #15
0
def test_CurveMin():  #Start tests for CurveADT normal cases

    testX = [1, 2, 3, 4, 5, 6, 7]
    testY = [1, 4, 9, 16, 25, 36, 49]

    a = CurveT(testX, testY, 2)

    assert (a.minD() == 1)  #Normal cases
Пример #16
0
def test_df2dx2():  #End tests for CurveADT normal cases

    testX = [1, 2, 3, 4, 5, 6, 7]
    testY = [1, 4, 9, 16, 25, 36, 49]

    a = CurveT(testX, testY, 2)

    assert (a.d2fdx2(2) == 2)  #Fail due to floating point approximation
Пример #17
0
def test_CurveMinExc():  #Start tests for CurveADT Exception cases

    testX = [1, 2, 1, 4, 5, 6, 7]
    testY = [1, 4, 9, 16, 25, 36, 49]

    a = CurveT(testX, testY, 2)

    assert (a.minD() == None)  #Fail due to exception thrown at init
Пример #18
0
def test_CurveOrderHigh():  #End tests for CurveADT Exception cases

    testX = [1, 2, 3, 4, 5, 6, 7]
    testY = [1, 4, 9, 16, 25, 36, 49]

    a = CurveT(testX, testY, 3)

    assert (a.order() == None)  #Fail due to exception thrown at init
Пример #19
0
def test_CurveMaxExc():

    testX = [1, 2, 3, 4, 5, 6, 7, 8]
    testY = [1, 4, 9, 16, 25, 36, 49]

    a = CurveT(testX, testY, 2)

    assert (a.maxD() == None)  #Fail due to exception thrown at init
Пример #20
0
def test_CurveMax():

    testX = [1, 2, 3, 4, 5, 6, 7]
    testY = [1, 4, 9, 16, 25, 36, 49]

    a = CurveT(testX, testY, 2)

    assert (a.maxD() == 7)
Пример #21
0
def test_CurveT_init():
    for X, Y, i, exp in (
        ([1, 2], [3, 4], 1, None),
        ([2, 1], [1, 1], 1, IndepVarNotAscending),
        ([1, 1, 3], [1, 1], 1, SeqSizeMismatch),
        ([1, 1], [1, 1, 1], 1, SeqSizeMismatch),
        ([1, 2], [1, 1], 0, InvalidInterpOrder),
        ([1, 2], [1, 1], 3, InvalidInterpOrder),
    ):
        if exp:
            with pytest.raises(exp):
                CurveT(X, Y, i)
        else:
            try:
                CurveT(X, Y, i)
            except Exception:
                pytest.fail('Must have no exceptions')
Пример #22
0
    def slice(x, i):

        Y = []
        j = 0
        while (j < len(Data.S)):
            Y.append(int(Data.S[j][1]))
            j += 1

        return CurveT(Data.Z, Y, i)
Пример #23
0
def test_Data_init():
    Data.init()
    with pytest.raises(InvalidIndex):
        Data.getC(0)
    Data.add(CurveT([0, 1], [2, 0], 1), -1.0)
    Data.getC(0)
    Data.init()
    with pytest.raises(InvalidIndex):
        Data.getC(0)
Пример #24
0
def main():
    newCurve = CurveT()

    #filename = raw_input("What is the name of the file?: ")
    filename = "linear.txt"
    newCurve.CurveT(filename)
    #xVal = input("What value of x would you like to find the y-value for?: ")
    xVal = newCurve.curveX.

    if (newCurve.degree == 1):
        linTest = newCurve.linVal(xVal)
        print("linval: %f" % linTest)
    elif (newCurve.degree == 2):
        polyTest = newCurve.quadVal(xVal)
        print("polyval: %f" % polyTest)

    yVal = newCurve.npolyval(xVal)

    print("The y value of %f is %f" % (xVal, yVal))
Пример #25
0
def test_CurveT_d2fdx2():
    X = [0, 1]
    Y = [2, 0]
    s = CurveT(X, Y, 1)
    with pytest.raises(OutOfDomain):
        s.d2fdx2(-1.0)
    with pytest.raises(OutOfDomain):
        s.d2fdx2(2.0)
    assert s.d2fdx2(0.0) == 0.0
    assert s.d2fdx2(0.5) == 0.0
    X = [-1, 0, 1]
    Y = [0, 1, 3]
    s = CurveT(X, Y, 2)
    with pytest.raises(OutOfDomain):
        s.eval(-2.0)
    with pytest.raises(OutOfDomain):
        s.eval(4.0)
    assert abs(s.d2fdx2(0.0) - 1.0) < 1e-6
    assert abs(s.d2fdx2(0.5) - 1.0) < 1e-6
Пример #26
0
def test_CurveT_dfdx():
    X = [0, 1]
    Y = [2, 0]
    s = CurveT(X, Y, 1)
    with pytest.raises(OutOfDomain):
        s.dfdx(-1.0)
    with pytest.raises(OutOfDomain):
        s.dfdx(2.0)
    assert abs(s.dfdx(0.0) - -2.0) < 1e-6
    assert abs(s.dfdx(0.5) - -2.0) < 1e-6
    X = [-1, 0, 1]
    Y = [0, 1, 3]
    s = CurveT(X, Y, 2)
    with pytest.raises(OutOfDomain):
        s.eval(-2.0)
    with pytest.raises(OutOfDomain):
        s.eval(4.0)
    assert abs(s.dfdx(0.0) - 1.5005) < 1e-6
    assert abs(s.dfdx(0.5) - 2.0005) < 1e-6
Пример #27
0
def test_CurveT_eval():
    X = [0, 1]
    Y = [2, 0]
    s = CurveT(X, Y, 1)
    with pytest.raises(OutOfDomain):
        s.eval(-1.0)
    with pytest.raises(OutOfDomain):
        s.eval(2.0)
    assert s.eval(0.0) == 2.0
    assert s.eval(0.5) == 1.0
    X = [-1, 0, 1]
    Y = [0, 1, 3]
    s = CurveT(X, Y, 2)
    with pytest.raises(OutOfDomain):
        s.eval(-2.0)
    with pytest.raises(OutOfDomain):
        s.eval(4.0)
    assert s.eval(-0.5) == 0.375
    assert s.eval(0.0) == 1.0
    assert s.eval(0.5) == 1.875
Пример #28
0
def test_CurveT_quadVal():
    with open('tmp_file', 'w') as f:
        f.write('\n'.join(
            ['1.0, 5.0', '2.0, 20.0', '3.0, 25.0', '4.0, 40.0', '5.0, 41.0']))
    try:
        curve = CurveT('tmp_file')
        assert (isClose(curve.quadVal(1.5), 13.75)
                and isClose(curve.quadVal(2.5), 23.75)
                and isClose(curve.quadVal(3.5), 31.25))
        assert (isClose(curve.quadVal(1.01), 5.1995)
                and isClose(curve.quadVal(2.01), 20.0995)
                and isClose(curve.quadVal(3.01), 25.1005))
        assert (isClose(curve.quadVal(1.99), 19.8995)
                and isClose(curve.quadVal(2.99), 24.9995)
                and isClose(curve.quadVal(3.99), 39.8005))
        try:
            curve.quadVal(0.5)
            raise AssertionError
        except ValueError:
            pass
        try:
            curve.quadVal(5.5)
            raise AssertionError
        except ValueError:
            pass
        print('test of CurveT.quadVal PASSED.')
    except (AssertionError, ValueError, IndexError):
        print('test of CurveT.quadVal FAILED.')

    with open('tmp_file', 'w') as f:
        f.write('\n'.join(['1.0, 5.0', '2.0, 20.0']))
    try:
        curve = CurveT('tmp_file')
        try:
            curve.quadVal(1.5)
            raise AssertionError
        except ValueError as e:
            pass
        print('test of CurveT.quadVal PASSED.')
    except (AssertionError, ValueError, IndexError):
        print('test of CurveT.quadVal FAILED.')
Пример #29
0
def test_Data_add(X, Y, i):
    ''' pytest test case
    '''
    Data.init()
    s = CurveT(X, Y, i)
    Data.add(s, 0)
    Data.init()
    z = 0
    for j in range(Data.MAX_SIZE):
        s = CurveT(X, Y, i)
        Data.add(s, z)
        z += 0.5
    with pytest.raises(Full):
        Data.add(s, z)
    Data.init()
    Data.add(s, z)
    with pytest.raises(IndepVarNotAscending):
        Data.add(s, z)
    z -= 1e-5
    with pytest.raises(IndepVarNotAscending):
        Data.add(s, z)
Пример #30
0
def test_Data_getC(X, Y, i):
    ''' pytest test case
    '''
    Data.init()
    z = 0
    for j in range(Data.MAX_SIZE):
        s = CurveT(X, Y, i)
        Data.add(s, z)
        for k in range(len(X)):
            X[k] += 2
        z += 1.1
    for j in reversed(range(Data.MAX_SIZE)):
        for k in range(len(X)):
            X[k] -= 2
        s1 = Data.getC(j)
        s2 = CurveT(X, Y, i)
        assert isSame(s1, s2)
    for j in range(-10, 0):
        with pytest.raises(InvalidIndex):
            Data.getC(j)
    for j in range(Data.MAX_SIZE, Data.MAX_SIZE + 10):
        with pytest.raises(InvalidIndex):
            Data.getC(j)