Ejemplo n.º 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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
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
Ejemplo n.º 6
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
Ejemplo n.º 7
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
Ejemplo n.º 8
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
Ejemplo n.º 9
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
Ejemplo n.º 10
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
Ejemplo n.º 11
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)
Ejemplo n.º 12
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))
Ejemplo n.º 13
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
Ejemplo n.º 14
0
def test_CurveT_d2fdx2(X, Y, i):
    ''' pytest test case
    '''
    s = CurveT(X, Y, i)

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

    for j in range(len(X) - 1):
        xj = X[j]
        deri = (s.eval(xj + CurveT.DX * 2) - 2 * s.eval(xj + CurveT.DX) +
                s.eval(xj)) / (CurveT.DX**2)
        assert isCloseEnough(s.d2fdx2(xj), deri)
Ejemplo n.º 15
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)
Ejemplo n.º 16
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)
Ejemplo n.º 17
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)
Ejemplo n.º 18
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
Ejemplo n.º 19
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')
Ejemplo n.º 20
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)
Ejemplo n.º 21
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)
Ejemplo n.º 22
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)
Ejemplo n.º 23
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)
Ejemplo n.º 24
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)
Ejemplo n.º 25
0
def test_CurveT_npolyVal():
    with open('tmp_file', 'w') as f:
        f.write('\n'.join(
            ['1.0, 10.0', '2.0, 20.0', '3.0, 50.0', '4.0, 30.0', '5.0, 10.0']))
    try:
        curve = CurveT('tmp_file')
        assert (isClose(curve.npolyVal(1, 1.5), 22.5)
                and isClose(curve.npolyVal(1, 2.5), 23.5)
                and isClose(curve.npolyVal(1, 3.5), 24.5))
        assert (isClose(curve.npolyVal(2, 1.5), 20.5357143)
                and isClose(curve.npolyVal(2, 2.5), 37.25)
                and isClose(curve.npolyVal(2, 3.5), 38.25))
        assert (isClose(curve.npolyVal(3, 1.5), 17.66071429)
                and isClose(curve.npolyVal(3, 2.5), 34.625)
                and isClose(curve.npolyVal(3, 3.5), 40.875))
        print('test of CurveT.npolyVal PASSED.')
    except (AssertionError, ValueError, IndexError):
        print('test of CurveT.npolyVal FAILED.')
Ejemplo n.º 26
0
def test_Data_slice():
    Data.init()
    X = [i * 1.0 for i in range(10)]
    Y = [i**0.5 * 10.0 for i in range(10)]
    z = [i**2 for i in range(10)]

    for i in range(10):
        Data.add(CurveT(list(X), list(Y), 1), z[i])
        Y = [y + 5.0 for y in Y]
    s = Data.slice(0.0, 1)
    assert (s.minD() == 0.0 and s.maxD() == 81.0 and s.order() == 1)
    assert s.eval(0.0) == 0.0
    assert s.eval(64.0) == 40.0

    s = Data.slice(8.0, 2)
    assert (s.minD() == 0.0 and s.maxD() == 81.0 and s.order() == 2)
    assert abs(s.eval(1.0) - 33.284271) < 1e-6
    assert abs(s.eval(64.0) - 68.284271) < 1e-6
Ejemplo n.º 27
0
def Load(s):
    Data.init()
    with open(s, 'r') as f:
        row = next(f)
        row = list(map(float, row.split(',')))
        Z = row
        n = len(Z)
        row = next(f)
        row = list(map(int, row.split(',')))
        O = row
        X_Y = [[] for _ in range(n + n)]
        for row in f:
            row = [e.strip() for e in row.split(',')]
            for i, e in enumerate(row):
                if e:
                    X_Y[i].append(float(e))
        for i, o in enumerate(O):
            Data.add(CurveT(X_Y[i + i], X_Y[i + i + 1], o), Z[i])
Ejemplo n.º 28
0
def test_Data_getC():
    Data.init()
    X = [0.0, 1.0]
    Y = [0.0, 10.0]
    z = 0.0
    Orders = []
    for i in range(10):
        X.append(X[-1] + 1.0)
        Y.append(Y[-1]**.5 + 10.0)
        z += 0.1
        o = random.randint(1, 2)
        Orders.append(o)
        Data.add(CurveT(list(X), list(Y), o), z)
    for i in range(10):
        s = Data.getC(i)
        assert (s.minD() == 0.0 and s.maxD() == X[i + 2]
                and s.order() == Orders[i])
    with pytest.raises(InvalidIndex):
        Data.getC(-1)
    with pytest.raises(InvalidIndex):
        Data.getC(10)
Ejemplo n.º 29
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))
Ejemplo n.º 30
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))