Search in sources :

Example 46 with Matrix

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

the class FCMCluster method run.

/**
 * The method run creates an instance of the RunFCM and creates the fuzzy clusters by applying the fuzzy c means algorithm.
 * Also creates fuzzy groups and the Fuzzy Cluster Table
 *
 * @param Task Monitor
 */
public void run(TaskMonitor taskmonitor) {
    this.monitor = taskmonitor;
    monitor.setTitle("Performing FCM cluster");
    if (network == null)
        network = clusterManager.getNetwork();
    // Make sure to update the context
    context.setNetwork(network);
    Long networkID = network.getSUID();
    CyTable netAttributes = network.getDefaultNetworkTable();
    CyTable nodeAttributes = network.getDefaultNodeTable();
    CyTable edgeAttributes = network.getDefaultEdgeTable();
    distanceMatrix = context.edgeAttributeHandler.getMatrix();
    if (distanceMatrix == null) {
        monitor.showMessage(TaskMonitor.Level.ERROR, "Can't get distance matrix: no attribute value?");
        return;
    }
    createGroups = context.advancedAttributes.createGroups;
    // Update our tunable results
    clusterAttributeName = "CnC_Bicluster";
    // distanceDataMatrix = new Matrix(network,0,0);
    // distanceDataMatrix.buildDistanceMatrix(distanceMatrix);
    distanceDataMatrix = distanceMatrix.copy();
    if (context.estimateClusterNumber && context.cNumber < 0) {
        // System.out.println("Estimated number of Clusters: "+ cEstimate);
        context.cNumber = cEstimate();
    }
    int[] mostRelevantCluster = new int[network.getNodeList().size()];
    FuzzyNodeCluster.fuzzyClusterCount = 0;
    runFCM = new RunFCM(distanceMatrix, context.iterations, context.cNumber, context.fIndex, context.beta, context.membershipThreshold.getValue(), context.maxThreads, this.monitor);
    runFCM.setDebug(debug);
    if (canceled)
        return;
    monitor.showMessage(TaskMonitor.Level.INFO, "Clustering...");
    List<FuzzyNodeCluster> clusters = runFCM.run(network, monitor, mostRelevantCluster);
    // Canceled?
    if (clusters == null)
        return;
    monitor.showMessage(TaskMonitor.Level.INFO, "Removing groups");
    // Remove any leftover groups from previous runs
    removeGroups(network, GROUP_ATTRIBUTE);
    monitor.showMessage(TaskMonitor.Level.INFO, "Creating groups");
    params = new ArrayList<String>();
    context.edgeAttributeHandler.setParams(params);
    List<List<CyNode>> nodeClusters = createFuzzyGroups(network, clusters, GROUP_ATTRIBUTE);
    results = new AbstractClusterResults(network, clusters);
    monitor.showMessage(TaskMonitor.Level.INFO, "Done.  FCM results:\n" + results);
    if (context.vizProperties.showUI) {
        monitor.showMessage(TaskMonitor.Level.INFO, "Creating network");
        insertTasksAfterCurrentTask(new NewNetworkView(network, clusterManager, true, context.vizProperties.restoreEdges, !selectedOnly));
    } else {
        monitor.showMessage(TaskMonitor.Level.INFO, "Done.  FCM results:\n" + results);
    }
    createFuzzyTable(clusters);
}
Also used : CyTable(org.cytoscape.model.CyTable) NewNetworkView(edu.ucsf.rbvi.clusterMaker2.internal.ui.NewNetworkView) ArrayList(java.util.ArrayList) List(java.util.List) AbstractClusterResults(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.AbstractClusterResults) FuzzyNodeCluster(edu.ucsf.rbvi.clusterMaker2.internal.algorithms.FuzzyNodeCluster)

Example 47 with Matrix

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

the class CyColtMatrix method getDistanceMatrix.

public CyMatrix getDistanceMatrix(DistanceMetric metric) {
    CyColtMatrix dist = new CyColtMatrix(network, nRows, nRows);
    if (rowNodes != null) {
        dist.rowNodes = Arrays.copyOf(rowNodes, nRows);
        dist.columnNodes = Arrays.copyOf(rowNodes, nRows);
    }
    Matrix cMatrix = super.getDistanceMatrix(metric);
    return dist.copy(cMatrix);
}
Also used : Matrix(edu.ucsf.rbvi.clusterMaker2.internal.api.Matrix) CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 48 with Matrix

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

the class CyMatrixFactory method makeTypedMatrix.

