Skip to content
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

ENH: Add DiscreteGuideTable wrapper #14828

Merged
merged 47 commits into from
Nov 8, 2021

Conversation

Kai-Striega
Copy link
Member

Reference issue

gh-14600

What does this implement/fix?

This is a very minimal start of to the DiscreteGuideTable wrapper. It is included in all_methods and the associated tests pass, however additional tests, documentation and type hinting must still be added.

Additional information

I'm having some problems having the wrapper recognize the probability vector memory view, this can be reproduced as follows:

import numpy as np
from scipy.stats import DiscreteGuideTable

pv = [0.1, 0.3, 0.6]
urng = np.random.default_rng()
rng = DiscreteGuideTable(pv, random_state=urng)
Traceback (most recent call last): File "", line 1, in File "unuran_wrapper.pyx", line 1632, in scipy.stats._unuran.unuran_wrapper.DiscreteGuideTable.__cinit__ AttributeError: 'scipy.stats._unuran.unuran_wrapper.DiscreteGuideTable' object has no attribute 'pv_view'

It's also interesting to note that the tests still pass despite this.

@tylerjereddy tylerjereddy added the enhancement A new feature or improvement label Oct 10, 2021
@Kai-Striega Kai-Striega changed the title ENH: Add minimal DGT wrapper ENH: Add DiscreteGuideTable wrapper Oct 10, 2021
@github-actions github-actions bot added the Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org label Oct 10, 2021
@chrisb83
Copy link
Member

Hi Kai, thanks for working on this. Don't forget to add the type hints in the pyi-file in the _unuran folder.

@tirthasheshpatel
Copy link
Member

tirthasheshpatel commented Oct 14, 2021

Sorry for all the conflicts, @Kai-Striega! I have resolved them for you. If you have changed something locally, I think you can save it in some other temporary workspace or stash the changes using git stash. After pulling the changes here, you can apply your local changes again. Sorry if this is too much trouble for you!

@Kai-Striega
Copy link
Member Author

Kai-Striega commented Oct 16, 2021

EDIT: Fixed by rebuilding all files from source.

I'm having some problems with importing DiscreteGuideTable after your changes, could you take a quick look if it's something obvious?

I haven't made any relevant changes so I suspect it's introduced by the code you merged, although I'm not 100% sure.

This PR copies much of the examples from the
DiscreteGuideTable examples modifying the docs
as appropriate. It further adds examples for how
to use the two kwargs.
Copy link
Member

@tirthasheshpatel tirthasheshpatel left a comment

Choose a reason for hiding this comment

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

Some comments on typos, style, and tests. Wrapper code, docs, and tests look very good! You can copy-paste the documentation in the tutorial and write a small benchmark for the method to finish this up. Given the similarity to DiscreteAliasUrn, I think we should be good to go if tuts and benchmarks look good. Can you take a look whenever you are free @chrisb83?

scipy/stats/_unuran/unuran_wrapper.pyx.templ Outdated Show resolved Hide resolved
scipy/stats/_unuran/unuran_wrapper.pyx.templ Outdated Show resolved Hide resolved
scipy/stats/_unuran/unuran_wrapper.pyx.templ Outdated Show resolved Hide resolved
scipy/stats/_unuran/unuran_wrapper.pyx.templ Outdated Show resolved Hide resolved
scipy/stats/_unuran/unuran_wrapper.pyx.templ Outdated Show resolved Hide resolved
scipy/stats/tests/test_sampling.py Outdated Show resolved Hide resolved
scipy/stats/tests/test_sampling.py Show resolved Hide resolved
DOC: Document PPF example for DGT wrapper
Co-authored-by: Tirth Patel <tirthasheshpatel@gmail.com>
@Kai-Striega
Copy link
Member Author

@tirthasheshpatel could you take a look at why there are merge issues. I don't think that it's anything I did but I'm not sure what's causing them

@tirthasheshpatel
Copy link
Member

Thanks for the changes, @Kai-Striega! I think the last thing left to do is return domain[0]-1 when ppf is evaluated at u = 0. Also, can you add the ppf example to the tutorial? Don't worry about the conflicts. We can resolve them before merging.

@Kai-Striega
Copy link
Member Author

I think the last thing left to do is ...

There's also one more instance of guide_factor that requires back-ticks around it

return domain[0]-1 when ppf is evaluated at u = 0

I'm having some weird issue with this. I've added the line self.domain = domain on line 2101 but, even without any changes to ppf I'm getting the error AttributeError: 'scipy.stats._unuran.unuran_wrapper.DiscreteGuideTable' object has no attribute 'domain'. I'm going to try an rebuild it from scratch to see if that helps otherwise it may take some time to figure out why I can't add that attribute.

Also, can you add the ppf example to the tutorial?

This should already be done starting on line 102 of doc/source/tutorial/stats/sampling_dgt.rst

Don't worry about the conflicts. We can resolve them before merging.

ok. I just don't normally like not having the tests run

@tirthasheshpatel
Copy link
Member

I just don't normally like not having the tests run

Oh, yes! Let me know once you are done with the domain change. I will help resolve conflicts afterwards.

@Kai-Striega
Copy link
Member Author

@tirthasheshpatel it should be ready now ;)

Copy link
Member

@tirthasheshpatel tirthasheshpatel left a comment

Choose a reason for hiding this comment

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

This is very close! Last batch of comments to resolve the failing tests.

scipy/stats/_unuran/unuran_wrapper.pyi Outdated Show resolved Hide resolved
scipy/stats/_unuran/unuran_wrapper.pyi Show resolved Hide resolved
scipy/stats/_unuran/unuran_wrapper.pyx.templ Outdated Show resolved Hide resolved
Kai-Striega and others added 2 commits November 7, 2021 15:28
Co-authored-by: Tirth Patel <tirthasheshpatel@gmail.com>
Co-authored-by: Tirth Patel <tirthasheshpatel@gmail.com>
@Kai-Striega
Copy link
Member Author

Kai-Striega commented Nov 8, 2021

@tirthasheshpatel I've fixed the CI failure test failure, there is one more on MyPy that I can't fix - short of removing the line in its entirety. Could you please take a look and check if there is any better way to get it to pass?

Copy link
Member

@tirthasheshpatel tirthasheshpatel left a comment

Choose a reason for hiding this comment

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

Everything looks good to me now! Tests are comprehensive; docs and tutorials are informative. Only one comment on the tutorial above. @chrisb83 Let me know if we still have your approval after the new changes. If you are happy with it, feel free to merge!

@Kai-Striega
Copy link
Member Author

@tirthasheshpatel @chrisb83 I'd like to thank both of you for all the help - and patience - on this PR. It is appreciated!

Co-authored-by: Kai <kaistriega+github@gmail.com>
@tirthasheshpatel
Copy link
Member

Thanks for your help too @Kai-Striega! This was a big one :)

@chrisb83 chrisb83 merged commit 7ae219e into scipy:master Nov 8, 2021
@chrisb83
Copy link
Member

chrisb83 commented Nov 8, 2021

Thanks Kai and Tirth! Nice to have this method in SciPy now

@tirthasheshpatel
Copy link
Member

Thanks again @Kai-Striega! And thanks @chrisb83 for the reviews!

@Kai-Striega Kai-Striega deleted the discrete_guide_table branch November 9, 2021 03:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Benchmarks Running, verifying or documenting benchmarks for SciPy Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org enhancement A new feature or improvement scipy.stats
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants