Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ENH: Support integer dtype inputs in rounding functions#26766

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mattip merged 1 commit intonumpy:mainfrommtsokol:rounding-dtypes
Jul 23, 2024

Conversation

mtsokol
Copy link
Member

@mtsokolmtsokol commentedJun 20, 2024
edited
Loading

Hi@seberg@ngoldbaum,

This PR adds support for integer dtype inputs infloor,ceil, andtrunc ufuncs to match the Array API standard.

For integer dtype inputs an identity is returned in a loop instead of casting to the float dtype.

@mtsokolmtsokol self-assigned thisJun 20, 2024
@mtsokolmtsokol changed the titleENH: Support integer dtype inputs in rounding functions.ENH: Support integer dtype inputs in rounding functionsJun 20, 2024
@mtsokolmtsokol added this to the2.1.0 release milestoneJun 21, 2024
@jakevdp
Copy link
Contributor

What about booleans? The Array API spec doesn't mention these here, but it seems like it would be consistent to return boolean outputs for boolean inputs to these functions.

@seberg
Copy link
Member

I suppose we have to make a choice for them, and it could go three ways:

  1. round is math, and math behaves like integers, as it does for Python for better or worse.
  2. We just raise, because rounding booleans is weird. (I.e. a normal deprecation.)
  3. Propagate booleans because it is nice to be able to no-op on non-floats.

But if you propagate, doesround(True, -1) actually returnFalse? I am not sure what I prefer, but since I doubt we will changeTrue + True in NumPy, maybe just going to the default integer is actually pretty sane?

@mattip
Copy link
Member

round is not one of the ufuncs we are dealing with here. Onlyfloor,ceil andtrunc. So in that case can we make both bool and int no-ops here? Or am I missing a corner case.

@mtsokol
Copy link
MemberAuthor

I think no-op support for bool dtype also makes sense to me.

@@ -61,6 +61,17 @@ NPY_NO_EXPORT void NPY_CPU_DISPATCH_CURFX(@TYPE@_reciprocal)
UNARY_LOOP_FAST(@type@, @type@, *out = 1.0 / in);
}

