code: def _do_stratification(
      data,
      ratio=0.75,
      min_rating=1,
      filter_by="user",
      is_random=True,
      seed=42,
      col_user=DEFAULT_USER_COL,
      col_item=DEFAULT_ITEM_COL,
      col_timestamp=DEFAULT_TIMESTAMP_COL,
  ):
      # A few preliminary checks.
      if not (filter_by == "user" or filter_by == "item"):
          raise ValueError(
              "filter_by should be either 'user' or 'item'."
          )
  
      if min_rating < 1:
          raise ValueError(
              "min_rating should be integer and larger than or equal to 1."
          )
  
      if col_user not in data.columns:
          raise ValueError("Schema of data not valid. Missing User Col")
  
      if col_item not in data.columns:
          raise ValueError("Schema of data not valid. Missing Item Col")
  
      if not is_random:
          if col_timestamp not in data.columns:
              raise ValueError(
                  "Schema of data not valid. Missing Timestamp Col"
              )
  
      multi_split, ratio = process_split_ratio(ratio)
  
      split_by_column = col_user if filter_by == "user" else col_item
  
      ratio = ratio if multi_split else [ratio, 1 - ratio]
  
      if min_rating > 1:
          data = min_rating_filter_pandas(
              data,
              min_rating=min_rating,
              filter_by=filter_by,
              col_user=col_user,
              col_item=col_item,
          )
  
      if is_random:
          np.random.seed(seed)
          data["random"] = np.random.rand(data.shape[0])
          order_by = "random"
      else:
          order_by = col_timestamp
  
      data = data.sort_values([split_by_column, order_by])
  
      groups = data.groupby(split_by_column)
  
      data["count"] = groups[split_by_column].transform("count")
      data["rank"] = groups.cumcount() + 1
  
      if is_random:
          data = data.drop("random", axis=1)
  
      splits = []
      prev_threshold = None
      for threshold in np.cumsum(ratio):
          condition = data["rank"] <= round(threshold * data["count"])
          if prev_threshold is not None:
              condition &= data["rank"] > round(
                  prev_threshold * data["count"]
              )
          splits.append(data[condition].drop(["rank", "count"], axis=1))
          prev_threshold = threshold
  
      return splits
  
file_path: recommenders/recommenders/datasets/python_splitters.py
imports: import numpy
  import pandas
  from sklearn.model_selection import train_test_split
  from recommenders.utils.constants import DEFAULT_ITEM_COL, DEFAULT_USER_COL, DEFAULT_TIMESTAMP_COL
  from recommenders.datasets.split_utils import process_split_ratio, min_rating_filter_pandas, split_pandas_data_with_ratios
label_desc: Use image node_id 2 for calling a global function with example usage: _do_stratification(data, ratio, min_rating, filter_by, is_random, seed, col_user, col_item, col_timestamp) and returns: splits