Skip to content

Commit

Permalink
make pipelines work for harmonic centrality
Browse files Browse the repository at this point in the history
  • Loading branch information
lassewesth committed May 28, 2024
1 parent f3d58d3 commit 6bbf801
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.neo4j.gds.algorithms.runner.AlgorithmRunner;
import org.neo4j.gds.config.MutateNodePropertyConfig;
import org.neo4j.gds.core.concurrency.DefaultPool;
import org.neo4j.gds.harmonic.HarmonicCentralityMutateConfig;
import org.neo4j.gds.influenceMaximization.CELFNodeProperties;
import org.neo4j.gds.influenceMaximization.InfluenceMaximizationMutateConfig;
import org.neo4j.gds.pagerank.PageRankMutateConfig;
Expand All @@ -54,24 +53,6 @@ public CentralityAlgorithmsMutateBusinessFacade(
this.mutateNodePropertyService = mutateNodePropertyService;
}

public NodePropertyMutateResult<DefaultCentralitySpecificFields> harmonicCentrality(
String graphName,
HarmonicCentralityMutateConfig configuration,
boolean shouldComputeCentralityDistribution
) {
// 1. Run the algorithm and time the execution
var intermediateResult = AlgorithmRunner.runWithTiming(
() -> centralityAlgorithmsFacade.harmonicCentrality(graphName, configuration)
);

return mutateNodeProperty(
intermediateResult.algorithmResult,
configuration,
shouldComputeCentralityDistribution,
intermediateResult.computeMilliseconds
);
}

