code: def sample_gen(dataset, xy, indexes=None, masked=False):
      """Sample pixels from a dataset
  
      Parameters
      ----------
      dataset : rasterio Dataset
          Opened in "r" mode.
      xy : iterable
          Pairs of x, y coordinates in the dataset's reference system.
  
          Note: Sorting coordinates can often yield better performance.
          A sort_xy function is provided in this module for convenience.
      indexes : int or list of int
          Indexes of dataset bands to sample.
      masked : bool, default: False
          Whether to mask samples that fall outside the extent of the
          dataset.
  
      Yields
      ------
      array
          A array of length equal to the number of specified indexes
          containing the dataset values for the bands corresponding to
          those indexes.
  
      """
      read = dataset.read
      height = dataset.height
      width = dataset.width
  
      if indexes is None:
          indexes = dataset.indexes
      elif isinstance(indexes, int):
          indexes = [indexes]
  
      nodata = np.full(
          len(indexes), (dataset.nodata or 0), dtype=dataset.dtypes[0]
      )
      if masked:
          # Masks for masked arrays are inverted (False means valid)
          mask_flags = [
              set(dataset.mask_flag_enums[i - 1]) for i in indexes
          ]
          dataset_is_masked = any(
              {MaskFlags.alpha, MaskFlags.per_dataset, MaskFlags.nodata}
              & enums
              for enums in mask_flags
          )
          mask = [
              False
              if dataset_is_masked and enums == {MaskFlags.all_valid}
              else True
              for enums in mask_flags
          ]
          nodata = np.ma.array(nodata, mask=mask)
  
      for row, col in _transform_xy(dataset, xy):
          if 0 <= row < height and 0 <= col < width:
              win = Window(col, row, 1, 1)
              data = read(indexes, window=win, masked=masked)
              yield data[:, 0, 0]
          else:
              yield nodata
  
file_path: rasterio/rasterio/sample.py
imports: import numpy
  from itertools import islice
  from rasterio.enums import MaskFlags
  from rasterio.windows import Window
  from rasterio.transform import rowcol
label_desc: Use image node_id 3 for calling a global function with example usage: sample_gen(dataset, xy, indexes, masked) without return types