original_docstring: Generate samples from the distribution of a random variable.
  
      Parameters
      ----------
      generator : function
          Function to generate the random samples. The function is
          expected take parameters for generating samples and
          a keyword argument ``size`` which determines the shape
          of the samples.
          The args and kwargs (stripped of the keywords below) will be
          passed to the generator function.
  
      keyword arguments
      ~~~~~~~~~~~~~~~~~
  
      dist_shape : int or tuple of int
          The shape of the random variable (i.e., the shape attribute).
      size : int or tuple of int
          The required shape of the samples.
      broadcast_shape: tuple of int or None
          The shape resulting from the broadcasting of the parameters.
          If not specified it will be inferred from the shape of the
          parameters. This may be required when the parameter shape
          does not determine the shape of a single sample, for example,
          the shape of the probabilities in the Categorical distribution.
      not_broadcast_kwargs: dict or None
          Key word argument dictionary to provide to the random generator, which
          must not be broadcasted with the rest of the args and kwargs.
  
      Any remaining args and kwargs are passed on to the generator function.
      
docstring: Generate samples from the distribution of a random variable.
  Parameters
  
  generator : function
  Function to generate the random samples. The function is
  expected take parameters for generating samples and
  a keyword argument ``size`` which determines the shape
  of the samples.
  The args and kwargs (stripped of the keywords below) will be
  passed to the generator function.
  
  
  
  dist_shape : int or tuple of int
  The shape of the random variable .
  size : int or tuple of int
  The required shape of the samples.
  broadcast_shape: tuple of int or None
  The shape resulting from the broadcasting of the parameters.
  If not specified it will be inferred from the shape of the
  parameters. This may be required when the parameter shape
  does not determine the shape of a single sample, for example,
  the shape of the probabilities in the Categorical distribution.
  not_broadcast_kwargs: dict or None
  Key word argument dictionary to provide to the random generator, which
  must not be broadcasted with the rest of the args and kwargs.
  
  Any remaining args and kwargs are passed on to the generator function.
