def create_expanded_suff_stats(Data, curModel, allSS, **kwargs):
  Kfresh = np.minimum(kwargs['Kfresh'], kwargs['Kmax'] - curModel.obsModel.K)
  if Kfresh < 2:
    msg = 'BIRTH: Skipped to avoid exceeding specified limit Kmax=%d'
    raise BirthProposalError(msg % (kwargs['Kmax']))
  kwargs['Kfresh'] = Kfresh

  curLP = curModel.calc_local_params(Data)
  curSS = curModel.get_global_suff_stats(Data, curLP, doPrecompEntropy=True)
  curELBO = curModel.calc_evidence(SS=curSS)

  expandModel = create_expanded_model(Data, curModel, allSS=allSS, 
                                            curLP=curLP, curSS=curSS, **kwargs)
  Korig = curModel.obsModel.K
  Kexpand = expandModel.obsModel.K

  # TODO: should we remember the xLP from previous laps?
  for lap in xrange(kwargs['nFreshLap']):
    xLP = expandModel.calc_local_params(Data)
    xSS = expandModel.get_global_suff_stats(Data, xLP)
    unusedRatio = np.abs(xSS.sumLogPiUnused) / np.abs(allSS.sumLogPiUnused)
    if unusedRatio > 100:
      msg = 'BIRTH failed. proposed suff stats invalid. %.2f' % (unusedRatio)
      raise BirthProposalError(msg)
    expandModel.obsModel.update_global_params(xSS, comps=range(Korig, Kexpand))
    expandModel.allocModel.update_global_params(xSS)

  if kwargs['birthVerbose']:
    Log.info("K= %3d | BRAND NEW FRESH MODEL" % (xSS.K))

  # Remove empty topics (assigned to less than 10 words in the target set
  for k in reversed(xrange(Korig, xSS.K)):
    if xSS.N[k] < 10:
      xSS.removeComp(k)
      del expandModel.obsModel.comp[k]
      expandModel.obsModel.K = xSS.K
  if xSS.K < expandModel.allocModel.K:
    xLP = None
    expandModel.allocModel.update_global_params(xSS)
    if kwargs['birthVerbose']:
      Log.info("K= %3d | after removal of empties" % (xSS.K))

  unusedRatio = np.abs(xSS.sumLogPiUnused) / np.abs(allSS.sumLogPiUnused)
  if unusedRatio > 100:
    msg = 'BIRTH failed. proposed suff stats invalid. %.4f' % (unusedRatio)
    raise BirthProposalError(msg)

  assert xSS.K == expandModel.allocModel.K
  Ebeta = expandModel.allocModel.Ebeta
  if np.allclose(Ebeta[:3], [1./2, 1./4, 1./8]):
    msg = 'BIRTH failed. new topic probabilities invalid.'
    raise BirthProposalError(msg)

  if kwargs['cleanupByDeletion']:
    expandModel, xLP, xSS, xELBO = delete_comps_that_improve_ELBO(
                                                      Data, expandModel,
                                                      Korig=Korig, **kwargs)
    if kwargs['birthVerbose']:
      Log.info( "K= %3d | %.3e | after deletion" % (xSS.K, xELBO))

  if expandModel.obsModel.K == Korig:
    msg = 'BIRTH failed. unable to create useful new comps'
    raise BirthProposalError(msg)


  # Merge within new comps only
  expandModel, xSS, xLP, xELBO = cleanup_mergenewcompsonly(Data, expandModel,
                                                    LP=xLP,  
                                                    Korig=Korig, **kwargs)
  if kwargs['birthVerbose']:
    Log.info("K= %3d | %.3e | after merges new-new" % (xSS.K, xELBO))

  if hasattr(Data, 'nDoc') and xELBO > 0:
    msg = 'BIRTH failed. proposed model ELBO invalid.'
    raise BirthProposalError(msg)

  if expandModel.obsModel.K == Korig:
    msg = 'BIRTH failed. unable to create useful new comps'
    raise BirthProposalError(msg)


  # Double-check that we aren't modifying the params for original topics
  diffInputSSandInputParams = curModel.obsModel.comp[0].lamvec[:4] \
                              - allSS.WordCounts[0, :4]
  isSynced = np.allclose(diffInputSSandInputParams,          
                         curModel.obsModel.obsPrior.lamvec[:4])
  if isSynced:
    assert np.allclose(curModel.obsModel.comp[0].lamvec,
                     expandModel.obsModel.comp[0].lamvec)

  if kwargs['cleanupModifyOrigComps']:
    # Merge between new comps and orig comps
    xSS, xELBO = cleanup_mergenewcompsintoexisting(Data, expandModel, xSS, xLP,
                                                    Korig=Korig, **kwargs)
    if kwargs['birthVerbose']:
      Log.info( "K= %3d | %.3e | after merges old-new" % (xSS.K, xELBO))

  if hasattr(Data, 'nDoc') and xELBO > 0:
    msg = 'BIRTH failed. proposed model ELBO invalid.'
    raise BirthProposalError(msg)

  if xSS.K == Korig:
    msg = 'BIRTH failed. unable to create useful new comps'
    raise BirthProposalError(msg)

  # Verify expanded model preferred over current model
  improveEvBound = xELBO - curELBO
  if improveEvBound <= 0 or improveEvBound < 0.00001 * abs(curELBO):
    msg = "BIRTH terminated. Not better than current model on target data."
    msg += "\n  expanded  | K=%3d | %.7e" % (xSS.K, xELBO)
    msg += "\n  current   | K=%3d | %.7e" % (curSS.K, curELBO)
    raise BirthProposalError(msg)

  xSS.setELBOFieldsToZero()
  xSS.setMergeFieldsToZero()
  return xSS
Create new suff stats that have useful new topics
dataset/ETHPy150Open daeilkim/refinery/refinery/bnpy/bnpy-dev/bnpy/learnalg/BirthMoveTopicModel.py/create_expanded_suff_stats