Skip to content

Commit

Permalink
code used in my phd
Browse files Browse the repository at this point in the history
  • Loading branch information
pthimon committed Feb 10, 2013
0 parents commit 9c982b0
Show file tree
Hide file tree
Showing 13 changed files with 882 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.svn*
49 changes: 49 additions & 0 deletions CMakeLists.txt
@@ -0,0 +1,49 @@
cmake_minimum_required (VERSION 2.6)
project (clustering)

IF(CMAKE_SIZEOF_VOID_P EQUAL 4)
SET(LIB_SUFFIX "")
ELSE(CMAKE_SIZEOF_VOID_P EQUAL 4)
SET(LIB_SUFFIX 64)
ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 4)

SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}")

find_package(Eigen2 REQUIRED)
include_directories(${Eigen2_INCLUDE_DIR})

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
FORCE)
endif(NOT CMAKE_BUILD_TYPE)

if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(LIB_NAME clusteringd)
else(CMAKE_BUILD_TYPE MATCHES "Debug")
set(LIB_NAME clustering)
endif(CMAKE_BUILD_TYPE MATCHES "Debug")

file(GLOB LIB_PUBLIC_HEADERS "${CMAKE_SOURCE_DIR}/*.h")
file(GLOB LIB_SOURCES "${CMAKE_SOURCE_DIR}/*.cpp")

#add_library (${LIB_NAME}-s STATIC ${LIB_PUBLIC_HEADERS} ${LIB_SOURCES})
add_library (${LIB_NAME} SHARED ${LIB_PUBLIC_HEADERS} ${LIB_SOURCES})

install(
TARGETS ${LIB_NAME}
LIBRARY DESTINATION lib${LIB_SUFFIX}
)

#install(
# TARGETS ${LIB_NAME}-s
# ARCHIVE DESTINATION lib${LIB_SUFFIX}
#)

install(
FILES ${LIB_PUBLIC_HEADERS}
DESTINATION include/${LIB_NAME}
)
77 changes: 77 additions & 0 deletions ClusterRotate.cpp
@@ -0,0 +1,77 @@
/*
* ClusterRotate.cpp
*
* Created on: 04-Mar-2009
* Author: sbutler
*/

#include "ClusterRotate.h"

#include <map>
#include <Eigen/Array>

ClusterRotate::ClusterRotate(int method):
mMethod(method),
mMaxQuality(0)
{

}

std::vector<std::vector<int> > ClusterRotate::cluster(Eigen::MatrixXd& X) {
mMaxQuality = 0;
std::vector<std::vector<int> > clusters;
Eigen::MatrixXd vecRot;
Eigen::MatrixXd vecIn = X.block(0,0,X.rows(),2);
Evrot* e = NULL;
for (int g=2; g <= X.cols(); g++) {
// make it incremental (used already aligned vectors)
if( g > 2 ) {
vecIn.resize(X.rows(),g);
vecIn.block(0,0,vecIn.rows(),g-1) = e->getRotatedEigenVectors();
vecIn.block(0,g-1,X.rows(),1) = X.block(0,g-1,X.rows(),1);
delete e;
}
//perform the rotation for the current number of dimensions
e = new Evrot(vecIn, mMethod);

//save max quality
if (e->getQuality() > mMaxQuality) {
mMaxQuality = e->getQuality();
}
//save cluster data for max cluster or if we're near the max cluster (so prefer more clusters)
if ((e->getQuality() > mMaxQuality) || (mMaxQuality - e->getQuality() <= 0.001)) {
clusters = e->getClusters();
vecRot = e->getRotatedEigenVectors();
}
}

Eigen::MatrixXd clusterCentres = Eigen::MatrixXd::Zero(clusters.size(),vecRot.cols());
for (unsigned int i=0; i < clusters.size(); i++) {
for (unsigned int j=0; j < clusters[i].size(); j++) {
//sum points within cluster
clusterCentres.row(i) += vecRot.row(clusters[i][j]);
}
}
for (unsigned int i=0; i < clusters.size(); i++) {
//find average point within cluster
clusterCentres.row(i) = clusterCentres.row(i) / clusters[i].size();
}

//order clustered points by (ascending) distance to cluster centre
for (unsigned int i=0; i < clusters.size(); i++) {
std::multimap<double,int> clusterDistance;
for (unsigned int j=0; j < clusters[i].size(); j++) {
double d2 = (vecRot.row(clusters[i][j]) - clusterCentres.row(i)).squaredNorm();
clusterDistance.insert(std::make_pair(d2, clusters[i][j]));
}
//the map will be sorted based on the key so just loop through it
//to get set of data indices sorted on the distance to cluster
clusters[i].clear();
for (std::multimap<double,int>::iterator it = clusterDistance.begin(); it != clusterDistance.end(); it++) {
clusters[i].push_back(it->second);
}
}

return clusters;
}

32 changes: 32 additions & 0 deletions ClusterRotate.h
@@ -0,0 +1,32 @@
/*
* ClusterRotate.h
*
* Created on: 04-Mar-2009
* Author: sbutler
*/

#ifndef CLUSTERROTATE_H_
#define CLUSTERROTATE_H_

#include <Eigen/Core>
#include <vector>

#include "Evrot.h"

class ClusterRotate {

public:
ClusterRotate(int method = 1);
virtual ~ClusterRotate() { }

std::vector<std::vector<int> > cluster(Eigen::MatrixXd& X);
double getMaxQuality() { return mMaxQuality; };

protected:

int mMethod;
double mMaxQuality;

};

#endif /* CLUSTERROTATE_H_ */

0 comments on commit 9c982b0

Please sign in to comment.