Skip to content

Commit efc805e

Browse files
Addressing review comments
Co-authored-by: Florentin Dörre <[email protected]>
1 parent cc61bd8 commit efc805e

File tree

2 files changed

+24
-48
lines changed

2 files changed

+24
-48
lines changed

algo/src/main/java/org/neo4j/gds/kmeans/Kmeans.java

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,6 @@
3434
import java.util.Optional;
3535
import java.util.SplittableRandom;
3636
import java.util.concurrent.ExecutorService;
37-
import java.util.concurrent.atomic.AtomicInteger;
38-
39-
enum InputCondition {
40-
NORMAL(0), NAN(1), UNEQUALDIMENSION(2);
41-
public final int value;
42-
43-
private InputCondition(int value) {
44-
this.value = value;
45-
}
46-
47-
}
4837

4938
public class Kmeans extends Algorithm<KmeansResult> {
5039

@@ -109,13 +98,8 @@ public static Kmeans createKmeans(Graph graph, KmeansBaseConfig config, KmeansCo
10998
public KmeansResult compute() {
11099
progressTracker.beginSubTask();
111100

112-
var inputCondition = checkInputValidity();
113-
if (inputCondition == InputCondition.NAN) {
114-
throw new IllegalArgumentException("Input for K-Means should not contain any NaN values");
115-
} else if (inputCondition == InputCondition.UNEQUALDIMENSION) {
116-
throw new IllegalStateException(
117-
"All property arrays for K-Means should have the same number of dimensions");
118-
}
101+
checkInputValidity();
102+
119103

120104
if (k > graph.nodeCount()) {
121105
// Every node in its own community. Warn and return early.
@@ -197,43 +181,35 @@ private static SplittableRandom getSplittableRandom(Optional<Long> randomSeed) {
197181
return randomSeed.map(SplittableRandom::new).orElseGet(SplittableRandom::new);
198182
}
199183

200-
private InputCondition checkInputValidity() {
201-
AtomicInteger inputState = new AtomicInteger(InputCondition.NORMAL.value);
184+
private void checkInputValidity() {
202185
ParallelUtil.parallelForEachNode(graph.nodeCount(), concurrency, nodeId -> {
203-
if (inputState.get() == InputCondition.NORMAL.value) {
204-
if (nodePropertyValues.valueType() == ValueType.FLOAT_ARRAY) {
205-
var value = nodePropertyValues.floatArrayValue(nodeId);
206-
if (value.length != dimensions) {
207-
inputState.set(InputCondition.UNEQUALDIMENSION.value);
208-
} else {
209-
for (int dimension = 0; dimension < dimensions; ++dimension) {
210-
if (Float.isNaN(value[dimension])) {
211-
inputState.set(InputCondition.NAN.value);
212-
break;
213-
}
186+
if (nodePropertyValues.valueType() == ValueType.FLOAT_ARRAY) {
187+
var value = nodePropertyValues.floatArrayValue(nodeId);
188+
if (value.length != dimensions) {
189+
throw new IllegalStateException(
190+
"All property arrays for K-Means should have the same number of dimensions");
191+
} else {
192+
for (int dimension = 0; dimension < dimensions; ++dimension) {
193+
if (Float.isNaN(value[dimension])) {
194+
throw new IllegalArgumentException("Input for K-Means should not contain any NaN values");
214195
}
215196
}
197+
}
198+
} else {
199+
var value = nodePropertyValues.doubleArrayValue(nodeId);
200+
if (value.length != dimensions) {
201+
throw new IllegalStateException(
202+
"All property arrays for K-Means should have the same number of dimensions");
216203
} else {
217-
var value = nodePropertyValues.doubleArrayValue(nodeId);
218-
if (value.length != dimensions) {
219-
inputState.set(InputCondition.UNEQUALDIMENSION.value);
220-
} else {
221-
for (int dimension = 0; dimension < dimensions; ++dimension) {
222-
if (Double.isNaN(value[dimension])) {
223-
inputState.set(InputCondition.NAN.value);
224-
break;
225-
}
204+
for (int dimension = 0; dimension < dimensions; ++dimension) {
205+
if (Double.isNaN(value[dimension])) {
206+
throw new IllegalArgumentException("Input for K-Means should not contain any NaN values");
207+
226208
}
227209
}
228210
}
211+
229212
}
230213
});
231-
InputCondition inputCondition = InputCondition.NORMAL;
232-
if (inputState.get() == InputCondition.UNEQUALDIMENSION.value) {
233-
inputCondition = InputCondition.UNEQUALDIMENSION;
234-
} else if (inputState.get() == InputCondition.NAN.value) {
235-
inputCondition = InputCondition.NAN;
236-
}
237-
return inputCondition;
238214
}
239215
}

doc/modules/ROOT/pages/algorithms/alpha/kmeans.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ For more information on this algorithm, see:
2323
[[algorithm-k-means-considerations]]
2424
== Considerations
2525

26-
In order for K-Means to work properly, the property arrays for all nodes must have the same number of elements. In addition, they must consist solely of well-defined float numbers and not contain any NaN values.
26+
In order for K-Means to work properly, the property arrays for all nodes must have the same number of elements. Also, they should contain exclusively numbers and not contain any NaN values.
2727

2828
[[algorithms-k-means-syntax]]
2929
== Syntax

0 commit comments

Comments
 (0)