Skip to content

Commit

Permalink
migrate KNN stats to new business layer facade
Browse files Browse the repository at this point in the history
  • Loading branch information
lassewesth committed Apr 18, 2024
1 parent efdf310 commit d30140b
Show file tree
Hide file tree
Showing 28 changed files with 313 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@
import org.neo4j.gds.similarity.SimilarityGraphResult;
import org.neo4j.gds.similarity.filteredknn.FilteredKnnStatsConfig;
import org.neo4j.gds.similarity.filterednodesim.FilteredNodeSimilarityStatsConfig;
import org.neo4j.gds.similarity.knn.KnnStatsConfig;
import org.neo4j.gds.similarity.nodesim.NodeSimilarityStatsConfig;

import java.util.function.Function;
import java.util.function.Supplier;

import static org.neo4j.gds.algorithms.similarity.SimilarityResultCompanion.FILTERED_KNN_SPECIFIC_FIELDS_SUPPLIER;
import static org.neo4j.gds.algorithms.similarity.SimilarityResultCompanion.KNN_SPECIFIC_FIELDS_SUPPLIER;
import static org.neo4j.gds.algorithms.similarity.SimilarityResultCompanion.NODE_SIMILARITY_SPECIFIC_FIELDS_SUPPLIER;

public class SimilarityAlgorithmsStatsBusinessFacade {
Expand Down Expand Up @@ -89,32 +87,6 @@ public StatsResult<SimilaritySpecificFieldsWithDistribution> filteredNodeSimilar
);
}

public StatsResult<KnnSpecificFields> knn(
String graphName,
KnnStatsConfig configuration,
boolean computeSimilarityDistribution
) {
// 1. Run the algorithm and time the execution
var intermediateResult = AlgorithmRunner.runWithTiming(
() -> similarityAlgorithmsFacade.knn(graphName, configuration)
);
var algorithmResult = intermediateResult.algorithmResult;

return statsResult(
algorithmResult,
result -> SimilarityResultCompanion.computeToGraph(
algorithmResult.graph(),
algorithmResult.graph().nodeCount(),
configuration.concurrency(),
result.streamSimilarityResult()
),
KNN_SPECIFIC_FIELDS_SUPPLIER,
intermediateResult.computeMilliseconds,
() -> KnnSpecificFields.EMPTY,
computeSimilarityDistribution
);
}

