Skip to content

Commit

Permalink
Initial import, switched from svn to git.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwf-zz committed Nov 4, 2009
0 parents commit 1ecfed5
Show file tree
Hide file tree
Showing 64 changed files with 126,929 additions and 0 deletions.
414 changes: 414 additions & 0 deletions .cproject

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions .project
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ManifoldExperiment</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
<dictionary>
<key>?name?</key>
<value></value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.append_environment</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.autoBuildTarget</key>
<value>all</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildArguments</key>
<value></value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildCommand</key>
<value>make</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildLocation</key>
<value>${workspace_loc:/ManifoldExperiment}</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.cleanBuildTarget</key>
<value>clean</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.contents</key>
<value>org.eclipse.cdt.make.core.activeConfigSettings</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableAutoBuild</key>
<value>false</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableCleanBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableFullBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.fullBuildTarget</key>
<value>all</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.stopOnError</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
<value>false</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
169 changes: 169 additions & 0 deletions BuildTree.cpp
@@ -0,0 +1,169 @@
//============================================================================
// Name : ManifoldExperiment.cpp
// Author :
// Version :
// Copyright : BSD
// Description : Constructs a time-delay embedding of the data, equipped with
// a model of the dynamics using an approximation of the
// one step transition matrix based on averaging the sample
// dynamics of the k-nearest neighbours.
//============================================================================
/*
* main.cpp
*
* Created on: Jun 28, 2009
* Author: jordan
*/

#include <stdlib.h>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <fstream>
#include <ANN/ANN.h>
#include <Tisean/tsa.h>

#include "Utils.h"
#include "BuildTree.h"
#include "TDEModel.h"

using namespace std;

using namespace std;

#define WID_STR "Build a model"

void show_options(char *progname) {
what_i_do(progname, (char*)WID_STR);
fprintf(stderr,"\nUsage: %s [options]\n",progname);
fprintf(stderr,"Options:\n");
fprintf(stderr,"Everything not being a valid option will be interpreted as a"
" possible datafile.\nIf no datafile is given stdin is read."
" Just - also means stdin\n");
fprintf(stderr,"\t-l # of data [default: whole file]\n");
fprintf(stderr,"\t-x # of rows to ignore [default: 0]\n");
fprintf(stderr,"\t-M num. of columns to read [default: 1]\n");
fprintf(stderr,"\t-c columns to read [default: 1,...,M]\n");
fprintf(stderr,"\t-m dimension [default: 2]\n");
fprintf(stderr,"\t-p # dimension to reduce to using PCA [default: same as m, ie. no reduction]\n");
fprintf(stderr,"\t-d delay [default: 1]\n");
fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
"0='only panic messages'\n\t\t"
"1='+ input/output messages'\n");
fprintf(stderr,"\t-o output file [default: 'datafile'.del, "
"without -o: stdout]\n");
fprintf(stderr,"\t-h show these options\n");
exit(0);
}
void scan_options(int n,char **str, Settings *settings) {
char *out;
if ((out=check_option(str,n,'l','u')) != NULL)
sscanf(out,"%lu",&settings->length);
if ((out=check_option(str,n,'x','u')) != NULL)
sscanf(out,"%lu",&settings->exclude);
if ((out=check_option(str,n,'c','s')) != NULL)
settings->column=out;
if ((out=check_option(str,n,'M','u')) != NULL) {
sscanf(out,"%u",&settings->indim);
settings->dimset=1;
}
if ((out=check_option(str,n,'m','u')) != NULL) {
sscanf(out,"%u",&settings->embdim);
settings->embset=1;
}
if ((out=check_option(str,n,'p','u')) != NULL) {
sscanf(out,"%u",&settings->pcaembdim);
settings->pcaembset=1;
}
if ((out=check_option(str,n,'d','u')) != NULL) {
sscanf(out,"%u",&settings->delay);
settings->delayset=1;
}
if ((out=check_option(str,n,'V','u')) != NULL)
sscanf(out,"%u",&settings->verbosity);
if ((out=check_option(str,n,'o','o')) != NULL) {
settings->stdo=0;
if (strlen(out) > 0)
settings->outfile=out;
}
}

