def clean_up_expanded_suff_stats(targetData, curModel, targetSS,
                                  randstate=np.random, **kwargs):
  ''' Create expanded model combining original and brand-new comps
        and try to identify brand-new comps that are redundant copies of   
        originals and can be removed 
  '''
  import MergeMove
  Korig = curModel.allocModel.K
  origLP = curModel.calc_local_params(targetData)
  expandSS = curModel.get_global_suff_stats(targetData, origLP) 
  expandSS.insertComps(targetSS)
  expandModel = curModel.copy()
  expandModel.update_global_params(expandSS)

  expandLP = expandModel.calc_local_params(targetData)
  expandSS = expandModel.get_global_suff_stats(targetData, expandLP,
                  doPrecompEntropy=True, doPrecompMergeEntropy=True)
  Kexpand = expandSS.K

  mPairIDs = MergeMove.preselect_all_merge_candidates(
              expandModel, expandSS, randstate=np.random,
              preselectroutine=kwargs['cleanuppreselectroutine'], 
              mergePerLap=kwargs['cleanupNumMergeTrials']*(Kexpand-Korig),
              compIDs=range(Korig, Kexpand))

  mPairIDsOrig = [x for x in mPairIDs]

  xModel, xSS, xEv, MTracker = MergeMove.run_many_merge_moves(
                               expandModel, targetData, expandSS,
                               nMergeTrials=expandSS.K**2, 
                               mPairIDs=mPairIDs,
                               randstate=randstate, **kwargs)

  if kwargs['doVizBirth']:
    viz_birth_proposal_2D(expandModel, xModel, None, None,
                          title1='expanded model',
                          title2='after merge')

  for x in MTracker.acceptedOrigIDs:
    assert x in mPairIDsOrig
  
  if kwargs['cleanupModifyOrigComps']:
    targetSS = xSS
    targetSS.setELBOFieldsToZero()
    targetSS.setMergeFieldsToZero()
  else:
    # Remove from targetSS all the comps whose merges were accepted
    kBList = [kB for kA,kB in MTracker.acceptedOrigIDs]

    if len(kBList) == targetSS.K:
      msg = 'BIRTH terminated. all new comps redundant with originals.'
      raise BirthProposalError(msg)
    for kB in reversed(sorted(kBList)):
      ktarget = kB - Korig
      if ktarget >= 0:
        targetSS.removeComp(ktarget)
  return targetSS
dataset/ETHPy150Open daeilkim/refinery/refinery/bnpy/bnpy-dev/bnpy/learnalg/BirthMove.py/clean_up_expanded_suff_stats/original