Search in sources :

Example 1 with CostMatrixReader

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.CostMatrixReader in project clusterMaker2 by RBVI.

the class GeneralParameterTraining method runOneGeneration.

private void runOneGeneration(IParameters[][] parameters, ArrayList<File> connectedComponents, int gen) throws InvalidInputFileException, InvalidTypeException {
    TaskConfig.monitor.setStatusMessage("Start training generation: " + gen);
    int size = parameters.length;
    /* do clustering for each set of parameter configurations */
    for (int i = 0; i < size; i++) {
        /* copy the cc (ConnectedComponent) ArrayList - shares same resources! */
        ArrayList<ConnectedComponent> newConnectedComponents = new ArrayList<ConnectedComponent>(connectedComponents.size());
        // for (int j = 0; j < connectedComponents.size(); j++) {
        // ConnectedComponent cc = new CostMatrixReader(connectedComponents.get(j)).getConnectedComponent();
        // ConnectedComponent newCC = cc.copy();
        /* sets a new positions array for the copy */
        // newCC.setCCPositions(cc.copyCCPositions());
        // newConnectedComponents.add(newCC);
        // }
        /* start clustering */
        clusteringManager.setConnectedComponents(connectedComponents);
        clusteringManager.setLayouterParameters(parameters[i]);
        clusteringManager.runClustering();
        FORCEnDParameters fndparam = (FORCEnDParameters) parameters[i][0];
        fndparam.printParamters();
        newConnectedComponents.clear();
    }
// log.debug("Finished training generation: "+gen);
}
Also used : FORCEnDParameters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.forcend.FORCEnDParameters) ConnectedComponent(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ConnectedComponent) ArrayList(java.util.ArrayList)

Example 2 with CostMatrixReader

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.CostMatrixReader 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) {
    }
}
Also used : IParameters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.IParameters) InfoFile(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.io.InfoFile) ClusterFile(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.io.ClusterFile) File(java.io.File)

Example 3 with CostMatrixReader

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.CostMatrixReader 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;
}
Also used : IParameters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.IParameters) ConnectedComponent(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ConnectedComponent) ArrayList(java.util.ArrayList) CostMatrixReader(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.CostMatrixReader) Semaphore(java.util.concurrent.Semaphore) ClusterFile(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.io.ClusterFile)

Example 4 with CostMatrixReader

use of edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.CostMatrixReader in project clusterMaker2 by RBVI.

the class GeneralParameterTraining method runGeneralTraining.

/**
 * Starts the general parameter training using a simple evolutionary method
 * for the whole input directory (or just one file if
 * this was given). Saves the best found parameters of each layout algorithm to the
 * respective config classes. But does not create a config file.
 *
 * @return The best set of IParameters as an Array.
 * @throws InvalidInputFileException If the input file/directory is somehow
 * 					incorrect or cannot be read.
 * @throws InvalidTypeException If an method type does not exist or hasn't
 * 					been included properly.
 */
public IParameters[] runGeneralTraining() throws InvalidInputFileException, InvalidTypeException {
    /* initialise connected components and parameters */
    this.clusteringManager.initParametersAndCCs();
    ArrayList<File> connectedComponents = clusteringManager.getConnectedComponents();
    /*
		 * initialise positions of the cc with respect to the FIRST layouter - the same 
		 * initial positions are used for all training rounds
		 */
    for (File cc : connectedComponents) {
        ILayoutInitialiser li = TaskConfig.layouterEnumTypes[0].createLayoutInitialiser();
        li.initLayoutInitialiser(new CostMatrixReader(cc).getConnectedComponent());
        li.run();
    }
    boolean terminateTraining = false;
    /* run initial generation */
    IParameters[][] initialGeneration = createInitialParameterGeneration();
    runOneGeneration(initialGeneration, connectedComponents, 0);
    terminateTraining = terminateTraining(initialGeneration);
    /* add the best 10 random configs to the bestConfigs collection Vector */
    for (int i = 0; i < initialGeneration.length; i++) {
        this.bestConfigs.add(initialGeneration[i]);
    }
    /* run all following generations */
    IParameters[][] generation;
    for (int i = 1; i <= this.noOfGenerations; i++) {
        if (terminateTraining) {
            break;
        }
        generation = createParameterGeneration();
        runOneGeneration(generation, connectedComponents, i);
        terminateTraining = terminateTraining(generation);
        for (int j = 0; j < this.generationSize / 2; j++) {
            this.bestConfigs.add(initialGeneration[j]);
        }
    }
    /* convert best configurations vector to array */
    IParameters[][] bestConfigsArray = new IParameters[bestConfigs.size()][TaskConfig.layouterEnumTypes.length];
    for (int i = 0; i < bestConfigs.size(); i++) {
        bestConfigsArray[i] = bestConfigs.get(i);
    }
    /*
		 * sort the IParameters array according to their score and return the
		 * best parameter set.
		 */
    Arrays.sort(bestConfigsArray, this.paramComparator);
    this.bestPreviousIParameters = bestConfigsArray[0];
    for (int i = 0; i < bestConfigsArray[0].length; i++) {
        bestConfigsArray[0][i].saveParametersToConfig();
    }
    for (int i = 0; i < 30; i++) {
        FORCEnDParameters ip = (FORCEnDParameters) bestConfigsArray[i][0];
        ip.printParamters();
    }
    return bestConfigsArray[0];
}
Also used : IParameters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.IParameters) FORCEnDParameters(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.forcend.FORCEnDParameters) CostMatrixReader(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.CostMatrixReader) ILayoutInitialiser(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.ILayoutInitialiser) File(java.io.File)

Aggregations

IParameters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.IParameters)3 ConnectedComponent (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.ConnectedComponent)2 CostMatrixReader (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.datastructure.CostMatrixReader)2 FORCEnDParameters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.forcend.FORCEnDParameters)2 ClusterFile (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.io.ClusterFile)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ILayoutInitialiser (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.layout.ILayoutInitialiser)1 InfoFile (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.networkClusterers.TransClust.de.layclust.taskmanaging.io.InfoFile)1 Semaphore (java.util.concurrent.Semaphore)1