Skip to content

Commit

Permalink
Merge pull request #4 from yymao/u/yymao/reduce-compare
Browse files Browse the repository at this point in the history
  • Loading branch information
yymao authored Nov 14, 2021
2 parents e46950e + b203e35 commit e92abb2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
max-parallel: 4
matrix:
python-version: ["2.7", "3.6", "3.7", "3.8", "3.9"]
python-version: ["3.6", "3.7", "3.8", "3.9"]

steps:
- uses: actions/checkout@v2
Expand Down
18 changes: 10 additions & 8 deletions easyquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@
import numpy as np
import numexpr as ne

if not hasattr(list, 'copy'):
try:
from builtins import list
except ImportError:
raise ImportError('Please install python package "future"')


__all__ = ['Query', 'QueryMaker']
__version__ = '0.2.0'
__version__ = '0.3.0'


def _is_string_like(obj):
Expand Down Expand Up @@ -511,3 +504,12 @@ def isnotnan(col_name):
@staticmethod
def isclose(col1_name, col2_name):
return QueryMaker.vectorize(np.isclose, col1_name, col2_name)

@staticmethod
def reduce_compare(columns, reduce_func, compare_func, compare_value):
"""
returns Query((compare_func(reduce_func(np.stack(arrays), axis=0), compare_value), *columns))
"""
def _func(*arrays, reduce_func=reduce_func, compare_func=compare_func, compare_value=compare_value):
return compare_func(reduce_func(np.stack(arrays), axis=0), compare_value)
return Query((_func,) + tuple(columns))
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
NumPy structured arrays, astropy Table, and Pandas DataFrame.
Project website: https://github.com/yymao/easyquery
The MIT License (MIT)
Copyright (c) 2017-2020 Yao-Yuan Mao (yymao)
Copyright (c) 2017-2021 Yao-Yuan Mao (yymao)
http://opensource.org/licenses/MIT
"""

Expand Down Expand Up @@ -36,12 +36,13 @@
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
],
keywords='easyquery query numpy',
py_modules=[_name],
install_requires=['numpy>=1.7', 'numexpr>=2.0', 'future>=0.12.0 ; python_version < "3.0"'],
python_requires='>=3.6',
install_requires=['numpy>=1.7', 'numexpr>=2.0'],
)
12 changes: 12 additions & 0 deletions test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ def test_query_maker():
check_query_on_table(t, QueryMaker.isnotnan("c"), ~np.isnan(t["c"]))
check_query_on_table(t, QueryMaker.isclose("a", "b"), np.isclose(t["a"], t["b"]))

check_query_on_table(
t,
QueryMaker.reduce_compare(["a", "b"], np.max, np.greater, 1),
np.maximum(t["a"], t["b"]) > 1,
)

check_query_on_table(
t,
QueryMaker.reduce_compare(["a", "b"], np.mean, np.less_equal, 2),
0.5 * (t["a"] + t["b"]) <= 2,
)

assert QueryMaker.equal_columns("s", "s").mask(t).all()


Expand Down

0 comments on commit e92abb2

Please sign in to comment.