Skip to content

Commit 2f01b83

Browse files
author
sjoshistrats
authored
Merge pull request #6 from sjoshistrats/feature/benchmarks
Add benchmarks
2 parents 4155195 + dcb13e0 commit 2f01b83

File tree

4 files changed

+85
-14
lines changed

4 files changed

+85
-14
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
- run:
1414
command: | # Run tests
1515
pipenv install pytest
16+
pipenv install pytest-benchmark
1617
pipenv run pytest python/fastgrouper/test/ -s -vv
1718
workflows:
1819
build_test:
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
import numpy as np
23
from numpy.testing import assert_almost_equal
34

@@ -14,37 +15,37 @@ def foobar_op(x, y):
1415
return np.mean(x + y)
1516

1617
def test_arr_grouped():
17-
18+
1819
# Prepare example
1920
arr_grpr = fastgrouper.arr.Grouped(GIDS)
20-
21+
2122
# Check apply with positional args
2223
result = arr_grpr.apply(foobar_op, XVALS, YVALS)
23-
24+
2425
# Ensure returned result is a numpy array
2526
assert isinstance(result, np.ndarray)
26-
27+
2728
# Check values
2829
assert_almost_equal(result, np.array(EXPECTED_APPLY))
29-
30+
3031
# Check values when using apply with keyword args
3132
result = arr_grpr.apply(foobar_op, XVALS, y=YVALS)
3233
assert_almost_equal(result, np.array(EXPECTED_APPLY))
33-
34+
3435
# Check values when using apply_expand
3536
result = arr_grpr.apply_expand(foobar_op, XVALS, y=YVALS)
3637
assert_almost_equal(result, np.array(EXPECTED_APPLY_EXPAND))
37-
38+
3839
def test_li_grouped():
39-
40+
4041
# Prepare example
4142
li_grpr = fastgrouper.li.Grouped(GIDS)
42-
43+
4344
# Check apply with keyword args
4445
result = li_grpr.apply(foobar_op, XVALS, y=YVALS)
45-
46+
4647
# Ensure returned result is a list
4748
assert isinstance(result, list)
48-
49+
4950
# Check values
50-
assert_almost_equal(result, EXPECTED_APPLY)
51+
assert_almost_equal(result, EXPECTED_APPLY)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import pytest
2+
import pandas as pd
3+
import numpy as np
4+
from numpy.testing import assert_almost_equal
5+
6+
import fastgrouper.arr
7+
import fastgrouper.li
8+
9+
# Create assymetrical groups
10+
XVALS = np.linspace(500, 1200, 50000)
11+
YVALS = np.linspace(-230, 177.3, 50000)
12+
GIDS = np.tile(np.arange(500), 100)
13+
GIDS[[14, 19, 230, 87]] = 4
14+
GIDS[[345, 1270, 63, 1287]] = 12
15+
16+
def beepbop(x, y):
17+
return np.min(np.abs(np.sin(x) + np.sin(y)))
18+
19+
def test_fastgrouper_arr_slice_apply_benchmark(benchmark):
20+
arr_grpr = fastgrouper.arr.Grouped(GIDS)
21+
idx = pd.Index(arr_grpr.dedup_gids, name="gids")
22+
23+
def apply_fn():
24+
result = arr_grpr.apply(beepbop, XVALS, YVALS)
25+
26+
# Sorting to make a more fair comparison against pure pandas benchmark
27+
return pd.Series(result, index=idx).sort_index()
28+
29+
benchmark(apply_fn)
30+
31+
def test_pure_pandas_slice_apply_benchmark(benchmark):
32+
df = pd.DataFrame({
33+
"gids": GIDS,
34+
"xvals": XVALS,
35+
"yvals": YVALS
36+
})
37+
pdgrpd = df.groupby("gids")
38+
39+
def apply_fn(r):
40+
return beepbop(r["xvals"].values, r["yvals"].values)
41+
42+
benchmark(pdgrpd.apply, apply_fn)
43+
44+
def test_fastgrouper_all_steps_benchmark(benchmark):
45+
def apply_fn():
46+
arr_grpr = fastgrouper.arr.Grouped(GIDS)
47+
idx = pd.Index(arr_grpr.dedup_gids, name="gids")
48+
result = arr_grpr.apply(beepbop, XVALS, YVALS)
49+
50+
# Sorting to make a more fair comparison against pure pandas benchmark
51+
return pd.Series(result, index=idx).sort_index()
52+
53+
benchmark(apply_fn)
54+
55+
def test_pure_pandas_all_steps_benchmark(benchmark):
56+
df = pd.DataFrame({
57+
"gids": GIDS,
58+
"xvals": XVALS,
59+
"yvals": YVALS
60+
})
61+
pdgrpd = df.groupby("gids")
62+
63+
def apply_fn():
64+
return df.groupby("gids").apply(lambda r: beepbop(r["xvals"].values, r["yvals"].values))
65+
66+
benchmark(apply_fn)

setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setuptools.setup(
1111
name="fastgrouper",
12-
version="0.1.1",
12+
version="0.1.2",
1313
author="Shreyas Joshi",
1414
author_email="sjoshistrats@gmail.com",
1515
description="A package for applying efficient groupby operations.",
@@ -26,5 +26,8 @@
2626
package_dir={"": "python"},
2727
packages=setuptools.find_packages(where="python"),
2828
python_requires=">=3.6",
29-
install_requires=["numpy", "pandas"]
29+
install_requires=["numpy", "pandas"],
30+
extras_require={
31+
"testing": ["pytest", "pytest-benchmark"]
32+
}
3033
)

0 commit comments

Comments
 (0)