Skip to content

Commit

Permalink
Stats facade
Browse files Browse the repository at this point in the history
  • Loading branch information
IoannisPanagiotas committed Nov 3, 2023
1 parent 3749696 commit 074f55f
Show file tree
Hide file tree
Showing 15 changed files with 479 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
package org.neo4j.gds.result;

import org.HdrHistogram.DoubleHistogram;
import org.neo4j.gds.annotation.ValueClass;
import org.neo4j.gds.core.ProcedureConstants;
import org.neo4j.gds.core.utils.partition.Partition;
import org.neo4j.gds.core.concurrency.ParallelUtil;
import org.neo4j.gds.core.utils.ProgressTimer;
import org.neo4j.gds.core.utils.partition.Partition;
import org.neo4j.gds.core.utils.partition.PartitionUtils;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.LongToDoubleFunction;

public final class CentralityStatistics {
Expand Down Expand Up @@ -83,4 +88,44 @@ public void run() {
});
}
}

public static CentralityStats centralityStatistics(
long nodeCount,
LongToDoubleFunction centralityProvider,
ExecutorService executorService,
int concurrency,
boolean shouldCompute
) {
Optional<DoubleHistogram> maybeHistogram = Optional.empty();
var computeMilliseconds = new AtomicLong(0);
try (var ignored = ProgressTimer.start(computeMilliseconds::set)) {
if (shouldCompute) {
maybeHistogram = Optional.of(histogram(
nodeCount,
centralityProvider,
executorService,
concurrency
));
}
}

return ImmutableCentralityStats.of(maybeHistogram, computeMilliseconds.get());
}

public static Map<String, Object> centralitySummary(Optional<DoubleHistogram> histogram) {
return histogram
.map(HistogramUtils::centralitySummary)
.orElseGet(Collections::emptyMap);
}

@ValueClass
@SuppressWarnings("immutables:incompat")
public interface CentralityStats {

Optional<DoubleHistogram> histogram();

long computeMilliseconds();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
*/
package org.neo4j.gds.algorithms.centrality;

import org.neo4j.gds.algorithms.AlgorithmComputationResult;
import org.neo4j.gds.algorithms.StatsResult;
import org.neo4j.gds.algorithms.centrality.specificfields.CentralityStatisticsSpecificFields;
import org.neo4j.gds.algorithms.centrality.specificfields.StandardCentralityStatisticsSpecificFields;
import org.neo4j.gds.algorithms.runner.AlgorithmRunner;
import org.neo4j.gds.api.DatabaseId;
import org.neo4j.gds.api.User;
import org.neo4j.gds.betweenness.BetweennessCentralityStatsConfig;
import org.neo4j.gds.config.AlgoBaseConfig;
import org.neo4j.gds.core.concurrency.DefaultPool;
import org.neo4j.gds.result.CentralityStatistics;

import java.util.function.Supplier;

public class CentralityAlgorithmsStatsBusinessFacade {

private final CentralityAlgorithmsFacade centralityAlgorithmsFacade;
Expand All @@ -28,4 +42,65 @@ public CentralityAlgorithmsStatsBusinessFacade(CentralityAlgorithmsFacade centra
}


public StatsResult<StandardCentralityStatisticsSpecificFields> betweennessCentrality(
String graphName,
BetweennessCentralityStatsConfig configuration,
User user,
DatabaseId databaseId,
boolean shouldComputeCentralityDistribution
) {
// 1. Run the algorithm and time the execution
var intermediateResult = AlgorithmRunner.runWithTiming(
() -> centralityAlgorithmsFacade.betweennessCentrality(graphName, configuration, user, databaseId)
);
var algorithmResult = intermediateResult.algorithmResult;

return statsResult(
algorithmResult,
configuration,
(result -> result::get),
(result, centralityDistribution) -> {
return new StandardCentralityStatisticsSpecificFields(
centralityDistribution
);
},
shouldComputeCentralityDistribution,
intermediateResult.computeMilliseconds,
() -> StandardCentralityStatisticsSpecificFields.EMPTY
);
}

<RESULT, CONFIG extends AlgoBaseConfig, ASF extends CentralityStatisticsSpecificFields> StatsResult<ASF> statsResult(
AlgorithmComputationResult<RESULT> algorithmResult,
CONFIG configuration,
CentralityFunctionSupplier<RESULT> centralityFunctionSupplier,
SpecificFieldsWithCentralityDistributionSupplier<RESULT, ASF> specificFieldsSupplier,
boolean shouldComputeCentralityDistribution,
long computeMilliseconds,
Supplier<ASF> emptyASFSupplier
) {

return algorithmResult.result().map(result -> {

// 2. Compute result statistics
var centralityStatistics = CentralityStatistics.centralityStatistics(
algorithmResult.graph().nodeCount(),
centralityFunctionSupplier.centralityFunction(result),
DefaultPool.INSTANCE,
configuration.concurrency(),
shouldComputeCentralityDistribution
);

var communitySummary = CentralityStatistics.centralitySummary(centralityStatistics.histogram());

var specificFields = specificFieldsSupplier.specificFields(result, communitySummary);

return StatsResult.<ASF>builder()
.computeMillis(computeMilliseconds)
.postProcessingMillis(centralityStatistics.computeMilliseconds())
.algorithmSpecificFields(specificFields)
.build();
}).orElseGet(() -> StatsResult.empty(emptyASFSupplier.get()));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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;

import java.util.function.LongToDoubleFunction;

interface CentralityFunctionSupplier<R> {
LongToDoubleFunction centralityFunction(R result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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;

import java.util.Map;

interface SpecificFieldsWithCentralityDistributionSupplier<R, ASF> {
ASF specificFields(R result, Map<String, Object> centralityDistribution);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 interface CentralityStatisticsSpecificFields {

Map<String, Object> centralityDistribution();
}
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.algorithms.centrality.specificfields;

import java.util.Map;


public class StandardCentralityStatisticsSpecificFields implements CentralityStatisticsSpecificFields {

public static final StandardCentralityStatisticsSpecificFields EMPTY = new StandardCentralityStatisticsSpecificFields(
Map.of()
);

private final Map<String, Object> centralityDistribution;

public StandardCentralityStatisticsSpecificFields(
Map<String, Object> centralityDistribution
) {
this.centralityDistribution = centralityDistribution;
}


@Override
public Map<String, Object> centralityDistribution(){
return centralityDistribution;
}

}
Loading

0 comments on commit 074f55f

Please sign in to comment.