Search in sources :

Example 31 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix 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;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) Random(java.util.Random) HashMap(java.util.HashMap)

Example 32 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix 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);
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)

Example 33 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix 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 34 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.

the class CalculationMatrix method getCoordinates.

// get the coordinates for PCoA
public CyMatrix[] getCoordinates(CyMatrix matrix) {
    CyMatrix[] components = new CyMatrix[eigen_values.length];
    for (int j = eigen_values.length - 1, k = 0; j >= 0; j--, k++) {
        // double[] w = new double[vectors.length];
        CyMatrix result = CyMatrixFactory.makeLargeMatrix(matrix.getNetwork(), eigen_values.length, 1);
        for (int i = 0; i < eigen_values.length; i++) {
            // System.out.println("Setting eigen_vector["+i+"]["+j+"]");
            result.setValue(i, 0, eigen_vectors[i][j]);
        }
        // matrix.writeMatrix("matrix");
        // result.writeMatrix("result");
        // System.out.println("matrix: "+matrix.printMatrixInfo());
        // System.out.println("vector: "+result.printMatrixInfo());
        // System.out.println("Matrix rows "+matrix.printMatrixInfo());
        // System.out.println("Result rows "+result.printMatrixInfo());
        Matrix mat = matrix.ops().multiplyMatrix(result);
        // System.out.println("After vector multiply: "+mat.printMatrixInfo());
        components[k] = matrix.copy(mat);
    // components[k].printMatrixInfo();
    // components[k].writeMatrix("component_"+k+".txt");
    // System.out.println("Component matrix "+k+" has "+components[k].getRowNodes().size()+" nodes");
    }
    return components;
}
Also used : Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 35 with Matrix

use of edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix in project clusterMaker2 by RBVI.

the class GowersMatrix method getGowersMatrix.

public static Matrix getGowersMatrix(CyMatrix distanceMatrix) {
    // Create the Identity matrix
    // DoubleMatrix2D I = DoubleFactory2D.sparse.identity(distancematrix.nRows());
    Matrix I = distanceMatrix.like(distanceMatrix.nRows(), distanceMatrix.nRows(), 0.0);
    for (int row = 0; row < distanceMatrix.nRows(); row++) I.setValue(row, row, 1.0);
    /*
		IntStream.range(0, distanceMatrix.nRows()).parallel()
			.forEach(row -> {
					I.setValue(row, row, 1.0);
				});
		*/
    // I.writeMatrix("I");
    // Create the ones matrix.  This is equivalent to 11'/n
    // DoubleMatrix2D one = DoubleFactory2D.dense.make(distancematrix.nRows(), distancematrix.nRows(), 1.0/distancematrix.nRows());
    Matrix one = distanceMatrix.like(distanceMatrix.nRows(), distanceMatrix.nRows(), 1.0 / distanceMatrix.nRows());
    // I.writeMatrix("one");
    // Create the subtraction matrix (I-11'/n)
    // DoubleMatrix2D mat = I.assign(one, DoubleFunctions.minus);
    Matrix mat = subtractElement(I, one);
    // mat.writeMatrix("mat");
    // Create our data matrix
    // final DoubleMatrix2D A = DoubleFactory2D.sparse.make(distancematrix.nRows(), distancematrix.nRows());
    Matrix A = distanceMatrix.like(distanceMatrix.nRows(), distanceMatrix.nRows());
    /*for (int row = 0; row < distancematrix.nRows(); row++) {
			for (int col = 0; col < distancematrix.nColumns(); col++) {
				System.out.print(distancematrix.getValue(row, col)+",");
			}
		}*/
    /*
		DoubleMatrix2D data = distancematrix.getColtMatrix();

		data.forEachNonZero(
			new IntIntDoubleFunction() {
				public double apply(int row, int column, double value) {
					double v = -Math.pow(value,2)/2.0;
					if (Math.abs(v) > EPSILON)
						A.setQuick(row, column, v);
					return value;
				}
			}
		);
		*/
    IntStream.range(0, distanceMatrix.nRows()).parallel().forEach(row -> IntStream.range(0, distanceMatrix.nColumns()).forEach(col -> {
        double v = -Math.pow(distanceMatrix.doubleValue(row, col), 2) / 2.0;
        if (Math.abs(v) > EPSILON)
            A.setValue(row, col, v);
    }));
    // A.writeMatrix("A");
    Matrix cMat = distanceMatrix.like(mat);
    Matrix cA = distanceMatrix.like(A);
    // System.out.println("Completed Gowers Matrix");
    // Finally, the Gower's matrix is mat*A*mat
    Matrix mat1 = cMat.ops().multiplyMatrix(cA);
    Matrix G = mat1.ops().multiplyMatrix(cMat);
    // G.writeMatrix("Gowers");
    return G;
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) CyMatrixFactory(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.matrix.CyMatrixFactory) Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) DecimalFormat(java.text.DecimalFormat) CommonOps.subtractElement(edu.ucsf.rbvi.clusterMaker2.internal.api.CommonOps.subtractElement) Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Aggregations

CyMatrix (edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)34 Matrix (edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix)22 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)13 NodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.NodeCluster)12 CyNode (org.cytoscape.model.CyNode)12 List (java.util.List)9 AbstractClusterResults (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults)7 NewNetworkView (edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView)7 FuzzyNodeCluster (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)5 CyTable (org.cytoscape.model.CyTable)5 Test (org.junit.Test)4 Clusters (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.attributeClusterers.Clusters)3 ColtMatrix (edu.ucsf.rbvi.clusterMaker2.internal.algorithms.matrix.ColtMatrix)3 DoubleMatrix2D (cern.colt.matrix.tdouble.DoubleMatrix2D)2 DistanceMetric (edu.ucsf.rbvi.clusterMaker2.internal.api.DistanceMetric)2 ScatterPlotDialog (edu.ucsf.rbvi.clusterMaker2.internal.ui.ScatterPlotDialog)2 CyEdge (org.cytoscape.model.CyEdge)2 DoubleArrayList (cern.colt.list.tdouble.DoubleArrayList)1 IntArrayList (cern.colt.list.tint.IntArrayList)1