Skip to content

Rename sgn to sign #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/library/sparse/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ List of Implemented Operations
- ``ceil``
- ``floor``
- ``trunc``
- ``sgn``
- ``sign``
- ``log1p``
- ``expm1``
- ``sqr``
Expand Down
2 changes: 1 addition & 1 deletion doc/library/tensor/basic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ Mathematical

Returns a variable representing the base e, 2 or 10 logarithm of a.

.. function:: sgn(a)
.. function:: sign(a)

Returns a variable representing the sign of a.

Expand Down
2 changes: 1 addition & 1 deletion pytensor/compile/profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ def print_tips(self, file):
aes.Second,
aes.Identity,
aes.Cast,
aes.Sgn,
aes.Sign,
aes.Neg,
aes.Reciprocal,
aes.Sqr,
Expand Down
8 changes: 4 additions & 4 deletions pytensor/scalar/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2566,7 +2566,7 @@ def L_op(self, inputs, outputs, gout):
return [x.zeros_like()]

if x.type in float_types:
return (gz * sgn(x),)
return (gz * sign(x),)
return (gz * x / _abs(x),) # formula works for complex and real

def c_code(self, node, name, inputs, outputs, sub):
Expand All @@ -2590,7 +2590,7 @@ def c_code(self, node, name, inputs, outputs, sub):
abs = Abs(same_out)


class Sgn(UnaryScalarOp):
class Sign(UnaryScalarOp):
nfunc_spec = ("sign", 1, 1)

@staticmethod
Expand Down Expand Up @@ -2625,7 +2625,7 @@ def c_code(self, node, name, inputs, outputs, sub):
)
if type in int_types:
return f"{z} = ({x} >= 0) ? ({x} == 0) ? 0 : 1 : -1;"
raise ComplexError("complex has no sgn")
raise ComplexError("complex has no sign")

def c_code_cache_version(self):
s = super().c_code_cache_version()
Expand All @@ -2635,7 +2635,7 @@ def c_code_cache_version(self):
return s


sgn = Sgn(name="sgn")
sign = Sign(name="sign")


class Ceil(UnaryScalarOp):
Expand Down
6 changes: 3 additions & 3 deletions pytensor/sparse/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
from pytensor.tensor.math import (
rad2deg,
round_half_to_even,
sgn,
sigmoid,
sign,
sin,
sinh,
sqr,
Expand Down Expand Up @@ -3184,8 +3184,8 @@ def rint(x):
rint.__name__ = "rint"


@structured_monoid(sgn) # type: ignore[no-redef]
def sgn(x):
@structured_monoid(sign) # type: ignore[no-redef]
def sign(x):
"""
Elemwise signe of `x`.

Expand Down
2 changes: 1 addition & 1 deletion pytensor/tensor/inplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def log10_inplace(a):


@scalar_elemwise
def sgn_inplace(a):
def sign_inplace(a):
"""sign of `a` (inplace on `a`)"""


Expand Down
13 changes: 12 additions & 1 deletion pytensor/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ def isclose(a, b, rtol=1.0e-5, atol=1.0e-8, equal_nan=False):
# deal with signed inf values. this will make an array inf_eq of 0's
# except where inf values have the same sign.
both_infs = bitwise_and(a_inf, b_inf)
inf_signs_eq = eq(a_inf * sgn(a), b_inf * sgn(b))
inf_signs_eq = eq(a_inf * sign(a), b_inf * sign(b))
inf_eq = bitwise_and(both_infs, inf_signs_eq)

# now create the potential result combining close and inf_eq
Expand Down Expand Up @@ -1092,9 +1092,19 @@ def log1p(a):


@scalar_elemwise
def sign(a):
"""sign of a"""


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding a compatible sgn like this (and make it accessible from pytensor.tensor as before.

Suggested change
def sgn(a):
warnings.warn(
"sgn is deprecated and will stop working in the future, use sign instead.",
FutureWarning,
)
return sign(a)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ricardoV94 I have implemented the suggested changes, could you please check if we need anything else in this PR?

def sgn(a):
"""sign of a"""

warnings.warn(
"sgn is deprecated and will stop working in the future, use sign instead.",
FutureWarning,
)
return sign(a)


@scalar_elemwise
def ceil(a):
Expand Down Expand Up @@ -3038,6 +3048,7 @@ def matmul(x1: "ArrayLike", x2: "ArrayLike", dtype: Optional["DTypeLike"] = None
"log10",
"log1p",
"sgn",
"sign",
"ceil",
"floor",
"trunc",
Expand Down
4 changes: 2 additions & 2 deletions pytensor/tensor/rewriting/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
from pytensor.tensor.math import (
prod,
reciprocal,
sgn,
sigmoid,
sign,
softplus,
sqr,
sqrt,
Expand Down Expand Up @@ -2289,7 +2289,7 @@ def check_for_x_over_absX(numerators, denominators):
else:
denominators.remove(den)
numerators.remove(den.owner.inputs[0])
numerators.append(sgn(den.owner.inputs[0]))
numerators.append(sign(den.owner.inputs[0]))
return numerators, denominators


Expand Down
4 changes: 2 additions & 2 deletions pytensor/tensor/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def get_canonical_form_slice(
if the resulting set of numbers needs to be reversed or not.

"""
from pytensor.tensor import ge, lt, sgn, switch
from pytensor.tensor import ge, lt, sign, switch

if not isinstance(theslice, slice):
try:
Expand Down Expand Up @@ -317,7 +317,7 @@ def switch_neg_step(a, b):
return switch(is_step_neg, a, b)

abs_step = abs(step)
sgn_step = sgn(step)
sgn_step = sign(step)

defstart = switch_neg_step(length - 1, 0)
defstop = switch_neg_step(-1, length)
Expand Down
2 changes: 1 addition & 1 deletion tests/sparse/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3111,7 +3111,7 @@ def structured_function(*args):
)

