def test_layering_index(s1_fix): result = Stairs( start=pd.Index([1, -4, 3, 6, 7]), end=pd.Index([10, 5, 5, 7, 10]), value=pd.Index([2, -1.75, 2.5, -2.5, -2.5]), ) assert result.identical(s1_fix)
def s2(date_func): int_seq2 = Stairs(initial_value=0) int_seq2.layer( timestamp(2020, 1, 1, date_func=date_func), timestamp(2020, 1, 7, date_func=date_func), -2.5, ) int_seq2.layer( timestamp(2020, 1, 8, date_func=date_func), timestamp(2020, 1, 10, date_func=date_func), 5, ) int_seq2.layer( timestamp(2020, 1, 2, date_func=date_func), timestamp(2020, 1, 5, date_func=date_func), 4.5, ) int_seq2.layer( timestamp(2020, 1, 2, 12, date_func=date_func), timestamp(2020, 1, 4, date_func=date_func), -2.5, ).layer( timestamp(2019, 12, 29, date_func=date_func), timestamp(2020, 1, 1, date_func=date_func), -1.75, ) return int_seq2
def test_from_values_exception(index, values): with pytest.raises(ValueError): Stairs.from_values( initial_value=0, values=pd.Series(values, index=index), closed="left", )
def test_shift(date_func): ans = Stairs(initial_value=0) ans.layer( timestamp(2020, 1, 2, date_func=date_func), timestamp(2020, 1, 11, date_func=date_func), 2, ) ans.layer( timestamp(2020, 1, 4, date_func=date_func), timestamp(2020, 1, 6, date_func=date_func), 2.5, ) ans.layer( timestamp(2020, 1, 7, date_func=date_func), timestamp(2020, 1, 8, date_func=date_func), -2.5, ) ans.layer( timestamp(2020, 1, 8, date_func=date_func), timestamp(2020, 1, 11, date_func=date_func), -2.5, ) result = s1(date_func).shift(pd.Timedelta(24, unit="H")) assert bool(result == ans) assert_expected_type(result, date_func)
def s1(date_func): int_seq1 = Stairs(initial_value=0) int_seq1.layer( timestamp(2020, 1, 1, date_func=date_func), timestamp(2020, 1, 10, date_func=date_func), 2, ).layer( timestamp(2019, 12, 27, date_func=date_func), timestamp(2020, 1, 5, date_func=date_func), -1.75, ) int_seq1.layer( timestamp(2020, 1, 3, date_func=date_func), timestamp(2020, 1, 5, date_func=date_func), 2.5, ) int_seq1.layer( timestamp(2020, 1, 6, date_func=date_func), timestamp(2020, 1, 7, date_func=date_func), -2.5, ) int_seq1.layer( timestamp(2020, 1, 7, date_func=date_func), timestamp(2020, 1, 10, date_func=date_func), -2.5, ) return int_seq1
def test_make_boolean(s2_fix): int_seq = s2_fix calc = int_seq.make_boolean() expected = Stairs() expected.layer(-2, 7, 1) expected.layer(8, 10, 1) assert calc.identical(expected), "Boolean calculation not what it should be" assert expected.identical(calc), "Boolean calculation not what it should be"
def test_invert(s2_fix): int_seq = s2_fix calc = ~int_seq expected = Stairs(initial_value=1) expected.layer(-2, 7, -1) expected.layer(8, 10, -1) assert calc.identical(expected), "Invert calculation not what it should be" assert expected.identical(calc), "Invert calculation not what it should be"
def s2(): int_seq2 = Stairs(initial_value=0) int_seq2.layer(1, 7, -2.5) int_seq2.layer(8, 10, 5) int_seq2.layer(2, 5, 4.5) int_seq2.layer(2.5, 4, -2.5) int_seq2.layer(-2, 1, -1.75) return int_seq2
def test_from_values_exception(index, values): with pytest.raises(ValueError): kwargs = {"dtype": "float64"} if values == [] else {} Stairs.from_values( initial_value=0, values=pd.Series(values, index=index, **kwargs), closed="left", )
def s4(): # boolean int_seq = Stairs(initial_value=0) int_seq.layer(-11, 9, 1) int_seq.layer(-9.5, -8, -1) int_seq.layer(-7.5, -7, -1) int_seq.layer(0, 3, -1) int_seq.layer(6, 6.5, -1) int_seq.layer(7, 8.5, -1) return int_seq
def test_layer2(init_value): intervals_to_add = [(-2, 1, 1), (3, 5, 2), (1, 5, -1), (-5, -3, 3)] int_seq = Stairs(initial_value=init_value) int_seq2 = Stairs(initial_value=init_value) for interval in intervals_to_add: int_seq.layer(*interval) starts, ends, values = list(zip(*intervals_to_add)) int_seq2.layer(starts, ends, values) assert int_seq.identical(int_seq2) assert int_seq2.identical(int_seq)
def test_layer1(init_value): intervals_to_add = [(-2, 1), (3, 5), (1, 5), (-5, -3), (None, 0), (0, None)] int_seq = Stairs(initial_value=init_value) int_seq2 = Stairs(initial_value=init_value) for start, end in intervals_to_add: int_seq.layer(start, end) starts, ends = list(zip(*intervals_to_add)) starts = [{None: np.nan}.get(x, x) for x in starts] ends = [{None: np.nan}.get(x, x) for x in ends] int_seq2.layer(starts, ends) assert int_seq.identical(int_seq2) assert int_seq2.identical(int_seq)
def s2_adj(): return (Stairs().layer(start=-2, value=-1.75).layer( start=1, value=-0.75).layer(start=2, value=4.5).layer( start=2.5, value=-2.5).layer(start=7, value=2.5).layer( start=8, value=-4.5).layer(start=10, value=2.5).layer( start=11, value=5).layer(start=13, value=-5).mask( (4, 7)).mask((None, -2)))
def test_layering_frame(s1_fix): df = pd.DataFrame({ "start": [1, -4, 3, 6, 7], "end": [10, 5, 5, 7, 10], "value": [2, -1.75, 2.5, -2.5, -2.5], }) assert Stairs(df, "start", "end", "value").identical(s1_fix)
def s3(): # boolean int_seq = Stairs(initial_value=0) int_seq.layer(-10, 10, 1) int_seq.layer(-8, -7, -1) int_seq.layer(-5, -2, -1) int_seq.layer(0.5, 1, -1) int_seq.layer(3, 3.5, -1) int_seq.layer(7, 9.5, -1) return int_seq
def test_init4(init_value): int_seq = Stairs(initial_value=init_value) assert (int_seq(-1) == init_value ), "Initialised Stairs should have initial value everywhere" assert (int_seq(0) == init_value ), "Initialised Stairs should have initial value everywhere" assert (int_seq(1) == init_value ), "Initialised Stairs should have initial value everywhere"
def test_eq_2(s1_fix, s2_fix): calc = s1_fix == s2_fix expected = Stairs(initial_value=1) expected.layer(-4, -2, -1) expected.layer(1, 10, -1) assert calc.identical(expected), "EQ calculation not what it should be" assert expected.identical(calc), "EQ calculation not what it should be"
def test_gt(s1_fix, s2_fix): calc = s1_fix > s2_fix expected = Stairs(initial_value=0) expected.layer(1, 2) expected.layer(2.5, 7) assert calc.identical(expected), "GT calculation not what it should be" assert expected.identical(calc), "GT calculation not what it should be"
def test_le(s1_fix, s2_fix): calc = s1_fix <= s2_fix expected = Stairs(initial_value=1) expected.layer(1, 2, -1) expected.layer(2.5, 7, -1) assert calc.identical(expected), "LE calculation not what it should be" assert expected.identical(calc), "LE calculation not what it should be"
def test_lt(s1_fix, s2_fix): calc = s1_fix < s2_fix expected = Stairs(initial_value=0) expected.layer(-4, -2) expected.layer(2, 2.5) expected.layer(7, 10) assert calc.identical(expected), "LT calculation not what it should be" assert expected.identical(calc), "LT calculation not what it should be"
def test_ge(s1_fix, s2_fix): calc = s1_fix >= s2_fix expected = Stairs(initial_value=1) expected.layer(-4, -2, -1) expected.layer(2, 2.5, -1) expected.layer(7, 10, -1) assert calc.identical(expected), "GE calculation not what it should be" assert expected.identical(calc), "GE calculation not what it should be"
def test_two_adjacent_finite_interval_same_value(init_value, endpoints, value): e = 0.0001 int_seq = Stairs(initial_value=init_value) point1, point2, point3 = endpoints int_seq.layer(point1, point2, value) int_seq.layer(point2, point3, value) assert int_seq.number_of_steps == 2, "Expected result to be 3 intervals" assert _compare_iterables( int_seq.step_points, (point1, point3)), "Finite endpoints are not what is expected" assert (int_seq(float("-inf")) == init_value ), "Adding finite interval should not change initial value" assert (int_seq(float("inf")) == init_value ), "Adding finite interval should not change final value" assert int_seq(point1 - e) == init_value assert int_seq(point1) == init_value + value assert int_seq(point2) == init_value + value assert int_seq(point3 - e) == init_value + value assert int_seq(point3) == init_value
def test_closed_binary_ops(op, closed, operands, stairs_kwargs): operand_dict = { "stairs": Stairs(closed=closed, **stairs_kwargs), "scalar": 1 } operand0 = operand_dict[operands[0]] operand1 = operand_dict[operands[1]] result = op(operand0, operand1) assert (result.closed == closed ), f"result has closed value of {result.closed}, expected {closed}"
def test_from_values(initial_value, closed): # this corresponds to the step function produced by S1 method values = pd.Series([-1.75, 0.25, 2.75, 2.00, -0.5, 0], index=[-4, 1, 3, 5, 6, 10]) sf = Stairs.from_values( initial_value=initial_value, values=values + initial_value, closed=closed, ) assert sf.identical(s1(closed) + initial_value)
def test_ne(s1_fix, s2_fix): calc = s1_fix != s2_fix expected = Stairs(initial_value=0) expected.layer(-4, -2, 1) expected.layer(1, 10, 1) assert calc.identical( expected), "NOT EQUAL calculation not what it should be" assert expected.identical( calc), "NOT EQUAL calculation not what it should be"
def test_layering_empty_start(start, end): result = Stairs(start=start, end=end) expected = pd.Series([1, 0], index=end) pd.testing.assert_series_equal( result.step_values, expected, check_names=False, check_index_type=False, check_dtype=False, ) assert result.initial_value == 2
def test_one_finite_interval(init_value, added_interval): e = 0.0001 int_seq = Stairs(initial_value=init_value) int_seq.layer(*added_interval) start, end, value = _expand_interval_definition(*added_interval) assert int_seq.number_of_steps == 2 - ( end is None ), "One finite interval added to initial infinite interval should result in 3 intervals" assert _compare_iterables( int_seq.step_points, (start, end)), "Finite endpoints are not what is expected" assert (int_seq(float("-inf")) == init_value ), "Adding finite interval should not change initial value" assert int_seq(float("inf")) == init_value + value * ( end is None), "Adding finite interval should not change final value" assert int_seq(start - e) == init_value assert int_seq(start) == init_value + value assert int_seq(start + e) == init_value + value if end is not None: assert int_seq(end - e) == init_value + value assert int_seq(end) == init_value
def test_scalar_divide(): s = Stairs().layer([1, 2, 5], [3, 4, 7], [1, -1, 2]) assert pd.Series.equals( (2 / s).step_values, pd.Series({ 1: 2.0, 2: np.nan, 3: -2.0, 4: np.nan, 5: 1.0, 7: np.nan, }), )
def test_layering_series_with_different_index(): # GH112 result = Stairs( start=pd.Series([0, 2, 4], index=[0, 2, 4]), end=pd.Series([1, 3, 5], index=[1, 3, 5]), ) expected = pd.Series([1, 0, 1, 0, 1, 0]) pd.testing.assert_series_equal( result.step_values, expected, check_names=False, check_index_type=False, check_dtype=False, ) assert result.initial_value == 0
def test_from_values(date_func): # this corresponds to the step function produced by S1 method values = pd.Series( [2, 4.5, 2, -0.5, 0], index=[ timestamp(2020, 1, 1, date_func=date_func), timestamp(2020, 1, 3, date_func=date_func), timestamp(2020, 1, 5, date_func=date_func), timestamp(2020, 1, 6, date_func=date_func), timestamp(2020, 1, 10, date_func=date_func), ], ) sf = Stairs.from_values( initial_value=0, values=values, ) print(sf._data) print(s1(date_func)._data) assert sf.identical(s1(date_func))