Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dovahcrow committed Jan 23, 2024
1 parent f5ea101 commit 4901d0c
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 256 deletions.
2 changes: 1 addition & 1 deletion native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Weiyuan Wu <[email protected]>"]
edition = "2018"
name = "factor-expr"
version = "0.2.3"
version = "0.3.0"

[lib]
crate-type = ["rlib", "cdylib"]
Expand Down
3 changes: 2 additions & 1 deletion native/src/ops/window/minmax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ macro_rules! impl_minmax {

self.window.push_back((self.seq, val));

let val = if self.window.len() == self.win_size {
let val = if self.i >= self.ready_offset() {
let val = ($($vfunc)+) (&self.window, self.seq, self.win_size);
val
} else {
self.i += 1;
f64::NAN
};
results.push(val);
Expand Down
1 change: 0 additions & 1 deletion python/factor_expr/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from tqdm.auto import tqdm

import numpy as np
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
import pyarrow.compute as pc
Expand Down
49 changes: 16 additions & 33 deletions python/factor_expr/tests/factors/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ def test_add():
[FILENAME],
[Factor("(+ :price_ask_l1_high :price_bid_l1_open)")],
pbar=False,
index_col="time",
)
)

assert np.isclose(
df.price_ask_l1_high + df.price_bid_l1_open,
result.iloc[:, 0],
result.to_pandas().iloc[:, 0],
).all()


Expand All @@ -34,28 +33,23 @@ def test_sub():
[FILENAME],
[Factor("(- :price_ask_l1_open :price_bid_l1_open)")],
pbar=False,
index_col="time",
)
)

assert np.isclose(
df.price_ask_l1_open - df.price_bid_l1_open,
result.iloc[:, 0],
result.to_pandas().iloc[:, 0],
).all()


def test_mul():
df = pd.read_parquet(FILENAME)

result = asyncio.run(
replay(
[FILENAME], [Factor("(* :price_ask_l1_open :price_bid_l1_low)")], pbar=False
)
)
result = asyncio.run(replay([FILENAME], [Factor("(* :price_ask_l1_open :price_bid_l1_low)")], pbar=False))

assert np.isclose(
df.price_ask_l1_open * df.price_bid_l1_low,
result.iloc[:, 0],
result.to_pandas().iloc[:, 0],
).all()


Expand All @@ -67,76 +61,65 @@ def test_div():
[FILENAME],
[Factor("(/ :price_ask_l1_close :price_bid_l1_high)")],
pbar=False,
index_col="time",
)
)

assert np.isclose(
df.price_ask_l1_close / df.price_bid_l1_high,
result.iloc[:, 0],
result.to_pandas().iloc[:, 0],
).all()


def test_power():
df = pd.read_parquet(FILENAME)

result = asyncio.run(
replay([FILENAME], [Factor("(^ 3 :price_ask_l1_open)")], pbar=False)
)
result = asyncio.run(replay([FILENAME], [Factor("(^ 3 :price_ask_l1_open)")], pbar=False))

assert np.isclose(
df.price_ask_l1_open ** 3,
result.iloc[:, 0],
df.price_ask_l1_open**3,
result.to_pandas().iloc[:, 0],
).all()


def test_signed_power():
df = pd.read_parquet(FILENAME)

result = asyncio.run(
replay([FILENAME], [Factor("(SPow 2 :price_ask_l1_low)")], pbar=False)
)
result = asyncio.run(replay([FILENAME], [Factor("(SPow 2 :price_ask_l1_low)")], pbar=False))

assert np.isclose(
np.sign(df.price_ask_l1_low) * df.price_ask_l1_low.abs() ** 2,
result.iloc[:, 0],
result.to_pandas().iloc[:, 0],
).all()


def test_log():
df = pd.read_parquet(FILENAME)

result = asyncio.run(
replay([FILENAME], [Factor("(LogAbs :volume_ask_l1_high)")], pbar=False)
)
result = asyncio.run(replay([FILENAME], [Factor("(LogAbs :volume_ask_l1_high)")], pbar=False))

assert np.isclose(
np.log(np.abs(df.volume_ask_l1_high)),
result.iloc[:, 0],
result.to_pandas().iloc[:, 0],
).all()


def test_sign():
df = pd.read_parquet(FILENAME)

result = asyncio.run(
replay([FILENAME], [Factor("(Sign :price_ask_l1_close)")], pbar=False)
)
result = asyncio.run(replay([FILENAME], [Factor("(Sign :price_ask_l1_close)")], pbar=False))

assert np.isclose(
np.sign(df.price_ask_l1_close),
result.iloc[:, 0],
result.to_pandas().iloc[:, 0],
).all()


def test_abs():
df = pd.read_parquet(FILENAME)

result = asyncio.run(
replay([FILENAME], [Factor("(Abs :price_ask_l1_open)")], pbar=False)
)
result = asyncio.run(replay([FILENAME], [Factor("(Abs :price_ask_l1_open)")], pbar=False))

