Search in sources :

Example 1 with DelimitedEdgeLineProcessor

use of org.deeplearning4j.graph.data.impl.DelimitedEdgeLineProcessor in project deeplearning4j by deeplearning4j.

the class TestGraphLoading method testGraphLoadingWithVertices.

@Test
public void testGraphLoadingWithVertices() throws IOException {
    ClassPathResource verticesCPR = new ClassPathResource("test_graph_vertices.txt");
    ClassPathResource edgesCPR = new ClassPathResource("test_graph_edges.txt");
    EdgeLineProcessor<String> edgeLineProcessor = new DelimitedEdgeLineProcessor(",", false, "//");
    VertexLoader<String> vertexLoader = new DelimitedVertexLoader(":", "//");
    Graph<String, String> graph = GraphLoader.loadGraph(verticesCPR.getTempFileFromArchive().getAbsolutePath(), edgesCPR.getTempFileFromArchive().getAbsolutePath(), vertexLoader, edgeLineProcessor, false);
    System.out.println(graph);
    for (int i = 0; i < 10; i++) {
        List<Edge<String>> edges = graph.getEdgesOut(i);
        assertEquals(2, edges.size());
        //expect for example 0->1 and 9->0
        Edge<String> first = edges.get(0);
        if (first.getFrom() == i) {
            //undirected edge: i -> i+1 (or 9 -> 0)
            assertEquals(i, first.getFrom());
            assertEquals((i + 1) % 10, first.getTo());
        } else {
            //undirected edge: i-1 -> i (or 9 -> 0)
            assertEquals((i + 10 - 1) % 10, first.getFrom());
            assertEquals(i, first.getTo());
        }
        Edge<String> second = edges.get(1);
        assertNotEquals(first.getFrom(), second.getFrom());
        if (second.getFrom() == i) {
            //undirected edge: i -> i+1 (or 9 -> 0)
            assertEquals(i, second.getFrom());
            assertEquals((i + 1) % 10, second.getTo());
        } else {
            //undirected edge: i-1 -> i (or 9 -> 0)
            assertEquals((i + 10 - 1) % 10, second.getFrom());
            assertEquals(i, second.getTo());
        }
    }
}
Also used : DelimitedEdgeLineProcessor(org.deeplearning4j.graph.data.impl.DelimitedEdgeLineProcessor) Edge(org.deeplearning4j.graph.api.Edge) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) DelimitedVertexLoader(org.deeplearning4j.graph.data.impl.DelimitedVertexLoader) Test(org.junit.Test)

Example 2 with DelimitedEdgeLineProcessor

use of org.deeplearning4j.graph.data.impl.DelimitedEdgeLineProcessor in project deeplearning4j by deeplearning4j.

the class GraphLoader method loadUndirectedGraphEdgeListFile.

/** Simple method for loading an undirected graph, where the graph is represented by a edge list with one edge
     * per line with a delimiter in between<br>
     * This method assumes that all lines in the file are of the form {@code i<delim>j} where i and j are integers
     * in range 0 to numVertices inclusive, and "<delim>" is the user-provided delimiter
     * @param path Path to the edge list file
     * @param numVertices number of vertices in the graph
     * @param allowMultipleEdges If set to false, the graph will not allow multiple edges between any two vertices to exist. However,
     *                           checking for duplicates during graph loading can be costly, so use allowMultipleEdges=true when
     *                           possible.
     * @return graph
     * @throws IOException if file cannot be read
     */
public static Graph<String, String> loadUndirectedGraphEdgeListFile(String path, int numVertices, String delim, boolean allowMultipleEdges) throws IOException {
    Graph<String, String> graph = new Graph<>(numVertices, allowMultipleEdges, new StringVertexFactory());
    EdgeLineProcessor<String> lineProcessor = new DelimitedEdgeLineProcessor(delim, false);
    try (BufferedReader br = new BufferedReader(new FileReader(new File(path)))) {
        String line;
        while ((line = br.readLine()) != null) {
            Edge<String> edge = lineProcessor.processLine(line);
            if (edge != null) {
                graph.addEdge(edge);
            }
        }
    }
    return graph;
}
Also used : Graph(org.deeplearning4j.graph.graph.Graph) DelimitedEdgeLineProcessor(org.deeplearning4j.graph.data.impl.DelimitedEdgeLineProcessor) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) StringVertexFactory(org.deeplearning4j.graph.vertexfactory.StringVertexFactory) File(java.io.File)

Example 3 with DelimitedEdgeLineProcessor

use of org.deeplearning4j.graph.data.impl.DelimitedEdgeLineProcessor in project deeplearning4j by deeplearning4j.

the class TestGraphLoading method testGraphLoading.

@Test
public void testGraphLoading() throws IOException {
    ClassPathResource cpr = new ClassPathResource("simplegraph.txt");
    EdgeLineProcessor<String> edgeLineProcessor = new DelimitedEdgeLineProcessor(",", false, "//");
    VertexFactory<String> vertexFactory = new StringVertexFactory("v_%d");
    Graph<String, String> graph = GraphLoader.loadGraph(cpr.getTempFileFromArchive().getAbsolutePath(), edgeLineProcessor, vertexFactory, 10, false);
    System.out.println(graph);
    for (int i = 0; i < 10; i++) {
        List<Edge<String>> edges = graph.getEdgesOut(i);
        assertEquals(2, edges.size());
        //expect for example 0->1 and 9->0
        Edge<String> first = edges.get(0);
        if (first.getFrom() == i) {
            //undirected edge: i -> i+1 (or 9 -> 0)
            assertEquals(i, first.getFrom());
            assertEquals((i + 1) % 10, first.getTo());
        } else {
            //undirected edge: i-1 -> i (or 9 -> 0)
            assertEquals((i + 10 - 1) % 10, first.getFrom());
            assertEquals(i, first.getTo());
        }
        Edge<String> second = edges.get(1);
        assertNotEquals(first.getFrom(), second.getFrom());
        if (second.getFrom() == i) {
            //undirected edge: i -> i+1 (or 9 -> 0)
            assertEquals(i, second.getFrom());
            assertEquals((i + 1) % 10, second.getTo());
        } else {
            //undirected edge: i-1 -> i (or 9 -> 0)
            assertEquals((i + 10 - 1) % 10, second.getFrom());
            assertEquals(i, second.getTo());
        }
    }
}
Also used : DelimitedEdgeLineProcessor(org.deeplearning4j.graph.data.impl.DelimitedEdgeLineProcessor) StringVertexFactory(org.deeplearning4j.graph.vertexfactory.StringVertexFactory) Edge(org.deeplearning4j.graph.api.Edge) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) Test(org.junit.Test)

Aggregations

DelimitedEdgeLineProcessor (org.deeplearning4j.graph.data.impl.DelimitedEdgeLineProcessor)3 Edge (org.deeplearning4j.graph.api.Edge)2 StringVertexFactory (org.deeplearning4j.graph.vertexfactory.StringVertexFactory)2 Test (org.junit.Test)2 ClassPathResource (org.nd4j.linalg.io.ClassPathResource)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 DelimitedVertexLoader (org.deeplearning4j.graph.data.impl.DelimitedVertexLoader)1 Graph (org.deeplearning4j.graph.graph.Graph)1