_pickle.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 __generator_ctor(bit_generator_name='MT19937'):
  14. """
  15. Pickling helper function that returns a Generator object
  16. Parameters
  17. ----------
  18. bit_generator_name : str
  19. String containing the core BitGenerator
  20. Returns
  21. -------
  22. rg: Generator
  23. Generator using the named core BitGenerator
  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 Generator(bit_generator())
  31. def __bit_generator_ctor(bit_generator_name='MT19937'):
  32. """
  33. Pickling helper function that returns a bit generator object
  34. Parameters
  35. ----------
  36. bit_generator_name : str
  37. String containing the name of the BitGenerator
  38. Returns
  39. -------
  40. bit_generator: BitGenerator
  41. BitGenerator instance
  42. """
  43. if bit_generator_name in BitGenerators:
  44. bit_generator = BitGenerators[bit_generator_name]
  45. else:
  46. raise ValueError(str(bit_generator_name) + ' is not a known '
  47. 'BitGenerator module.')
  48. return bit_generator()
  49. def __randomstate_ctor(bit_generator_name='MT19937'):
  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
  56. Returns
  57. -------
  58. rs: RandomState
  59. Legacy RandomState using the named core BitGenerator
  60. """
  61. if bit_generator_name in BitGenerators:
  62. bit_generator = BitGenerators[bit_generator_name]
  63. else:
  64. raise ValueError(str(bit_generator_name) + ' is not a known '
  65. 'BitGenerator module.')
  66. return RandomState(bit_generator())