public NodePropertyMutateResult<PageRankSpecificFields> pageRank(
String graphName,
PageRankMutateConfig configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import org.neo4j.gds.degree.DegreeCentrality;
import org.neo4j.gds.degree.DegreeCentralityConfig;
import org.neo4j.gds.degree.DegreeCentralityResult;
import org.neo4j.gds.harmonic.HarmonicCentrality;
import org.neo4j.gds.harmonic.HarmonicCentralityBaseConfig;
import org.neo4j.gds.harmonic.HarmonicResult;
import org.neo4j.gds.termination.TerminationFlag;

public class CentralityAlgorithms {
Expand Down Expand Up @@ -127,4 +130,18 @@ DegreeCentralityResult degreeCentrality(Graph graph, DegreeCentralityConfig conf

return algorithmMachinery.runAlgorithmsAndManageProgressTracker(algorithm, progressTracker, true);
}

HarmonicResult harmonicCentrality(Graph graph, HarmonicCentralityBaseConfig configuration) {
var task = Tasks.leaf(LabelForProgressTracking.HarmonicCentrality.value);
var progressTracker = progressTrackerCreator.createProgressTracker(configuration, task);

var algorithm = new HarmonicCentrality(
graph,
configuration.concurrency(),
DefaultPool.INSTANCE,
progressTracker
);

return algorithmMachinery.runAlgorithmsAndManageProgressTracker(algorithm, progressTracker, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ public MemoryEstimateResult degreeCentrality(
memoryEstimation
);
}

public MemoryEstimation harmonicCentrality() {
throw new MemoryEstimationNotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
import org.neo4j.gds.closeness.ClosenessCentralityResult;
import org.neo4j.gds.degree.DegreeCentralityMutateConfig;
import org.neo4j.gds.degree.DegreeCentralityResult;
import org.neo4j.gds.harmonic.HarmonicCentralityMutateConfig;
import org.neo4j.gds.harmonic.HarmonicResult;

import java.util.Optional;

import static org.neo4j.gds.applications.algorithms.metadata.LabelForProgressTracking.BetweennessCentrality;
import static org.neo4j.gds.applications.algorithms.metadata.LabelForProgressTracking.ClosenessCentrality;
import static org.neo4j.gds.applications.algorithms.metadata.LabelForProgressTracking.DegreeCentrality;
import static org.neo4j.gds.applications.algorithms.metadata.LabelForProgressTracking.HarmonicCentrality;

public class CentralityAlgorithmsMutateModeBusinessFacade {
private final CentralityAlgorithmsEstimationModeBusinessFacade estimation;
Expand Down Expand Up @@ -116,4 +119,25 @@ public <RESULT> RESULT degreeCentrality(
resultBuilder
);
}

public <RESULT> RESULT harmonicCentrality(
GraphName graphName,
HarmonicCentralityMutateConfig configuration,
ResultBuilder<HarmonicCentralityMutateConfig, HarmonicResult, RESULT, NodePropertiesWritten> resultBuilder
) {
var mutateStep = new HarmonicCentralityMutateStep(
mutateNodeProperty,
configuration
);

return template.processAlgorithm(
graphName,
configuration,
HarmonicCentrality,
estimation::harmonicCentrality,
graph -> algorithms.harmonicCentrality(graph, configuration),
Optional.of(mutateStep),
resultBuilder
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.applications.algorithms.centrality;

import org.neo4j.gds.api.Graph;
import org.neo4j.gds.api.GraphStore;
import org.neo4j.gds.api.ResultStore;
import org.neo4j.gds.applications.algorithms.machinery.MutateOrWriteStep;
import org.neo4j.gds.applications.algorithms.metadata.NodePropertiesWritten;
import org.neo4j.gds.core.utils.progress.JobId;
import org.neo4j.gds.harmonic.HarmonicCentralityMutateConfig;
import org.neo4j.gds.harmonic.HarmonicResult;

class HarmonicCentralityMutateStep implements MutateOrWriteStep<HarmonicResult, NodePropertiesWritten> {
private final MutateNodeProperty mutateNodeProperty;
private final HarmonicCentralityMutateConfig configuration;

HarmonicCentralityMutateStep(
MutateNodeProperty mutateNodeProperty,
HarmonicCentralityMutateConfig configuration
) {
this.mutateNodeProperty = mutateNodeProperty;
this.configuration = configuration;
}

@Override
public NodePropertiesWritten execute(
Graph graph,
GraphStore graphStore,
ResultStore resultStore,
HarmonicResult result,
JobId jobId
) {
return mutateNodeProperty.mutateNodeProperties(
graph,
graphStore,
configuration,
result.nodePropertyValues()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum Algorithm {
Dijkstra,
FilteredKNN,
FilteredNodeSimilarity,
HarmonicCentrality,
KNN,
KSpanningTree,
LongestPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum LabelForProgressTracking {
Dijkstra("Dijkstra"),
FilteredKNN("Filtered K-Nearest Neighbours"),
FilteredNodeSimilarity("Filtered Node Similarity"),
HarmonicCentrality("HarmonicCentrality"),
KNN("K-Nearest Neighbours"),
KSpanningTree("K Spanning Tree"),
LongestPath("LongestPath"),
Expand Down Expand Up @@ -63,6 +64,7 @@ public static LabelForProgressTracking from(Algorithm algorithm) {
case Dijkstra -> Dijkstra;
case FilteredKNN -> FilteredKNN;
case FilteredNodeSimilarity -> FilteredNodeSimilarity;
case HarmonicCentrality -> HarmonicCentrality;
case KNN -> KNN;
case KSpanningTree -> KSpanningTree;
case LongestPath -> LongestPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.neo4j.gds.config.AlgoBaseConfig;
import org.neo4j.gds.core.CypherMapWrapper;
import org.neo4j.gds.degree.DegreeCentralityMutateConfig;
import org.neo4j.gds.harmonic.HarmonicCentralityMutateConfig;
import org.neo4j.gds.paths.astar.config.ShortestPathAStarMutateConfig;
import org.neo4j.gds.paths.bellmanford.BellmanFordMutateConfig;
import org.neo4j.gds.paths.delta.config.AllShortestPathsDeltaMutateConfig;
Expand Down Expand Up @@ -64,6 +65,7 @@ public Function<CypherMapWrapper, AlgoBaseConfig> lookup(Algorithm algorithm) {
case Dijkstra -> ShortestPathDijkstraMutateConfig::of;
case FilteredKNN -> FilteredKnnMutateConfig::of;
case FilteredNodeSimilarity -> FilteredNodeSimilarityMutateConfig::of;
case HarmonicCentrality -> HarmonicCentralityMutateConfig::of;
case KNN -> KnnMutateConfig::of;
case KSpanningTree -> null;
case LongestPath -> null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private static CanonicalProcedureName algorithmToName(Algorithm algorithm) {
case Dijkstra -> CanonicalProcedureName.parse("gds.shortestpath.dijkstra");
case FilteredKNN -> CanonicalProcedureName.parse("gds.knn.filtered");
case FilteredNodeSimilarity -> CanonicalProcedureName.parse("gds.nodesimilarity.filtered");
case HarmonicCentrality -> CanonicalProcedureName.parse("gds.closeness.harmonic.mutate");
case KNN -> CanonicalProcedureName.parse("gds.knn");
case KSpanningTree -> null;
case LongestPath -> null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.neo4j.gds.ml.pipeline.stubs.DepthFirstSearchStub;
import org.neo4j.gds.ml.pipeline.stubs.FilteredKnnStub;
import org.neo4j.gds.ml.pipeline.stubs.FilteredNodeSimilarityStub;
import org.neo4j.gds.ml.pipeline.stubs.HarmonicCentralityStub;
import org.neo4j.gds.ml.pipeline.stubs.KnnStub;
import org.neo4j.gds.ml.pipeline.stubs.NodeSimilarityStub;
import org.neo4j.gds.ml.pipeline.stubs.SinglePairShortestPathAStarStub;
Expand Down Expand Up @@ -63,6 +64,7 @@ Stub get(Algorithm algorithm) {
case Dijkstra -> new SinglePairShortestPathDijkstraStub();
case FilteredKNN -> new FilteredKnnStub();
case FilteredNodeSimilarity -> new FilteredNodeSimilarityStub();
case HarmonicCentrality -> new HarmonicCentralityStub();
case KNN -> new KnnStub();
case KSpanningTree -> null;
case LongestPath -> null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.ml.pipeline.stubs;

import org.neo4j.gds.harmonic.HarmonicCentralityMutateConfig;
import org.neo4j.gds.procedures.algorithms.AlgorithmsProcedureFacade;
import org.neo4j.gds.procedures.algorithms.centrality.CentralityMutateResult;
import org.neo4j.gds.procedures.algorithms.stubs.MutateStub;

public class HarmonicCentralityStub extends AbstractStub<HarmonicCentralityMutateConfig, CentralityMutateResult> {
protected MutateStub<HarmonicCentralityMutateConfig, CentralityMutateResult> stub(AlgorithmsProcedureFacade facade) {
return facade.centrality().harmonicCentralityMutateStub();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public Stream<CentralityMutateResult> mutate(
@Name(value = "graphName") String graphName,
@Name(value = "configuration", defaultValue = "{}") Map<String, Object> configuration
) {
return facade.centrality().harmonicCentralityMutate(graphName, configuration);
return facade.algorithms().centrality().harmonicCentralityMutateStub().execute(graphName, configuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.neo4j.gds.procedures.algorithms.centrality.stubs.BetweennessCentralityMutateStub;
import org.neo4j.gds.procedures.algorithms.centrality.stubs.ClosenessCentralityMutateStub;
import org.neo4j.gds.procedures.algorithms.centrality.stubs.DegreeCentralityMutateStub;
import org.neo4j.gds.procedures.algorithms.centrality.stubs.HarmonicCentralityMutateStub;
import org.neo4j.gds.procedures.algorithms.runners.EstimationModeRunner;
import org.neo4j.gds.procedures.algorithms.runners.StatsModeAlgorithmRunner;
import org.neo4j.gds.procedures.algorithms.runners.StreamModeAlgorithmRunner;
Expand All @@ -54,6 +55,7 @@ public final class CentralityProcedureFacade {
private final ClosenessCentralityMutateStub closenessCentralityMutateStub;
private final DegreeCentralityMutateStub degreeCentralityMutateStub;

private final HarmonicCentralityMutateStub harmonicCentralityMutateStub;
private final ApplicationsFacade applicationsFacade;

private final EstimationModeRunner estimationModeRunner;
Expand All @@ -67,6 +69,7 @@ private CentralityProcedureFacade(
BetweennessCentralityMutateStub betweennessCentralityMutateStub,
ClosenessCentralityMutateStub closenessCentralityMutateStub,
DegreeCentralityMutateStub degreeCentralityMutateStub,
HarmonicCentralityMutateStub harmonicCentralityMutateStub,
ApplicationsFacade applicationsFacade,
EstimationModeRunner estimationModeRunner,
StatsModeAlgorithmRunner statsModeRunner,
Expand All @@ -78,6 +81,7 @@ private CentralityProcedureFacade(
this.betweennessCentralityMutateStub = betweennessCentralityMutateStub;
this.closenessCentralityMutateStub = closenessCentralityMutateStub;
this.degreeCentralityMutateStub = degreeCentralityMutateStub;
this.harmonicCentralityMutateStub = harmonicCentralityMutateStub;
this.applicationsFacade = applicationsFacade;
this.estimationModeRunner = estimationModeRunner;
this.statsModeRunner = statsModeRunner;
Expand Down Expand Up @@ -114,13 +118,19 @@ public static CentralityProcedureFacade create(
applicationsFacade,
procedureReturnColumns
);
var harmonicCentralityMutateStub = new HarmonicCentralityMutateStub(
genericStub,
applicationsFacade,
procedureReturnColumns
);

return new CentralityProcedureFacade(
procedureReturnColumns,
betaClosenessCentralityMutateStub,
betweennessCentralityMutateStub,
closenessCentralityMutateStub,
degreeCentralityMutateStub,
harmonicCentralityMutateStub,
applicationsFacade,
estimationModeRunner,
statsModeRunner,
Expand Down Expand Up @@ -326,6 +336,10 @@ public Stream<MemoryEstimateResult> degreeCentralityStatsEstimate(
return Stream.of(result);
}

public HarmonicCentralityMutateStub harmonicCentralityMutateStub() {
return harmonicCentralityMutateStub;
}

private CentralityAlgorithmsEstimationModeBusinessFacade estimationMode() {
return applicationsFacade.centrality().estimate();
}
Expand Down
Loading

0 comments on commit 6bbf801

Please sign in to comment.