Skip to content

Commit

Permalink
migrate celf stats
Browse files Browse the repository at this point in the history
  • Loading branch information
lassewesth committed May 30, 2024
1 parent 949926e commit ba79834
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
import org.jetbrains.annotations.NotNull;
import org.neo4j.gds.algorithms.AlgorithmComputationResult;
import org.neo4j.gds.algorithms.StatsResult;
import org.neo4j.gds.algorithms.centrality.specificfields.CELFSpecificFields;
import org.neo4j.gds.algorithms.centrality.specificfields.CentralityStatisticsSpecificFields;
import org.neo4j.gds.algorithms.centrality.specificfields.PageRankSpecificFields;
import org.neo4j.gds.algorithms.runner.AlgorithmResultWithTiming;
import org.neo4j.gds.algorithms.runner.AlgorithmRunner;
import org.neo4j.gds.config.AlgoBaseConfig;
import org.neo4j.gds.core.concurrency.DefaultPool;
import org.neo4j.gds.influenceMaximization.InfluenceMaximizationStatsConfig;
import org.neo4j.gds.pagerank.PageRankResult;
import org.neo4j.gds.pagerank.PageRankStatsConfig;
import org.neo4j.gds.result.CentralityStatistics;
Expand Down Expand Up @@ -122,33 +120,6 @@ private static StatsResult<PageRankSpecificFields> pageRankVariantStats(

}

public StatsResult<CELFSpecificFields> celf(
String graphName,
InfluenceMaximizationStatsConfig configuration
) {
// 1. Run the algorithm and time the execution
var intermediateResult = AlgorithmRunner.runWithTiming(
() -> centralityAlgorithmsFacade.celf(graphName, configuration)
);

var statsResultBuilder = StatsResult.<CELFSpecificFields>builder()
.computeMillis(intermediateResult.computeMilliseconds)
.postProcessingMillis(0);

var algorithmResult = intermediateResult.algorithmResult;
statsResultBuilder.algorithmSpecificFields(
algorithmResult.result().map(
result -> new CELFSpecificFields(
result.totalSpread(),
intermediateResult.algorithmResult.graph().nodeCount()
))
.orElse(
CELFSpecificFields.EMPTY
));

return statsResultBuilder.build();
}

