Skip to content

Commit

Permalink
add: exclusive support for np.ufunc in MathExpTrans
Browse files Browse the repository at this point in the history
  • Loading branch information
premsrii committed Feb 14, 2023
1 parent 6af63f3 commit 5292d53
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
6 changes: 3 additions & 3 deletions poetry.lock

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

6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1208,9 +1208,9 @@ scipy==1.10.0 ; python_version >= "3.8" and python_version < "3.11" \
--hash=sha256:b901b423c91281a974f6cd1c36f5c6c523e665b5a6d5e80fcb2334e14670eefd \
--hash=sha256:c8b3cbc636a87a89b770c6afc999baa6bcbb01691b5ccbbc1b1791c7c0a07540 \
--hash=sha256:e096b062d2efdea57f972d232358cb068413dc54eec4f24158bcbb5cb8bddfd8
setuptools==67.2.0 ; python_version >= "3.8" and python_version < "3.11" \
--hash=sha256:16ccf598aab3b506593c17378473978908a2734d7336755a8769b480906bec1c \
--hash=sha256:b440ee5f7e607bb8c9de15259dba2583dd41a38879a7abc1d43a71c59524da48
setuptools==67.3.1 ; python_version >= "3.8" and python_version < "3.11" \
--hash=sha256:23c86b4e44432bfd8899384afc08872ec166a24f48a3f99f293b0a557e6a6b5d \
--hash=sha256:daec07fd848d80676694d6bf69c009d28910aeece68a38dbe88b7e1bb6dba12e
simpful==2.9.0 ; python_version >= "3.8" and python_version < "3.11" \
--hash=sha256:29b0c1fe1dab5dc9eb137cf8ac7fad5ca44443398bd3917f14a37df6dc7f55a4 \
--hash=sha256:6d7e8077ac2b2499c3a1b86185cac52cc366d0a578428197b12575e451300855
Expand Down
9 changes: 7 additions & 2 deletions src/sk_transformers/number_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class MathExpressionTransformer(BaseTransformer):
The operation can be a function from NumPy's mathematical functions or
operator package.
**Warning!** Some functions/operators may not work as expected. Especially not all NumPy methods are supported. For example:
various NumPy methods return values which are not fitting the size of the source column.
**Warning!** Some functions/operators may not work as expected. Especially, functions that don't
belong in [`numpy.ufunc`](https://numpy.org/doc/stable/reference/ufuncs.html) are not supported.
NumPy functions with return values that don't fit the size of the source column are also not supported.
Example:
```python
Expand Down Expand Up @@ -54,6 +55,10 @@ def __verify_operation(self, operation: str) -> Tuple[bool, Any]:
if hasattr(np, operation[3:]):
op = getattr(np, operation[3:])
is_np_op = True
if not isinstance(op, np.ufunc):
raise ValueError(
f"The function `{operation}` is not a NumPy universal function. If you are using `np.sum` or `np.prod`, please use `np.add` or `np.multiply` instead."
)
else:
raise AttributeError(f"Operation {operation[3:]} not found in NumPy!")

Expand Down
13 changes: 11 additions & 2 deletions tests/test_transformer/test_number_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ def test_math_expression_transformer_no_operation_method(X_numbers) -> None:
)


def test_math_expression_transformer_non_ufunc(X_numbers) -> None:
with pytest.raises(ValueError) as error:
MathExpressionTransformer(
[("small_numbers", "np.sum", "small_numbers", None)]
).fit_transform(X_numbers)
assert (
"The function `np.sum` is not a NumPy universal function. If you are using `np.sum` or `np.prod`, please use `np.add` or `np.multiply` instead."
== str(error.value)
)


def test_math_expression_transformer_in_pipeline(X_numbers) -> None:
pipeline = make_pipeline(
MathExpressionTransformer(
Expand All @@ -40,7 +51,6 @@ def test_math_expression_transformer_in_pipeline(X_numbers) -> None:
),
("small_numbers", "np.divide", "small_numbers", None),
("small_numbers", "numpy.sin", None, None),
("small_numbers", "np.sum", 1, None),
("big_numbers", "neg", None, None),
]
)
Expand Down Expand Up @@ -71,7 +81,6 @@ def test_math_expression_transformer_in_pipeline(X_numbers) -> None:

assert pipeline.steps[0][0] == "mathexpressiontransformer"
assert np.array_equal(result["small_numbers_add_1"].to_numpy(), expected_add)
assert np.array_equal(result["small_numbers_sum_1"].to_numpy(), expected_add)
assert np.array_equal(
result["small_numbers_mul_small_numbers"].to_numpy(), expected_mul
)
Expand Down

0 comments on commit 5292d53

Please sign in to comment.