/**begin repeat1
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a major worry, but just wondering: Aren't there loops fornp.positive already? Is it possible to reuse those rather than define new ones?

seberg reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I think there's a loop per each dtype and function, like@TYPE@_@kind@.

Here, forpositive we have:

NPY_NO_EXPORTvoidNPY_CPU_DISPATCH_CURFX(@TYPE@_positive)(char**args,npy_intpconst*dimensions,npy_intpconst*steps,void*NPY_UNUSED(func)){UNARY_LOOP_FAST(@type@, @type@,*out=+in);}

I think forfloor,ceil, andtrunc we need separate ones anyway to follow the conventiondtype_func. Or do you mean changing@TYPE@_positive to@TYPE@_@kind@ and adding an innerrepeat1 loop here? (with#kind = floor, ceil, trunc, positive#)

Copy link
Member

Choose a reason for hiding this comment

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

You can just add an alias define to the header file.

mtsokol and mhvk reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

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

yes, an alias seems the right solution, to avoid compiling the same code four times!

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Done! I added#define alias pointing topositive loops forfloor,ceil, andtrunc ufuncs.

But this alias covers only ints, aspositive doesn't support boolean dtype. For boolean dtype I added separate no-op loops (I didn't find other ufunc that would be a no-op for booleans), is it OK?

Copy link
MemberAuthor

@mtsokolmtsokolJul 1, 2024
edited
Loading

Choose a reason for hiding this comment

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

Sure! But raising would be a new backward incompatible behavior. Then I propose to leavebool and continue to cast (floor,ceil, andtrunc cast bool dtype right now inmain). Is it correct?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see what you mean, I hadn't quite realized currently it cast tofloat. If you do nothing about bool in your PR, does it still cast to float, or does it become int? If we don't pass onbool or raise, I prefer casting toint to casting tofloat...

If this becomes unclear, one way out is simply not to touch it here, but raise a separate issue to discuss, hoping still to decide before 5.1.

Copy link
Member

Choose a reason for hiding this comment

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

It would go to the first type that matches, which is either uint8 or int8 (dunno).

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

It casts toint8:

In [1]:np.ceil(np.array([1,0,1]).astype(bool))Out[1]:array([1,0,1],dtype=int8)

Then let me update it here.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Done! Ready from my side!

@mtsokolmtsokolforce-pushed therounding-dtypes branch 4 times, most recently from93d4ebe toc084f64CompareJuly 1, 2024 13:50
@mtsokol
Copy link
MemberAuthor

mtsokol commentedJul 1, 2024
edited
Loading

It's ready for another review! (I think CI failures are unrelated as they occur in other PRs as well.)

@mtsokolmtsokolforce-pushed therounding-dtypes branch 2 times, most recently from9f625d8 to52792d4CompareJuly 2, 2024 19:39
@seberg
Copy link
Member

@mhvk, we discussed this a bit and slightly leaned towards pass through being maybe the simplest (even if we don't touchpositive)...

I'll say that I might slightly prefer just deprecating it, but I just don't think it matters much and I won't have squirms with deprecating it later.

I do not like the currentint8, but would be just as happy (maybe even more so?) with pass through as default integer, i.e.??->n loop, that at least aligns withmath.ceil(True).


There are a lot more float only functions with similar wonky behavior for ints and bools. However, for most of them, the return dtype should just be changed to promote to float64.

@mhvk
Copy link
Contributor

mhvk commentedJul 3, 2024

@seberg - I don't really mind much either way, just felt the easiest way to get this (nice) addition in was not to worry aboutbool at all in this PR, and discuss separately. If we are going to deal with it here, then, given thatnp.negative andnp.positive both do not work, my very slight preference would be to just bail (i.e., deprecate), but, really, I do not care as I think in the end this is a corner case that will happen essentially never.

@mtsokol
Copy link
MemberAuthor

@seberg I added no-op for boolean dtype - it's completed from my side.

Copy link
Member

@ngoldbaumngoldbaum left a comment

Choose a reason for hiding this comment

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

This looks good to me given the decision to make the bool ops a no-op. Also nice and simple.

@mattipmattip merged commit5cf7883 intonumpy:mainJul 23, 2024
67 of 68 checks passed
@mattip
Copy link
Member

Thanks@mtsokol

@mtsokolmtsokol deleted the rounding-dtypes branchJuly 23, 2024 07:34
lucascolley added a commit to lucascolley/scipy that referenced this pull requestJul 29, 2024
`rv_discrete._cdf` relied on `np.floor` promoting its integer input to `np.float64`. This is no longer the case sincenumpy/numpy#26766.[skip cirrus] [skip circle]
mdhaber pushed a commit to scipy/scipy that referenced this pull requestAug 10, 2024
* BUG: stats: adapt to `np.floor` type promotion removal`rv_discrete._cdf` relied on `np.floor` promoting its integer input to `np.float64`. This is no longer the case sincenumpy/numpy#26766.
tylerjereddy pushed a commit to tylerjereddy/scipy that referenced this pull requestAug 11, 2024
* BUG: stats: adapt to `np.floor` type promotion removal`rv_discrete._cdf` relied on `np.floor` promoting its integer input to `np.float64`. This is no longer the case sincenumpy/numpy#26766.
sklam added a commit to sklam/numba that referenced this pull requestAug 21, 2024
serge-sans-paille added a commit to serge-sans-paille/pythran that referenced this pull requestAug 22, 2024
This one changed with recent numpy upgrade, seenumpy/numpy#26766
serge-sans-paille added a commit to serge-sans-paille/pythran that referenced this pull requestAug 22, 2024
This one changed with recent numpy upgrade, seenumpy/numpy#26766
anushkasuyal pushed a commit to anushkasuyal/scipy that referenced this pull requestSep 13, 2024
* BUG: stats: adapt to `np.floor` type promotion removal`rv_discrete._cdf` relied on `np.floor` promoting its integer input to `np.float64`. This is no longer the case sincenumpy/numpy#26766.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@sebergsebergseberg left review comments

@mhvkmhvkmhvk left review comments

@ngoldbaumngoldbaumngoldbaum approved these changes

Assignees

@mtsokolmtsokol

Projects
None yet
Milestone
2.1.0 release
Development

Successfully merging this pull request may close these issues.

6 participants
@mtsokol@jakevdp@seberg@mattip@mhvk@ngoldbaum

[8]ページ先頭

©2009-2025 Movatter.jp