<RESULT, CONFIG extends AlgoBaseConfig, ASF extends CentralityStatisticsSpecificFields> StatsResult<ASF> statsResult(
AlgorithmComputationResult<RESULT> algorithmResult,
CONFIG configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public MemoryEstimation celf(InfluenceMaximizationBaseConfig configuration) {
return new CELFMemoryEstimateDefinition(configuration.toParameters()).memoryEstimation();
}

public MemoryEstimateResult celf(InfluenceMaximizationBaseConfig configuration, Object graphNameOrConfiguration) {
var memoryEstimation = celf(configuration);

return algorithmEstimationTemplate.estimate(
configuration,
graphNameOrConfiguration,
memoryEstimation
);
}

public MemoryEstimation closenessCentrality(ClosenessCentralityBaseConfig ignored) {
throw new MemoryEstimationNotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
import org.neo4j.gds.closeness.ClosenessCentralityStatsConfig;
import org.neo4j.gds.degree.DegreeCentralityStatsConfig;
import org.neo4j.gds.harmonic.HarmonicCentralityStatsConfig;
import org.neo4j.gds.influenceMaximization.CELFResult;
import org.neo4j.gds.influenceMaximization.InfluenceMaximizationStatsConfig;

import java.util.Optional;

import static org.neo4j.gds.applications.algorithms.metadata.LabelForProgressTracking.BetweennessCentrality;
import static org.neo4j.gds.applications.algorithms.metadata.LabelForProgressTracking.CELF;
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;
Expand Down Expand Up @@ -113,4 +116,20 @@ public <RESULT> RESULT harmonicCentrality(
resultBuilder
);
}

public <RESULT> RESULT celf(
GraphName graphName,
InfluenceMaximizationStatsConfig configuration,
ResultBuilder<InfluenceMaximizationStatsConfig, CELFResult, RESULT, Void> resultBuilder
) {
return algorithmProcessingTemplate.processAlgorithm(
graphName,
configuration,
CELF,
() -> estimationFacade.celf(configuration),
graph -> centralityAlgorithms.celf(graph, configuration),
Optional.empty(),
resultBuilder
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.neo4j.gds.influenceMaximization;

import org.neo4j.gds.procedures.GraphDataScienceProcedures;
import org.neo4j.gds.procedures.centrality.celf.CELFStatsResult;
import org.neo4j.gds.procedures.algorithms.centrality.CELFStatsResult;
import org.neo4j.gds.applications.algorithms.machinery.MemoryEstimateResult;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
Expand All @@ -45,7 +45,7 @@ public Stream<CELFStatsResult> stats(
@Name(value = "graphName") String graphName,
@Name(value = "configuration", defaultValue = "{}") Map<String, Object> configuration
) {
return facade.centrality().celfStats(graphName, configuration);
return facade.algorithms().centrality().celfStats(graphName, configuration);
}

@Procedure(name = "gds.influenceMaximization.celf.stats.estimate", mode = READ)
Expand All @@ -54,7 +54,7 @@ public Stream<MemoryEstimateResult> estimate(
@Name(value = "graphNameOrConfiguration") Object graphNameOrConfiguration,
@Name(value = "algoConfiguration") Map<String, Object> algoConfiguration
) {
return facade.centrality().celfStatsEstimate(graphNameOrConfiguration, algoConfiguration);
return facade.algorithms().centrality().celfStatsEstimate(graphNameOrConfiguration, algoConfiguration);
}

@Procedure(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.neo4j.gds.executor.ExecutionContext;
import org.neo4j.gds.executor.GdsCallable;
import org.neo4j.gds.procedures.algorithms.configuration.NewConfigFunction;
import org.neo4j.gds.procedures.centrality.celf.CELFStatsResult;
import org.neo4j.gds.procedures.algorithms.centrality.CELFStatsResult;

import java.util.stream.Stream;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
* 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.procedures.centrality.celf;
package org.neo4j.gds.procedures.algorithms.centrality;

import org.neo4j.gds.applications.algorithms.machinery.AlgorithmProcessingTimings;
import org.neo4j.gds.result.AbstractResultBuilder;

import java.util.Map;

public final class CELFStatsResult {

public final long computeMillis;
public final double totalSpread;
public final long nodeCount;
Expand All @@ -42,6 +42,10 @@ public static Builder builder() {
return new Builder();
}

static CELFStatsResult emptyFrom(AlgorithmProcessingTimings timings, Map<String, Object> configurationMap) {
return new CELFStatsResult(timings.computeMillis, 0, 0, configurationMap);
}

public static class Builder extends AbstractResultBuilder<CELFStatsResult> {
private double totalSpread;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.procedures.algorithms.centrality;

import org.neo4j.gds.api.Graph;
import org.neo4j.gds.api.GraphStore;
import org.neo4j.gds.applications.algorithms.machinery.AlgorithmProcessingTimings;
import org.neo4j.gds.applications.algorithms.machinery.ResultBuilder;
import org.neo4j.gds.influenceMaximization.CELFResult;
import org.neo4j.gds.influenceMaximization.InfluenceMaximizationStatsConfig;

import java.util.Optional;
import java.util.stream.Stream;

class CelfResultBuilderForStatsMode implements ResultBuilder<InfluenceMaximizationStatsConfig, CELFResult, Stream<CELFStatsResult>, Void> {
@Override
public Stream<CELFStatsResult> build(
Graph graph,
GraphStore graphStore,
InfluenceMaximizationStatsConfig configuration,
Optional<CELFResult> result,
AlgorithmProcessingTimings timings,
Optional<Void> unused
) {
if (result.isEmpty()) return Stream.of(CELFStatsResult.emptyFrom(timings, configuration.toMap()));

var celfResult = result.get();

return Stream.of(
new CELFStatsResult(
timings.computeMillis,
celfResult.totalSpread(),
graph.nodeCount(),
configuration.toMap()
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.neo4j.gds.harmonic.HarmonicCentralityStatsConfig;
import org.neo4j.gds.harmonic.HarmonicCentralityStreamConfig;
import org.neo4j.gds.harmonic.HarmonicCentralityWriteConfig;
import org.neo4j.gds.influenceMaximization.InfluenceMaximizationStatsConfig;
import org.neo4j.gds.procedures.algorithms.centrality.stubs.BetaClosenessCentralityMutateStub;
import org.neo4j.gds.procedures.algorithms.centrality.stubs.BetweennessCentralityMutateStub;
import org.neo4j.gds.procedures.algorithms.centrality.stubs.CelfMutateStub;
Expand Down Expand Up @@ -292,6 +293,37 @@ public CelfMutateStub celfMutateStub() {
return celfMutateStub;
}

public Stream<CELFStatsResult> celfStats(
String graphName,
Map<String, Object> configuration
) {
var resultBuilder = new CelfResultBuilderForStatsMode();

return statsModeRunner.runStatsModeAlgorithm(
graphName,
configuration,
InfluenceMaximizationStatsConfig::of,
resultBuilder,
statsMode()::celf
);
}

public Stream<MemoryEstimateResult> celfStatsEstimate(
Object graphNameOrConfiguration,
Map<String, Object> algorithmConfiguration
) {
var result = estimationModeRunner.runEstimation(
algorithmConfiguration,
InfluenceMaximizationStatsConfig::of,
configuration -> estimationMode().celf(
configuration,
graphNameOrConfiguration
)
);

return Stream.of(result);
}

public ClosenessCentralityMutateStub closenessCentralityMutateStub() {
return closenessCentralityMutateStub;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@
package org.neo4j.gds.procedures.centrality;

import org.neo4j.gds.algorithms.NodePropertyWriteResult;
import org.neo4j.gds.algorithms.StatsResult;
import org.neo4j.gds.algorithms.StreamComputationResult;
import org.neo4j.gds.algorithms.centrality.specificfields.CELFSpecificFields;
import org.neo4j.gds.influenceMaximization.CELFResult;
import org.neo4j.gds.influenceMaximization.InfluenceMaximizationStatsConfig;
import org.neo4j.gds.procedures.centrality.celf.CELFStatsResult;
import org.neo4j.gds.procedures.centrality.celf.CELFStreamResult;
import org.neo4j.gds.procedures.centrality.celf.CELFWriteResult;

Expand Down Expand Up @@ -54,17 +51,6 @@ static Stream<CELFStreamResult> toStreamResult(
}).orElseGet(Stream::empty);
}

static CELFStatsResult toStatsResult(
StatsResult<CELFSpecificFields> statsResult,
InfluenceMaximizationStatsConfig configuration
) {
return new CELFStatsResult(statsResult.computeMillis(),
statsResult.algorithmSpecificFields().totalSpread(),
statsResult.algorithmSpecificFields().nodeCount(),
configuration.toMap()
);
}

static CELFWriteResult toWriteResult(
NodePropertyWriteResult<CELFSpecificFields> mutateResult
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.neo4j.gds.algorithms.centrality.CentralityAlgorithmsWriteBusinessFacade;
import org.neo4j.gds.api.ProcedureReturnColumns;
import org.neo4j.gds.applications.algorithms.machinery.MemoryEstimateResult;
import org.neo4j.gds.influenceMaximization.InfluenceMaximizationStatsConfig;
import org.neo4j.gds.influenceMaximization.InfluenceMaximizationStreamConfig;
import org.neo4j.gds.influenceMaximization.InfluenceMaximizationWriteConfig;
import org.neo4j.gds.pagerank.PageRankMutateConfig;
Expand All @@ -35,7 +34,6 @@
import org.neo4j.gds.pagerank.PageRankWriteConfig;
import org.neo4j.gds.procedures.algorithms.centrality.CentralityStreamResult;
import org.neo4j.gds.procedures.algorithms.configuration.ConfigurationCreator;
import org.neo4j.gds.procedures.centrality.celf.CELFStatsResult;
import org.neo4j.gds.procedures.centrality.celf.CELFStreamResult;
import org.neo4j.gds.procedures.centrality.celf.CELFWriteResult;
import org.neo4j.gds.procedures.centrality.pagerank.PageRankComputationalResultTransformer;
Expand Down Expand Up @@ -92,20 +90,6 @@ public Stream<CELFStreamResult> celfStream(
return CELFComputationalResultTransformer.toStreamResult(computationResult);
}

public Stream<CELFStatsResult> celfStats(
String graphName,
Map<String, Object> configuration
) {
var config = configurationCreator.createConfiguration(configuration, InfluenceMaximizationStatsConfig::of);

var statsResult = statsBusinessFacade.celf(
graphName,
config
);

return Stream.of(CELFComputationalResultTransformer.toStatsResult(statsResult, config));
}

public Stream<CELFWriteResult> celfWrite(
String graphName,
Map<String, Object> configuration
Expand All @@ -131,16 +115,6 @@ public Stream<MemoryEstimateResult> celfStreamEstimate(

}

public Stream<MemoryEstimateResult> celfStatsEstimate(
Object graphNameOrConfiguration,
Map<String, Object> configuration
) {
var config = configurationCreator.createConfiguration(configuration, InfluenceMaximizationStatsConfig::of);

return Stream.of(estimateBusinessFacade.celf(graphNameOrConfiguration, config));

}

public Stream<MemoryEstimateResult> celfWriteEstimate(
Object graphNameOrConfiguration,
Map<String, Object> configuration
Expand Down

0 comments on commit ba79834

Please sign in to comment.