def test_max(): s1 = Stream.source([-1, 2, -3, 4, -5], dtype="float") s2 = Stream.source([-1, 2, 3, 2, 1], dtype="float") w = s1.max(s2).rename("w") expected = [-1, 2, 3, 4, 1] assert_op([w], expected)
def test_reduce_prod(): s1 = Stream.source([1, 2, 3, 4, 5, 6, 7], dtype="float").rename("s1") s2 = Stream.source([3, 6, 4, 2, 0, 4, 10], dtype="float").rename("s2") w = Stream.reduce([s1, s2]).prod().rename("w") expected = [3, 12, 12, 8, 0, 24, 70] assert_op([w], expected)
def test_reduce_sum(): s1 = Stream.source([1, 2, 3, 4, 5, 6, 7], dtype="float").rename("s1") s2 = Stream.source([7, 6, 5, 4, 3, 2, 1], dtype="float").rename("s2") w = Stream.reduce([s1, s2]).sum().rename("w") expected = [8, 8, 8, 8, 8, 8, 8] assert_op([w], expected)
def test_reduce_max(): s1 = Stream.source([1, 2, 3, 4, 5, 6, 7], dtype="float").rename("s1") s2 = Stream.source([-3, 6, 4, 2, 0, 4, 10], dtype="float").rename("s2") w = Stream.reduce([s1, s2]).max().rename("w") expected = [1, 6, 4, 4, 5, 6, 10] assert_op([w], expected)
def test_generic(): s1 = Stream.source(["hello", "my", "name", "is"], dtype="string") s2 = Stream.source([1, 2, 3, 4, 5, 6]) g1 = s1.apply(lambda x: x[0]).rename("g1") g2 = s2.lag().rename("g2") feed = DataFeed([g1, g2]) feed.compile() feed.next() assert feed.next() == {"g1": "m", "g2": 1}
def test_cummax(): for array in arrays: s = Stream.source(array, dtype="float") w = s.cummax(skipna=True).rename("w") expected = list(pd.Series(array).cummax(skipna=True)) assert_op([w], expected) for array in arrays: s = Stream.source(array, dtype="float") w = s.cummin(skipna=False).rename("w") expected = list(pd.Series(array).cummin(skipna=False)) assert_op([w], expected)
def test_diff(): for array in arrays: s = Stream.source(array, dtype="float") w = s.diff(periods=1).rename("w") expected = list(pd.Series(array).diff(periods=1)) assert_op([w], expected) for array in arrays: s = Stream.source(array, dtype="float") w = s.diff(periods=2).rename("w") expected = list(pd.Series(array).diff(periods=2)) assert_op([w], expected)
def test_float_accessor(): s = Stream.source([1, 2, 3, 4, 5]) w = s.float.square().rename("w") assert_op([w], [1, 4, 9, 16, 25])
def test_neg(): s = Stream.source([3, -4, 6, -7, 2, -6], dtype="float") s1 = s.neg().rename("s1") s2 = (-s).rename("s2") assert_op([s1, s2], [-3, 4, -6, 7, -2, 6])
def test_abs(): s = Stream.source([3, -4, 6, -7, 2, -6], dtype="float") s1 = s.abs().rename("s1") s2 = abs(s).rename("s2") assert_op([s1, s2], [3, 4, 6, 7, 2, 6])
def test_log(): for array in arrays: s = Stream.source(array, dtype="float") w = s.log().rename("w") expected = list(pd.Series(array).apply(np.log)) assert_op([w], expected)
def test_cumprod(): for array in arrays: s = Stream.source(array, dtype="float") w = s.cumprod().rename("w") expected = list(pd.Series(array).cumprod()) assert_op([w], expected)
def test_rolling_median(): for array, config in product(arrays, configurations): s = Stream.source(array, dtype="float") w = s.rolling(**config).median().rename("w") expected = list(pd.Series(array).rolling(**config).median()) assert_op([w], expected)
def test_sub(): expected = [0, 1, 2, 3, 4, 5] # (left, right) : (Stream, Stream) s1 = Stream.source([1, 2, 3, 4, 5, 6], dtype="float") s2 = Stream.source([1, 1, 1, 1, 1, 1], dtype="float") w1 = s1.sub(s2).rename("w1") w2 = (s1 - s2).rename("w2") assert_op([w1, w2], expected) # (left, right) : (Stream, float) w1 = s1.sub(1).rename("w1") w2 = (s1 - 1).rename("w2") assert_op([w1, w2], expected)
def test_rsub(): # (left, right) : (float, Stream) s1 = 6 s2 = Stream.source([1, 2, 3, 4, 5, 6], dtype="float") w = (s1 - s2).rename("w") assert_op([w], [5, 4, 3, 2, 1, 0])
def test_radd(): # (left, right) : (float, Stream) s1 = 1 s2 = Stream.source([1, 2, 3, 4, 5, 6], dtype="float") w = (s1 + s2).rename("w") assert_op([w], [2, 3, 4, 5, 6, 7])
def test_div(): expected = [1, 2, 3, 4, 5, 6] # (left, right) : (Stream, Stream) s1 = Stream.source([2, 4, 6, 8, 10, 12], dtype="float") s2 = Stream.source([2, 2, 2, 2, 2, 2], dtype="float") w1 = s1.div(s2).rename("w1") w2 = (s1 / s2).rename("w2") assert_op([w1, w2], expected) # (left, right) : (Stream, float) w1 = s1.div(2).rename("w1") w2 = (s1 / 2).rename("w2") assert_op([w1, w2], expected)
def test_clamp_min(): array = [-1, 2, -3, 4, -5] s = Stream.source(array, dtype="float") w = s.clamp_min(0).rename("w") expected = list(pd.Series(array).apply(lambda x: x if x > 0 else 0)) assert_op([w], expected)
def test_ffill(): array = [-1, np.nan, -3, 4, np.nan] s = Stream.source(array, dtype="float") w = s.ffill().rename("w") expected = list(pd.Series(array).ffill()) assert_op([w], expected)
def test_rdiv(): expected = [6, 3, 2, 3 / 2, 6 / 5, 1] # (left, right) : (Stream, Stream) s = Stream.source([2, 4, 6, 8, 10, 12], dtype="float") # (left, right) : (Stream, float) w = (12 / s).rename("w") assert_op([w], expected)
def test_rmul(): expected = [2, 4, 6, 8, 10, 12] # (left, right) : (Stream, Stream) s = Stream.source([1, 2, 3, 4, 5, 6], dtype="float") # (left, right) : (Stream, float) w = (2 * s).rename("w") assert_op([w], expected)
def test_add(): # (left, right) : (Stream, Stream) s1 = Stream.source([3, -4, 6, -7, 2, -6], dtype="float") s2 = Stream.source([-3, 4, -6, 7, -2, 6], dtype="float") w1 = s1.add(s2).rename("w1") w2 = (s1 + s2).rename("w2") assert_op([w1, w2], 6 * [0]) # (left, right) : (Stream, float) s1 = Stream.source([1, 2, 3, 4, 5, 6], dtype="float") s2 = 1 w1 = s1.add(s2).rename("w1") w2 = (s1 + s2).rename("w2") assert_op([w1, w2], [2, 3, 4, 5, 6, 7])
def test_bool_methods(): s = Stream.source(["hello", "my", "name", "i", "am", "the", "data", "feed"], dtype="string") w1 = s.upper().rename("w1") w2 = s.lower().rename("w2") w3 = w1.endswith("E").invert().rename("w3") feed = DataFeed([w1, w2, w3]) feed.compile() assert feed.next() == {"w1": "HELLO", "w2": "hello", "w3": True}
def test_ewm_mean(): array = [1, np.nan, 3, 4, 5, 6, np.nan, 7] s = Stream.source(array, dtype="float") for config in configurations: w = s.ewm(**config).mean().rename("w") expected = list(pd.Series(array).ewm(**config).mean()) assert_op([w], expected)
def test_pow(): array = [1, -2, 3, -4, 5, -6] s = Stream.source(array, dtype="float") s1 = s.pow(3).rename("s1") s2 = (s**3).rename("s2") expected = list(pd.Series(array)**3) assert_op([s1, s2], expected)
def test_str_accessor(): s = Stream.source(["hello", "my", "name", "i", "am", "the", "data", "feed"]) w1 = s.str.upper().rename("w1") w2 = s.str.lower().rename("w2") w3 = w1.str.endswith("E").rename("w3") feed = DataFeed([w1, w2, w3]) feed.compile() assert feed.next() == {"w1": "HELLO", "w2": "hello", "w3": False}
def test_pct_change(): configs = [ { "periods": 1, "fill_method": None }, { "periods": 1, "fill_method": "pad" }, { "periods": 1, "fill_method": "ffill" }, { "periods": 2, "fill_method": None }, { "periods": 2, "fill_method": "pad" }, { "periods": 2, "fill_method": "ffill" }, ] for array, config in product(arrays, configs): s = Stream.source(array, dtype="float") w = s.pct_change(**config).rename("w") expected = list(pd.Series(array).pct_change(**config)) print(config) assert_op([w], expected)