code: def thin_samples_for_writing(fp, samples, parameters, last_iteration):
      
      if fp.thinned_by > 1:
          if last_iteration is None:
              raise ValueError("File's thinned_by attribute is > 1 ({}), "
                               "but last_iteration not provided."
                               .format(fp.thinned_by))
          thinned_samples = {}
          for param in parameters:
              data = samples[param]
              nsamples = data.shape[-1]
              # To figure out where to start:
              # the last iteration in the file + the file's thinning interval
              # gives the iteration of the next sample that should be written;
              # last_iteration - nsamples gives the iteration of the first
              # sample in samples. Subtracting the latter from the former - 1
              # (-1 to convert from iteration to index) therefore gives the index
              # in the samples data to start using samples.
              thin_start = fp.last_iteration(param) + fp.thinned_by \
                  - (last_iteration - nsamples) - 1
              thinned_samples[param] = data[..., thin_start::fp.thinned_by]
      else:
          thinned_samples = samples
      return thinned_samples
docs: Thins samples for writing to disk.
  
      The thinning interval to use is determined by the given file handler's
      ``thinned_by`` attribute. If that attribute is 1, just returns the samples.
  
      Parameters
      ----------
      fp : MCMCMetadataIO instance
          The file the sampels will be written to. Needed to determine the
          thin interval used on disk.
      samples : dict
          Dictionary mapping parameter names to arrays of (unthinned) samples.
          The arrays are thinned along their last dimension.
      parameters : list of str
          The parameters to thin in ``samples`` before writing. All listed
          parameters must be in ``samples``.
      last_iteration : int
          The iteration that the last sample in ``samples`` occurred at. This is
          needed to figure out where to start the thinning in ``samples``, such
          that the interval between the last sample on disk and the first new
          sample is the same as all of the other samples.
  
      Returns
      -------
      dict :
          Dictionary of the thinned samples to write.