Skip to content

Commit e78ff85

Browse files
Merge pull request #5538 from IoannisPanagiotas/port-consecutiveIds-louvain
Fix Louvain bug when consecutiveIds is enabled
2 parents bc68893 + 4459549 commit e78ff85

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

proc/community/src/main/java/org/neo4j/gds/louvain/LouvainStreamProc.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,29 @@ protected Stream<StreamResult> stream(ComputationResult<Louvain, Louvain, Louvai
7979
return runWithExceptionLogging("Graph streaming failed", () -> {
8080
Graph graph = computationResult.graph();
8181

82+
boolean consecutiveIds = computationResult
83+
.config()
84+
.consecutiveIds();
85+
86+
var nodeProperties = (computationResult.isGraphEmpty() || !consecutiveIds) ? null : nodeProperties((computationResult));
87+
8288
return LongStream
8389
.range(0, graph.nodeCount())
8490
.boxed()
8591
.map((nodeId) -> {
8692
boolean includeIntermediateCommunities = computationResult
8793
.config()
8894
.includeIntermediateCommunities();
95+
8996
Louvain louvain = computationResult.result();
9097
long[] communities = includeIntermediateCommunities ? louvain.getCommunities(nodeId) : null;
91-
92-
return new StreamResult(graph.toOriginalNodeId(nodeId), communities, louvain.getCommunity(nodeId));
98+
long communityId = consecutiveIds ? nodeProperties.longValue(
99+
nodeId) : louvain.getCommunity(nodeId);
100+
return new StreamResult(
101+
graph.toOriginalNodeId(nodeId),
102+
communities,
103+
communityId
104+
);
93105
});
94106
});
95107
}

proc/community/src/test/java/org/neo4j/gds/louvain/LouvainStreamProcTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.neo4j.gds.core.CypherMapWrapper;
2727

2828
import java.util.ArrayList;
29+
import java.util.HashSet;
2930
import java.util.List;
3031

3132
import static org.assertj.core.api.Assertions.assertThat;
@@ -58,6 +59,30 @@ void testStream() {
5859
assertCommunities(actualCommunities, RESULT);
5960
}
6061

62+
@Test
63+
void testStreamConsecutiveIds() {
64+
@Language("Cypher") String query = GdsCypher.call("myGraph")
65+
.algo("louvain")
66+
.streamMode()
67+
.addParameter("consecutiveIds", true)
68+
.yields("nodeId", "communityId", "intermediateCommunityIds");
69+
70+
var communityMap = new HashSet<Long>();
71+
72+
List<Long> actualCommunities = new ArrayList<>();
73+
runQueryWithRowConsumer(query, row -> {
74+
int id = row.getNumber("nodeId").intValue();
75+
long community = row.getNumber("communityId").longValue();
76+
communityMap.add(community);
77+
assertNull(row.get("intermediateCommunityIds"));
78+
actualCommunities.add(id, community);
79+
});
80+
assertCommunities(actualCommunities, RESULT);
81+
assertThat(communityMap).hasSize(3).containsExactlyInAnyOrder(0L, 1L, 2L);
82+
83+
}
84+
85+
6186
@Test
6287
void testStreamCommunities() {
6388
@Language("Cypher") String query = GdsCypher.call("myGraph")

0 commit comments

Comments
 (0)