Пример #1
0
 def __init__(self, data, k):
     if(DBG): tl.comment("KF (Kalman Filter)")
     (self.n,self.d)=np.shape(data)
     # data (observations)
     self.data=data
     self.k=k
     self.initParams()
Пример #2
0
def nl_fit(nlds, ftype, wtype, dps):
    nlds_org = tl.dcopy(nlds)
    #--------------------------------#
    if (ftype != 'si' and ftype != 'A2' and ftype != 'A01' and ftype != 'B01'):
        tl.warning("fit.py: usage: [si/M2/A01/B01]")
    #--------------------------------#
    if (ftype == 'si'):  # if si, i.e., arg{s(0)=si}
        global MAXFEV
        MAXFEV = MAXFEVi
        global XTL
        XTL = XTLi
        global FTL
        FTL = FTLi
    #--------------------------------#
    if (DBG): tl.comment("=== start LMfit ===")
    if (DBG): tl.comment("(%s,%s,%d)" % (ftype, wtype, dps))
    if (DBG): tl.msg("rmse: %f" % _distfunc_rmse(nlds))
    if (DBG): tl.comment("start fitting")
    #--------------------------------#
    if (DBG): tic = time.clock()
    nlds = _nl_fit(nlds, ftype, wtype, dps)
    if (DBG):
        toc = time.clock()
        fittime = toc - tic
    #--------------------------------#
    if (_distfunc_rmse(nlds_org) < _distfunc_rmse(nlds)):
        nlds = nlds_org  #notfin
    if (DBG): tl.comment("end fitting")
    if (DBG): tl.msg("time: %f" % fittime)
    if (DBG): tl.msg("rmse: %f" % _distfunc_rmse(nlds))
    if (DBG): tl.comment("=== end LMfit ===")
    #--------------------------------#
    return nlds
Пример #3
0
def fit(Xe, mdb, ftype, dictMS, wtype='uniform', dps=1):
    #--------------------------------#
    if (DBG): tl.comment("=== start LMfit ===")
    if (DBG): tl.comment("(%s,%s,%d)" % (ftype, wtype, dps))
    if (DBG): tl.msg("rmse: %f" % _distfunc_rmse(Xe, mdb, dictMS))
    if (DBG): tl.comment("start fitting")
    #--------------------------------#
    if (DBG): tic = time.clock()
    (dictMS) = _fit(Xe, mdb, ftype, dictMS, wtype, dps)
    if (DBG):
        toc = time.clock()
        fittime = toc - tic
    dictMS['errV'] = _distfunc_rmse(Xe, mdb, dictMS)
    #--------------------------------#
    if (DBG): tl.comment("end fitting")
    if (DBG): tl.msg("time: %f" % fittime)
    if (DBG): tl.comment("=== end LMfit ===")
    #--------------------------------#
    return dictMS