public StatsResult<KnnSpecificFields> filteredKnn(
String graphName,
FilteredKnnStatsConfig configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.neo4j.gds.similarity.filteredknn.FilteredKnnResult;
import org.neo4j.gds.similarity.filteredknn.FilteredKnnStreamConfig;
import org.neo4j.gds.similarity.filterednodesim.FilteredNodeSimilarityStreamConfig;
import org.neo4j.gds.similarity.knn.KnnResult;
import org.neo4j.gds.similarity.knn.KnnStreamConfig;
import org.neo4j.gds.similarity.nodesim.NodeSimilarityResult;
import org.neo4j.gds.similarity.nodesim.NodeSimilarityStreamConfig;

Expand All @@ -46,15 +44,6 @@ public StreamComputationResult<NodeSimilarityResult> nodeSimilarity(
return createStreamComputationResult(result);
}

public StreamComputationResult<KnnResult> knn(
String graphName,
KnnStreamConfig config
) {
var result = similarityAlgorithmsFacade.knn(graphName, config);

return createStreamComputationResult(result);
}

public StreamComputationResult<NodeSimilarityResult> filteredNodeSimilarity(
String graphName,
FilteredNodeSimilarityStreamConfig config
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.similarity;

import org.neo4j.gds.api.GraphName;
import org.neo4j.gds.applications.algorithms.machinery.AlgorithmProcessingTemplate;
import org.neo4j.gds.applications.algorithms.machinery.ResultBuilder;
import org.neo4j.gds.similarity.knn.KnnResult;
import org.neo4j.gds.similarity.knn.KnnStatsConfig;

import java.util.Optional;

import static org.neo4j.gds.applications.algorithms.similarity.AlgorithmLabels.KNN;

public class SimilarityAlgorithmsStatsModeBusinessFacade {
private final SimilarityAlgorithmsEstimationModeBusinessFacade estimationFacade;
private final SimilarityAlgorithms similarityAlgorithms;
private final AlgorithmProcessingTemplate algorithmProcessingTemplate;

public SimilarityAlgorithmsStatsModeBusinessFacade(
SimilarityAlgorithmsEstimationModeBusinessFacade estimationFacade,
SimilarityAlgorithms similarityAlgorithms,
AlgorithmProcessingTemplate algorithmProcessingTemplate
) {
this.estimationFacade = estimationFacade;
this.similarityAlgorithms = similarityAlgorithms;
this.algorithmProcessingTemplate = algorithmProcessingTemplate;
}

public <RESULT> RESULT knn(
GraphName graphName,
KnnStatsConfig configuration,
ResultBuilder<KnnStatsConfig, KnnResult, RESULT, Void> resultBuilder
) {
return algorithmProcessingTemplate.processAlgorithm(
graphName,
configuration,
KNN,
() -> estimationFacade.knn(configuration),
graph -> similarityAlgorithms.knn(graph, configuration),
Optional.empty(),
resultBuilder
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@
import org.neo4j.gds.applications.algorithms.similarity.SimilarityAlgorithms;
import org.neo4j.gds.applications.algorithms.similarity.SimilarityAlgorithmsEstimationModeBusinessFacade;
import org.neo4j.gds.applications.algorithms.similarity.SimilarityAlgorithmsMutateModeBusinessFacade;
import org.neo4j.gds.applications.algorithms.similarity.SimilarityAlgorithmsStatsModeBusinessFacade;
import org.neo4j.gds.applications.algorithms.similarity.SimilarityAlgorithmsStreamModeBusinessFacade;
import org.neo4j.gds.logging.Log;

public final class SimilarityApplications {
private final SimilarityAlgorithmsEstimationModeBusinessFacade estimationModeFacade;
private final SimilarityAlgorithmsMutateModeBusinessFacade mutateModeFacade;
private final SimilarityAlgorithmsStatsModeBusinessFacade statsModeFacade;
private final SimilarityAlgorithmsStreamModeBusinessFacade streamModeFacade;

private SimilarityApplications(
SimilarityAlgorithmsEstimationModeBusinessFacade estimationModeFacade,
SimilarityAlgorithmsMutateModeBusinessFacade mutateModeFacade,
SimilarityAlgorithmsStatsModeBusinessFacade statsModeFacade,
SimilarityAlgorithmsStreamModeBusinessFacade streamModeFacade
) {
this.estimationModeFacade = estimationModeFacade;
this.mutateModeFacade = mutateModeFacade;
this.streamModeFacade = streamModeFacade;
this.statsModeFacade = statsModeFacade;
}

static SimilarityApplications create(
Expand All @@ -57,13 +61,19 @@ static SimilarityApplications create(
algorithmProcessingTemplate
);

var statsModeFacade = new SimilarityAlgorithmsStatsModeBusinessFacade(
estimationModeFacade,
similarityAlgorithms,
algorithmProcessingTemplate
);

var streamModeFacade = new SimilarityAlgorithmsStreamModeBusinessFacade(
estimationModeFacade,
similarityAlgorithms,
algorithmProcessingTemplate
);

return new SimilarityApplications(estimationModeFacade, mutateModeFacade, streamModeFacade);
return new SimilarityApplications(estimationModeFacade, mutateModeFacade, statsModeFacade, streamModeFacade);
}

public SimilarityAlgorithmsEstimationModeBusinessFacade estimate() {
Expand All @@ -74,6 +84,10 @@ public SimilarityAlgorithmsMutateModeBusinessFacade mutate() {
return mutateModeFacade;
}

public SimilarityAlgorithmsStatsModeBusinessFacade stats() {
return statsModeFacade;
}

public SimilarityAlgorithmsStreamModeBusinessFacade stream() {
return streamModeFacade;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import org.neo4j.gds.BaseProc;
import org.neo4j.gds.procedures.GraphDataScienceProcedures;
import org.neo4j.gds.procedures.similarity.knn.KnnStatsResult;
import org.neo4j.gds.procedures.algorithms.similarity.KnnStatsResult;
import org.neo4j.gds.results.MemoryEstimateResult;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
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.similarity.knn.KnnStatsResult;
import org.neo4j.gds.procedures.algorithms.similarity.KnnStatsResult;

import java.util.stream.Stream;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import org.neo4j.gds.BaseProc;
import org.neo4j.gds.procedures.GraphDataScienceProcedures;
import org.neo4j.gds.procedures.similarity.SimilarityStatsResult;
import org.neo4j.gds.procedures.algorithms.similarity.SimilarityStatsResult;
import org.neo4j.gds.results.MemoryEstimateResult;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.neo4j.gds.executor.ExecutionMode;
import org.neo4j.gds.executor.GdsCallable;
import org.neo4j.gds.procedures.algorithms.configuration.NewConfigFunction;
import org.neo4j.gds.procedures.similarity.SimilarityStatsResult;
import org.neo4j.gds.procedures.algorithms.similarity.SimilarityStatsResult;
import org.neo4j.gds.similarity.nodesim.NodeSimilarity;
import org.neo4j.gds.similarity.nodesim.NodeSimilarityResult;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.neo4j.gds.similarity.knn;

import org.neo4j.gds.procedures.GraphDataScienceProcedures;
import org.neo4j.gds.procedures.similarity.knn.KnnStatsResult;
import org.neo4j.gds.procedures.algorithms.similarity.KnnStatsResult;
import org.neo4j.gds.results.MemoryEstimateResult;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
Expand All @@ -44,7 +44,7 @@ public Stream<KnnStatsResult> stats(
@Name(value = "graphName") String graphName,
@Name(value = "configuration", defaultValue = "{}") Map<String, Object> configuration
) {
return facade.similarity().knnStats(graphName, configuration);
return facade.similarity().theOtherFacade().knnStats(graphName, configuration);
}

@Procedure(value = "gds.knn.stats.estimate", mode = READ)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
package org.neo4j.gds.similarity.knn;

import org.neo4j.gds.procedures.similarity.knn.KnnStatsResult;
import org.neo4j.gds.procedures.algorithms.similarity.KnnStatsResult;
import org.neo4j.gds.similarity.SimilarityResultBuilder;

public class KnnStatsResultBuilder extends SimilarityResultBuilder<KnnStatsResult> {
Expand Down Expand Up @@ -58,4 +58,3 @@ public KnnStatsResultBuilder withNodePairsConsidered(long nodePairsConsidered) {
return this;
}
}

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.similarity.knn.KnnStatsResult;
import org.neo4j.gds.procedures.algorithms.similarity.KnnStatsResult;

import java.util.stream.Stream;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.neo4j.gds.similarity.nodesim;

import org.neo4j.gds.procedures.GraphDataScienceProcedures;
import org.neo4j.gds.procedures.similarity.SimilarityStatsResult;
import org.neo4j.gds.procedures.algorithms.similarity.SimilarityStatsResult;
import org.neo4j.gds.results.MemoryEstimateResult;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
Expand Down

This file was deleted.

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.similarity.SimilarityStatsResult;
import org.neo4j.gds.procedures.algorithms.similarity.SimilarityStatsResult;

import java.util.stream.Stream;

Expand Down
1 change: 1 addition & 0 deletions procedures/algorithms-facade/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
}

implementation project(':algo')
implementation project(':algo-common')
implementation project(':algorithms-machinery')
implementation project(':annotations')
implementation project(':applications-facade')
Expand Down
Loading

0 comments on commit d30140b

Please sign in to comment.