assert np.isclose(
np.abs(df.price_ask_l1_open),
result.iloc[:, 0],
result.to_pandas().iloc[:, 0],
).all()
30 changes: 9 additions & 21 deletions python/factor_expr/tests/factors/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ def test_gt():
[FILENAME],
[Factor("(> :price_ask_l1_open 0)")],
pbar=False,
index_col="time",
)
)

assert ((df.price_ask_l1_open > 0).values == result.iloc[:, 0].values).all()
assert ((df.price_ask_l1_open > 0).values == result.to_pandas().iloc[:, 0].values).all()


def test_gte():
Expand All @@ -31,11 +30,10 @@ def test_gte():
[FILENAME],
[Factor("(>= :price_ask_l1_low 0)")],
pbar=False,
index_col="time",
)
)

assert ((df.price_ask_l1_low >= 0).values == result.iloc[:, 0].values).all()
assert ((df.price_ask_l1_low >= 0).values == result.to_pandas().iloc[:, 0].values).all()


def test_lt():
Expand All @@ -46,11 +44,10 @@ def test_lt():
[FILENAME],
[Factor("(< :price_ask_l1_high 0)")],
pbar=False,
index_col="time",
)
)

assert ((df.price_ask_l1_high < 0).values == result.iloc[:, 0].values).all()
assert ((df.price_ask_l1_high < 0).values == result.to_pandas().iloc[:, 0].values).all()


def test_lte():
Expand All @@ -61,11 +58,10 @@ def test_lte():
[FILENAME],
[Factor("(<= :price_ask_l1_close 0)")],
pbar=False,
index_col="time",
)
)

assert ((df.price_ask_l1_close <= 0).values == result.iloc[:, 0].values).all()
assert ((df.price_ask_l1_close <= 0).values == result.to_pandas().iloc[:, 0].values).all()


def test_or():
Expand All @@ -79,8 +75,7 @@ def test_or():
)

assert (
((df.price_ask_l1_close < 0).values | (df.price_bid_l1_high > 0).values)
== result.iloc[:, 0].values
((df.price_ask_l1_close < 0).values | (df.price_bid_l1_high > 0).values) == result.to_pandas().iloc[:, 0].values
).all()


Expand All @@ -95,8 +90,7 @@ def test_and():
)

assert (
((df.price_ask_l1_open < 0).values & (df.price_bid_l1_low > 0).values)
== result.iloc[:, 0].values
((df.price_ask_l1_open < 0).values & (df.price_bid_l1_low > 0).values) == result.to_pandas().iloc[:, 0].values
).all()


Expand All @@ -111,8 +105,7 @@ def test_not():
)

assert (
~((df.price_ask_l1_close < 0).values & (df.price_bid_l1_low > 0).values)
== result.iloc[:, 0].values
~((df.price_ask_l1_close < 0).values & (df.price_bid_l1_low > 0).values) == result.to_pandas().iloc[:, 0].values
).all()


Expand All @@ -122,13 +115,8 @@ def test_if():
result = asyncio.run(
replay(
[FILENAME],
[
Factor(
"(If (< :price_ask_l1_high 0) :price_ask_l1_close :price_bid_l1_open)"
)
],
[Factor("(If (< :price_ask_l1_high 0) :price_ask_l1_close :price_bid_l1_open)")],
pbar=False,
index_col="time",
)
)

Expand All @@ -138,5 +126,5 @@ def test_if():
df.price_ask_l1_close.values,
df.price_bid_l1_open.values,
)
== result.iloc[:, 0].values
== result.to_pandas().iloc[:, 0].values
).all()
25 changes: 1 addition & 24 deletions python/factor_expr/tests/factors/test_sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,11 @@


def test_index():
f = Factor("(TSMean 10 :price_ask_l1_open)")
f = Factor("(Mean 10 :price_ask_l1_open)")
asyncio.run(
replay(
[FILENAME],
[f],
trim=False,
index_col="time",
pbar=False,
)
)


def test_trim():
f = Factor("(TSMean 10 :price_ask_l1_open)")
asyncio.run(replay([FILENAME], [f], trim=True, pbar=False))


def test_predicate():
f = Factor("(TSMean 10 :price_ask_l1_open)")
predicate = Factor(
"(> (TSStd 60 (TSLogReturn 120 (+ :price_bid_l1_close :price_ask_l1_close))) 0.005)"
)
asyncio.run(replay([FILENAME], [f], predicate=predicate, pbar=False))


def test_predicate_with_trim():
f = Factor("(TSMean 10 :price_ask_l1_open)")
predicate = Factor(
"(> (TSStd 60 (TSLogReturn 120 (+ :price_bid_l1_close :price_ask_l1_close))) 0.005)"
)
asyncio.run(replay([FILENAME], [f], predicate=predicate, trim=True, pbar=False))
Loading

0 comments on commit 4901d0c

Please sign in to comment.