code: def training_pipeline(model):
      r"""AlphaPy Training Pipeline
  
      Parameters
      ----------
      model : alphapy.Model
          The model object for controlling the pipeline.
  
      Returns
      -------
      model : alphapy.Model
          The final results are stored in the model object.
  
      Raises
      ------
      KeyError
          If the number of columns of the train and test data do not match,
          then this exception is raised.
  
      """
  
      logger.info("Training Pipeline")
  
      # Unpack the model specifications
  
      calibration = model.specs["calibration"]
      directory = model.specs["directory"]
      drop = model.specs["drop"]
      extension = model.specs["extension"]
      feature_selection = model.specs["feature_selection"]
      grid_search = model.specs["grid_search"]
      model_type = model.specs["model_type"]
      rfe = model.specs["rfe"]
      sampling = model.specs["sampling"]
      scorer = model.specs["scorer"]
      seed = model.specs["seed"]
      separator = model.specs["separator"]
      split = model.specs["split"]
      target = model.specs["target"]
  
      # Get train and test data
  
      X_train, y_train = get_data(model, Partition.train)
      X_test, y_test = get_data(model, Partition.test)
  
      # If there is no test partition, then we will split the train partition
  
      if X_test.empty:
          logger.info("No Test Data Found")
          logger.info("Splitting Training Data")
          X_train, X_test, y_train, y_test = train_test_split(
              X_train, y_train, test_size=split, random_state=seed
          )
  
      # Determine if there are any test labels
  
      if y_test.any():
          logger.info("Test Labels Found")
          model.test_labels = True
      model = save_features(model, X_train, X_test, y_train, y_test)
  
      # Log feature statistics
  
      logger.info("Original Feature Statistics")
      logger.info("Number of Training Rows    : %d", X_train.shape[0])
      logger.info("Number of Training Columns : %d", X_train.shape[1])
      if model_type == ModelType.classification:
          uv, uc = np.unique(y_train, return_counts=True)
          logger.info("Unique Training Values for %s : %s", target, uv)
          logger.info("Unique Training Counts for %s : %s", target, uc)
      logger.info("Number of Testing Rows     : %d", X_test.shape[0])
      logger.info("Number of Testing Columns  : %d", X_test.shape[1])
      if model_type == ModelType.classification and model.test_labels:
          uv, uc = np.unique(y_test, return_counts=True)
          logger.info("Unique Testing Values for %s : %s", target, uv)
          logger.info("Unique Testing Counts for %s : %s", target, uc)
  
      # Merge training and test data
  
      if X_train.shape[1] == X_test.shape[1]:
          split_point = X_train.shape[0]
          X_all = pd.concat([X_train, X_test])
      else:
          raise IndexError(
              "The number of training and test columns [%d, %d] must match."
              % (X_train.shape[1], X_test.shape[1])
          )
  
      # Apply transforms to the feature matrix
      X_all = apply_transforms(model, X_all)
  
      # Drop features
      X_all = drop_features(X_all, drop)
  
      # Save the train and test files with extracted and dropped features
  
      datestamp = get_datestamp()
      data_dir = SSEP.join([directory, "input"])
      df_train = X_all.iloc[:split_point, :]
      df_train[target] = y_train
      output_file = USEP.join([model.train_file, datestamp])
      write_frame(
          df_train,
          data_dir,
          output_file,
          extension,
          separator,
          index=False,
      )
      df_test = X_all.iloc[split_point:, :]
      if y_test.any():
          df_test[target] = y_test
      output_file = USEP.join([model.test_file, datestamp])
      write_frame(
          df_test,
          data_dir,
          output_file,
          extension,
          separator,
          index=False,
      )
  
      # Create crosstabs for any categorical features
  
      if model_type == ModelType.classification:
          create_crosstabs(model)
  
      # Create initial features
  
      X_all = create_features(model, X_all, X_train, X_test, y_train)
      X_train, X_test = np.array_split(X_all, [split_point])
      model = save_features(model, X_train, X_test)
  
      # Generate interactions
  
      X_all = create_interactions(model, X_all)
      X_train, X_test = np.array_split(X_all, [split_point])
      model = save_features(model, X_train, X_test)
  
      # Remove low-variance features
  
      X_all = remove_lv_features(model, X_all)
      X_train, X_test = np.array_split(X_all, [split_point])
      model = save_features(model, X_train, X_test)
  
      # Shuffle the data [if specified]
      model = shuffle_data(model)
  
      # Oversampling or Undersampling [if specified]
  
      if model_type == ModelType.classification:
          if sampling:
              model = sample_data(model)
          else:
              logger.info("Skipping Sampling")
  
      # Perform feature selection, independent of algorithm
  
      if feature_selection:
          model = select_features(model)
  
      # Get the available classifiers and regressors
  
      logger.info("Getting All Estimators")
      estimators = get_estimators(model)
  
      # Get the available scorers
  
      if scorer not in scorers:
          raise KeyError("Scorer function %s not found" % scorer)
  
      # Model Selection
  
      logger.info("Selecting Models")
  
      for algo in model.algolist:
          logger.info("Algorithm: %s", algo)
          # select estimator
          try:
              estimator = estimators[algo]
              est = estimator.estimator
          except KeyError:
              logger.info("Algorithm %s not found", algo)
          # initial fit
          model = first_fit(model, algo, est)
          # copy feature name master into feature names per algorithm
          model.fnames_algo[algo] = model.feature_names
          # recursive feature elimination
          if rfe:
              has_coef = hasattr(est, "coef_")
              has_fimp = hasattr(est, "feature_importances_")
              if has_coef or has_fimp:
                  model = rfecv_search(model, algo)
              else:
                  logger.info("No RFE Available for %s", algo)
          # grid search
          if grid_search:
              model = hyper_grid_search(model, estimator)
          # predictions
          model = make_predictions(model, algo, calibration)
  
      # Create a blended estimator
  
      if len(model.algolist) > 1:
          model = predict_blend(model)
  
      # Generate metrics
  
      model = generate_metrics(model, Partition.train)
      model = generate_metrics(model, Partition.test)
  
      # Store the best estimator
      model = predict_best(model)
  
      # Generate plots
  
      generate_plots(model, Partition.train)
      if model.test_labels:
          generate_plots(model, Partition.test)
  
      # Save best features and predictions
      save_model(model, "BEST", Partition.test)
  
      # Return the model
      return model
  
