API: rearrange the cython files in numpy.random#14608
API: rearrange the cython files in numpy.random#14608rgommers merged 10 commits intonumpy:masterfrom
Conversation
|
The first changeset moves things around without any function renaming yet. |
numpy/random/setup.py
Outdated
There was a problem hiding this comment.
Still need to create these pxd files so users can do from pcg64 cimport PCG64 to use the BitGenerator directly from cython
There was a problem hiding this comment.
Why not keep this in the main namespace? PCG64 is in numpy.random so I'd much prefer it not be in separate pxd files here
There was a problem hiding this comment.
main namespace
This mimics the way we import the BitGenerators now.
Do you mean we should refactor the module so that __init__.py imports differently?
There was a problem hiding this comment.
I am still seeing the BitGenerators in both the c-extension module and as imported by __init__.py:
>>> np.random.mt19937.MT19937
<class 'numpy.random.mt19937.MT19937'>
>>> np.random.MT19937
<class 'numpy.random.mt19937.MT19937'>
>>>
I am not sure we can flatten the namespace without unifying all the c-extension modules into one
There was a problem hiding this comment.
Hmm, missing some more underscores.
I am not sure we can flatten the namespace without unifying all the c-extension modules into one
Of course we can, it's just a matter of adding underscores to the .pyx files.
There was a problem hiding this comment.
made private in 9bdf1749e. mtrand must remain public as per NEP 19
... the global
RandomStateinstance MUST be accessible in this initial release by the namenumpy.random.mtrand._rand...
|
Those principles all sound good, thanks @mattip. |
There was no reply yet to your mailing list question on whether these really need to be exposed. Your preference on the mailing list was "no" I believe. I'd like to see a good rationale too before we expose the legacy generator. |
|
the next commit moves |
|
I am going back and forth about shipping |
I think that would be better indeed.
I still don't quite understand why Is there anything else in those two submodules that is public but not imported into the |
082e17e to
724ec06
Compare
|
I think this is ready for review now. Note there are no public c-extension modules, all of them (except mtrand, which is private but cannot be renamed as per NEP 19) are private. Once this is merged, we can revisit #14604 to try to develop the correct C-API. |
|
Awesome, thanks Matti.
sounds like a good plan |
| bit_generator.ISeedSequence | ||
| bit_generator.ISpawnableSeedSequence | ||
| bit_generator.SeedlessSeedSequence | ||
| _bit_generator.ISeedSequence |
There was a problem hiding this comment.
This looks wrong. You should never have something starting with an underscore in a toctree listing (or anywhere in the docs really).
So when I asked if there was anything in bit_generator besides SeedSequence that needs to be public, I guess the right answer was not "no" but "ISeedSequence, ISpawnableSeedSequence and SeedlessSeedSequence" (these are needed for writing other bitgenerators outside of NumPy, right?)?
There was a problem hiding this comment.
No, they are needed to implement new SeedSequence interfaces as kind of an abstract base class. BitGenerators should use bit_generator.SeedSequence. I think we should remove these then, and maybe document them in the (as yet nonexistant) random C-API documentation, not in the random python reference documentation.
There was a problem hiding this comment.
If they're not supposed to be used from Python at all, but only from Cython/C for new BitGenerator authors, then that sounds like the right course of action.
There was a problem hiding this comment.
What should we do with text like this "One may also pass in an implementor of the ISeedSequence interface like SeedSequence"? How about simplifying it to not mention ISeedSequence ?
There was a problem hiding this comment.
adopted the "don't mention" approach for now.
|
API and docs part of this PR look quite good to me now, module the few typos I just pointed out. |
|
I'm confused why |
|
The public/private distinction is for python access. C and Cython access still need to be worked out, once we have solidified the python side, see #14604. |
|
Did you mean to commit |
|
Same comment for |
bashtage
left a comment
There was a problem hiding this comment.
Very small points. Only worthwhile ones are the commits of the pyx and pxd that come from templates.
| PCG64 <pcg64> | ||
| Philox <philox> | ||
| SFC64 <sfc64> | ||
| MT19937 <mt19937> |
There was a problem hiding this comment.
Does 3 or 4 spaces matter? File seem to use both.
There was a problem hiding this comment.
unified to 4, would be nice to do this gradually across the documentation
There was a problem hiding this comment.
I think it only has to be consistent per file to work. Pretty sure the default is 3 spaces everywhere.
|
|
||
| np.import_array() | ||
|
|
||
| cdef extern from "include/distributions.h": |
There was a problem hiding this comment.
Should these be in _bounded_integers.pxd.in?
There was a problem hiding this comment.
I don't think so, then we would have to ship distributions.h. But I think we should revisit this in terms of documented use-cases in #14604 after this goes in.
|
Okay this seems good to go now. Let's get this in so we can start looking at the API part. Thanks @mattip! |
This is the code part of #14604 to cleanup the api and allow numpy.random to be used from cython.
Principles:
pxdfiles define the cython API, any public except mtrand*.soshould ship a*.pxdand any*.pxdshould have a*.so. Any "private"*.soshould begin with_. Edit: qualify to state "public except mtrand", which cannot be made private as per NEP 19. We only have private c-extension modules now, so there are nopxdfiles to ship.numpy/random/includeship theEdit: there are no pxd or headers to shippxdfiles and headersCleanup
common->_commonbound_integers->_bound_integers?*libcimports from_common, let eachpxdimport on its ownbitgen_tfromcommontobit_generator.pxddistributions.pxdintogenerator.pxd,legacy_distributions.pxdintomtrans.pxd.Renaming and removing unused functions in
distributions.handlegacy_distributions.hrandom_gauss_zig->random_standard_normalzigsuffixedit: except
random_standard_exponential_zig_fsuffixrandom_double->random_randomfor consistency withrandom.randomrandom_standard_uniformrandom_geometric_*do not need to be exposedgaussnamesAfter this cleanup we can create some cython user stories and check that the API is sufficient.