SgnTester = elemwise_checker(
sparse.sgn,
sparse.sign,
np.sign,
grad_test=False,
test_dtypes=[
Expand Down
6 changes: 3 additions & 3 deletions tests/tensor/rewriting/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
prod,
rad2deg,
reciprocal,
sgn,
sigmoid,
sign,
sinh,
softplus,
sqr,
Expand Down Expand Up @@ -877,7 +877,7 @@ def test_abs_mul_div(self):
assert np.isfinite(f(0))

assert len(f.maker.fgraph.toposort()) == 2
assert f.maker.fgraph.toposort()[0].op == sgn
assert f.maker.fgraph.toposort()[0].op == sign

f = function([x], [(4 * x) / abs(x / 2)], mode=mode)
f(0.1)
Expand All @@ -886,7 +886,7 @@ def test_abs_mul_div(self):
assert np.isfinite(f(0))

assert len(f.maker.fgraph.toposort()) == 2
assert f.maker.fgraph.toposort()[0].op == sgn
assert f.maker.fgraph.toposort()[0].op == sign

@pytest.mark.skip(
reason="Current implementation of AlgebraicCanonizer does not "
Expand Down
4 changes: 2 additions & 2 deletions tests/tensor/test_inplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
reciprocal_inplace,
round_half_away_from_zero_inplace,
round_half_to_even_inplace,
sgn_inplace,
sign_inplace,
sin_inplace,
sinh_inplace,
sqr_inplace,
Expand Down Expand Up @@ -177,7 +177,7 @@
)

TestSgnInplaceBroadcast = makeBroadcastTester(
op=sgn_inplace,
op=sign_inplace,
expected=np.sign,
good=_good_broadcast_unary_normal_no_complex,
inplace=True,
Expand Down
4 changes: 2 additions & 2 deletions tests/tensor/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@
reciprocal,
round_half_away_from_zero,
round_half_to_even,
sgn,
sigmoid,
sign,
sin,
sinh,
smallest,
Expand Down Expand Up @@ -386,7 +386,7 @@ def test_maximum_minimum_grad():
)

TestSgnBroadcast = makeBroadcastTester(
op=sgn,
op=sign,
expected=np.sign,
good=_good_broadcast_unary_normal_no_complex,
grad=_grad_broadcast_unary_normal,
Expand Down