imports: import warnings
  from alphapy.data import get_data
  from alphapy.data import sample_data
  from alphapy.data import shuffle_data
  from alphapy.estimators import get_estimators
  from alphapy.estimators import scorers
  from alphapy.features import apply_transforms
  from alphapy.features import create_crosstabs
  from alphapy.features import create_features
  from alphapy.features import create_interactions
  from alphapy.features import drop_features
  from alphapy.features import remove_lv_features
  from alphapy.features import save_features
  from alphapy.features import select_features
  from alphapy.frame import write_frame
  from alphapy.globals import CSEP, PSEP, SSEP, USEP
  from alphapy.globals import ModelType
  from alphapy.globals import Partition, datasets
  from alphapy.globals import WILDCARD
  from alphapy.model import first_fit
  from alphapy.model import generate_metrics
  from alphapy.model import get_model_config
  from alphapy.model import load_feature_map
  from alphapy.model import load_predictor
  from alphapy.model import make_predictions
  from alphapy.model import Model
  from alphapy.model import predict_best
  from alphapy.model import predict_blend
  from alphapy.model import save_model
  from alphapy.model import save_predictions
  from alphapy.optimize import hyper_grid_search
  from alphapy.optimize import rfecv_search
  from alphapy.plots import generate_plots
  from alphapy.utilities import get_datestamp
  import argparse
  from datetime import datetime
  import logging
  import numpy
  import os
  import pandas
  from sklearn.model_selection import train_test_split
  import sys
label_desc: Use image node_id 1 for calling a global function with example usage: training_pipeline(model) and returns: model