code: def _sample_guest(
      ctx,
      df: DataFrame,
      n: int = None,
      frac: Union[float, Dict[Any, float]] = None,
      replace: bool = True,
      random_state=None,
      sync=True,
  ):
      if n is not None and frac is not None:
          raise ValueError(
              "sample's parameters n and frac should not be set in the same time."
          )
  
      if frac is not None:
          if isinstance(frac, float):
              if frac > 1:
                  raise ValueError(
                      f"sample's parameter frac={frac} should <= 1.0"
                  )
              n = max(1, int(frac * df.shape[0]))
          else:
              for k, f in frac.items():
                  if f > 1 and replace is False:
                      raise ValueError(
                          f"sample's parameter frac's label={k}, fraction={f} "
                          f"should <= 1.0 if replace=False"
                      )
  
      if n is not None:
          if n > df.shape[0] and replace is False:
              raise ValueError(
                  f"sample's parameter n={n} should <= data_size={df.shape[0]} if replace=False"
              )
  
          if replace:
              choices = resample(
                  list(range(df.shape[0])),
                  replace=True,
                  n_samples=n,
                  random_state=random_state,
              )
              indexer = list(
                  df.get_indexer(target="sample_id").collect()
              )
              regenerated_sample_id_prefix = generate_sample_id_prefix()
              regenerated_ids = generate_sample_id(
                  n, regenerated_sample_id_prefix
              )
              choice_with_regenerated_ids = _agg_choices(
                  ctx,
                  indexer,
                  choices,
                  regenerated_ids,
                  df.block_table.num_partitions,
              )
  
              if sync:
                  ctx.hosts.put(REGENERATED_TAG, True)
                  ctx.hosts.put(
                      REGENERATED_IDS, choice_with_regenerated_ids
                  )
  
              regenerated_raw_table = _regenerated_sample_ids(
                  df, choice_with_regenerated_ids
              )
              sample_df = _convert_raw_table_to_df(
                  df._ctx, regenerated_raw_table, df.data_manager
              )
              if sync:
                  sample_indexer = sample_df.get_indexer(
                      target="sample_id"
                  )
                  ctx.hosts.put(SAMPLE_INDEX_TAG, sample_indexer)
  
          else:
              sample_df = df.sample(n=n, random_state=random_state)
              if sync:
                  sample_indexer = sample_df.get_indexer(
                      target="sample_id"
                  )
                  ctx.hosts.put(REGENERATED_TAG, False)
                  ctx.hosts.put(SAMPLE_INDEX_TAG, sample_indexer)
      else:
          up_sample = False
          for label, f in frac.items():
              if f > 1.0:
                  up_sample = True
  
          if up_sample:
              regenerated_sample_id_prefix = generate_sample_id_prefix()
              choice_with_regenerated_ids = None
              for label, f in frac.items():
                  label_df = df.iloc(df.label == label)
                  label_n = max(1, int(label_df.shape[0] * f))
                  choices = resample(
                      list(range(label_df.shape[0])),
                      replace=True,
                      n_samples=label_n,
                      random_state=random_state,
                  )
                  label_indexer = list(
                      label_df.get_indexer(target="sample_id").collect()
                  )
                  regenerated_ids = generate_sample_id(
                      label_n, regenerated_sample_id_prefix
                  )
                  label_choice_with_regenerated_ids = _agg_choices(
                      ctx,
                      label_indexer,
                      choices,
                      regenerated_ids,
                      df.block_table.num_partitions,
                  )
                  if choice_with_regenerated_ids is None:
                      choice_with_regenerated_ids = (
                          label_choice_with_regenerated_ids
                      )
                  else:
                      choice_with_regenerated_ids = (
                          choice_with_regenerated_ids.union(
                              label_choice_with_regenerated_ids
                          )
                      )
  
              if sync:
                  ctx.hosts.put(REGENERATED_TAG, True)
                  ctx.hosts.put(
                      REGENERATED_IDS, choice_with_regenerated_ids
                  )
              regenerated_raw_table = _regenerated_sample_ids(
                  df, choice_with_regenerated_ids
              )
              sample_df = _convert_raw_table_to_df(
                  df._ctx, regenerated_raw_table, df.data_manager
              )
              if sync:
                  sample_indexer = sample_df.get_indexer(
                      target="sample_id"
                  )
                  ctx.hosts.put(SAMPLE_INDEX_TAG, sample_indexer)
          else:
              sample_df = None
              for label, f in frac.items():
                  label_df = df.iloc(df.label == label)
                  label_n = max(1, int(label_df.shape[0] * f))
                  sample_label_df = label_df.sample(
                      n=label_n, random_state=random_state
                  )
  
                  if sample_df is None:
                      sample_df = sample_label_df
                  else:
                      sample_df = DataFrame.vstack(
                          [sample_df, sample_label_df]
                      )
  
              if sync:
                  sample_indexer = sample_df.get_indexer(
                      target="sample_id"
                  )
                  ctx.hosts.put(REGENERATED_TAG, False)
                  ctx.hosts.put(SAMPLE_INDEX_TAG, sample_indexer)
  
      return sample_df
  
file_path: FATE/python/fate/arch/dataframe/utils/_sample.py
imports: import functools
  from typing import Union, Dict, Any
  from sklearn.utils import resample
  from ._id_generator import generate_sample_id, generate_sample_id_prefix
  from .._dataframe import DataFrame
label_desc: Use image node_id 3 for calling a global function with example usage: _sample_guest(ctx, df, n, frac, replace, random_state, sync) and returns: sample_df