def test_rbfK_noncentered(): par = RBFParam() par._kCen = False dataFile = os.path.join(TESTBASE, "data/random.txt") data = np.genfromtxt(dataFile, dtype=np.double) rbfK(data, data, par) kernelFile = os.path.join(TESTBASE, "data/random-rbf-nocenter.txt") kernel = np.genfromtxt(kernelFile, dtype=np.double) err = np.linalg.norm(kernel - par._kMat, "fro") np.testing.assert_almost_equal(err, 0, 2)
def test_rbfK_noncentered(): par = RBFParam() par._kCen = False dataFile = os.path.join(TESTBASE, "data/random.txt") data = np.genfromtxt(dataFile, dtype=np.double) rbfK(data, data, par) kernelFile = os.path.join(TESTBASE, "data/random-rbf-nocenter.txt") kernel = np.genfromtxt(kernelFile, dtype=np.double) err = np.linalg.norm(kernel - par._kMat, 'fro') np.testing.assert_almost_equal(err, 0, 2)
def main(argv=None): if argv is None: argv = sys.argv parser = OptionParser(add_help_option=False) parser.add_option("-i", dest="iFile") parser.add_option("-t", dest="iType") parser.add_option("-o", dest="oFile") parser.add_option("-n", dest="nStates", type="int", default=5) parser.add_option("-h", dest="shoHelp", action="store_true", default=False) parser.add_option("-v", dest="verbose", action="store_true", default=False) opt, args = parser.parse_args() if opt.shoHelp: usage() dataMat = None dataSiz = None try: if opt.iType == 'vFile': (dataMat, dataSiz) = dsutil.loadDataFromVideoFile(opt.iFile) elif opt.iType == 'aFile': (dataMat, dataSiz) = dsutil.loadDataFromASCIIFile(opt.iFile) elif opt.iType == 'lFile': (dataMat, dataSiz) = dsutil.loadDataFromIListFile(opt.iFile) else: dsinfo.fail("Unsupported file type : %s" % opt.iType) return -1 # catch pyds exceptions except ErrorDS as e: msg.fail(e) return -1 try: kpcaP = KPCAParam() kpcaP._kPar = RBFParam() kpcaP._kPar._kCen = True kpcaP._kFun = rbfK kdt = NonLinearDS(opt.nStates, kpcaP, opt.verbose) kdt.suboptimalSysID(dataMat) if not opt.oFile is None: if not kdt.check(): dsinfo.fail('cannot write invalid model!') return -1 dsinfo.info('writing model to %s' % opt.oFile) with open(opt.oFile, 'w') as fid: pickle.dump(kdt, fid) except ErrorDS as e: dsinfo.fail(e) return -1
def test_NonLinearDS_check(): """Test NonLinearDS parameter checking. """ kpcaP = KPCAParam() kpcaP._kPar = RBFParam() kpcaP._kPar._kCen = True kpcaP._kFun = rbfK nlds = NonLinearDS(5, kpcaP, False) assert nlds.check() is False
def test_nldsMartinDistance(): """Test Martin distance computation for NLDS's. Config: 5 states, kernel centering """ fileA = os.path.join(TESTBASE, "data/data1.txt") fileB = os.path.join(TESTBASE, "data/data2.txt") # load data files dataA, _ = loadDataFromASCIIFile(fileA) dataB, _ = loadDataFromASCIIFile(fileB) # configure kernels kpcaPA = KPCAParam() kpcaPA._kPar = RBFParam() kpcaPA._kPar._kCen = True kpcaPA._kFun = rbfK kpcaPB = KPCAParam() kpcaPB._kPar = RBFParam() kpcaPB._kPar._kCen = True kpcaPB._kFun = rbfK nldsA = NonLinearDS(5, kpcaPA, False) nldsB = NonLinearDS(5, kpcaPB, False) # estimate NLDS's nldsA.suboptimalSysID(dataA) nldsB.suboptimalSysID(dataB) # compute distances A<->A, A<->B dAA = nldsMartinDistance(nldsA, nldsA, 20) dAB = nldsMartinDistance(nldsA, nldsB, 20) truth = np.genfromtxt(os.path.join(TESTBASE, "data/nldsMartinDistanceData1Data2.txt" )) np.testing.assert_almost_equal(dAA, 0, 2) np.testing.assert_almost_equal(dAB, truth, 2)
def test_kpca(): dataFile = os.path.join(TESTBASE, "data/data1.txt") data, _ = loadDataFromASCIIFile(dataFile) kpcaP = KPCAParam() kpcaP._kPar = RBFParam() kpcaP._kPar._kCen = True kpcaP._kFun = rbfK X = kpca(data, 5, kpcaP) baseKPCACoeffFile = os.path.join(TESTBASE, "data/data1-rbf-kpca-5c-center.txt") baseKPCACoeff = np.genfromtxt(baseKPCACoeffFile, dtype=np.double) # don't care about the sign err = np.linalg.norm(np.abs(baseKPCACoeff) - np.abs(X), 'fro') np.testing.assert_almost_equal(err, 0, 2)
def test_NonLinearDS_suboptimalSysID(): """Test NonLinearDS system identification. """ dataFile = os.path.join(TESTBASE, "data/data1.txt") data, _ = loadDataFromASCIIFile(dataFile) kpcaP = KPCAParam() kpcaP._kPar = RBFParam() kpcaP._kPar._kCen = True kpcaP._kFun = rbfK nlds = NonLinearDS(5, kpcaP, False) nlds.suboptimalSysID(data) baseNLDSFile = os.path.join(TESTBASE, "data/data1-rbf-kdt-5c-center.pkl") baseNLDS = pickle.load(open(baseNLDSFile)) err = NonLinearDS.naiveCompare(baseNLDS, nlds) np.testing.assert_almost_equal(err, 0, 2)
def main(argv=None): if argv is None: argv = sys.argv parser = OptionParser(add_help_option=False) parser.add_option("-s", dest="inFile") parser.add_option("-d", dest="dbFile") parser.add_option("-m", dest="models") parser.add_option("-v", dest="videos") parser.add_option("-c", dest="config") parser.add_option("-o", dest="mdFile") parser.add_option("-h", dest="doUsage", action="store_true", default=False) parser.add_option("-x", dest="verbose", action="store_true", default=False) options, args = parser.parse_args() if options.doUsage: usage() # read config file config = json.load(open(options.config)) # get DS config settings dynType = config["dynType"] shiftMe = config["shiftMe"] numIter = config["numIter"] verbose = options.verbose # I/O configuration inFile = options.inFile dbFile = options.dbFile models = options.models videos = options.videos mdFile = options.mdFile # check if the required options are present if (inFile is None or dbFile is None or models is None or videos is None): dsinfo.warn('Options missing!') usage() inVideo, inVideoSize = dsutil.loadDataFromVideoFile(inFile) if verbose: dsinfo.info("Loaded source video with %d frames!" % inVideo.shape[1]) (db, winSize, nStates, dynType) = loadDB(videos, models, dbFile) if verbose: dsinfo.info("#Templates: %d #States: %d, WinSize: %d, Shift: %d" % (len(db), nStates, winSize, shiftMe)) if dynType.__name__ == "LinearDS": # create online version of LinearDS ds = OnlineLinearDS(nStates, winSize, shiftMe, False, verbose) elif dynType.__name__ == "NonLinearDS": kpcaP = KPCAParam() # select kernel if config["kdtKern"] == "rbf": kpcaP._kPar = RBFParam() kpcaP._kFun = rbfK else: dsinfo.fail("Kernel %s not supported!" % kdtKern) return -1 # configure kernel if config["kCenter"] == 1: kpcaP._kPar._kCen = True else: kpcaP._kPar._kCen = False # create online version of KDT ds = OnlineNonLinearDS(nStates, kpcaP, winSize, shiftMe, verbose) else: dsinfo.fail('System type %s not supported!' % options.dsType) return -1 dList = [] for f in range(inVideo.shape[1]): ds.update(inVideo[:, f]) if ds.check() and ds.hasChanged(): dists = np.zeros((len(db), )) for j, dbentry in enumerate(db): dists[j] = { "LinearDS": dsdist.ldsMartinDistance, "NonLinearDS": dsdist.nldsMartinDistance }[dynType.__name__](ds, dbentry["model"], numIter) dList.append(dists) # write distance matrix if not mdFile is None: np.savetxt(mdFile, np.asmatrix(dList), fmt='%.5f', delimiter=' ')