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)
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)
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)
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)
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
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
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])
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)
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)
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)
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)