GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: Fix np.quantile([Fraction(2,1)], 0.5) by eendebakpt · Pull Request #24711 · numpy/numpy

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Sep 14, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

On main:

import numpy as np
from fractions import Fraction
arr = np.array([Fraction(1, 1), Fraction(2, 1), Fraction(3, 1)])
np.quantile(arr, 0) # output: Fraction(1, 1)
np.quantile(arr, Fraction(1, 2)) # output: Fraction(2, 1)
np.quantile(arr, 0.5) # raises an error

In this PR we update the np.quantile to handle the case np.quantile(arr, 0.5) as well.

Also see #24592

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@eendebakpt eendebakpt changed the title Draft: BUG: Fix np.quantile([Fraction(2,1)], 0.5) BUG: Fix np.quantile([Fraction(2,1)], 0.5) Sep 14, 2023
@charris

charris commented Sep 15, 2023

Copy link
Copy Markdown
Member

The debug test failure seems to be an Ubuntu problem, the requested file is missing from the upstream directory. I suspect it is due to a micro upgrade in the Ubuntu version.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg
seberg reviewed Sep 19, 2023
View reviewed changes
Comment thread numpy/lib/_function_base_impl.py Outdated
lerp_interpolation = asanyarray(add(a, diff_b_a * t, out=out))
subtract(b, diff_b_a * (1 - t), out=lerp_interpolation, where=t >= 0.5)
subtract(b, diff_b_a * (1 - t), out=lerp_interpolation, where=t >= 0.5,
casting='unsafe')

@seberg seberg Sep 19, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This may sound weird, but we should probably also add dtype=type(lerp_interpolation.dtype).

If we have unsafe casting, this means that at least to some degree we ignore the precision of the weights of the quantiles for the output precision (promotion). Can you give me a hint in how things are currently? Does the same happen if we use np.longdouble for the quantile level (q)?

(Sorry, but I am a bit surprised how many subtleties there still seem to be along promotion in quantiles :(, and hoping we can clarify things a bit for me and everyone else.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eendebakpt eendebakpt Nov 8, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@seberg I tested with

import numpy as np
print(np,np.__version__)

x = np.linspace(.1, .7, 5, dtype=np.longdouble)
for q in [0, 1, 0., 1., .5]:
    r = np.quantile(x, q)
    print(f'{q} ({type(q)}) -> {r} ({type(r)})')
    r = np.quantile(x, np.longdouble(q))
    print(f'{q} ({type(q)}) -> {r} ({type(r)})')
    
    
print('----')    
x = np.array([0, 1])
for q in [0, 1, 0., 1., .5]:
    r = np.quantile(x, q)
    print(f'{q} ({type(q)}) -> {r} ({type(r)})')
    r = np.quantile(x, np.longdouble(q))
    print(f'{q} ({type(q)}) -> {r} ({type(r)})')    

Output on 1.26:

<module 'numpy' from '/home/eendebakpt/py310/lib/python3.10/site-packages/numpy/__init__.py'> 1.26.0
0 (<class 'int'>) -> 0.1 (<class 'numpy.longdouble'>)
0 (<class 'int'>) -> 0.1 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 0.7 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 0.7 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.1 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.1 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 0.7 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 0.7 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.4 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.4 (<class 'numpy.longdouble'>)
----
0 (<class 'int'>) -> 0 (<class 'numpy.int64'>)
0 (<class 'int'>) -> 0.0 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 1 (<class 'numpy.int64'>)
1 (<class 'int'>) -> 1.0 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.0 (<class 'numpy.float64'>)
0.0 (<class 'float'>) -> 0.0 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 1.0 (<class 'numpy.float64'>)
1.0 (<class 'float'>) -> 1.0 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.5 (<class 'numpy.float64'>)
0.5 (<class 'float'>) -> 0.5 (<class 'numpy.longdouble'>)

Output with this PR:

<module 'numpy' from '/mnt/data/numpy/numpy/__init__.py'> 2.0.0.dev0+git20231107.a555e7d
0 (<class 'int'>) -> 0.1 (<class 'numpy.longdouble'>)
0 (<class 'int'>) -> 0.1 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 0.7 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 0.7 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.1 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.1 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 0.7 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 0.7 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.4 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.4 (<class 'numpy.longdouble'>)
----
0 (<class 'int'>) -> 0 (<class 'numpy.int64'>)
0 (<class 'int'>) -> 0.0 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 1 (<class 'numpy.int64'>)
1 (<class 'int'>) -> 1.0 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.0 (<class 'numpy.float64'>)
0.0 (<class 'float'>) -> 0.0 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 1.0 (<class 'numpy.float64'>)
1.0 (<class 'float'>) -> 1.0 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.5 (<class 'numpy.float64'>)
0.5 (<class 'float'>) -> 0.5 (<class 'numpy.longdouble'>)

If you want I can add tests for this case as well.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@eendebakpt
eendebakpt requested a review from seberg October 10, 2023 21:01
eendebakpt added 3 commits November 8, 2023 10:25
@eendebakpt
BUG: Fix np.quantile([Fraction(2,1)], 0.5)
5b9730c
@eendebakpt
address review comments
4332695
@eendebakpt
pass type instead of dtype instance
577d8d4
@eendebakpt
eendebakpt force-pushed the quantile_fraction_float branch from 0f8b04d to 577d8d4 Compare November 8, 2023 09:27
@charris charris added 00 - Bug component: numpy.lib 09 - Backport-Candidate PRs tagged should be backported labels Nov 8, 2023
@charris

charris commented Jan 4, 2024

Copy link
Copy Markdown
Member

@seberg ping.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seberg

seberg commented Jan 30, 2024

Copy link
Copy Markdown
Member

Thanks @eendebakpt, sorry for forgetting to look over this again. I think it should be fine to do this cast and it seems like you gave it a spin with that test. So let's give it a shot.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seberg
seberg merged commit cae2234 into numpy:main Jan 30, 2024
charris pushed a commit to charris/numpy that referenced this pull request Feb 3, 2024
@eendebakpt @charris
BUG: Fix np.quantile([Fraction(2,1)], 0.5) (numpy#24711)
fee88ab
* BUG: Fix np.quantile([Fraction(2,1)], 0.5)

* address review comments

* pass type instead of dtype instance
@charris charris mentioned this pull request Feb 3, 2024
BUG: Fix np.quantile([Fraction(2,1)], 0.5) (#24711) #25756
Merged
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Feb 3, 2024
charris added a commit that referenced this pull request Feb 3, 2024
@charris
Merge pull request #25756 from charris/backport-24711
ce89a0a
BUG: Fix np.quantile([Fraction(2,1)], 0.5) (#24711)
@TimotheusBachinger TimotheusBachinger mentioned this pull request Nov 25, 2024
fix build #27836
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@seberg seberg Awaiting requested review from seberg

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

00 - Bug component: numpy.lib

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@eendebakpt @charris @seberg
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.