int main (int argc, char *argv[]) {
srand((unsigned)time(NULL));
TDEModel *tdeModel;
Settings settings = { ULONG_MAX, 0, 0xff, 1, 1, 2, 2, NULL, NULL, NULL, 0, 0, 0, 0, 1 };
char stin=0;
uint i, j;

if (scan_help(argc,argv))
show_options(argv[0]);
scan_options(argc,argv, &settings);

settings.infile=search_datafile(argc,argv,NULL,settings.verbosity);
if (settings.infile == NULL)
stin=1;
if (settings.outfile == NULL) {
if (!stin) {
check_alloc(settings.outfile=(char*)calloc(strlen(settings.infile)+5,sizeof(char)));
strcpy(settings.outfile,settings.infile);
strcat(settings.outfile,".dmp");
} else {
check_alloc(settings.outfile=(char*)calloc(10,sizeof(char)));
strcpy(settings.outfile,"stdin.dmp");
}
}
if (!settings.stdo) {
test_outfile(settings.outfile);
}

if (settings.delay < 1) {
fprintf(stderr,"Delay has to be larger than 0. Exiting!\n");
exit(DELAY_SMALL_ZERO);
}

if (!settings.pcaembset) {
settings.pcaembdim = settings.embdim;
}

tdeModel = new TDEModel(&settings);
tdeModel->DumpTree(settings.outfile);

ANNpoint ap = tdeModel->getDataPoint(0);
uint N = 1000;
ANNpointArray pts = annAllocPts(N, settings.embdim);;
tdeModel->simulateTrajectory(ap, pts, settings.embdim, N);

/*
uint k = 8;
ANNidxArray nn_idx;
ANNdistArray dists;
nn_idx = new ANNidx[k];
dists = new ANNdist[k];
tdeModel->getKNN(ap, k, nn_idx, dists);
delete [] nn_idx;
delete [] dists;
*/
// DUMP Manifold and Trajectory
ofstream fout("/tmp/trajectory.csv");
for (i = 0; i < N; i++) {
fout << pts[i][0];
for (j = 1; j < settings.embdim; j++) {
fout << "\t";
fout << pts[i][j];
}
fout << endl;
}
fout.close();
annDeallocPt(ap);
delete [] pts;

delete tdeModel;
annClose();
if (settings.column != NULL) free(settings.column);
if (settings.infile != NULL) free(settings.infile);
if (settings.outfile != NULL) free(settings.outfile);
return 0;
}
11 changes: 11 additions & 0 deletions BuildTree.h
@@ -0,0 +1,11 @@
/*
* ManifoldExperiment.h
*
* Created on: 2009-06-29
* Author: jfrank8
*/

#ifndef MANIFOLDEXPERIMENT_H_
#define MANIFOLDEXPERIMENT_H_

#endif /* MANIFOLDEXPERIMENT_H_ */
Binary file added BuildTree.o
Binary file not shown.
116 changes: 116 additions & 0 deletions Classifier.cpp
@@ -0,0 +1,116 @@
/*
* Classifier.cpp
*
* Created on: 2009-06-30
* Author: jfrank8
*/

#include <stdlib.h>
#include <vector>
#include <omp.h>
#include <ANN/ANN.h>

#include "ClassifyTrajectory.h"
#include "Classifier.h"

#define MATCH_STEPS 32
#define NEIGHBOURS 4

using namespace std;

Classifier::Classifier(vector<NamedModel*> models) {
this->models = models;
}

Classifier::~Classifier() {
// TODO Auto-generated destructor stub
}