Пример #4
0
def _OrbitMap(Xorg,
              lstep,
              wd_level,
              mdb,
              outdir,
              save_fig=True,
              save_full=True):
    #--------------------------------#
    (n, d) = np.shape(Xorg)
    Snaps = init_Snaps(Xorg, lstep, wd_level)  # SnapShots
    CGs = []  # for CGraph_snap_shots
    n = Snaps['n']
    d = Snaps['d']
    pstep = Snaps['pstep']
    PE_vc = _initP()  # current best
    PE_vp = _initP()  # previous pattern
    PE_vb = _initP()  # previous-previous pattern
    # init time-ticks
    tc = pstep
    PE_vc['tm_st'] = 0
    PE_vc['tm_ed'] = tc
    if (COMMENT):
        tl.msg(
            "_OrbitMap X(%d,%d), lstep=%d, pstep=%d, wd_level=%d, c=%d, rho=%f, dps=%d"
            % (n, d, lstep, pstep, wd_level, mdb.get_c(), mdb.rho_RE, DPS))
    #--------------------------------#
    while (True):
        #===================#
        tic = tl.time.clock()
        #===================#
        if (tc >= n): break
        if (np.isnan(Xorg[PE_vc['tm_st']:PE_vc['tm_ed'], :]).sum() > 0):
            PE_vc['tm_st'] = tc
            tc += pstep
            continue  # if, nan-value, then ignore
        if (DBG0): tl.comment("tc:%d" % (tc))
        mdb = mdb.update_Xminmax(Xorg[(tc - pstep):tc, :])  # update Xmin, Xmax
        #--------------------------------#
        # 1. O-estimator
        (PE_vb, PE_vp, PE_vc, mdb,
         Snaps) = _O_estimator(tc, Xorg, PE_vb, PE_vp, PE_vc, mdb, Snaps)
        tic2 = tl.time.clock()
        if (DBG1): tl.msg("time(estimate):%f" % (tl.time.clock() - tic))
        #--------------------------------#
        if (tc <= lstep * FSTART_R or tc + lstep >= n):
            tc += pstep
            continue
        #--------------------------------#
        # 2. O-generator
        (Snaps) = _O_generator(tc, Xorg, PE_vb, PE_vp, PE_vc, mdb, Snaps)
        #===================#
        if (DBG1): tl.msg("time(forecast):%f" % (tl.time.clock() - tic2))
        toc = tl.time.clock()
        fittime = toc - tic
        if (DBG1): tl.msg("time:%f" % (fittime))
        #if(save_full and TSAVE and outdir != ''): tl.save_mat(Snaps, "%sSnaps"%(outdir))
        Snaps['T_full'][tc] = fittime
        CGs.append(mdb.create_CGraph_rcds())
        # continue
        tc += pstep
        #===================#
    #--------------------------------#
    # final (SAVE)
    #--------------------------------#
    if (mdb.CC is tl.NO): Snaps['Ve_full'] = Snaps['Xorg']
    Snaps = compute_Snaps_Errs(Snaps)
    want_refinement = tl.NO
    (mdb, Snaps) = mdb.update_apoptosis(
        Snaps, 0, 0, want_refinement
    )  # if there's any un-used regime, then delete (but, no-refinement)
    # save results
    if (save_full and outdir != ''):
        tl.save_mat(Snaps, "%sSnaps" % (outdir))
        tl.save_obj(mdb, "%sMDB" % (outdir))
        tl.save_obj(CGs, "%sCGs" % (outdir))
    if (save_fig and outdir != ''):
        om_viz.saveResults_txt(Snaps, mdb, outdir)
        om_viz.plotResultsE(Snaps, mdb, outdir)
        om_viz.plotResultsF(Snaps, mdb, outdir)
        om_viz.plotCG(mdb, outdir)
    #--------------------------------#
    # return mdb, Snaps
    #--------------------------------#
    return (mdb, Snaps)
Пример #5
0
    else: my_tst = 0
    #--- check n (duration) ---#
    if (args.n != None): my_n = args.n
    else: my_n = tl.INF
    #--- check modelfn ---#
    if (args.mdir != None):
        mdir = args.mdir
    else:
        mdir = ''
    #--- check multi-scale/process ---#
    if (args.mscale != None):
        mscale = args.mscale
    else:
        mscale = 1  # single-scale
    #--- print args ---#
    tl.comment('main_om.py - args')
    tl.msg("-------------------------")
    tl.msg("input-seqfn: [%s]" % iseqfn)
    tl.msg("ls-step-ahead: [%d]" % lstep)
    tl.msg("multi-scale: [%d]" % mscale)
    tl.msg("outdir: [%s]" % outdir)
    tl.msg("model dir (scan): [%s]" % mdir)
    tl.msg("t_st (opt): [%s]" % my_tst)
    tl.msg("duration (opt): [%s]" % my_n)
    tl.msg("-------------------------")
    try:
        tl.mkdir("%s" % outdir)
    except:
        tl.error("cannot find: %s" % outdir)

    #------------------------------#