docstring_tokens: Generate samples from the distribution of a random variable . Parameters generator : function Function to generate the random samples . The function is expected take parameters for generating samples and a keyword argument ` ` size ` ` which determines the shape of the samples . The args and kwargs ( stripped of the keywords below ) will be passed to the generator function . dist_shape : int or tuple of int The shape of the random variable . size : int or tuple of int The required shape of the samples . broadcast_shape : tuple of int or None The shape resulting from the broadcasting of the parameters . If not specified it will be inferred from the shape of the parameters . This may be required when the parameter shape does not determine the shape of a single sample for example the shape of the probabilities in the Categorical distribution . not_broadcast_kwargs : dict or None Key word argument dictionary to provide to the random generator which must not be broadcasted with the rest of the args and kwargs . Any remaining args and kwargs are passed on to the generator function .
code: def generate_samples(generator, *args, **kwargs):
      """Generate samples from the distribution of a random variable.
  
      Parameters
      ----------
      generator : function
          Function to generate the random samples. The function is
          expected take parameters for generating samples and
          a keyword argument ``size`` which determines the shape
          of the samples.
          The args and kwargs (stripped of the keywords below) will be
          passed to the generator function.
  
      keyword arguments
      ~~~~~~~~~~~~~~~~~
  
      dist_shape : int or tuple of int
          The shape of the random variable (i.e., the shape attribute).
      size : int or tuple of int
          The required shape of the samples.
      broadcast_shape: tuple of int or None
          The shape resulting from the broadcasting of the parameters.
          If not specified it will be inferred from the shape of the
          parameters. This may be required when the parameter shape
          does not determine the shape of a single sample, for example,
          the shape of the probabilities in the Categorical distribution.
      not_broadcast_kwargs: dict or None
          Key word argument dictionary to provide to the random generator, which
          must not be broadcasted with the rest of the args and kwargs.
  
      Any remaining args and kwargs are passed on to the generator function.
      """
      dist_shape = kwargs.pop('dist_shape', ())
      one_d = _is_one_d(dist_shape)
      size = kwargs.pop('size', None)
      broadcast_shape = kwargs.pop('broadcast_shape', None)
      not_broadcast_kwargs = kwargs.pop('not_broadcast_kwargs', None)
      if not_broadcast_kwargs is None:
          not_broadcast_kwargs = dict()
  
      # Parse out raw input parameters for the generator
      args = tuple(p[0] if isinstance(p, tuple) else p for p in args)
      for key in kwargs:
          p = kwargs[key]
          kwargs[key] = p[0] if isinstance(p, tuple) else p
  
      # Convert size and dist_shape to tuples
      size_tup = to_tuple(size)
      dist_shape = to_tuple(dist_shape)
      if dist_shape[:len(size_tup)] == size_tup:
          # dist_shape is prepended with size_tup. This is not a consequence
          # of the parameters being drawn size_tup times! By chance, the
          # distribution's shape has its first elements equal to size_tup.
          # This means that we must prepend the size_tup to dist_shape, and
          # check if that broadcasts well with the parameters
          _dist_shape = size_tup + dist_shape
      else:
          _dist_shape = dist_shape
  
  
      if broadcast_shape is None:
          # If broadcast_shape is not explicitly provided, it is inferred as the
          # broadcasted shape of the input parameter and dist_shape, taking into
          # account the potential size prefix
          inputs = args + tuple(kwargs.values())
          broadcast_shape = broadcast_dist_samples_shape(
              [np.asarray(i).shape for i in inputs] + [_dist_shape],
              size=size_tup
          )
          # We do this instead of broadcast_distribution_samples to avoid
          # creating a dummy array with dist_shape in memory
          inputs = get_broadcastable_dist_samples(
              inputs,
              size=size_tup,
              must_bcast_with=broadcast_shape,
          )
          # We modify the arguments with their broadcasted counterparts
          args = tuple(inputs[:len(args)])
          for offset, key in enumerate(kwargs):
              kwargs[key] = inputs[len(args) + offset]
      # Update kwargs with the keyword arguments that were not broadcasted
      kwargs.update(not_broadcast_kwargs)
  
      # We ensure that broadcast_shape is a tuple
      broadcast_shape = to_tuple(broadcast_shape)
  
      try:
          dist_bcast_shape = broadcast_dist_samples_shape(
              [_dist_shape, broadcast_shape],
              size=size,
          )
      except (ValueError, TypeError):
          raise TypeError('''Attempted to generate values with incompatible shapes:
              size: {size}
              size_tup: {size_tup}
              broadcast_shape[:len(size_tup)] == size_tup: {size_prepended}
              dist_shape: {dist_shape}
              broadcast_shape: {broadcast_shape}
          '''.format(size=size,
                     size_tup=size_tup,
                     dist_shape=dist_shape,
                     broadcast_shape=broadcast_shape,
                     size_prepended=broadcast_shape[:len(size_tup)] == size_tup)
          )
      if dist_bcast_shape[:len(size_tup)] == size_tup:
          samples = generator(size=dist_bcast_shape, *args, **kwargs)
      else:
          samples = generator(size=size_tup + dist_bcast_shape, *args, **kwargs)
      samples = np.asarray(samples)
  
      # reshape samples here
      if samples.ndim > 0 and samples.shape[0] == 1 and size_tup == (1,):
          if (len(samples.shape) > len(dist_shape) and
              samples.shape[-len(dist_shape):] == dist_shape[-len(dist_shape):]
          ):
              samples = samples.reshape(samples.shape[1:])
  
      if (one_d and samples.ndim > 0 and samples.shape[-1] == 1
          and (samples.shape != size_tup or
               size_tup == tuple() or
               size_tup == (1,))
      ):
          samples = samples.reshape(samples.shape[:-1])
      return np.asarray(samples)
