code: def get_optim_f_stop(
      maxiter, maxtime, dftol_stop, freltol_stop, minibatch=True
  ):
      """
      Check stopping conditions.
      """
  
      discount_factor = 1.0 / 3
  
      total_t = [0.0]
      df_store = [np.nan]
      it_store = [0]
      relchange_store = [np.nan]
      f_ma = EMA(discount_factor=discount_factor)
      df_ma = EMA(discount_factor=discount_factor)
  
      def f_stop(f0, v0, it, t):
          flag_stop = False
  
          total_t[-1] += t
          g = f0.x.grad.clone().cpu().detach()
          df = g.abs().max().numpy().squeeze()
          v = v0.clone().cpu().detach()
          f = v.numpy().squeeze()
  
          if it >= maxiter:
              flag_stop = True
  
          elif total_t[-1] >= maxtime:
              flag_stop = True
  
          f_ma.update(f)
          df_ma.update(df)
          rel_change = f_ma.relchange()
  
          if ((not minibatch) and (df < dftol_stop)) or (
              minibatch and (df_ma() < dftol_stop)
          ):
              flag_stop = True
  
          if rel_change < freltol_stop:
              flag_stop = True
  
          if not minibatch:
              df_store[-1] = df
          else:
              df_store[-1] = df_ma()
          relchange_store[-1] = rel_change
          it_store[-1] = it
  
          return flag_stop
  
      return f_stop, {
          "t": total_t,
          "it": it_store,
          "df": df_store,
          "relchange": relchange_store,
      }
  
file_path: nni/nni/algorithms/feature_engineering/gradient_selector/fgtrain.py
imports: import time
  import numpy
  import torch
  from sklearn.feature_selection import SelectKBest, f_classif, mutual_info_classif, f_regression, mutual_info_regression
  from .None import constants
  from .None import syssettings
  from .learnability import Solver
  from .utils import EMA
label_desc: Use image node_id 1 for calling a global function with example usage: get_optim_f_stop(maxiter, maxtime, dftol_stop, freltol_stop, minibatch) and returns: f_stop,  dict, flag_stop