parameters: (details, composite, data1=None, data2=None)
return_statement: return res
docstring: balances the composite using the parameters provided in details
  
     **Arguments**
  
       - details a _CompositeRun.RunDetails_ object
  
       - composite: the composite model to be balanced
  
       - data1: (optional) if provided, this should be the
         data set used to construct the original models
  
       - data2: (optional) if provided, this should be the
         data set used to construct the new individual models
docstring_summary: balances the composite using the parameters provided in details
docstring_tokens: balances the composite using the parameters provided in details
function: def BalanceComposite(details, composite, data1=None, data2=None):
    """ balances the composite using the parameters provided in details
  
     **Arguments**
  
       - details a _CompositeRun.RunDetails_ object
  
       - composite: the composite model to be balanced
  
       - data1: (optional) if provided, this should be the
         data set used to construct the original models
  
       - data2: (optional) if provided, this should be the
         data set used to construct the new individual models
  
    """
    if not details.balCnt or details.balCnt > len(composite):
      return composite
    message("Balancing Composite")
  
    #
    # start by getting data set 1: which is the data set used to build the
    #  original models
    #
    if data1 is None:
      message("\tReading First Data Set")
      fName = details.balTable.strip()
      tmp = details.tableName
      details.tableName = fName
      dbName = details.dbName
      details.dbName = details.balDb
      data1 = details.GetDataSet()
      details.tableName = tmp
      details.dbName = dbName
    if data1 is None:
      return composite
    details.splitFrac = composite._splitFrac
    details.randomSeed = composite._randomSeed
    DataUtils.InitRandomNumbers(details.randomSeed)
    if details.shuffleActivities == 1:
      DataUtils.RandomizeActivities(data1, shuffle=1, runDetails=details)
    elif details.randomActivities == 1:
      DataUtils.RandomizeActivities(data1, shuffle=0, runDetails=details)
    namedExamples = data1.GetNamedData()
    if details.balDoHoldout or details.balDoTrain:
      trainIdx, testIdx = SplitData.SplitIndices(len(namedExamples), details.splitFrac, silent=1)
      trainExamples = [namedExamples[x] for x in trainIdx]
      testExamples = [namedExamples[x] for x in testIdx]
      if details.filterFrac != 0.0:
        trainIdx, temp = DataUtils.FilterData(trainExamples, details.filterVal, details.filterFrac,
                                              -1, indicesOnly=1)
        tmp = [trainExamples[x] for x in trainIdx]
        testExamples += [trainExamples[x] for x in temp]
        trainExamples = tmp
      if details.balDoHoldout:
        testExamples, trainExamples = trainExamples, testExamples
    else:
      trainExamples = namedExamples
    dataSet1 = trainExamples
    cols1 = [x.upper() for x in data1.GetVarNames()]
    data1 = None
  
    #
    # now grab data set 2: the data used to build the new individual models
    #
    if data2 is None:
      message("\tReading Second Data Set")
      data2 = details.GetDataSet()
    if data2 is None:
      return composite
    details.splitFrac = composite._splitFrac
    details.randomSeed = composite._randomSeed
    DataUtils.InitRandomNumbers(details.randomSeed)
    if details.shuffleActivities == 1:
      DataUtils.RandomizeActivities(data2, shuffle=1, runDetails=details)
    elif details.randomActivities == 1:
      DataUtils.RandomizeActivities(data2, shuffle=0, runDetails=details)
    dataSet2 = data2.GetNamedData()
    cols2 = [x.upper() for x in data2.GetVarNames()]
    data2 = None
  
    # and balance it:
    res = []
    weights = details.balWeight
    if not isinstance(weights, (tuple, list)):
      weights = (weights, )
    for weight in weights:
      message("\tBalancing with Weight: %.4f" % (weight))
      res.append(
        AdjustComposite.BalanceComposite(composite, dataSet1, dataSet2, weight, details.balCnt,
                                         names1=cols1, names2=cols2))
    return res
function_tokens: def BalanceComposite ( details , composite , data1 = None , data2 = None ) : if not details . balCnt or details . balCnt > len ( composite ) : return composite message ( "Balancing Composite" ) # # start by getting data set 1: which is the data set used to build the #  original models # if data1 is None : message ( "\tReading First Data Set" ) fName = details . balTable . strip ( ) tmp = details . tableName details . tableName = fName dbName = details . dbName details . dbName = details . balDb data1 = details . GetDataSet ( ) details . tableName = tmp details . dbName = dbName if data1 is None : return composite details . splitFrac = composite . _splitFrac details . randomSeed = composite . _randomSeed DataUtils . InitRandomNumbers ( details . randomSeed ) if details . shuffleActivities == 1 : DataUtils . RandomizeActivities ( data1 , shuffle = 1 , runDetails = details ) elif details . randomActivities == 1 : DataUtils . RandomizeActivities ( data1 , shuffle = 0 , runDetails = details ) namedExamples = data1 . GetNamedData ( ) if details . balDoHoldout or details . balDoTrain : trainIdx , testIdx = SplitData . SplitIndices ( len ( namedExamples ) , details . splitFrac , silent = 1 ) trainExamples = [ namedExamples [ x ] for x in trainIdx ] testExamples = [ namedExamples [ x ] for x in testIdx ] if details . filterFrac != 0.0 : trainIdx , temp = DataUtils . FilterData ( trainExamples , details . filterVal , details . filterFrac , - 1 , indicesOnly = 1 ) tmp = [ trainExamples [ x ] for x in trainIdx ] testExamples += [ trainExamples [ x ] for x in temp ] trainExamples = tmp if details . balDoHoldout : testExamples , trainExamples = trainExamples , testExamples else : trainExamples = namedExamples dataSet1 = trainExamples cols1 = [ x . upper ( ) for x in data1 . GetVarNames ( ) ] data1 = None # # now grab data set 2: the data used to build the new individual models # if data2 is None : message ( "\tReading Second Data Set" ) data2 = details . GetDataSet ( ) if data2 is None : return composite details . splitFrac = composite . _splitFrac details . randomSeed = composite . _randomSeed DataUtils . InitRandomNumbers ( details . randomSeed ) if details . shuffleActivities == 1 : DataUtils . RandomizeActivities ( data2 , shuffle = 1 , runDetails = details ) elif details . randomActivities == 1 : DataUtils . RandomizeActivities ( data2 , shuffle = 0 , runDetails = details ) dataSet2 = data2 . GetNamedData ( ) cols2 = [ x . upper ( ) for x in data2 . GetVarNames ( ) ] data2 = None # and balance it: res = [ ] weights = details . balWeight if not isinstance ( weights , ( tuple , list ) ) : weights = ( weights , ) for weight in weights : message ( "\tBalancing with Weight: %.4f" % ( weight ) ) res . append ( AdjustComposite . BalanceComposite ( composite , dataSet1 , dataSet2 , weight , details . balCnt , names1 = cols1 , names2 = cols2 ) ) return res