use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.ClusteringManager in project clusterMaker2 by RBVI.
the class PP_RearrangeAndMergeBest method mergeBest.
protected void mergeBest(ArrayList<ArrayList<Integer>> clusterObject) throws InvalidInputFileException {
// System.out.println("start merging");
ICCEdges cc2d = TaskConfig.ccEdgesEnum.createCCEdges(clusterObject.size());
String[] ids = new String[clusterObject.size()];
for (int i = 0; i < ids.length; i++) {
ids[i] = i + "";
for (int j = i + 1; j < ids.length; j++) {
cc2d.setEdgeCost(i, j, -(float) calculateCostChange(clusterObject.get(i), clusterObject.get(j)));
// System.out.println(i + "\t" + j + "\t" + cc2d.getEdgeCost(i, j));
}
}
ConnectedComponent cc2 = new ConnectedComponent(cc2d, ids, null, false);
ClusteringManager cm = new ClusteringManager(null);
// cm.runClusteringForOneConnectedComponent(cc2, null, null, null, System.currentTimeMillis());
new GreedyClusterer(cc2);
// long dummy = TaskConfig.fpMaxTimeMillis;
// TaskConfig.fpMaxTimeMillis = Long.MAX_VALUE;
// FixedParameterClusterer dc = new FixedParameterClusterer(cc2,cc2.getClusteringScore());
// TaskConfig.fpMaxTimeMillis = dummy;
// if(cc2.getClusteringScore() != this.bestScore){
// PP_DivideAndRecluster pp = new PP_DivideAndRecluster();
// pp.initPostProcessing(cc2);
// pp.run();
// }
ArrayList<ArrayList<Integer>> clusterObjectNew = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < cc2.getNumberOfClusters(); i++) {
ArrayList<Integer> v = new ArrayList<Integer>();
clusterObjectNew.add(v);
}
int[] clustering = cc2.getClusters();
for (int i = 0; i < clustering.length; i++) {
clusterObjectNew.get(clustering[i]).addAll(clusterObject.get(i));
}
clusterObject = clusterObjectNew;
this.bestScore = cc2.calculateClusteringScore(clustering);
// System.out.println("end merging");
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.ClusteringManager 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();
// }
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.ClusteringManager in project clusterMaker2 by RBVI.
the class ClusteringManager method runClustering.
/**
* Runs the clustering with the given configurations in the config class: {@link TaskConfig}.
* Clusters each {@link ConnectedComponent} separately and waits until all are done.
* Differes between the modes clustering and general training. Creates a Config file if
* the training mode is used.
* @throws InvalidInputFileException If the file/directory given produces an error.
* @throws InvalidTypeException An incorrect method implementation was given, or some
* other error occured with this.
*/
public void runClustering() throws InvalidInputFileException, InvalidTypeException {
/* initialise ClusterFile if in clustering mode */
ClusterFile clusterFile = null;
if (TaskConfig.mode == TaskConfig.CLUSTERING_MODE) {
log.fine("Running clustering in clustering mode!");
clusterFile = new ClusterFile();
clusterFile.instantiateFile(TaskConfig.clustersPath);
clusterFile.printPreProcessingClusters(TaskConfig.transitiveConnectedComponents);
/* check whether connectedComponents has been initialised */
if (this.connectedComponents == null) {
if (TaskConfig.transitiveConnectedComponents == null) {
log.warning("Incorrect use of the ClusteringManager, the connected components list" + "hadn't been initialised. Called method to initialise this and the parameters from " + "the config. Or only a TCC file was given and no connected components.");
this.initParametersAndCCs();
} else {
log.info("No cost matrices were given, just a transitive connected components file, which" + "is converted to a clusters file. NO CLUSTERING IS PERFORMED!");
InfoFile.appendToProjectDetails("No cost matrices were given, just a transitive connected components file, which" + "is converted to a clusters file. NO CLUSTERING IS PERFORMED!");
}
}
}
if (this.connectedComponents != null) {
/* go through cc list and start training for each and control thread use */
ArrayList<Semaphore> allSemaphores = new ArrayList<Semaphore>();
Semaphore maxThreadSemaphore = new Semaphore(TaskConfig.maxNoThreads, true);
for (int i = 0; i < this.connectedComponents.size(); i++) {
Semaphore semaphore = new Semaphore(1);
allSemaphores.add(semaphore);
long time = System.currentTimeMillis();
CostMatrixReader cmReader = new CostMatrixReader(this.connectedComponents.get(i));
ConnectedComponent cc = cmReader.getConnectedComponent();
runClusteringForOneConnectedComponent(cc, clusterFile, semaphore, maxThreadSemaphore, time);
}
/* wait for all clustering tasks to finish */
for (Semaphore s : allSemaphores) {
try {
s.acquire();
} catch (InterruptedException e) {
log.severe(e.getMessage());
e.printStackTrace();
}
}
}
if (clusterFile != null)
clusterFile.closeFile();
/* END OF CLUSTERING */
log.info("Clustering scores sum: " + totalScoreSum);
if (TaskConfig.mode == TaskConfig.CLUSTERING_MODE) {
InfoFile.appendLnProjectResults("Total sum of clustering scores for given input: " + TaskUtility.round(totalScoreSum, 2));
}
/* set score to IParameters objects for general training mode */
if (TaskConfig.mode == TaskConfig.GENERAL_TRAINING_MODE) {
log.fine("Setting parameters score for training mode!");
for (IParameters parameter : this.layouterParameters) {
parameter.setScore(totalScoreSum);
}
}
totalScoreSum = 0;
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.ClusteringManager in project clusterMaker2 by RBVI.
the class ClusteringManagerTask method run.
public void run() {
try {
TaskConfig.monitor.setStatusMessage("-----------------------------------");
TaskConfig.monitor.setStatusMessage("Running ... " + TaskConfig.NAME + " v" + TaskConfig.VERSION);
Date date = new Date(System.currentTimeMillis());
TaskConfig.monitor.setStatusMessage(date.toString());
TaskConfig.monitor.setStatusMessage("-----------------------------------");
if (TaskConfig.mode == TaskConfig.CLUSTERING_MODE) {
long time = System.currentTimeMillis();
this.clusterManager = new ClusteringManager(TaskConfig.cmPath);
try {
this.clusterManager.initParametersAndCCs();
this.clusterManager.runClustering();
// System.out.println(this.clusterManager.getConnectedComponents().size());
time = System.currentTimeMillis() - time;
TaskConfig.monitor.setStatusMessage("Time taken for complete clustering process: " + TaskUtility.convertTime(time));
} catch (InvalidInputFileException e) {
TaskConfig.monitor.showMessage(TaskMonitor.Level.ERROR, e.getMessage());
// e.printStackTrace();
System.exit(-1);
} catch (InvalidTypeException e) {
TaskConfig.monitor.showMessage(TaskMonitor.Level.ERROR, e.getMessage());
// e.printStackTrace();
System.exit(-1);
}
} else /* start general training */
if (TaskConfig.mode == TaskConfig.GENERAL_TRAINING_MODE) {
try {
long time = System.currentTimeMillis();
ClusteringManager clustermanage = new ClusteringManager(TaskConfig.cmPath);
GeneralParameterTraining generalParameterTraining = new GeneralParameterTraining(clustermanage);
IParameters[] layoutParams = generalParameterTraining.runGeneralTraining();
/* save newly found parameters to config file */
TaskConfig.saveConfigurationsToConfigFile(TaskConfig.outConfigPath);
/* print out information about training */
TaskConfig.monitor.setStatusMessage(TaskConfig.NL + "###### Best Parameter Configurations Found ######");
for (int i = 0; i < layoutParams.length; i++) {
TaskConfig.monitor.setStatusMessage(layoutParams[i].toString());
InfoFile.appendLnProjectResults(layoutParams[i].toString());
}
time = System.currentTimeMillis() - time;
TaskConfig.monitor.setStatusMessage("Time taken for complete general training: " + TaskUtility.convertTime(time));
} catch (InvalidInputFileException e) {
TaskConfig.monitor.showMessage(TaskMonitor.Level.ERROR, e.getMessage());
// e.printStackTrace();
} catch (InvalidTypeException e) {
TaskConfig.monitor.showMessage(TaskMonitor.Level.ERROR, e.getMessage());
// e.printStackTrace();
}
} else if (TaskConfig.mode == TaskConfig.COMPARISON_MODE || TaskConfig.mode == TaskConfig.HIERARICHAL_MODE) {
Thread t = new IteratorThread();
t.start();
} else /* no such mode */
{
TaskConfig.monitor.showMessage(TaskMonitor.Level.ERROR, "ERROR: This mode does not exist: " + TaskConfig.mode);
}
/* create info file */
if (TaskConfig.info) {
InfoFile info = new InfoFile();
info.instantiateFile(TaskConfig.infoPath);
info.createAndCloseInfoFile();
/* clear information from StringBuffers in InfoFile */
InfoFile.clearData();
}
// /* ====== CLEAN UP AT END ====== */
} catch (Exception e) {
TaskConfig.monitor.showMessage(TaskMonitor.Level.ERROR, "ERROR occured in run with the following message: " + e.getMessage());
// e.printStackTrace();
}
/* release permit in semaphore if necessary */
if (semaphore != null) {
semaphore.release();
}
/* reset the run button back to "RUN" and reset action command */
}
use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.ClusteringManager in project clusterMaker2 by RBVI.
the class IteratorThread method calculateHierarichal.
private Hashtable<String, Hashtable<String, Boolean>> calculateHierarichal(double threshold, BufferedWriter bw, Edges es, HashMap<Integer, String> proteins2integers, HashMap<String, Integer> integers2proteins, Hashtable<String, Hashtable<String, Boolean>> clusterReference, Hashtable<String, Hashtable<String, Boolean>> clusters, Vector<String> singletons, Hashtable<Integer, Vector<Integer>> mergedNodes) throws IOException, ArgsParseException, InvalidInputFileException, InvalidTypeException {
Vector<ConnectedComponent> connectedComponents = new Vector<ConnectedComponent>();
if (threshold == TaskConfig.minThreshold) {
Vector<Vector<Integer>> v = Splitter.splitIntoConnectedComponents(es, proteins2integers, (float) threshold, false);
boolean[] already = new boolean[proteins2integers.size()];
for (Vector<Integer> vector : v) {
int count = 0;
Vector<Integer> representants = new Vector<Integer>();
for (int i = 0; i < vector.size(); i++) {
if (!already[vector.get(i)]) {
representants.add(vector.get(i));
Vector<Integer> v2 = mergedNodes.get(vector.get(i));
for (int j = 0; j < v2.size(); j++) {
already[v2.get(j)] = true;
}
count++;
}
}
ICCEdges cc2d2 = TaskConfig.ccEdgesEnum.createCCEdges(count);
String[] ids = new String[count];
Arrays.fill(ids, "");
for (int i = 0; i < representants.size(); i++) {
Vector<Integer> merged1 = mergedNodes.get(representants.get(i));
for (int j = 0; j < merged1.size(); j++) {
if (j == 0)
ids[i] += proteins2integers.get(merged1.get(j));
else
ids[i] += "," + proteins2integers.get(merged1.get(j));
}
for (int j = i + 1; j < representants.size(); j++) {
Vector<Integer> merged2 = mergedNodes.get(representants.get(j));
float costs = 0;
for (int k = 0; k < merged1.size(); k++) {
for (int k2 = 0; k2 < merged2.size(); k2++) {
costs += (float) (InOut.getEdgeValue(merged1.get(k), merged2.get(k2), es) - threshold);
}
}
cc2d2.setEdgeCost(i, j, costs);
}
}
ConnectedComponent cc = new ConnectedComponent(cc2d2, ids, null);
connectedComponents.add(cc);
}
} else {
boolean[] already = new boolean[proteins2integers.size()];
for (Iterator<String> iterator = clusters.keySet().iterator(); iterator.hasNext(); ) {
String key = iterator.next();
Hashtable<String, Boolean> cluster = clusters.get(key);
Vector<Integer> vector = new Vector<Integer>();
for (Iterator<String> iter = cluster.keySet().iterator(); iter.hasNext(); ) {
String element = iter.next();
vector.add(integers2proteins.get(element));
}
int count = 0;
Vector<Integer> representants = new Vector<Integer>();
for (int i = 0; i < vector.size(); i++) {
if (!already[vector.get(i)]) {
representants.add(vector.get(i));
Vector<Integer> v2 = mergedNodes.get(vector.get(i));
for (int j = 0; j < v2.size(); j++) {
already[v2.get(j)] = true;
}
count++;
}
}
ICCEdges cc2d2 = TaskConfig.ccEdgesEnum.createCCEdges(count);
String[] ids = new String[count];
Arrays.fill(ids, "");
for (int i = 0; i < representants.size(); i++) {
Vector<Integer> merged1 = mergedNodes.get(representants.get(i));
for (int j = 0; j < merged1.size(); j++) {
if (j == 0)
ids[i] += proteins2integers.get(merged1.get(j));
else
ids[i] += "," + proteins2integers.get(merged1.get(j));
}
for (int j = i + 1; j < representants.size(); j++) {
Vector<Integer> merged2 = mergedNodes.get(representants.get(j));
float costs = 0;
for (int k = 0; k < merged1.size(); k++) {
for (int k2 = 0; k2 < merged2.size(); k2++) {
costs += (float) (InOut.getEdgeValue(merged1.get(k), merged2.get(k2), es) - threshold);
}
}
cc2d2.setEdgeCost(i, j, costs);
}
}
ConnectedComponent cc = new ConnectedComponent(cc2d2, ids, null);
connectedComponents.add(cc);
// String key = iterator.next();
// Hashtable<String, Boolean> cluster = clusters.get(key);
// CC2DArray cc2d = new CC2DArray(cluster.size());
// String[] ids = new String[cluster.size()];
// int iterator_i = 0;
// for (Iterator<String> iterator2 = cluster.keySet().iterator(); iterator2.hasNext();) {
// String key2 = iterator2.next();
// ids[iterator_i] = key2;
// iterator_i++;
// }
// for (int i = 0; i < ids.length; i++) {
// for (int j = i+1; j < ids.length; j++) {
// cc2d.setEdgeCost(i, j, (float) (InOut.getEdgeValue(integers2proteins.get(ids[i]), integers2proteins.get(ids[j]), es)-threshold));
// }
// }
// ConnectedComponent cc = new ConnectedComponent(cc2d,ids,null);
// connectedComponents.add(cc);
}
}
clusters = new Hashtable<String, Hashtable<String, Boolean>>();
ClusteringManager cm = new ClusteringManager(null);
ArrayList<Semaphore> allSemaphores = new ArrayList<Semaphore>();
Semaphore maxThreadSemaphore = new Semaphore(TaskConfig.maxNoThreads, true);
for (int i = 0; i < connectedComponents.size(); i++) {
Semaphore semaphore = new Semaphore(1);
allSemaphores.add(semaphore);
cm.runClusteringForOneConnectedComponent(connectedComponents.get(i), null, semaphore, maxThreadSemaphore, System.currentTimeMillis());
int[] elements2cluster = connectedComponents.get(i).getClusters();
for (int j = 0; j < connectedComponents.get(i).getNumberOfClusters(); j++) {
Hashtable<String, Boolean> cluster = new Hashtable<String, Boolean>();
for (int k = 0; k < elements2cluster.length; k++) {
if (elements2cluster[k] == j) {
String[] ids = connectedComponents.get(i).getObjectID(k).split(",");
for (int l = 0; l < ids.length; l++) {
cluster.put(ids[l], true);
}
}
}
clusters.put(new Random().nextDouble() + "", cluster);
}
}
/* wait for all clustering tasks to finish */
for (Semaphore s : allSemaphores) {
try {
s.acquire();
} catch (InterruptedException e) {
TaskConfig.monitor.showMessage(TaskMonitor.Level.ERROR, e.getMessage());
// e.printStackTrace();
}
}
bw.write(threshold + "\t");
if (clusterReference != null) {
double fmeasure = Fmeassure.fMeassure(clusterReference, clusters);
bw.write(fmeasure + "\t");
TaskConfig.monitor.setStatusMessage("fmeasure: " + fmeasure);
} else {
bw.write("-\t");
}
int[] distribution = new int[1000000];
int max = 1;
boolean first = true;
Vector<String> keysToRemove = new Vector<String>();
for (Iterator<String> iterator = clusters.keySet().iterator(); iterator.hasNext(); ) {
String key = iterator.next();
Hashtable<String, Boolean> h = clusters.get(key);
if (!first)
bw.write(";");
if (h.size() == 1) {
singletons.add(h.keySet().iterator().next());
keysToRemove.add(key);
} else {
first = true;
for (Iterator<String> iterator2 = h.keySet().iterator(); iterator2.hasNext(); ) {
String id = iterator2.next();
if (first) {
first = false;
bw.write(id);
} else
bw.write("," + id);
}
distribution[h.size()]++;
if (h.size() > max)
max = h.size();
}
}
for (String key : keysToRemove) {
clusters.remove(key);
}
for (String id : singletons) {
bw.write(";" + id);
}
distribution[1] = singletons.size();
StringBuffer sb = new StringBuffer("cluster distribution: ");
for (int i = max; i >= 0; i--) {
if (distribution[i] > 0)
sb.append(i + ":" + distribution[i] + ", ");
}
TaskConfig.monitor.setStatusMessage(sb.toString());
TaskConfig.monitor.setStatusMessage("");
bw.newLine();
return clusters;
}
Aggregations