void Classifier::go(double** data, ulong length) {
// For each set of MATCH_STEPS points, compute the likelihood under each model.
TDEModel* model;
uint M = models.size();
char* model_name;
double *projected_data;
ANNpointArray ap;
ANNidx nn_idx[NEIGHBOURS+1];
ANNdist dists[NEIGHBOURS+1];
CvMat *navg[M], *navg_next[M], *proj_next[M];
CvMat p, *mdists, *nn[M], *nnn[M];
ulong i,j,k,l,a,N,pcaembdim;
double dist, dist_next, *dst, *p1, *p2, *p3, *p4;
mdists = cvCreateMat(length-MATCH_STEPS-1,models.size(),CV_64FC1);
cvZero(mdists);

for (i = 0; i < M; i++) {
pcaembdim = models[i]->model->getPCAEmbDim();
navg[i] = cvCreateMat(1,pcaembdim,CV_64FC1);
navg_next[i] = cvCreateMat(1,pcaembdim,CV_64FC1);
proj_next[i] = cvCreateMat(1,pcaembdim, CV_64FC1);
nn[i] = cvCreateMat(NEIGHBOURS,pcaembdim, CV_64FC1);
nnn[i] = cvCreateMat(NEIGHBOURS,pcaembdim, CV_64FC1);
}

for (i = 0; i < length-MATCH_STEPS-1; i++) { //=MATCH_STEPS) {
// Get the MATCH_STEPS points
// cout << "Step " << i << endl;
for (k = 0; k < M; k++) {
model = models[k]->model;
model_name = models[k]->name;
N = model->getLength();
pcaembdim = model->getPCAEmbDim();
//projected_data = model->projectData(data+i*embdim,MATCH_STEPS+1,embdim);
get_ann_points(ap, data[k]+i*pcaembdim, MATCH_STEPS+1, pcaembdim);

for (j = 0; j < MATCH_STEPS; j++) {
p = cvMat(1,pcaembdim,CV_64FC1, ap[j]);// &projected_data[j*pcaembdim]);
// cout << "Checking point: ";
// print_matrix(&p);

model->getKNN(ap[j], NEIGHBOURS+1, nn_idx, dists);
for (l = 0; l < NEIGHBOURS; l++) {
// Make sure none of the first NEIGHBOURS neighbours is N
if (nn_idx[l] == ANN_NULL_IDX) break;
else if ((ulong)nn_idx[l] == N-1) nn_idx[l] = nn_idx[NEIGHBOURS];
p1 = (double*)(nn[k]->data.ptr+l*nn[k]->step);
p2 = (double*)(nnn[k]->data.ptr+l*nnn[k]->step);
p3 = model->getDataPoint(nn_idx[l]);
p4 = model->getDataPoint(nn_idx[l]+1);
for (a = 0; a < pcaembdim; a++) {
*p1++ = *p3++;
*p2++ = *p4++;
}
}
if (l < NEIGHBOURS) cout << "Warning: Couldn't find enough neighbours." << endl;

cvReduce(nn[k], navg[k], 0, CV_REDUCE_AVG );
dist = -sqrt(annDist(pcaembdim,(double*)p.data.ptr,(double*)navg[k]->data.ptr));

cvReduce(nnn[k], navg_next[k], 0, CV_REDUCE_AVG );
p1 = (double*)navg_next[k]->data.ptr;
p2 = (double*)navg[k]->data.ptr;
dst = (double*)proj_next[k]->data.ptr;
for (l = 0; l < pcaembdim; l++) {
*dst++ = ap[j][l] + (*p1++ - *p2++);
}
p = cvMat(1,pcaembdim,CV_64FC1, ap[j+1]);
dist_next = -sqrt(annDist(pcaembdim,(double*)p.data.ptr,(double*)proj_next[k]->data.ptr));
/*
cout << "Expected next point: "; print_matrix(proj_next);
cout << "Real next point: "; print_matrix(&p);
cout << model_name << ". Dist1: " << dist << ", Dist2: " << dist_next << endl;
*/
cvmSet(mdists,i,k,cvmGet(mdists,i,k)+(dist+dist_next));
}
//cout << model_name << ": " << cvmGet(mdists,i,k) << endl;
}
}
print_matrix(mdists);
for (i = 0; i < M; i++) {
cvReleaseMat(&navg[i]);
cvReleaseMat(&navg_next[i]);
cvReleaseMat(&nn[i]);
cvReleaseMat(&nnn[i]);
}
cvReleaseMat(&mdists);
}

0 comments on commit 1ecfed5

Please sign in to comment.