|
18 | 18 | */ |
19 | 19 | package org.neo4j.graphalgo; |
20 | 20 |
|
| 21 | +import com.carrotsearch.hppc.LongLongMap; |
| 22 | +import org.HdrHistogram.Histogram; |
21 | 23 | import org.neo4j.graphalgo.api.*; |
22 | 24 | import org.neo4j.graphalgo.core.GraphLoader; |
23 | 25 | import org.neo4j.graphalgo.core.ProcedureConfiguration; |
24 | | -import org.neo4j.graphalgo.core.ProcedureConstants; |
25 | | -import org.neo4j.graphalgo.core.heavyweight.HeavyGraph; |
26 | | -import org.neo4j.graphalgo.core.heavyweight.HeavyGraphFactory; |
27 | 26 | import org.neo4j.graphalgo.core.utils.Pools; |
28 | 27 | import org.neo4j.graphalgo.core.utils.ProgressLogger; |
29 | 28 | import org.neo4j.graphalgo.core.utils.ProgressTimer; |
30 | 29 | import org.neo4j.graphalgo.core.utils.TerminationFlag; |
31 | 30 | import org.neo4j.graphalgo.core.utils.paged.AllocationTracker; |
32 | 31 | import org.neo4j.graphalgo.impl.louvain.*; |
33 | | -import org.neo4j.graphalgo.results.LouvainResult; |
| 32 | +import org.neo4j.graphalgo.results.AbstractCommunityResultBuilder; |
34 | 33 | import org.neo4j.kernel.api.KernelTransaction; |
35 | | -import org.neo4j.kernel.impl.store.PropertyType; |
36 | 34 | import org.neo4j.kernel.internal.GraphDatabaseAPI; |
37 | 35 | import org.neo4j.logging.Log; |
38 | 36 | import org.neo4j.procedure.*; |
39 | 37 |
|
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.List; |
40 | 40 | import java.util.Map; |
41 | 41 | import java.util.stream.Stream; |
42 | 42 |
|
@@ -75,44 +75,45 @@ public Stream<LouvainResult> louvain( |
75 | 75 | .overrideNodeLabelOrQuery(label) |
76 | 76 | .overrideRelationshipTypeOrQuery(relationship); |
77 | 77 |
|
78 | | - LouvainResult.Builder builder = LouvainResult.builder(); |
| 78 | + |
| 79 | + final Builder builder = new Builder(); |
79 | 80 |
|
80 | 81 | final Graph graph; |
81 | 82 | try (ProgressTimer timer = builder.timeLoad()) { |
82 | 83 | graph = graph(label, relationship, configuration); |
83 | 84 | } |
84 | 85 |
|
85 | | - builder.withNodeCount(graph.nodeCount()); |
86 | | - |
87 | 86 | if(graph.nodeCount() == 0) { |
88 | 87 | graph.release(); |
89 | | - return Stream.of(builder.build()); |
| 88 | + return Stream.of(LouvainResult.EMPTY); |
90 | 89 | } |
91 | 90 |
|
92 | 91 | final Louvain louvain = new Louvain(graph, Pools.DEFAULT, configuration.getConcurrency(), AllocationTracker.create()) |
93 | 92 | .withProgressLogger(ProgressLogger.wrap(log, "Louvain")) |
94 | 93 | .withTerminationFlag(TerminationFlag.wrap(transaction)); |
95 | 94 |
|
96 | 95 | // evaluation |
| 96 | + int iterations = configuration.getIterations(10); |
97 | 97 | try (ProgressTimer timer = builder.timeEval()) { |
98 | 98 | if (configuration.getString(DEFAULT_CLUSTER_PROPERTY).isPresent()) { |
99 | 99 | // use predefined clustering |
100 | 100 | final WeightMapping communityMap = ((NodeProperties) graph).nodeProperties(CLUSTERING_IDENTIFIER); |
101 | | - louvain.compute(communityMap, configuration.getIterations(10), configuration.get("innerIterations", 10)); |
| 101 | + louvain.compute(communityMap, iterations, configuration.get("innerIterations", 10)); |
102 | 102 | } else { |
103 | | - louvain.compute(configuration.getIterations(10), configuration.get("innerIterations", 10)); |
| 103 | + louvain.compute(iterations, configuration.get("innerIterations", 10)); |
104 | 104 | } |
105 | | - builder.withIterations(louvain.getLevel()) |
106 | | - .withCommunityCount(louvain.getCommunityCount()) |
107 | | - .withModularities(louvain.getModularities()) |
108 | | - .withFinalModularity(louvain.getFinalModularity()); |
109 | 105 | } |
110 | 106 |
|
111 | 107 | if (configuration.isWriteFlag()) { |
112 | 108 | builder.timeWrite(() -> write(graph, louvain.getDendrogram(), louvain.getCommunityIds(), configuration)); |
113 | 109 | } |
114 | 110 |
|
115 | | - return Stream.of(builder.build()); |
| 111 | + builder.withIterations(louvain.getLevel()); |
| 112 | + builder.withModularities(louvain.getModularities() ); |
| 113 | + builder.withFinalModularity(louvain.getFinalModularity()); |
| 114 | + |
| 115 | + final int[] communityIds = louvain.getCommunityIds(); |
| 116 | + return Stream.of(builder.build(graph.nodeCount(), n -> (long) communityIds[(int) n])); |
116 | 117 | } |
117 | 118 |
|
118 | 119 | @Procedure(value = "algo.louvain.stream") |
@@ -183,4 +184,117 @@ private void write(Graph graph, int[][] allCommunities, int[] finalCommunities, |
183 | 184 | configuration.get(INTERMEDIATE_COMMUNITIES_WRITE_PROPERTY, "communities")) |
184 | 185 | .export(allCommunities, finalCommunities, includeIntermediateCommunities); |
185 | 186 | } |
| 187 | + |
| 188 | + public static class LouvainResult { |
| 189 | + |
| 190 | + public static final LouvainResult EMPTY = new LouvainResult( |
| 191 | + 0, |
| 192 | + 0, |
| 193 | + 0, |
| 194 | + 0, |
| 195 | + 0, |
| 196 | + 0, |
| 197 | + -1, |
| 198 | + -1, |
| 199 | + -1, |
| 200 | + -1, |
| 201 | + -1, |
| 202 | + -1, |
| 203 | + -1, |
| 204 | + -1, |
| 205 | + -1, |
| 206 | + -1, |
| 207 | + 0, |
| 208 | + new double[] {}, -1); |
| 209 | + |
| 210 | + public final long loadMillis; |
| 211 | + public final long computeMillis; |
| 212 | + public final long postProcessingMillis; |
| 213 | + public final long writeMillis; |
| 214 | + public final long nodes; |
| 215 | + public final long communityCount; |
| 216 | + public final long p100; |
| 217 | + public final long p99; |
| 218 | + public final long p95; |
| 219 | + public final long p90; |
| 220 | + public final long p75; |
| 221 | + public final long p50; |
| 222 | + public final long p25; |
| 223 | + public final long p10; |
| 224 | + public final long p05; |
| 225 | + public final long p01; |
| 226 | + public final long iterations; |
| 227 | + public final List<Double> modularities; |
| 228 | + public final double modularity; |
| 229 | + |
| 230 | + public LouvainResult(long loadMillis, long computeMillis, long postProcessingMillis, long writeMillis, long nodes, long communityCount, long p100, long p99, long p95, long p90, long p75, long p50, long p25, long p10, long p05, long p01, long iterations, double[] modularities, double finalModularity) { |
| 231 | + this.loadMillis = loadMillis; |
| 232 | + this.computeMillis = computeMillis; |
| 233 | + this.postProcessingMillis = postProcessingMillis; |
| 234 | + this.writeMillis = writeMillis; |
| 235 | + this.nodes = nodes; |
| 236 | + this.communityCount = communityCount; |
| 237 | + this.p100 = p100; |
| 238 | + this.p99 = p99; |
| 239 | + this.p95 = p95; |
| 240 | + this.p90 = p90; |
| 241 | + this.p75 = p75; |
| 242 | + this.p50 = p50; |
| 243 | + this.p25 = p25; |
| 244 | + this.p10 = p10; |
| 245 | + this.p05 = p05; |
| 246 | + this.p01 = p01; |
| 247 | + this.iterations = iterations; |
| 248 | + this.modularities = new ArrayList<>(modularities.length); |
| 249 | + for (double mod : modularities) this.modularities.add(mod); |
| 250 | + this.modularity = finalModularity; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + public static class Builder extends AbstractCommunityResultBuilder<LouvainResult> { |
| 255 | + |
| 256 | + private long iterations = -1; |
| 257 | + private double[] modularities = new double[] {}; |
| 258 | + private double finalModularity = -1; |
| 259 | + |
| 260 | + public Builder withIterations(long iterations) { |
| 261 | + this.iterations = iterations; |
| 262 | + return this; |
| 263 | + } |
| 264 | + |
| 265 | + @Override |
| 266 | + protected LouvainResult build(long loadMillis, long computeMillis, long writeMillis, long postProcessingMillis, long nodeCount, long communityCount, LongLongMap communitySizeMap, Histogram communityHistogram) { |
| 267 | + return new LouvainResult( |
| 268 | + loadMillis, |
| 269 | + computeMillis, |
| 270 | + postProcessingMillis, |
| 271 | + writeMillis, |
| 272 | + nodeCount, |
| 273 | + communityCount, |
| 274 | + communityHistogram.getValueAtPercentile(100), |
| 275 | + communityHistogram.getValueAtPercentile(99), |
| 276 | + communityHistogram.getValueAtPercentile(95), |
| 277 | + communityHistogram.getValueAtPercentile(90), |
| 278 | + communityHistogram.getValueAtPercentile(75), |
| 279 | + communityHistogram.getValueAtPercentile(50), |
| 280 | + communityHistogram.getValueAtPercentile(25), |
| 281 | + communityHistogram.getValueAtPercentile(10), |
| 282 | + communityHistogram.getValueAtPercentile(5), |
| 283 | + communityHistogram.getValueAtPercentile(1), |
| 284 | + iterations, modularities, finalModularity |
| 285 | + ); |
| 286 | + } |
| 287 | + |
| 288 | + public Builder withModularities(double[] modularities) { |
| 289 | + this.modularities = modularities; |
| 290 | + return this; |
| 291 | + } |
| 292 | + |
| 293 | + public Builder withFinalModularity(double finalModularity) { |
| 294 | + this.finalModularity = finalModularity; |
| 295 | + return null; |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + |
186 | 300 | } |
0 commit comments