private static CyMatrix makeTypedMatrix(CyNetwork network, int rows, int columns, boolean transpose, MatrixType type) {
    int nrows = rows;
    int ncolumns = columns;
    if (transpose) {
        nrows = columns;
        ncolumns = rows;
    }
    CyMatrix matrix = null;
    switch(type) {
        case SIMPLE:
            matrix = new CySimpleMatrix(network, nrows, ncolumns);
            break;
        // For now, we use Colt for both large and sparse matrices
        case COLT:
        case SPARSE:
            matrix = new CyColtMatrix(network, nrows, ncolumns);
            break;
        case LARGE:
        case OJALGO:
            matrix = new CyOjAlgoMatrix(network, nrows, ncolumns);
            break;
    }
    matrix.setTransposed(transpose);
    return matrix;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix)

Example 49 with Matrix

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

the class CyMatrixFactory method makeLargeMatrix.

/**
 * Create a large, possibly sparse matrix populated with data from
 * the indicated edge attribute
 *
 * @param network the network that will be the source of the data
 * @param edgeAttribute the edge attribute to pull the data from
 * @param selectedOnly only include selected edges
 * @param converter the edge weight converter to use
 * @param unDirected if true, the edges are undirected
 * @param cutOff the minimum edge value to consider
 * @return the resulting matrix
 */
public static CyMatrix makeLargeMatrix(CyNetwork network, String edgeAttribute, boolean selectedOnly, EdgeWeightConverter converter, boolean unDirected, double cutOff) {
    List<CyNode> nodes;
    List<CyEdge> edges;
    double maxAttribute = Double.MIN_VALUE;
    double minAttribute = Double.MAX_VALUE;
    if (!selectedOnly) {
        nodes = network.getNodeList();
        edges = network.getEdgeList();
    } else {
        nodes = new ArrayList<CyNode>();
        edges = new ArrayList<CyEdge>();
        nodes.addAll(CyTableUtil.getNodesInState(network, CyNetwork.SELECTED, true));
        edges.addAll(ModelUtils.getConnectingEdges(network, nodes));
    }
    CyMatrix matrix = makeTypedMatrix(network, nodes.size(), nodes.size(), false, MatrixType.LARGE);
    matrix.setRowNodes(nodes);
    matrix.setColumnNodes(nodes);
    Map<CyNode, Integer> nodeMap = new HashMap<CyNode, Integer>(nodes.size());
    for (int row = 0; row < nodes.size(); row++) {
        CyNode node = nodes.get(row);
        nodeMap.put(node, row);
        matrix.setRowLabel(row, ModelUtils.getNodeName(network, node));
        matrix.setColumnLabel(row, ModelUtils.getNodeName(network, node));
    }
    matrix.setSymmetrical(unDirected);
    CyTable edgeAttributes = network.getDefaultEdgeTable();
    // First, we need the min and max values for our converter
    if (edgeAttributes.getColumn(edgeAttribute) == null) {
        minAttribute = 1.0;
        maxAttribute = 1.0;
    } else {
        for (CyEdge edge : edges) {
            if (network.getRow(edge).getRaw(edgeAttribute) == null)
                continue;
            double edgeWeight = ModelUtils.getNumericValue(network, edge, edgeAttribute);
            if (edgeWeight < cutOff)
                continue;
            minAttribute = Math.min(minAttribute, edgeWeight);
            maxAttribute = Math.max(maxAttribute, edgeWeight);
        }
    }
    for (CyEdge edge : edges) {
        double value;
        if (minAttribute == 1.0 && maxAttribute == 1.0) {
            value = 1.0;
        } else {
            Double val = ModelUtils.getNumericValue(network, edge, edgeAttribute);
            if (val == null)
                continue;
            value = val.doubleValue();
        }
        double weight = converter.convert(value, minAttribute, maxAttribute);
        if (weight < cutOff)
            continue;
        int sourceIndex = nodeMap.get(edge.getSource());
        int targetIndex = nodeMap.get(edge.getTarget());
        matrix.setValue(targetIndex, sourceIndex, weight);
        // TODO: should we consider maybe doing this on the getValue side?
        if (unDirected)
            matrix.setValue(sourceIndex, targetIndex, weight);
    }
    // System.out.println("distance matrix: "+matrix.printMatrix());
    return matrix;
}
Also used : CyMatrix(edu.ucsf.rbvi.clusterMaker2.internal.api.CyMatrix) HashMap(java.util.HashMap) CyEdge(org.cytoscape.model.CyEdge) CyTable(org.cytoscape.model.CyTable) CyNode(org.cytoscape.model.CyNode)

Example 50 with Matrix

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

the class CyOjAlgoMatrix method getDistanceMatrix.

public CyMatrix getDistanceMatrix(DistanceMetric metric) {
    CyOjAlgoMatrix dist = new CyOjAlgoMatrix(network, nRows, nRows);
    if (rowNodes != null) {
        dist.rowNodes = Arrays.copyOf(rowNodes, nRows);
        dist.columnNodes = Arrays.copyOf(rowNodes, nRows);
    }
    Matrix cMatrix = super.getDistanceMatrix(metric);
    return dist.copy(cMatrix);
}
Also used : 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