code_tokens: def generate_samples ( generator , * args , ** kwargs ) : dist_shape = kwargs . pop ( 'dist_shape' , ( ) ) one_d = _is_one_d ( dist_shape ) size = kwargs . pop ( 'size' , None ) broadcast_shape = kwargs . pop ( 'broadcast_shape' , None ) not_broadcast_kwargs = kwargs . pop ( 'not_broadcast_kwargs' , None ) if not_broadcast_kwargs is None : not_broadcast_kwargs = dict ( ) args = tuple ( p [ 0 ] if isinstance ( p , tuple ) else p for p in args ) for key in kwargs : p = kwargs [ key ] kwargs [ key ] = p [ 0 ] if isinstance ( p , tuple ) else p size_tup = to_tuple ( size ) dist_shape = to_tuple ( dist_shape ) if dist_shape [ : len ( size_tup ) ] == size_tup : _dist_shape = size_tup + dist_shape else : _dist_shape = dist_shape if broadcast_shape is None : inputs = args + tuple ( kwargs . values ( ) ) broadcast_shape = broadcast_dist_samples_shape ( [ np . asarray ( i ) . shape for i in inputs ] + [ _dist_shape ] , size = size_tup ) inputs = get_broadcastable_dist_samples ( inputs , size = size_tup , must_bcast_with = broadcast_shape , ) args = tuple ( inputs [ : len ( args ) ] ) for offset , key in enumerate ( kwargs ) : kwargs [ key ] = inputs [ len ( args ) + offset ] kwargs . update ( not_broadcast_kwargs ) broadcast_shape = to_tuple ( broadcast_shape ) try : dist_bcast_shape = broadcast_dist_samples_shape ( [ _dist_shape , broadcast_shape ] , size = size , ) except ( ValueError , TypeError ) : raise TypeError ( '''Attempted to generate values with incompatible shapes:
              size: {size}
              size_tup: {size_tup}
              broadcast_shape[:len(size_tup)] == size_tup: {size_prepended}
              dist_shape: {dist_shape}
              broadcast_shape: {broadcast_shape}
          ''' . format ( size = size , size_tup = size_tup , dist_shape = dist_shape , broadcast_shape = broadcast_shape , size_prepended = broadcast_shape [ : len ( size_tup ) ] == size_tup ) ) if dist_bcast_shape [ : len ( size_tup ) ] == size_tup : samples = generator ( size = dist_bcast_shape , * args , ** kwargs ) else : samples = generator ( size = size_tup + dist_bcast_shape , * args , ** kwargs ) samples = np . asarray ( samples ) if samples . ndim > 0 and samples . shape [ 0 ] == 1 and size_tup == ( 1 , ) : if ( len ( samples . shape ) > len ( dist_shape ) and samples . shape [ - len ( dist_shape ) : ] == dist_shape [ - len ( dist_shape ) : ] ) : samples = samples . reshape ( samples . shape [ 1 : ] ) if ( one_d and samples . ndim > 0 and samples . shape [ - 1 ] == 1 and ( samples . shape != size_tup or size_tup == tuple ( ) or size_tup == ( 1 , ) ) ) : samples = samples . reshape ( samples . shape [ : - 1 ] ) return np . asarray ( samples )
short_docstring: Generate samples from the distribution of a random variable.
short_docstring_tokens: Generate samples from the distribution of a random variable .
comment: """Generate samples from the distribution of a random variable.
  
      Parameters
      ----------
      generator : function
          Function to generate the random samples. The function is
          expected take parameters for generating samples and
          a keyword argument ``size`` which determines the shape
          of the samples.
          The args and kwargs (stripped of the keywords below) will be
          passed to the generator function.
  
      keyword arguments
      ~~~~~~~~~~~~~~~~~
  
      dist_shape : int or tuple of int
          The shape of the random variable (i.e., the shape attribute).
      size : int or tuple of int
          The required shape of the samples.
      broadcast_shape: tuple of int or None
          The shape resulting from the broadcasting of the parameters.
          If not specified it will be inferred from the shape of the
          parameters. This may be required when the parameter shape
          does not determine the shape of a single sample, for example,
          the shape of the probabilities in the Categorical distribution.
      not_broadcast_kwargs: dict or None
          Key word argument dictionary to provide to the random generator, which
          must not be broadcasted with the rest of the args and kwargs.
  
      Any remaining args and kwargs are passed on to the generator function.
      """$SEP$# Parse out raw input parameters for the generator$SEP$# Convert size and dist_shape to tuples$SEP$# dist_shape is prepended with size_tup. This is not a consequence$SEP$# of the parameters being drawn size_tup times! By chance, the$SEP$# distribution's shape has its first elements equal to size_tup.$SEP$# This means that we must prepend the size_tup to dist_shape, and$SEP$# check if that broadcasts well with the parameters$SEP$# If broadcast_shape is not explicitly provided, it is inferred as the$SEP$# broadcasted shape of the input parameter and dist_shape, taking into$SEP$# account the potential size prefix$SEP$# We do this instead of broadcast_distribution_samples to avoid$SEP$# creating a dummy array with dist_shape in memory$SEP$# We modify the arguments with their broadcasted counterparts$SEP$# Update kwargs with the keyword arguments that were not broadcasted$SEP$# We ensure that broadcast_shape is a tuple$SEP$# reshape samples here