use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class RunFFT method kcluster.
@Override
public int kcluster(int nClusters, int nIterations, CyMatrix matrix, DistanceMetric metric, int[] clusterID) {
random = null;
int nelements = matrix.nRows();
int[] tclusterid = new int[nelements];
int[] saved = new int[nelements];
int[] mapping = new int[nClusters];
int[] counts = new int[nClusters];
CyMatrix distanceMatrix = matrix.getDistanceMatrix(metric);
HashMap<Integer, Integer> centers = new HashMap<Integer, Integer>();
double error = Double.MAX_VALUE;
if (monitor != null)
monitor.setProgress(0);
for (int i = 0; i < nelements; i++) clusterID[i] = 0;
// the first center
Random randomGenerator = new Random();
centers.put(0, randomGenerator.nextInt(nelements));
// now find the remaining centers
for (int i = 1; i < nClusters; i++) {
int y = getMaxMin(centers, distanceMatrix);
centers.put(i, y);
clusterID[y] = i;
}
/*
for (int i = 0; i < nClusters; i++)
System.out.printf("Center for %d = %d\n", i, centers.get(i));
*/
// assign clusters now
int k = centers.get(0);
for (int i = 0; i < nelements; i++) {
// Is this one of our centers?
if (centers.containsValue(i))
continue;
double minDistance = Double.MAX_VALUE;
int center = 0;
for (int cluster = 0; cluster < nClusters; cluster++) {
double dist = distanceMatrix.doubleValue(i, centers.get(cluster));
if (dist < minDistance) {
center = cluster;
minDistance = dist;
}
}
clusterID[i] = center;
}
return nClusters;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class RunAutoSOME method run.
public List<NodeCluster> run(TaskMonitor monitor) {
long startTime = System.currentTimeMillis();
debugln("Initial matrix:");
// printMatrix(matrix);
// Normalize
// normalize(matrix, clusteringThresh, false);
debugln("Normalized matrix:");
if (dataAttributes == null || dataAttributes.isEmpty()) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Error: no attribute list selected");
return null;
}
if (selectedOnly && network.getDefaultNodeTable().countMatchingRows(CyNetwork.SELECTED, true) == 0) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Error: no nodes selected from network");
return null;
}
Settings s = new Settings();
// get parameter settings
s.ensemble_runs = settings.ensemble_runs;
s.mst_pval = settings.mst_pval;
s.threads = settings.threads;
s.logNorm = settings.logNorm;
s.unitVar = settings.unitVar;
s.medCenter = settings.medCenter;
s.medCenterCol = settings.medCenterCol;
s.sumSqrRows = settings.sumSqrRows;
s.sumSqrCol = settings.sumSqrCol;
s.som_iters = settings.som_iters;
s.de_resolution = settings.de_resolution;
s.distMatrix = settings.distMatrix;
s.dmDist = settings.dmDist;
s.FCNrows = settings.FCNrows;
s.som_maxGrid = 20;
s.htmlOut = false;
s.textOut = false;
// System.out.println("network "+network.toString()+" has "+network.getNodeCount()+" nodes");
// System.out.println("attributes: "+dataAttributes);
String[] attrArray = new String[dataAttributes.size()];
int att = 0;
for (String attribute : dataAttributes) {
attrArray[att++] = "node." + attribute;
}
// Create the matrix
CyMatrix matrix = CyMatrixFactory.makeSmallMatrix(network, attrArray, selectedOnly, ignoreMissing, false, false);
if (!selectedOnly) {
nodes = network.getNodeList();
} else {
nodes = CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true);
}
s.input = new dataItem[matrix.nRows()];
// matrix.printMatrix();
Map<String, Integer> key = new HashMap<String, Integer>();
for (int i = 0; i < nodes.size(); i++) {
String id = ModelUtils.getNodeName(network, nodes.get(i));
if (!key.containsKey(id))
key.put(id, i);
}
for (int k = 0, itor = 0; k < matrix.nRows(); k++) {
float[] f = new float[matrix.nColumns()];
// System.out.println(matrix.getRowLabels()[k]+" "+nodes.get(k).getIdentifier());
if (k == 0) {
s.columnHeaders = new String[f.length + 1];
s.columnHeaders[0] = "NAME";
}
for (int l = 0; l < f.length; l++) {
if (k == 0) {
s.columnHeaders[l + 1] = matrix.getColumnLabel(l);
s.columnHeaders[l + 1] = s.columnHeaders[l + 1].replace("\"", "");
s.columnHeaders[l + 1] = s.columnHeaders[l + 1].replace(",", "");
// System.out.println(s.columnHeaders[l+1]);
}
// System.out.println(matrix.getValue(k,l).floatValue());
if (matrix.getValue(k, l) != null) {
f[l] = matrix.getValue(k, l).floatValue();
} else {
f[l] = -99999999;
s.fillMissing = true;
}
}
s.input[itor++] = new dataItem(f, matrix.getRowLabel(k));
}
if (s.FCNrows && s.distMatrix)
s = transpose(s);
if (s.input == null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Insufficient data for clustering (1 or less rows or columns)");
return null;
} else if (s.input.length < 2) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Insufficient data for clustering (1 or less rows or columns)");
return null;
} else if (s.input[0].getValues() == null) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Insufficient data for clustering (1 or less rows or columns)");
return null;
} else if (s.input[0].getValues().length < 2) {
monitor.showMessage(TaskMonitor.Level.ERROR, "Insufficient data for clustering (1 or less rows or columns)");
return null;
}
autRun = new Run();
cr = autRun.runAutoSOMEBasic(s, monitor);
if (cr == null) {
monitor.setStatusMessage("Clustering failed!");
return null;
}
monitor.setStatusMessage("Assigning nodes to clusters");
clusterCount = cr.c.length;
Map<NodeCluster, NodeCluster> cMap = (!s.distMatrix) ? getNodeClusters(cr, key, matrix, s) : getNodeClustersFCN(cr, matrix, s);
if (canceled) {
monitor.setStatusMessage("canceled");
return null;
}
// Update node attributes in network to include clusters. Create cygroups from clustered nodes
monitor.setStatusMessage("Created " + clusterCount + " clusters");
//
if (clusterCount == 0) {
monitor.showMessage(TaskMonitor.Level.WARN, "Created 0 clusters!!!!");
return null;
}
Set<NodeCluster> clusters = cMap.keySet();
return new ArrayList<NodeCluster>(clusters);
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class PP_DivideAndReclusterRecursively method recursiveReclustering.
/**
* This method goes through each cluster in the tmpClusterObject and
* re-clusters them. If the clusters have less than 4 objects, then it is
* added to the finalClusterObject. If after the re-clustering, no changes
* have occured (it is still one cluster) it is added to the
* finalClusterObject. Otherwise if changes have occured the sub-clusters
* are then re-clustered again. This occurs recursively until no more
* improvements are found.
*
* @param finalClusterObject
* This should only include finished clusters that can not be
* improved anymore by reclustering.
* @param tmpClusterObject
* This includes all clusters that are to be re-clustered.
*/
private void recursiveReclustering(ArrayList<ArrayList<Integer>> finalClusterObject, ArrayList<ArrayList<Integer>> tmpClusterObject) {
for (int i = 0; i < tmpClusterObject.size(); i++) {
ArrayList<Integer> cluster = tmpClusterObject.get(i);
int clusterSize = cluster.size();
/*all clusters <=3 are taken care of in the rearrange method of
* post-processing */
if (clusterSize <= 3) {
finalClusterObject.add(cluster);
} else {
/* run clustering for this one cluster to see if an improvement can be found */
ConnectedComponent ccForCluster = this.cc.createConnectedComponentForCluster(i, cluster);
ClusterPostProcessingTask clusterTask = new ClusterPostProcessingTask(ccForCluster, this.params, this.layouterEnumTypes);
clusterTask.run();
/* if there has been no change, add this cluster to the final cluster object */
if (ccForCluster.getNumberOfClusters() == 1) {
finalClusterObject.add(cluster);
/* otherwise recluster */
} else {
/* new cluster object for the resulting clusters of the improved cluster */
ArrayList<ArrayList<Integer>> nextClusterObject = PostProcessingUtility.createClusterObject(ccForCluster, true);
recursiveReclustering(finalClusterObject, nextClusterObject);
}
}
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class ClusteringManager method initParametersAndCCs.
/**
* Sets transitive connected components file if in input directory. Finds all cost matrix files
* given and creates a new {@link ConnectedComponent} instance for each and adds them to
* the list of ConnectedComponents.
*
* Also initialises the {@link IParameters} from the input configuration.
* @throws InvalidInputFileException
*/
public void initParametersAndCCs() throws InvalidInputFileException {
if (TaskConfig.mode == TaskConfig.CLUSTERING_MODE || TaskConfig.mode == TaskConfig.GENERAL_TRAINING_MODE) {
// this.connectedComponents = new ArrayList<ConnectedComponent>();
this.connectedComponents = new ArrayList<File>();
TaskConfig.transitiveConnectedComponents = null;
/* read the input file or directory */
File cmFile = new File(this.cmPath);
if (cmFile.isDirectory()) {
log.finer("Input is a directory!");
/* get all files in the directory */
File[] files = cmFile.listFiles();
/* check boolean whether cm files exist in directory */
boolean noCostMatrices = true;
boolean noTCCfile = true;
for (int i = 0; i < files.length; i++) {
String filePath = files[i].toString();
/* find tcc file in directory */
if (filePath.endsWith(".tcc") || filePath.endsWith(".rtcc")) {
noTCCfile = false;
TaskConfig.transitiveConnectedComponents = filePath;
log.info("Transitive connected components file: " + filePath);
InfoFile.appendToProjectDetails("Transitive connected component file: " + filePath);
}
/* find cm files*/
if (files[i].toString().endsWith(".cm") || files[i].toString().endsWith(".rcm")) {
// cm files exist
noCostMatrices = false;
connectedComponents.add(files[i]);
// create the connected component (cc) object and add to list
// CostMatrixReader cmReader = new CostMatrixReader(files[i]);
// ConnectedComponent cc = cmReader.getConnectedComponent();
// connectedComponents.add(cc);
}
}
if (noCostMatrices) {
if (noTCCfile) {
throw new InvalidInputFileException("There are no cost matrix " + "files in the input directory and also no transitive connected components file, " + "or check whether the file extensions equal .cm, .rcm, or .tcc");
}
}
} else {
/* only one cost matrix file is given */
log.finer("One cm file given");
/* only one cost matrix as input - start clustering process */
if (!cmFile.toString().endsWith(".tcc")) {
// create the connected component (cc) object and add to list
// CostMatrixReader cmReader = new CostMatrixReader(cmFile);
// ConnectedComponent cc = cmReader.getConnectedComponent();
// connectedComponents.add(cc);
connectedComponents.add(cmFile);
} else {
if (cmFile.toString().endsWith(".tcc")) {
TaskConfig.transitiveConnectedComponents = cmFile.toString();
log.info("Only a transitive connected component file is given: " + cmFile.toString());
InfoFile.appendToProjectDetails("Only a transitive connected component file is given: " + cmFile.toString() + ". Therefore NO CLUSTERING IS PERFORMED, just the the " + "clusters from the TCC file are written into the clusters file.");
} else {
throw new InvalidInputFileException("Either the input cost matrix is of " + "wrong file type. The file extension should be \".cm\" or \".rcm\"," + "or in the given TCC file is of the wrong type and should be \".tcc\".");
}
}
}
}
/* initialise parameters from config */
// LayoutFactory.EnumLayouterClass[] layouterEnumTypes = TaskConfig.layouterEnumTypes;
layouterParameters = new IParameters[TaskConfig.layouterEnumTypes.length];
for (int i = 0; i < TaskConfig.layouterEnumTypes.length; i++) {
IParameters param = TaskConfig.layouterEnumTypes[i].createIParameters();
param.readParametersFromConfig();
layouterParameters[i] = param;
}
if (this.connectedComponents == null) {
}
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters in project clusterMaker2 by RBVI.
the class ClusteringManager method runClusteringForOneConnectedComponent.
/**
* Runs clustering for one {@link ConnectedComponent} and sets the total score to
* the parameters if in the general training mode.
* @param cc The connected component object.
* @param clusterFile The clusters file (null if in general training mode)
* @param semaphore The Semaphore to give to the clustering task to keep track of it.
* @param time
* @throws InvalidInputFileException
*/
public void runClusteringForOneConnectedComponent(ConnectedComponent cc, ClusterFile clusterFile, Semaphore semaphore, Semaphore maxThreadSemaphore, long time) throws InvalidInputFileException {
/* check whether layouterParameters has been initialised */
if (this.layouterParameters == null) {
if (TaskConfig.mode == TaskConfig.CLUSTERING_MODE || TaskConfig.mode == TaskConfig.GENERAL_TRAINING_MODE)
log.warning("Incorrect use of the ClusteringManager, the layouter parameters list" + "hadn't been initialised. Called method to initialise this and the connected components from " + "the config");
this.initParametersAndCCs();
}
// if(TaskConfig.mode == TaskConfig.CLUSTERING_MODE){
if (TaskConfig.doLayoutParameterTraining && !TaskConfig.greedy) {
for (int i = 0; i < this.layouterParameters.length; i++) {
/* start parameter training for the cc */
IParameterTraining paramTrain = TaskConfig.parameterTrainingEnum.createParameterTrainer();
paramTrain.initialise(TaskConfig.layouterEnumTypes[i], TaskConfig.noOfParameterConfigurationsPerGeneration, TaskConfig.noOfGenerations);
paramTrain.setMaxThreadSemaphoreAndThreadsList(maxThreadSemaphore, this.allThreads);
IParameters bestparam = paramTrain.run(cc);
log.fine("PARAMETER TRAINING RESULT\n: " + cc.getCcPath() + "\n" + bestparam.toString());
this.layouterParameters[i] = bestparam;
}
}
// }
/* run clustering with the previously determined parameters */
ClusteringTask clusterTask = new ClusteringTask(cc, this.layouterParameters, TaskConfig.layouterEnumTypes, clusterFile);
clusterTask.setTime(time);
// if(!TaskConfig.doLayoutParameterTraining&&TaskConfig.useThreads){
// clusterTask.setSemaphore(semaphore);
// Thread t = new Thread(clusterTask);
// clusterTask.setMaxThreadSemaphore(maxThreadSemaphore, allThreads, t);
// t.start();
// }else{
clusterTask.run();
// }
}
Aggregations