Skip to content

Commit

Permalink
Migrate PageRank stats to facade
Browse files Browse the repository at this point in the history
  • Loading branch information
vnickolov committed Nov 22, 2023
1 parent 9015ad3 commit d3a1cca
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
import org.neo4j.gds.algorithms.StatsResult;
import org.neo4j.gds.algorithms.centrality.specificfields.CentralityStatisticsSpecificFields;
import org.neo4j.gds.algorithms.centrality.specificfields.DefaultCentralitySpecificFields;
import org.neo4j.gds.algorithms.centrality.specificfields.PageRankSpecificFields;
import org.neo4j.gds.algorithms.runner.AlgorithmRunner;
import org.neo4j.gds.betweenness.BetweennessCentralityStatsConfig;
import org.neo4j.gds.closeness.ClosenessCentralityStatsConfig;
import org.neo4j.gds.config.AlgoBaseConfig;
import org.neo4j.gds.core.concurrency.DefaultPool;
import org.neo4j.gds.degree.DegreeCentralityStatsConfig;
import org.neo4j.gds.harmonic.HarmonicCentralityStatsConfig;
import org.neo4j.gds.pagerank.PageRankResult;
import org.neo4j.gds.pagerank.PageRankStatsConfig;
import org.neo4j.gds.result.CentralityStatistics;

import java.util.function.Supplier;
Expand Down Expand Up @@ -115,16 +118,45 @@ public StatsResult<DefaultCentralitySpecificFields> harmonicCentrality(
);
}

