Пример #1
0
def main():
  args = parse_args()
  jobpath, taskids = parse_jobpath_and_taskids(args)

  for taskid in taskids:
    taskpath = os.path.join(jobpath, taskid)
    if args.lap is not None:
      prefix, bLap = ModelReader.getPrefixForLapQuery(taskpath, args.lap)      
      if bLap != args.lap:
        print 'Using saved lap: ', bLap
    else:
      prefix = 'Best' # default

    hmodel = ModelReader.load_model(taskpath, prefix)
    plotModelInNewFigure(jobpath, hmodel, args)
    if args.savefilename is not None:
      pylab.show(block=False)
      pylab.savefig(args.savefilename % (taskid))
  
  if args.savefilename is None:
    pylab.show(block=True)
Пример #2
0
def init_global_params_from_bnpy_format(hmodel, Data, initname, prefix):
    storedModel = ModelReader.load_model(initname, prefix)
    # TODO check if dimension matches
    aTypesMatch = type(storedModel.allocModel) == type(hmodel.allocModel)
    oTypesMatch = type(storedModel.obsModel) == type(hmodel.obsModel)
    inferTypesMatch = storedModel.inferType == hmodel.inferType

    if aTypesMatch and oTypesMatch and inferTypesMatch:
        hmodel.set_global_params(hmodel=storedModel)
    else:
        LP = storedModel.calc_local_params(Data)
        SS = hmodel.get_global_suff_stats(Data, LP)
        hmodel.update_global_params(SS)
Пример #3
0
def init_global_params_from_bnpy_format(hmodel, Data, initname, prefix):
  storedModel = ModelReader.load_model(initname, prefix)
  # TODO check if dimension matches
  aTypesMatch = type(storedModel.allocModel) == type(hmodel.allocModel)
  oTypesMatch = type(storedModel.obsModel) == type(hmodel.obsModel)
  inferTypesMatch = storedModel.inferType == hmodel.inferType

  if aTypesMatch and oTypesMatch and inferTypesMatch:
    hmodel.set_global_params(hmodel=storedModel)
  else:
    LP = storedModel.calc_local_params(Data)
    SS = hmodel.get_global_suff_stats(Data, LP)
    hmodel.update_global_params(SS)
Пример #4
0
def init_global_params_from_bnpy_format(hmodel,
                                        Data,
                                        initname,
                                        initLapFrac=-1,
                                        prefix='Best',
                                        **kwargs):
    """ Initialize global parameters for hmodel from bnpy disk format.

    Post Condition
    -------
    hmodel has valid global parameters.
    '''
    """
    if initLapFrac > -1:
        storedModel, lap = ModelReader.load_model_at_lap(initname, initLapFrac)
    else:
        storedModel = ModelReader.load_model_at_prefix(initname, prefix)
    try:
        hmodel.set_global_params(hmodel=storedModel,
                                 obsModel=storedModel.obsModel)
    except Exception:
        LP = storedModel.calc_local_params(Data)
        SS = hmodel.get_global_suff_stats(Data, LP)
        hmodel.update_global_params(SS)
Пример #5
0
  def test_save_then_load_same_model(self, prefix='Test'):
    ''' Verify that we can save model H to disk, load it back in as G, and 
        get same results from calculations on H,G with same inputs
    '''
    ModelWriter.save_model(self.hmodel, '/tmp/', prefix)
    gmodel = ModelReader.load_model('/tmp/', prefix)
    assert type(gmodel.allocModel) == type(self.hmodel.allocModel)
    assert gmodel.allocModel.K == self.hmodel.allocModel.K
    K = gmodel.allocModel.K
    for k in range(K):
      gSig = gmodel.obsModel.comp[k].ECovMat()
      hSig = self.hmodel.obsModel.comp[k].ECovMat()
      assert np.allclose(gSig, hSig)

    # Make sure we get same responsibilities before and after
    hLP = self.hmodel.calc_local_params(self.Data)
    gLP = gmodel.calc_local_params(self.Data)
    assert np.allclose(hLP['resp'], gLP['resp'])