Changeset 36 for branches


Ignore:
Timestamp:
Jan 6, 2011, 9:41:42 AM (14 years ago)
Author:
toshi
Message:

ultimo commit

Location:
branches/localSpectral
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • branches/localSpectral/nbproject/project.properties

    r34 r36  
    4141    ${file.reference.jung-api-2.0.1.jar}:\
    4242    ${file.reference.jung-visualization-2.0.1.jar}:\
    43     ${file.reference.jung-graph-impl-2.0.1.jar}:\
    4443    ${file.reference.jung-io-2.0.1.jar}:\
    45     ${file.reference.jung-jai-2.0.1.jar}
     44    ${file.reference.jung-jai-2.0.1.jar}:\
     45    ${file.reference.jung-graph-impl-2.0.1.jar}
    4646# Space-separated list of extra javac options
    4747javac.compilerargs=
     
    7676    ${javac.classpath}:\
    7777    ${build.classes.dir}
     78run.jvmargs=-Xms1500M -Xmx1500M
    7879run.test.classpath=\
    7980    ${javac.test.classpath}:\
  • branches/localSpectral/src/clustering/GraphClusterer.java

    r35 r36  
    66import edu.uci.ics.jung.graph.Graph;
    77import edu.uci.ics.jung.graph.SparseGraph;
     8import edu.uci.ics.jung.graph.util.Pair;
    89import java.awt.event.ActionEvent;
    910import java.awt.event.ActionListener;
     11import java.util.ArrayList;
    1012import java.util.Collection;
    1113import java.util.List;
     14import java.util.Scanner;
    1215import view.Viewer;
    1316
     
    1720    private int currentElement;
    1821    private Viewer viewGraph;
    19     private Graph<String, Edge<String>> graph;
    20     private boolean graph_output;
     22    private Graph<String, Integer> graph;
     23    private boolean main_graph_output;
     24    private boolean cluster_graph_output;
    2125    private boolean isGraphDirected;
    2226    private int minvolume;
    23     LocalSpectral<String,Edge<String>> clusterer;
     27    LocalSpectral<String,String> clusterer;
    2428
    25     public GraphClusterer(String path, boolean isGraphDirected,int min_volume) {
     29    public GraphClusterer(String path, boolean isGraphDirected,int min_volume, boolean graph_output) {
    2630        global_ranking = null;
    2731        this.currentElement = 0;
    2832        GraphBuilder builder = new GraphBuilder(isGraphDirected);
    29         builder.buildGraphFromARFF(path, 1000000);
     33        builder.buildGraphFromCVS(path, 1);
    3034        graph = builder.getGraph();
    31         viewGraph = new Viewer(graph, this);
    32         graph_output = true;
     35        if(graph_output){
     36            viewGraph = new Viewer(graph, this);
     37            this.main_graph_output = true;
     38        }
     39        else
     40            this.main_graph_output = false;
    3341        this.isGraphDirected = isGraphDirected;
    3442        this.minvolume = min_volume;
     43        System.out.println("Graph data: nodes "+graph.getVertexCount()+" edges "+graph.getEdgeCount());
     44        System.out.flush();
     45        //System.out.println("What is the min cluster volume? ");
     46        //Scanner in = new Scanner(System.in);
     47        //this.minvolume = in.nextInt();
    3548        this.clusterer = new LocalSpectral(graph);
    3649    }
    3750
    3851    public void actionPerformed(ActionEvent ae) {
    39         System.out.println("Clicked!");
    4052        this.visualNextCluster();
    4153    }
    4254
    43     public void clusterize(boolean graph_output){
     55    public void clusterize(boolean main_graph_output, boolean cluster_graph_output){
    4456
    4557       global_ranking = clusterer.getGlobalRank();
    46        this.graph_output = graph_output;
     58       this.main_graph_output = main_graph_output;
     59       this.cluster_graph_output = cluster_graph_output;
    4760
    4861       System.out.println("GLOBAL RANKING");
     
    5063           System.out.println(v.toString());
    5164       
    52        if (graph_output)
    53             viewGraph.viewGraphRank(global_ranking,null);
     65       if (main_graph_output){
     66           viewGraph.setText("Nodes: "+graph.getVertexCount() + "\nEdges: "+graph.getEdgeCount());
     67           viewGraph.viewGraphRank(global_ranking,null);
     68       }
     69       else{
     70           for(int i=0; i< this.global_ranking.size(); i++)
     71               visualNextCluster();
     72       }
     73    }
     74
     75    public float getConductance(Graph<String,Integer> graph, List<String> cluster){
     76        int volume_graph = 2 * graph.getEdgeCount();
     77        int volume_cluster = 0;
     78        int edge_boundary = 0;
     79        for(String vertex : cluster){
     80            volume_cluster += graph.getInEdges(vertex).size();
     81            for(Integer e : graph.getOutEdges(vertex)){
     82                String opposite = graph.getOpposite(vertex, e);
     83                if(!cluster.contains(opposite))
     84                    edge_boundary++;
     85            }
     86
     87        }
     88        if(volume_cluster > 0){
     89           double volume = volume_cluster;
     90           if (volume_cluster > (volume_graph - volume_cluster))
     91                volume = volume_graph - volume_cluster;
     92           double conductance = edge_boundary / volume;
     93           return (float) conductance;
     94        }
     95        else
     96            return 0;
     97
    5498    }
    5599
     
    57101       if(this.currentElement > global_ranking.size()-1)
    58102           return;
    59        
    60        List<String> cut = clusterer.clusterPageRankPriors(global_ranking.get(currentElement).getVertex(),this.minvolume);
     103
     104       List<String> seed = new ArrayList<String>();
     105       seed.add(global_ranking.get(currentElement).getVertex());
     106       List<String> cut = clusterer.clusterPageRankPriors(seed,this.minvolume);
    61107       Graph cut_graph;
    62108       if (isGraphDirected)
     
    65111            cut_graph = new SparseGraph<String, Edge<String>>();
    66112       for(String vertex : cut){
    67            Collection<Edge<String>> out_edges = graph.getOutEdges(vertex);
    68            for(Edge<String> edge : out_edges){
     113           Collection<Integer> out_edges = graph.getOutEdges(vertex);
     114           for(Integer edge : out_edges){
    69115               String out_node = graph.getOpposite(vertex, edge);
    70116               if (cut.contains(out_node)){
    71                    cut_graph.addEdge(edge, edge.getVertex1(),edge.getVertex2());
     117                   Pair<String> edge_nodes = graph.getEndpoints(edge);
     118                   cut_graph.addEdge(edge,edge_nodes.getFirst() ,edge_nodes.getSecond());
    72119               }
    73120           }
    74121       }
    75        viewGraph.setGraph(cut_graph);
    76        float vertex_rank = (float) global_ranking.get(this.currentElement).getScore();
    77        viewGraph.setText("Page Rank Value: "+vertex_rank);
    78        if (graph_output)
    79             viewGraph.viewGraphRank(global_ranking, global_ranking.get(this.currentElement).getVertex());
     122       if (cluster_graph_output){
     123           viewGraph.setGraph(cut_graph);
     124           float vertex_rank = (float) global_ranking.get(this.currentElement).getScore();
     125           viewGraph.setText("Nodes: "+cut_graph.getVertexCount() + "\nEdges: "+cut_graph.getEdgeCount()
     126                   + "\nPage Rank Value: "+vertex_rank + "\nConductance: "+this.getConductance(graph, cut) + "\nSeed Element: "+global_ranking.get(this.currentElement).getVertex());
     127           //viewGraph.viewClusterRankedInGraph(global_ranking, global_ranking.get(this.currentElement).getVertex(),cut);
     128           viewGraph.viewGraphRank(global_ranking, global_ranking.get(this.currentElement).getVertex());
     129       }
    80130
    81131       this.currentElement++;
  • branches/localSpectral/src/clustering/LocalSpectral.java

    r34 r36  
    4949        rank.evaluate();
    5050        Collection<V> vertexs = graph.getVertices();
    51         Iterator<?> vertexsIterator = vertexs.iterator();
     51        Iterator<V> vertexsIterator = vertexs.iterator();
    5252        ArrayList<VertexScore<V>> vertexsScore = new ArrayList<VertexScore<V>>();
    5353        while(vertexsIterator.hasNext()){
    54             V vertex = (V) vertexsIterator.next();
     54            V vertex = vertexsIterator.next();
    5555            Double score = (Double) rank.getVertexScore(vertex);
    56             VertexScore<V> vertexscore = new VertexScore<V>(vertex,score);
     56            VertexScore<V> vertexscore = new VertexScore<V>(vertex,score.floatValue());
    5757            vertexsScore.add(vertexscore);
    5858        }
     
    6262    }
    6363   
    64     public List<V> clusterPageRankPriors(V seed, double min_volume){
     64    public List<V> clusterPageRankPriors(List<V> seed, double min_volume){
    6565        PageRankWithPriors rank;
    6666        if(seed != null){
    67             Transformer transf = new SeedTransformer(seed);
    68             rank = new PageRankWithPriors(graph, transf, alpha);
     67            if(seed.size() == 1){
     68                Transformer transf = new SeedTransformer(seed.get(0));
     69                rank = new PageRankWithPriors(graph, transf, alpha);
     70            }
     71            else{
     72                Transformer transf = new ListSeedTransformer(seed);
     73                rank = new PageRankWithPriors(graph, transf, alpha);
     74            }
     75
    6976        }
    7077        else{
     
    7986            V vertex = (V) vertexsIterator.next();
    8087            Double score = (Double) rank.getVertexScore(vertex);
    81             int degree = graph.getIncidentEdges(vertex).size();
    82             VertexScore<V> vertexscore = new VertexScore<V>(vertex,score/degree);
     88            int degree = graph.inDegree(vertex);
     89            VertexScore<V> vertexscore = new VertexScore<V>(vertex,score.floatValue()/degree);
    8390            vertexsScore.add(vertexscore);
    8491        }
     
    103110            int edge_boundary = 0;
    104111            for(int j=0; j<i; j++){
    105                 volume_subset += graph.getIncidentEdges(vertexsScore.get(j).getVertex()).size();
     112                volume_subset += graph.inDegree(vertexsScore.get(j).getVertex());
    106113                for(E out_edge : graph.getOutEdges(vertexsScore.get(j).getVertex())){
    107114                    V opposite = graph.getOpposite(vertexsScore.get(j).getVertex(), out_edge);
     
    117124
    118125                if ((volume_subset > min_volume) && (volume_subset < max_volume)){
    119                 //if (volume_subset < min_volume){
    120126                    if (conductance < min_conductance_subset){
    121127                        min_conductance_subset = conductance;
     
    143149    }
    144150
     151
    145152}
  • branches/localSpectral/src/clustering/VertexScore.java

    r27 r36  
    55
    66    private V vertex;
    7     private double score;
     7    private float score;
    88
    9     public VertexScore(V vertex, double score) {
     9    public VertexScore(V vertex, float score) {
    1010        this.vertex = vertex;
    1111        this.score = score;
     
    1616    }
    1717
    18     public void setScore(double score) {
     18    public void setScore(float score) {
    1919        this.score = score;
    2020    }
  • branches/localSpectral/src/data/GraphBuilder.java

    r34 r36  
    22package data;
    33
     4import GraphType.DirectedDenseGraph;
    45import clustering.Edge;
    5 import clustering.VertexString;
    66import edu.uci.ics.jung.graph.DirectedSparseGraph;
    77import edu.uci.ics.jung.graph.Graph;
    88import edu.uci.ics.jung.graph.SparseGraph;
    9 import java.io.BufferedInputStream;
    109import java.io.BufferedReader;
    1110import java.io.DataInputStream;
     
    2120public class GraphBuilder {
    2221
    23     Graph<String,Edge<String>> graph;
     22    Graph<String,Integer> graph;
    2423
    2524    public GraphBuilder(boolean directed){
    2625        if (directed)
    27            graph = new DirectedSparseGraph<String, Edge<String>>();
     26            graph = new DirectedDenseGraph<String, Integer>();
     27           //graph = new DirectedSparseGraph<String, Integer>();
    2828
    2929        else
    30             graph = new SparseGraph<String, Edge<String>>();
     30            graph = new SparseGraph<String, Integer>();
    3131    }
    3232
    33     public Graph<String, Edge<String>> getGraph() {
     33    public Graph<String, Integer> getGraph() {
    3434        return graph;
    3535    }
    3636
    37     public void buildGraphFromARFF(String path, int maxreadline){
     37    public void buildGraphFromCVS(String path, int maxreadline){
    3838        try {
    3939            FileInputStream fstream = new FileInputStream(path);
     
    4141            BufferedReader br = new BufferedReader(new InputStreamReader(in));
    4242
    43             Set<String> vertex = new HashSet<String>();
    44 
    4543            String read;
    4644            int edge=0;
    47             int count=0;
    48             while(((read = br.readLine()) != null) && (count < maxreadline)){
    49                 count++;
    50                 if(!(read.contains("@DATA") || read.contains("@RELATION") || read.contains("@ATTRIBUTE") || read.trim().isEmpty())){
    51                     String[] splitted = read.trim().split(",");
    52                     vertex.addAll(Arrays.asList(splitted));
    53                     graph.addEdge(new Edge<String>(splitted[0], splitted[1]), splitted[0], splitted[1]);
     45            String[] splitted;
     46            while((read = br.readLine()) != null){
     47                if(!read.trim().isEmpty()){
     48                    splitted = read.trim().split(",");
     49                    graph.addEdge(edge, splitted[0], splitted[1]);
    5450                    edge++;
    5551                }
  • branches/localSpectral/src/jung/Main.java

    r34 r36  
    33
    44import clustering.GraphClusterer;
     5import data.GraphBuilder;
     6import view.GraphToGraphviz;
    57
    68
     
    1012    public static void main(String[] args) {
    1113
    12         String path_simply_net = "/home/luke/Desktop/reteSemplice.txt";
    13         String path_mike_hiddent = "/home/luke/Desktop/reteUtentiMikeHidden.arff";
    14         String karate_club = "/home/luke/Desktop/karate.csv";
    15         String les_miserables = "/home/luke/Desktop/lesmis.csv";
    16         String power_grids = "/home/luke/Desktop/power.csv";
    17         String pol_blogs = "/home/luke/Desktop/polblogs.csv";
    18         String dolphins = "/home/luke/Desktop/dolphins.csv";
    19         String erdos_co_authors = "/home/luke/Desktop/erdos.csv";
    20         GraphClusterer cl = new GraphClusterer(dolphins,false,60);
    21         cl.clusterize(true);
     14        String maindir = "/home/luke/Desktop/nets/";
     15        String path_simply_net = maindir+"reteSemplice.txt";
     16        String path_mike_hidden = maindir+"reteUtentiMikeHidden.arff";
     17        String path_mike = maindir+"reteUtentiMike.arff";
     18        String karate_club = maindir+"karate.csv";
     19        String les_miserables = maindir+"lesmis.csv";
     20        String power_grids = maindir+"power.csv";
     21        String pol_blogs = maindir+"polblogs.csv";
     22        String dolphins = maindir+"dolphins.csv";
     23        String erdos_co_authors = maindir+"erdos.csv";
     24        String net_science = maindir+"netscience.csv";
     25        String epinions = maindir + "soc-Epinions1.csv";
     26        String friend_feed = maindir + "ffeed.csv";
     27        boolean isDirected = true;
     28        boolean mainGraphOutput = false;
     29        boolean clusterGraphOutput = false;
     30        GraphClusterer cl = new GraphClusterer(friend_feed,isDirected,600000, mainGraphOutput);
     31        cl.clusterize(mainGraphOutput,clusterGraphOutput);
    2232
    2333    }
  • branches/localSpectral/src/view/VertexPaintRankTransformer.java

    r34 r36  
    1414    List<VertexScore<V>> pagerank;
    1515    V seed_node;
     16    List<V> cluster;
    1617
    1718    public VertexPaintRankTransformer(List<VertexScore<V>> ranking, V seed_node){
    1819        this.pagerank = ranking;
    1920        this.seed_node = seed_node;
     21        this.cluster = null;
    2022    }
    2123
     24    public VertexPaintRankTransformer(List<VertexScore<V>> ranking, V seed_node, List<V> cluster){
     25        this.pagerank = ranking;
     26        this.seed_node = seed_node;
     27        this.cluster = cluster;
     28    }
     29   
    2230    public Paint transform(V node) {
    2331
     
    2533            if (seed_node.equals(node)){
    2634                return (Paint) Color.GREEN;
     35            }
     36        }
     37        if(cluster != null){
     38            if (!cluster.contains(node)){
     39                return (Paint) Color.WHITE;
    2740            }
    2841        }
  • branches/localSpectral/src/view/Viewer.java

    r34 r36  
    2424import javax.swing.JPanel;
    2525import javax.swing.JTextArea;
    26 import javax.xml.bind.JAXB;
    2726import org.apache.commons.collections15.Transformer;
    2827
     
    8382    }
    8483
     84    public void viewClusterRankedInGraph(List<VertexScore<V>> pagerank, V seed_node, List<V> cluster){
     85         VertexPaintRankTransformer vertexPaint = new VertexPaintRankTransformer(pagerank,seed_node, cluster);
     86         vv.getRenderContext().setVertexFillPaintTransformer((Transformer<V, Paint>) vertexPaint);
     87
     88         frame.getContentPane().add(vv);
     89         frame.pack();
     90         frame.repaint();
     91         frame.setVisible(true);
     92    }
     93
    8594
    8695    public void viewGraph(List<V> cut){
     
    98107     }
    99108
     109
    100110}
Note: See TracChangeset for help on using the changeset viewer.