public StatsResult<PageRankSpecificFields> pageRank(
String graphName,
PageRankStatsConfig configuration,
boolean shouldComputeCentralityDistribution
) {
// 1. Run the algorithm and time the execution
var intermediateResult = AlgorithmRunner.runWithTiming(
() -> centralityAlgorithmsFacade.pageRank(graphName, configuration)
);

<RESULT extends CentralityAlgorithmResult, CONFIG extends AlgoBaseConfig> StatsResult<DefaultCentralitySpecificFields> statsResult(
CentralityFunctionSupplier<PageRankResult> centralityFunctionSupplier = CentralityAlgorithmResult::centralityScoreProvider;
SpecificFieldsWithCentralityDistributionSupplier<PageRankResult, PageRankSpecificFields> specificFieldsSupplier =
(r, c) -> new PageRankSpecificFields(r.iterations(), r.didConverge(), c);

Supplier<PageRankSpecificFields> emptyASFSupplier = () -> PageRankSpecificFields.EMPTY;

return statsResult(
intermediateResult.algorithmResult,
configuration,
centralityFunctionSupplier,
specificFieldsSupplier,
shouldComputeCentralityDistribution,
intermediateResult.computeMilliseconds,
emptyASFSupplier
);

}


private <RESULT extends CentralityAlgorithmResult, CONFIG extends AlgoBaseConfig> StatsResult<DefaultCentralitySpecificFields> statsResult(
AlgorithmComputationResult<RESULT> algorithmResult,
CONFIG configuration,
boolean shouldComputeCentralityDistribution,
long computeMilliseconds
) {
CentralityFunctionSupplier<RESULT> centralityFunctionSupplier = (r) -> r.centralityScoreProvider();
SpecificFieldsWithCentralityDistributionSupplier<RESULT, DefaultCentralitySpecificFields> specificFieldsSupplier = (r, c) -> new DefaultCentralitySpecificFields(
c);
CentralityFunctionSupplier<RESULT> centralityFunctionSupplier = CentralityAlgorithmResult::centralityScoreProvider;
SpecificFieldsWithCentralityDistributionSupplier<RESULT, DefaultCentralitySpecificFields> specificFieldsSupplier =
(r, c) -> new DefaultCentralitySpecificFields(c);

Supplier<DefaultCentralitySpecificFields> emptyASFSupplier = () -> DefaultCentralitySpecificFields.EMPTY;

return statsResult(
Expand Down
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.algorithms.centrality.specificfields;

import java.util.Map;

public class PageRankSpecificFields implements CentralityStatisticsSpecificFields {

public static final PageRankSpecificFields EMPTY = new PageRankSpecificFields(
0,
false,
Map.of()
);

private final long ranIterations;
private final boolean didConverge;
private final Map<String, Object> centralityDistribution;

public PageRankSpecificFields(
long ranIterations,
boolean didConverge,
Map<String, Object> centralityDistribution
) {
this.ranIterations = ranIterations;
this.didConverge = didConverge;
this.centralityDistribution = centralityDistribution;
}

public long ranIterations() {
return ranIterations;
}

public boolean didConverge() {
return didConverge;
}

@Override
public Map<String, Object> centralityDistribution() {
return centralityDistribution;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

import org.neo4j.gds.BaseProc;
import org.neo4j.gds.executor.MemoryEstimationExecutor;
import org.neo4j.gds.executor.ProcedureExecutor;
import org.neo4j.gds.procedures.GraphDataScience;
import org.neo4j.gds.procedures.centrality.pagerank.PageRankStatsResult;
import org.neo4j.gds.results.MemoryEstimateResult;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.Procedure;
Expand All @@ -35,16 +36,16 @@

public class PageRankStatsProc extends BaseProc {

@Context
public GraphDataScience facade;

@Procedure(value = "gds.pageRank.stats", mode = READ)
@Description(STATS_DESCRIPTION)
public Stream<PageRankStatsResult> stats(
@Name(value = "graphName") String graphName,
@Name(value = "configuration", defaultValue = "{}") Map<String, Object> configuration
) {
return new ProcedureExecutor<>(
new PageRankStatsSpec(),
executionContext()
).compute(graphName, configuration);
return facade.centrality().pageRankStats(graphName, configuration);
}

@Procedure(value = "gds.pageRank.stats.estimate", mode = READ)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@
import org.neo4j.gds.harmonic.HarmonicCentralityWriteConfig;
import org.neo4j.gds.procedures.centrality.alphaharmonic.AlphaHarmonicStreamResult;
import org.neo4j.gds.procedures.centrality.alphaharmonic.AlphaHarmonicWriteResult;
import org.neo4j.gds.pagerank.PageRankStatsConfig;
import org.neo4j.gds.pagerank.PageRankStreamConfig;
import org.neo4j.gds.procedures.centrality.betacloseness.BetaClosenessCentralityMutateResult;
import org.neo4j.gds.procedures.centrality.betacloseness.BetaClosenessCentralityWriteResult;
import org.neo4j.gds.procedures.centrality.pagerank.PageRankComputationalResultTransformer;
import org.neo4j.gds.procedures.centrality.pagerank.PageRankStatsResult;
import org.neo4j.gds.procedures.configparser.ConfigurationParser;
import org.neo4j.gds.results.MemoryEstimateResult;

Expand Down Expand Up @@ -499,6 +502,23 @@ public Stream<CentralityStreamResult> pageRankStream(
return DefaultCentralityComputationalResultTransformer.toStreamResult(computationResult);
}


public Stream<PageRankStatsResult> pageRankStats(
String graphName,
Map<String, Object> configuration
) {
var config = createConfig(configuration, PageRankStatsConfig::of);

var computationResult = statsBusinessFacade.pageRank(
graphName,
config,
procedureReturnColumns.contains("centralityDistribution")
);

return Stream.of(PageRankComputationalResultTransformer.toStatsResult(computationResult, config));
}


// ################################################################################################################

// FIXME: the following two methods are duplicate, find a good place for them.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.centrality.pagerank;

import org.neo4j.gds.algorithms.StatsResult;
import org.neo4j.gds.algorithms.centrality.specificfields.PageRankSpecificFields;
import org.neo4j.gds.pagerank.PageRankStatsConfig;

public final class PageRankComputationalResultTransformer {

private PageRankComputationalResultTransformer() {}

public static PageRankStatsResult toStatsResult(
StatsResult<PageRankSpecificFields> computationResult,
PageRankStatsConfig configuration
) {
return new PageRankStatsResult(
computationResult.algorithmSpecificFields().ranIterations(),
computationResult.algorithmSpecificFields().didConverge(),
computationResult.algorithmSpecificFields().centralityDistribution(),
computationResult.preProcessingMillis(),
computationResult.computeMillis(),
computationResult.postProcessingMillis(),
configuration.toMap()
);
}

}

0 comments on commit d3a1cca

Please sign in to comment.