Skip to content

Commit 5f11486

Browse files
authored
Merge pull request #5333 from vnickolov/leiden-mutate-proc
leiden mutate proc
2 parents ff859be + 90a37bf commit 5f11486

File tree

10 files changed

+405
-9
lines changed

10 files changed

+405
-9
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.leiden;
21+
22+
import org.neo4j.gds.annotation.Configuration;
23+
import org.neo4j.gds.config.MutatePropertyConfig;
24+
import org.neo4j.gds.core.CypherMapWrapper;
25+
26+
@Configuration
27+
public interface LeidenMutateConfig extends LeidenBaseConfig, MutatePropertyConfig {
28+
29+
static LeidenMutateConfig of(CypherMapWrapper userInput) {
30+
return new LeidenMutateConfigImpl(userInput);
31+
}
32+
33+
}

algo/src/main/java/org/neo4j/gds/leiden/LeidenStatsConfig.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@
2121

2222

2323
import org.neo4j.gds.annotation.Configuration;
24-
import org.neo4j.gds.annotation.ValueClass;
2524
import org.neo4j.gds.core.CypherMapWrapper;
2625

27-
@ValueClass
2826
@Configuration
29-
@SuppressWarnings("immutables:subtype")
3027
public interface LeidenStatsConfig extends LeidenBaseConfig {
3128
static LeidenStatsConfig of(CypherMapWrapper userInput) {
3229
return new LeidenStatsConfigImpl(userInput);

algo/src/main/java/org/neo4j/gds/leiden/LeidenStreamConfig.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@
2121

2222

2323
import org.neo4j.gds.annotation.Configuration;
24-
import org.neo4j.gds.annotation.ValueClass;
2524
import org.neo4j.gds.core.CypherMapWrapper;
2625

27-
@ValueClass
2826
@Configuration
29-
@SuppressWarnings("immutables:subtype")
3027
public interface LeidenStreamConfig extends LeidenBaseConfig {
3128
static LeidenStreamConfig of(CypherMapWrapper userInput) {
3229
return new LeidenStreamConfigImpl(userInput);

doc/asciidoc/operations-reference/appendix-a-graph-algos.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ include::../algorithms/algorithm-tiers.adoc[]
309309
| `gds.alpha.kmeans.mutate`
310310
| `gds.alpha.kmeans.stats`
311311
| `gds.alpha.kmeans.stream`
312-
.2+<.^| Leiden
312+
.3+<.^| Leiden
313+
| `gds.alpha.leiden.mutate`
313314
| `gds.alpha.leiden.stats`
314315
| `gds.alpha.leiden.stream`
315316
|===

open-packaging/src/test/java/org/neo4j/gds/OpenGdsProcedureSmokeTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class OpenGdsProcedureSmokeTest extends BaseProcTest {
6262
"gds.alpha.closeness.harmonic.write",
6363
"gds.alpha.closeness.harmonic.stream",
6464

65+
"gds.alpha.leiden.mutate",
6566
"gds.alpha.leiden.stats",
6667
"gds.alpha.leiden.stream",
6768

@@ -458,7 +459,7 @@ void countShouldMatch() {
458459
);
459460

460461
// If you find yourself updating this count, please also update the count in SmokeTest.kt
461-
int expectedCount = 316;
462+
int expectedCount = 317;
462463
assertEquals(
463464
expectedCount,
464465
registeredProcedures.size(),
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.leiden;
21+
22+
import org.neo4j.gds.BaseProc;
23+
import org.neo4j.gds.executor.ProcedureExecutor;
24+
import org.neo4j.procedure.Description;
25+
import org.neo4j.procedure.Name;
26+
import org.neo4j.procedure.Procedure;
27+
28+
import java.util.Map;
29+
import java.util.stream.Stream;
30+
31+
import static org.neo4j.gds.leiden.LeidenStreamProc.DESCRIPTION;
32+
import static org.neo4j.procedure.Mode.READ;
33+
34+
public class LeidenMutateProc extends BaseProc {
35+
36+
@Procedure(value = "gds.alpha.leiden.mutate", mode = READ)
37+
@Description(DESCRIPTION)
38+
public Stream<MutateResult> mutate(
39+
@Name(value = "graphName") String graphName,
40+
@Name(value = "configuration", defaultValue = "{}") Map<String, Object> configuration
41+
) {
42+
return new ProcedureExecutor<>(
43+
new LeidenMutateSpec(),
44+
executionContext()
45+
).compute(graphName, configuration, true, true);
46+
}
47+
48+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.leiden;
21+
22+
import org.jetbrains.annotations.NotNull;
23+
import org.neo4j.gds.MutatePropertyComputationResultConsumer;
24+
import org.neo4j.gds.core.write.ImmutableNodeProperty;
25+
import org.neo4j.gds.executor.AlgorithmSpec;
26+
import org.neo4j.gds.executor.ComputationResult;
27+
import org.neo4j.gds.executor.ComputationResultConsumer;
28+
import org.neo4j.gds.executor.ExecutionContext;
29+
import org.neo4j.gds.executor.ExecutionMode;
30+
import org.neo4j.gds.executor.GdsCallable;
31+
import org.neo4j.gds.executor.NewConfigFunction;
32+
import org.neo4j.gds.result.AbstractResultBuilder;
33+
34+
import java.util.List;
35+
import java.util.stream.Stream;
36+
37+
import static org.neo4j.gds.leiden.LeidenStreamProc.DESCRIPTION;
38+
39+
40+
@GdsCallable(name = "gds.alpha.leiden.mutate", description = DESCRIPTION, executionMode = ExecutionMode.MUTATE_NODE_PROPERTY)
41+
public class LeidenMutateSpec implements AlgorithmSpec<Leiden, LeidenResult, LeidenMutateConfig, Stream<MutateResult>, LeidenAlgorithmFactory<LeidenMutateConfig>> {
42+
@Override
43+
public String name() {
44+
return "LeidenMutate";
45+
}
46+
47+
@Override
48+
public LeidenAlgorithmFactory<LeidenMutateConfig> algorithmFactory() {
49+
return new LeidenAlgorithmFactory<>();
50+
}
51+
52+
@Override
53+
public NewConfigFunction<LeidenMutateConfig> newConfigFunction() {
54+
return (__, config) -> LeidenMutateConfig.of(config);
55+
}
56+
57+
@Override
58+
public ComputationResultConsumer<Leiden, LeidenResult, LeidenMutateConfig, Stream<MutateResult>> computationResultConsumer() {
59+
MutatePropertyComputationResultConsumer.MutateNodePropertyListFunction<Leiden, LeidenResult, LeidenMutateConfig> mutateConfigNodePropertyListFunction =
60+
computationResult -> List.of(ImmutableNodeProperty.of(
61+
computationResult.config().mutateProperty(),
62+
computationResult.result().communities().asNodeProperties()
63+
));
64+
return new MutatePropertyComputationResultConsumer<>(
65+
mutateConfigNodePropertyListFunction,
66+
this::resultBuilder
67+
);
68+
}
69+
70+
@NotNull
71+
private AbstractResultBuilder<MutateResult> resultBuilder(
72+
ComputationResult<Leiden, LeidenResult, LeidenMutateConfig> computationResult,
73+
ExecutionContext executionContext
74+
) {
75+
var leidenResult = computationResult.result();
76+
return new MutateResult.Builder(executionContext.callContext(), computationResult.config().concurrency())
77+
.withLevels(leidenResult.ranLevels())
78+
.withDidConverge(leidenResult.didConverge())
79+
.withCommunityFunction(leidenResult.communitiesFunction())
80+
.withConfig(computationResult.config());
81+
}
82+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.leiden;
21+
22+
import org.jetbrains.annotations.Nullable;
23+
import org.neo4j.gds.result.AbstractCommunityResultBuilder;
24+
import org.neo4j.internal.kernel.api.procs.ProcedureCallContext;
25+
26+
import java.util.Map;
27+
28+
public final class MutateResult extends StatsResult {
29+
30+
public final long mutateMillis;
31+
public final long nodePropertiesWritten;
32+
33+
private MutateResult(
34+
long ranLevels,
35+
boolean didConverge,
36+
long nodeCount,
37+
long communityCount,
38+
39+
long preProcessingMillis,
40+
long computeMillis,
41+
long postProcessingMillis,
42+
long mutateMillis,
43+
long nodePropertiesWritten,
44+
@Nullable Map<String, Object> communityDistribution,
45+
Map<String, Object> configuration
46+
) {
47+
super(
48+
ranLevels,
49+
didConverge,
50+
nodeCount,
51+
communityCount,
52+
communityDistribution,
53+
preProcessingMillis,
54+
computeMillis,
55+
postProcessingMillis,
56+
configuration
57+
);
58+
this.mutateMillis = mutateMillis;
59+
this.nodePropertiesWritten = nodePropertiesWritten;
60+
}
61+
62+
static class Builder extends AbstractCommunityResultBuilder<MutateResult> {
63+
64+
long levels = -1;
65+
boolean didConverge = false;
66+
67+
Builder(
68+
ProcedureCallContext context,
69+
int concurrency
70+
) {
71+
super(context, concurrency);
72+
}
73+
74+
Builder withLevels(long levels) {
75+
this.levels = levels;
76+
return this;
77+
}
78+
79+
Builder withDidConverge(boolean didConverge) {
80+
this.didConverge = didConverge;
81+
return this;
82+
}
83+
84+
@Override
85+
protected MutateResult buildResult() {
86+
return new MutateResult(
87+
levels,
88+
didConverge,
89+
nodeCount,
90+
maybeCommunityCount.orElse(0L),
91+
preProcessingMillis,
92+
computeMillis,
93+
postProcessingDuration,
94+
mutateMillis,
95+
nodePropertiesWritten,
96+
communityHistogramOrNull(),
97+
config.toMap()
98+
);
99+
}
100+
}
101+
102+
}

proc/community/src/main/java/org/neo4j/gds/leiden/StatsResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class StatsResult extends StandardStatsResult {
3232
public final long communityCount;
3333
public final Map<String, Object> communityDistribution;
3434

35-
private StatsResult(
35+
StatsResult(
3636
long ranLevels,
3737
boolean didConverge,
3838
long nodeCount,

0 commit comments

Comments
 (0)