Пример #6
0
 def printParams(self, fn=''):
     tl.comment("print/save params [%s]"%(fn))
     _printParams(self, fn)
     tl.save_obj(self, fn)
Пример #7
0
def _example1():
    # create observations
    n=300
    d=1
    x = np.linspace(0, 3 * np.pi, n)
    data=np.zeros((n,d))
    observations = 20 * (np.sin(x) + 0.005 * np.random.random(n))
    data[:,0]=observations
    return (data) 
 
#----------------------------------#
#     main 
#----------------------------------#
if __name__ == "__main__":
    from kf import KF
    tl.comment("kf.py -- example ")
    tl.msg("create synthetic sequence (1-dim)")
    (data)=_example1()
    fn='../output/tmp/kf_sample'
    
   
    k=2 #4 
    tl.msg("k=%d: # of latent variables"%(k)) 
    tl.msg("init params")
    mykf=KF(data,k)
    tl.msg("em algorithm -- START")
    tic = tl.time.clock()
    mykf.em(ITER=2, iter_each=10)
    toc = tl.time.clock(); fittime= toc-tic;
    tl.msg("em algorithm -- END (%f sec.)"%(fittime))
    tl.msg("plot/save models")
Пример #8
0
    if (args.wd != None):
        wd = args.wd
    else:
        wd = 1
    if (args.swd != None):
        swd = args.swd
    else:
        swd = 1
    #--- check output ---#
    if (args.outfn != None):
        outfn = args.outfn
    else:
        parser.print_help()
        tl.error("parser")
    #--- print args ---#
    tl.comment('main_norm.py - args')
    lstep = swd
    pstep = int(np.ceil(lstep * orbitmap.LP_R))
    print("-------------------------")
    print("seqfn:  [%s]" % seqfn)
    print("sampling window size: [%d]" % wd)
    print("smooth window size: [%d]" % swd)
    print("lstep: [%d], pstep: [%d]" % (lstep, pstep))
    print("outfn:  [%s]" % (outfn))
    print("-------------------------")

    #------------------------------#
    # LOAD DATA
    #------------------------------#
    data = tl.loadsq(seqfn).T
    (n, d) = np.shape(data)
Пример #9
0
def _example2():
    inputfn = "_dat/mocap/20_01.amc.4d"
    inputfn = "../../DATA/mocap_modf/seq/X20_21.dat"
    data = tl.loadsq(inputfn)
    data = data.T
    data = data[320:400, :]
    return data


#---------------#
#     main      #
#---------------#
if __name__ == "__main__":
    from nlds import NLDS

    tl.comment("load data")
    data = _example1()
    data = _example2()
    (n, d) = np.shape(data)
    outdir = "../output/tmp/nlds_out"
    wtype = 'uniform'  #linear

    tl.comment("create NLDS")
    nlds = NLDS(data, outdir)
    tl.comment("start LMfit")
    tic = time.clock()
    #nlds=nlds.fit_lin(wtype)
    nlds = nlds.fit(wtype, TH=1)
    toc = time.clock()
    fittime = toc - tic
    tl.comment("END LMfit")
Пример #10
0
                        "--animation",
                        type=int,
                        help="animation (1:yes, 0:no)")
    args = parser.parse_args()
    if (len(sys.argv) < 2):
        parser.print_help()
        tl.error("parser")
    #--- check output dir ---#
    if (args.outdir != None):
        outdir = args.outdir
    else:
        parser.print_help()
        tl.error("parser")
    #--- check animation ---#
    if (args.animation != None):
        animation = args.animation
    else:
        animation = tl.NO
    #--- print args ---#
    tl.comment('main_om_viz.py - args')
    print("-------------------------")
    print("outdir:[%s]" % outdir)
    print("plot animation (1:yes, 0:no):[%d]" % animation)
    print("-------------------------")

    #------------------------------#
    # Start VIZ
    #------------------------------#
    om_viz.run_viz(outdir, animation)
    sys.exit(0)