Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit e87dd8c

Browse files
committed
exporter needs to normalize values too
1 parent 3afa481 commit e87dd8c

File tree

9 files changed

+126
-6
lines changed

9 files changed

+126
-6
lines changed

algo/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@
109109
<scope>test</scope>
110110
</dependency>
111111

112+
<dependency>
113+
<groupId>org.mockito</groupId>
114+
<artifactId>mockito-inline</artifactId>
115+
<version>2.23.4</version>
116+
<scope>test</scope>
117+
</dependency>
118+
112119
</dependencies>
113120

114121
<build>

algo/src/main/java/org/neo4j/graphalgo/CentralityUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.neo4j.kernel.internal.GraphDatabaseAPI;
1616
import org.neo4j.logging.Log;
1717

18+
import java.util.function.Function;
1819
import java.util.stream.IntStream;
1920
import java.util.stream.LongStream;
2021
import java.util.stream.Stream;
@@ -63,4 +64,12 @@ static Stream<CentralityScore> streamResults(Graph graph, CentralityResult score
6364
);
6465
});
6566
}
67+
68+
public static void normalizeArray(double[][] partitions, Function<Double, Double> normalizationFunction) {
69+
for (double[] partition : partitions) {
70+
for (int j = 0; j < partition.length; j++) {
71+
partition[j] = normalizationFunction.apply(partition[j]);
72+
}
73+
}
74+
}
6675
}

algo/src/main/java/org/neo4j/graphalgo/impl/results/CentralityResult.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.neo4j.graphalgo.core.write.Exporter;
2222

