_pickle.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from .mtrand import RandomState
  2. from ._philox import Philox
  3. from ._pcg64 import PCG64, PCG64DXSM
  4. from ._sfc64 import SFC64
  5. from ._generator import Generator
  6. from ._mt19937 import MT19937
  7. BitGenerators = {'MT19937': MT19937,
  8. 'PCG64': PCG64,
  9. 'PCG64DXSM': PCG64DXSM,
  10. 'Philox': Philox,
  11. 'SFC64': SFC64,
  12. }
  13. def __bit_generator_ctor(bit_generator_name='MT19937'):
  14. """
  15. Pickling helper function that returns a bit generator object
  16. Parameters
  17. ----------
  18. bit_generator_name : str
  19. String containing the name of the BitGenerator
  20. Returns
  21. -------
  22. bit_generator : BitGenerator
  23. BitGenerator instance
  24. """
  25. if bit_generator_name in BitGenerators:
  26. bit_generator = BitGenerators[bit_generator_name]
  27. else:
  28. raise ValueError(str(bit_generator_name) + ' is not a known '
  29. 'BitGenerator module.')
  30. return bit_generator()
  31. def __generator_ctor(bit_generator_name="MT19937",
  32. bit_generator_ctor=__bit_generator_ctor):
  33. """
  34. Pickling helper function that returns a Generator object
  35. Parameters
  36. ----------
  37. bit_generator_name : str
  38. String containing the core BitGenerator's name
  39. bit_generator_ctor : callable, optional
  40. Callable function that takes bit_generator_name as its only argument
  41. and returns an instantized bit generator.
  42. Returns
  43. -------
  44. rg : Generator
  45. Generator using the named core BitGenerator
  46. """
  47. return Generator(bit_generator_ctor(bit_generator_name))
  48. def __randomstate_ctor(bit_generator_name="MT19937",
  49. bit_generator_ctor=__bit_generator_ctor):
  50. """
  51. Pickling helper function that returns a legacy RandomState-like object
  52. Parameters
  53. ----------
  54. bit_generator_name : str
  55. String containing the core BitGenerator's name
  56. bit_generator_ctor : callable, optional
  57. Callable function that takes bit_generator_name as its only argument
  58. and returns an instantized bit generator.
  59. Returns
  60. -------
  61. rs : RandomState
  62. Legacy RandomState using the named core BitGenerator
  63. """
  64. return RandomState(bit_generator_ctor(bit_generator_name))