Skip to content

Commit fd7062f

Browse files
committed
Add PushbackNodeEmbeddingsStatsProcedureFacade
1 parent c8ffffd commit fd7062f

File tree

4 files changed

+199
-2
lines changed

4 files changed

+199
-2
lines changed

procedures/pushback-procedures-facade/src/main/java/org/neo4j/gds/procedures/algorithms/embeddings/PushbackNodeEmbeddingsProcedureFacade.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.neo4j.gds.procedures.algorithms.embeddings;
2121

2222
import org.neo4j.gds.applications.algorithms.machinery.MemoryEstimateResult;
23+
import org.neo4j.gds.procedures.algorithms.embeddings.stats.PushbackNodeEmbeddingsStatsProcedureFacade;
2324
import org.neo4j.gds.procedures.algorithms.embeddings.stream.PushbackNodeEmbeddingsStreamProcedureFacade;
2425
import org.neo4j.gds.procedures.algorithms.embeddings.stubs.NodeEmbeddingsStubs;
2526

@@ -29,11 +30,14 @@
2930
public class PushbackNodeEmbeddingsProcedureFacade implements NodeEmbeddingsProcedureFacade {
3031

3132
private final PushbackNodeEmbeddingsStreamProcedureFacade streamProcedureFacade;
33+
private final PushbackNodeEmbeddingsStatsProcedureFacade statsProcedureFacade;
3234

3335
public PushbackNodeEmbeddingsProcedureFacade(
34-
PushbackNodeEmbeddingsStreamProcedureFacade streamProcedureFacade
36+
PushbackNodeEmbeddingsStreamProcedureFacade streamProcedureFacade,
37+
PushbackNodeEmbeddingsStatsProcedureFacade statsProcedureFacade
3538
) {
3639
this.streamProcedureFacade = streamProcedureFacade;
40+
this.statsProcedureFacade = statsProcedureFacade;
3741
}
3842

3943
@Override
@@ -43,7 +47,7 @@ public NodeEmbeddingsStubs nodeEmbeddingStubs() {
4347

4448
@Override
4549
public Stream<FastRPStatsResult> fastRPStats(String graphName, Map<String, Object> configuration) {
46-
return Stream.empty();
50+
return statsProcedureFacade.fastRP(graphName, configuration);
4751
}
4852

4953
@Override
@@ -202,6 +206,7 @@ public Stream<DefaultNodeEmbeddingsStreamResult> node2VecStream(
202206
Map<String, Object> configuration
203207
) {
204208
return streamProcedureFacade.node2Vec(graphName, configuration);
209+
205210
}
206211

207212
@Override
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.procedures.algorithms.embeddings.stats;
21+
22+
import org.neo4j.gds.api.Graph;
23+
import org.neo4j.gds.embeddings.fastrp.FastRPResult;
24+
import org.neo4j.gds.procedures.algorithms.embeddings.FastRPStatsResult;
25+
import org.neo4j.gds.result.TimedAlgorithmResult;
26+
import org.neo4j.gds.results.ResultTransformer;
27+
28+
import java.util.Map;
29+
import java.util.stream.Stream;
30+
31+
class FastRPStatsResultTransformer implements ResultTransformer<TimedAlgorithmResult<FastRPResult>, Stream<FastRPStatsResult>> {
32+
private final Graph graph;
33+
private final Map<String, Object> configuration;
34+
35+
FastRPStatsResultTransformer(Graph graph, Map<String, Object> configuration) {
36+
this.graph = graph;
37+
this.configuration = configuration;
38+
}
39+
40+
@Override
41+
public Stream<FastRPStatsResult> apply(TimedAlgorithmResult<FastRPResult> algorithmResult) {
42+
return Stream.of(new FastRPStatsResult(
43+
graph.nodeCount(),
44+
-1, // TODO
45+
algorithmResult.computeMillis(),
46+
configuration
47+
));
48+
}
49+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.procedures.algorithms.embeddings.stats;
21+
22+
import org.neo4j.gds.api.GraphName;
23+
import org.neo4j.gds.embeddings.NodeEmbeddingComputeBusinessFacade;
24+
import org.neo4j.gds.embeddings.fastrp.FastRPConfigTransformer;
25+
import org.neo4j.gds.embeddings.fastrp.FastRPStatsConfig;
26+
import org.neo4j.gds.procedures.algorithms.configuration.UserSpecificConfigurationParser;
27+
import org.neo4j.gds.procedures.algorithms.embeddings.FastRPStatsResult;
28+
29+
import java.util.Map;
30+
import java.util.stream.Stream;
31+
32+
public class PushbackNodeEmbeddingsStatsProcedureFacade {
33+
private final NodeEmbeddingComputeBusinessFacade businessFacade;
34+
private final UserSpecificConfigurationParser configurationParser;
35+
36+
public PushbackNodeEmbeddingsStatsProcedureFacade(
37+
NodeEmbeddingComputeBusinessFacade businessFacade,
38+
UserSpecificConfigurationParser configurationParser
39+
) {
40+
this.businessFacade = businessFacade;
41+
this.configurationParser = configurationParser;
42+
}
43+
44+
public Stream<FastRPStatsResult> fastRP(String graphName, Map<String, Object> configuration) {
45+
var config = configurationParser.parseConfiguration(configuration, FastRPStatsConfig::of);
46+
47+
return businessFacade.fastRP(
48+
GraphName.parse(graphName),
49+
config.toGraphParameters(),
50+
config.relationshipWeightProperty(),
51+
FastRPConfigTransformer.toParameters(config),
52+
config.jobId(),
53+
config.logProgress(),
54+
graphResources -> new FastRPStatsResultTransformer(graphResources.graph(), config.toMap())
55+
).join();
56+
}
57+
58+
59+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
package org.neo4j.gds.procedures.algorithms.embeddings.stats;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.neo4j.gds.api.Graph;
25+
import org.neo4j.gds.embeddings.fastrp.FastRPResult;
26+
import org.neo4j.gds.procedures.algorithms.embeddings.FastRPStatsResult;
27+
import org.neo4j.gds.result.TimedAlgorithmResult;
28+
29+
import java.util.Map;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.Mockito.mock;
33+
import static org.mockito.Mockito.times;
34+
import static org.mockito.Mockito.verify;
35+
import static org.mockito.Mockito.verifyNoMoreInteractions;
36+
import static org.mockito.Mockito.when;
37+
38+
class FastRPStatsResultTransformerTest {
39+
40+
@Test
41+
void shouldTransformFastRPResultToStatsResultStream() {
42+
var graphMock = mock(Graph.class);
43+
when(graphMock.nodeCount()).thenReturn(42L);
44+
45+
var fastRPResultMock = mock(FastRPResult.class);
46+
var config = Map.<String, Object>of("dim", 128, "seed", 42);
47+
48+
var timedResult = new TimedAlgorithmResult<>(fastRPResultMock, 1234);
49+
50+
var transformer = new FastRPStatsResultTransformer(graphMock, config);
51+
52+
var resultStream = transformer.apply(timedResult);
53+
54+
assertThat(resultStream)
55+
.hasSize(1)
56+
.first()
57+
.isEqualTo(new FastRPStatsResult(42L, -1, 1234L, config));
58+
59+
verify(graphMock, times(1)).nodeCount();
60+
verifyNoMoreInteractions(graphMock);
61+
}
62+
63+
@Test
64+
void shouldReturnStatsResultWithZeroNodesWhenTimedAlgorithmResultIsEmpty() {
65+
var graphMock = mock(Graph.class);
66+
when(graphMock.nodeCount()).thenReturn(0L);
67+
68+
var config = Map.<String, Object>of("dim", 128, "seed", 42);
69+
70+
var timedResult = TimedAlgorithmResult.empty(FastRPResult.empty());
71+
72+
var transformer = new FastRPStatsResultTransformer(graphMock, config);
73+
74+
var resultStream = transformer.apply(timedResult);
75+
76+
assertThat(resultStream)
77+
.hasSize(1)
78+
.first()
79+
.isEqualTo(new FastRPStatsResult(0L, -1, 0L, config));
80+
81+
verify(graphMock, times(1)).nodeCount();
82+
verifyNoMoreInteractions(graphMock);
83+
}
84+
}

0 commit comments

Comments
 (0)