23+
import java.util.function.Function;
24+
2325
public interface CentralityResult {
2426

2527
double score(int nodeId);
@@ -28,6 +30,8 @@ public interface CentralityResult {
2830

2931
void export(String propertyName, Exporter exporter);
3032

33+
void export(String propertyName, Exporter exporter, Function<Double, Double> normalizationFunction);
34+
3135
double computeMax();
3236

3337
double computeL2Norm();

algo/src/main/java/org/neo4j/graphalgo/impl/results/DoubleArrayResult.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.neo4j.graphalgo.core.write.Exporter;
44
import org.neo4j.graphalgo.core.write.Translators;
55

6+
import java.util.Arrays;
7+
import java.util.function.Function;
8+
69
public final class DoubleArrayResult implements CentralityResult {
710
private final double[] result;
811

@@ -19,6 +22,14 @@ public void export(
1922
Translators.DOUBLE_ARRAY_TRANSLATOR);
2023
}
2124

25+
@Override
26+
public void export(String propertyName, Exporter exporter, Function<Double, Double> normalizationFunction) {
27+
exporter.write(
28+
propertyName,
29+
Arrays.stream(result).map(normalizationFunction::apply).toArray(),
30+
Translators.DOUBLE_ARRAY_TRANSLATOR);
31+
}
32+
2233
@Override
2334
public double computeMax() {
2435
return NormalizationComputations.max(result, 1.0);

algo/src/main/java/org/neo4j/graphalgo/impl/results/NormalizedCentralityResult.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public NormalizedCentralityResult(CentralityResult result, Function<Double, Doub
1414
}
1515

1616
@Override
17-
public void export(String propertyName, Exporter exporter) {
18-
result.export(propertyName, exporter);
17+
public void export(String propertyName, Exporter exporter, Function<Double, Double> normalizationFunction) {
18+
result.export(propertyName, exporter, normalizationFunction);
1919
}
2020

2121
@Override
@@ -28,6 +28,11 @@ public double score(long nodeId) {
2828
return normalizationFunction.apply(result.score(nodeId));
2929
}
3030

31+
@Override
32+
public void export(String propertyName, Exporter exporter) {
33+
export(propertyName, exporter, normalizationFunction);
34+
}
35+
3136
@Override
3237
public double computeMax() {
3338
return result.computeMax();

algo/src/main/java/org/neo4j/graphalgo/impl/results/PartitionedDoubleArrayResult.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.neo4j.graphalgo.impl.results;
22

3+
import org.neo4j.graphalgo.CentralityUtils;
34
import org.neo4j.graphalgo.core.write.Exporter;
45
import org.neo4j.graphalgo.core.write.PropertyTranslator;
56

7+
import java.util.function.Function;
8+
69
import static org.neo4j.graphalgo.core.utils.ArrayUtil.binaryLookup;
710

811
public final class PartitionedDoubleArrayResult implements CentralityResult, PropertyTranslator.OfDouble<double[][]> {
@@ -21,6 +24,14 @@ public void export(final String propertyName, final Exporter exporter) {
2124
exporter.write(propertyName, partitions, this);
2225
}
2326

27+
28+
@Override
29+
public void export(String propertyName, Exporter exporter, Function<Double, Double> normalizationFunction) {
30+
CentralityUtils.normalizeArray(partitions, normalizationFunction);
31+
export(propertyName, exporter);
32+
}
33+
34+
2435
@Override
2536
public double computeMax() {
2637
return NormalizationComputations.max(partitions);

algo/src/main/java/org/neo4j/graphalgo/impl/results/PartitionedPrimitiveDoubleArrayResult.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.neo4j.graphalgo.impl.results;
22

3+
import org.neo4j.graphalgo.CentralityUtils;
34
import org.neo4j.graphalgo.core.write.Exporter;
45
import org.neo4j.graphalgo.core.write.PropertyTranslator;
56

7+
import java.util.function.Function;
8+
69
import static org.neo4j.graphalgo.core.utils.ArrayUtil.binaryLookup;
710

811
public final class PartitionedPrimitiveDoubleArrayResult implements CentralityResult, PropertyTranslator.OfDouble<double[][]> {
@@ -27,10 +30,15 @@ public void export(
2730
);
2831
}
2932

33+
@Override
34+
public void export(String propertyName, Exporter exporter, Function<Double, Double> normalizationFunction) {
35+
CentralityUtils.normalizeArray(partitions, normalizationFunction);
36+
export(propertyName, exporter);
37+
}
38+
3039
@Override
3140
public double computeMax() {
3241
return NormalizationComputations.max(partitions);
33-
3442
}
3543

3644
@Override

algo/src/test/java/org/neo4j/graphalgo/impl/results/CentralityResultTest.java

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package org.neo4j.graphalgo.impl.results;
22

33
import org.junit.Test;
4+
import org.mockito.ArgumentMatcher;
5+
import org.neo4j.graphalgo.Normalization;
6+
import org.neo4j.graphalgo.core.write.Exporter;
7+
import org.neo4j.graphalgo.core.write.Translators;
8+
9+
import java.util.Arrays;
410

511
import static org.junit.Assert.assertEquals;
12+
import static org.mockito.Mockito.*;
613

714
public class CentralityResultTest {
815
@Test
@@ -14,6 +21,18 @@ public void doubleArrayResult() {
1421
assertEquals(5.477225575051661, result.computeL2Norm(), 0.01);
1522
}
1623

24+
25+
@Test
26+
public void doubleArrayResultExport() {
27+
String property = "eigenvector";
28+
DoubleArrayResult result = new DoubleArrayResult(new double[] {1,2,3,4});
29+
30+
Exporter exporter = mock(Exporter.class);
31+
Normalization.MAX.apply(result).export(property, exporter);
32+
33+
verify(exporter).write(property, new double[] {0.25,0.5,0.75,1.0}, Translators.DOUBLE_ARRAY_TRANSLATOR);
34+
}
35+
1736
@Test
1837
public void partitionedPrimitiveDoubleArrayResult() {
1938
double[][] partitions = new double[][] { {1.0,2.0}, {3.0,4.0} };
@@ -25,6 +44,20 @@ public void partitionedPrimitiveDoubleArrayResult() {
2544
assertEquals(5.477225575051661, result.computeL2Norm(), 0.01);
2645
}
2746

47+
@Test
48+
public void partitionedPrimitiveDoubleArrayResultExport() {
49+
String property = "eigenvector";
50+
double[][] partitions = new double[][] { {1.0,2.0}, {3.0,4.0} };
51+
int[] starts = new int[] { 0, 2};
52+
PartitionedPrimitiveDoubleArrayResult result = new PartitionedPrimitiveDoubleArrayResult(partitions, starts);
53+
54+
Exporter exporter = mock(Exporter.class);
55+
Normalization.MAX.apply(result).export(property, exporter);
56+
57+
verify(exporter).write(eq(property), argThat(arrayEq(new double[][] { {0.25, 0.5}, {0.75, 1.0} })), eq(result));
58+
}
59+
60+
2861
@Test
2962
public void partitionedDoubleArrayResult() {
3063
double[][] partitions = new double[][] { {1.0,2.0}, {3.0,4.0} };
@@ -35,4 +68,36 @@ public void partitionedDoubleArrayResult() {
3568
assertEquals(10.0, result.computeL1Norm(), 0.01);
3669
assertEquals(5.477225575051661, result.computeL2Norm(), 0.01);
3770
}
38-
}
71+
72+
@Test
73+
public void partitionedDoubleArrayResultExport() {
74+
String property = "eigenvector";
75+
double[][] partitions = new double[][] { {1.0,2.0}, {3.0,4.0} };
76+
long[] starts = new long[] { 0, 2};
77+
PartitionedDoubleArrayResult result = new PartitionedDoubleArrayResult(partitions, starts);
78+
79+
Exporter exporter = mock(Exporter.class);
80+
Normalization.MAX.apply(result).export(property, exporter);
81+
82+
verify(exporter).write(eq(property), argThat(arrayEq(new double[][] { {0.25, 0.5}, {0.75, 1.0} })), eq(result));
83+
}
84+
85+
86+
private ArrayMatcher arrayEq(double[][] expected) {
87+
return new ArrayMatcher(expected);
88+
}
89+
90+
91+
class ArrayMatcher implements ArgumentMatcher<double[][]> {
92+
private double[][] expected;
93+
94+
public ArrayMatcher(double[][] expected) {
95+
this.expected = expected;
96+
}
97+
98+
@Override
99+
public boolean matches(double[][] actual) {
100+
return Arrays.deepEquals(expected, actual);
101+
}
102+
}
103+
}

algo/src/test/java/org/neo4j/graphalgo/impl/results/NormalizedCentralityResultTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import org.junit.Test;
44
import org.neo4j.graphalgo.Normalization;
5+
import org.neo4j.graphalgo.core.write.Exporter;
56

67
import static org.junit.Assert.*;
7-
import static org.mockito.Mockito.mock;
8-
import static org.mockito.Mockito.when;
8+
import static org.mockito.Mockito.*;
99

1010
public class NormalizedCentralityResultTest {
1111
@Test

0 commit comments

Comments
 (0)