/*
 * SimilarityLoader.java
 *
 * Created on February 27, 2007, 12:31 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package medusa.spectral;

import cern.colt.matrix.DoubleMatrix2D;
import cern.colt.matrix.impl.SparseDoubleMatrix2D;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import medusa.graph.Edge;
import medusa.graph.Graph;
import medusa.graph.Node;

/**
 *
 * @author shooper
 */
public class SparseSimilarityLoader {

    /** Creates a new instance of SimilarityLoader */
    public SparseSimilarityLoader() {
    }

    public static SparseSimilarityCollection GraphToCollection(Graph g)
            throws IOException {
        // load a tab-delimited dile of name1, name2 and cons score


        // first loop through and build the hashmap
        // we need a map of names as keys
        HashMap tempMap = new HashMap();
   
        Integer index = 0;
        for(Iterator i=g.nodesIterator(); i.hasNext();){
   
            Node n = i.next();

            if (!tempMap.containsKey(n.getLabel())) {
                tempMap.put(n.getLabel(), index++);
            }
            
        }

        //DoubleMatrix2D matrix;
        // map built. restart the buffered reader
        
        //System.out.println("data matrix "+tempMap.size()+" rows");
        // set up the sparse matrix;
        DoubleMatrix2D matrix = new SparseDoubleMatrix2D(tempMap.size(), tempMap.size());

        // double[][] data=new double[tempMap.size()][tempMap.size()];
        int row, col;
        //System.out.println("Reading doubles into data");
        for (Iterator i = g.edgesIterator();i.hasNext();){
            Edge e = i.next();
                // get the coords for the keys
                row = tempMap.get(e.getFromName()).intValue();
                col = tempMap.get(e.getToName()).intValue();
                // set the value in the matrix
                //data[row][col]=Double.parseDouble(result[2]);
                //data[col][row]=Double.parseDouble(result[2]);
                matrix.setQuick(row, col, e.getConf());
                matrix.setQuick(col, row, e.getConf());
            
        }
        //matrix=new SparseDoubleMatrix2D(data);
        //set diagonal to 100
        for (int i = 0; i < matrix.rows(); i++) {
            matrix.setQuick(i, i, 100.0);
        }
        //System.out.println("Create new matrix m");
        //Matrix m = new Matrix(data);
        //m.print(3,3);
        // now reverse the hashmap
        // new hash map
        //System.out.println("Reversing hashmap");
        HashMap names = new HashMap();
        Set keyset = tempMap.keySet();
        Iterator i;
        String key;
        for (i = keyset.iterator(); i.hasNext();) {
            key = i.next();
            names.put(tempMap.get(key), key);
        }
        
        SparseSimilarityCollection sc = new SparseSimilarityCollection(matrix, names);
        return sc;
    }
}
    


 

Ads help maintain this website.