From ad37748b81ff053ae362c53a3f36f85002f63c88 Mon Sep 17 00:00:00 2001 From: Yaliang Wu Date: Fri, 25 Mar 2022 04:26:25 +0000 Subject: [PATCH] revert: implement Serializable for ThresholdedRandomCutForestState Signed-off-by: Yaliang Wu --- .../state/ExecutionContext.java | 6 +- .../state/RandomCutForestState.java | 4 +- .../state/returntypes/DiVectorState.java | 6 +- .../state/sampler/CompactSamplerState.java | 6 +- .../state/store/NodeStoreState.java | 5 +- .../state/store/PointStoreState.java | 6 +- .../state/tree/CompactRandomCutTreeState.java | 6 +- .../serialization/ObjectStreamExample.java | 134 ------------------ .../ThresholdedRandomCutForestState.java | 6 +- .../state/preprocessor/PreprocessorState.java | 5 +- .../state/statistics/DeviationState.java | 6 +- .../threshold/BasicThresholderState.java | 5 +- 12 files changed, 11 insertions(+), 184 deletions(-) delete mode 100644 Java/examples/src/main/java/com/amazon/randomcutforest/examples/serialization/ObjectStreamExample.java diff --git a/Java/core/src/main/java/com/amazon/randomcutforest/state/ExecutionContext.java b/Java/core/src/main/java/com/amazon/randomcutforest/state/ExecutionContext.java index ed1b9734..5b2822f9 100644 --- a/Java/core/src/main/java/com/amazon/randomcutforest/state/ExecutionContext.java +++ b/Java/core/src/main/java/com/amazon/randomcutforest/state/ExecutionContext.java @@ -15,14 +15,10 @@ package com.amazon.randomcutforest.state; -import java.io.Serializable; - import lombok.Data; @Data -public class ExecutionContext implements Serializable { - private static final long serialVersionUID = 1L; - +public class ExecutionContext { private boolean parallelExecutionEnabled; private int threadPoolSize; diff --git a/Java/core/src/main/java/com/amazon/randomcutforest/state/RandomCutForestState.java b/Java/core/src/main/java/com/amazon/randomcutforest/state/RandomCutForestState.java index 9cd6d10f..13d026b4 100644 --- a/Java/core/src/main/java/com/amazon/randomcutforest/state/RandomCutForestState.java +++ b/Java/core/src/main/java/com/amazon/randomcutforest/state/RandomCutForestState.java @@ -17,7 +17,6 @@ import static com.amazon.randomcutforest.state.Version.V3_0; -import java.io.Serializable; import java.util.List; import lombok.Data; @@ -31,8 +30,7 @@ * that the forest can be serialized and deserialized. */ @Data -public class RandomCutForestState implements Serializable { - private static final long serialVersionUID = 1L; +public class RandomCutForestState { private String version = V3_0; diff --git a/Java/core/src/main/java/com/amazon/randomcutforest/state/returntypes/DiVectorState.java b/Java/core/src/main/java/com/amazon/randomcutforest/state/returntypes/DiVectorState.java index 09ceb203..282d033c 100644 --- a/Java/core/src/main/java/com/amazon/randomcutforest/state/returntypes/DiVectorState.java +++ b/Java/core/src/main/java/com/amazon/randomcutforest/state/returntypes/DiVectorState.java @@ -15,8 +15,6 @@ package com.amazon.randomcutforest.state.returntypes; -import java.io.Serializable; - import lombok.Getter; import lombok.Setter; @@ -34,9 +32,7 @@ */ @Getter @Setter -public class DiVectorState implements Serializable { - private static final long serialVersionUID = 1L; - +public class DiVectorState { double[] high; double[] low; } \ No newline at end of file diff --git a/Java/core/src/main/java/com/amazon/randomcutforest/state/sampler/CompactSamplerState.java b/Java/core/src/main/java/com/amazon/randomcutforest/state/sampler/CompactSamplerState.java index 74806af4..c46c7016 100644 --- a/Java/core/src/main/java/com/amazon/randomcutforest/state/sampler/CompactSamplerState.java +++ b/Java/core/src/main/java/com/amazon/randomcutforest/state/sampler/CompactSamplerState.java @@ -17,8 +17,6 @@ import static com.amazon.randomcutforest.state.Version.V2_0; -import java.io.Serializable; - import lombok.Data; /** @@ -26,9 +24,7 @@ * {@link com.amazon.randomcutforest.sampler.CompactSampler}. */ @Data -public class CompactSamplerState implements Serializable { - private static final long serialVersionUID = 1L; - +public class CompactSamplerState { /** * a version string for extensibility */ diff --git a/Java/core/src/main/java/com/amazon/randomcutforest/state/store/NodeStoreState.java b/Java/core/src/main/java/com/amazon/randomcutforest/state/store/NodeStoreState.java index ec4d3b71..38a8929d 100644 --- a/Java/core/src/main/java/com/amazon/randomcutforest/state/store/NodeStoreState.java +++ b/Java/core/src/main/java/com/amazon/randomcutforest/state/store/NodeStoreState.java @@ -17,13 +17,10 @@ import static com.amazon.randomcutforest.state.Version.V2_0; -import java.io.Serializable; - import lombok.Data; @Data -public class NodeStoreState implements Serializable { - private static final long serialVersionUID = 1L; +public class NodeStoreState { private String version = V2_0; diff --git a/Java/core/src/main/java/com/amazon/randomcutforest/state/store/PointStoreState.java b/Java/core/src/main/java/com/amazon/randomcutforest/state/store/PointStoreState.java index 49cf7d6d..db61d5bf 100644 --- a/Java/core/src/main/java/com/amazon/randomcutforest/state/store/PointStoreState.java +++ b/Java/core/src/main/java/com/amazon/randomcutforest/state/store/PointStoreState.java @@ -17,8 +17,6 @@ import static com.amazon.randomcutforest.state.Version.V2_0; -import java.io.Serializable; - import lombok.Data; /** @@ -28,9 +26,7 @@ * {@code floatData} will be null. */ @Data -public class PointStoreState implements Serializable { - private static final long serialVersionUID = 1L; - +public class PointStoreState { /** * version string for future extensibility */ diff --git a/Java/core/src/main/java/com/amazon/randomcutforest/state/tree/CompactRandomCutTreeState.java b/Java/core/src/main/java/com/amazon/randomcutforest/state/tree/CompactRandomCutTreeState.java index 22effebb..140fe832 100644 --- a/Java/core/src/main/java/com/amazon/randomcutforest/state/tree/CompactRandomCutTreeState.java +++ b/Java/core/src/main/java/com/amazon/randomcutforest/state/tree/CompactRandomCutTreeState.java @@ -17,16 +17,12 @@ import static com.amazon.randomcutforest.state.Version.V2_0; -import java.io.Serializable; - import lombok.Data; import com.amazon.randomcutforest.state.store.NodeStoreState; @Data -public class CompactRandomCutTreeState implements Serializable { - private static final long serialVersionUID = 1L; - +public class CompactRandomCutTreeState { private String version = V2_0; private int root; private int maxSize; diff --git a/Java/examples/src/main/java/com/amazon/randomcutforest/examples/serialization/ObjectStreamExample.java b/Java/examples/src/main/java/com/amazon/randomcutforest/examples/serialization/ObjectStreamExample.java deleted file mode 100644 index f7cd08c6..00000000 --- a/Java/examples/src/main/java/com/amazon/randomcutforest/examples/serialization/ObjectStreamExample.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - -package com.amazon.randomcutforest.examples.serialization; - -import com.amazon.randomcutforest.RandomCutForest; -import com.amazon.randomcutforest.config.Precision; -import com.amazon.randomcutforest.examples.Example; -import com.amazon.randomcutforest.state.RandomCutForestMapper; -import com.amazon.randomcutforest.state.RandomCutForestState; -import com.amazon.randomcutforest.testutils.NormalMixtureTestData; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - -public class ObjectStreamExample implements Example { - - public static void main(String[] args) throws Exception { - new ObjectStreamExample().run(); - } - - @Override - public String command() { - return "object_stream"; - } - - @Override - public String description() { - return "serialize a Random Cut Forest with object stream"; - } - - @Override - public void run() throws Exception { - // Create and populate a random cut forest - - int dimensions = 10; - int numberOfTrees = 50; - int sampleSize = 256; - Precision precision = Precision.FLOAT_32; - - RandomCutForest forest = RandomCutForest.builder().compact(true).dimensions(dimensions) - .numberOfTrees(numberOfTrees).sampleSize(sampleSize).precision(precision).build(); - - int dataSize = 1000 * sampleSize; - NormalMixtureTestData testData = new NormalMixtureTestData(); - for (double[] point : testData.generateTestData(dataSize, dimensions)) { - forest.update(point); - } - - // Convert to an array of bytes and print the size - - RandomCutForestMapper mapper = new RandomCutForestMapper(); - mapper.setSaveExecutorContextEnabled(true); - System.out.printf("dimensions = %d, numberOfTrees = %d, sampleSize = %d, precision = %s%n", dimensions, - numberOfTrees, sampleSize, precision); - byte[] bytes = serialize(mapper.toState(forest)); - System.out.printf("Object output stream size = %d bytes%n", bytes.length); - - // Restore from object stream and compare anomaly scores produced by the two - // forests - - RandomCutForestState state2 = (RandomCutForestState) deserialize(bytes); - RandomCutForest forest2 = mapper.toModel(state2); - - int testSize = 100; - double delta = Math.log(sampleSize) / Math.log(2) * 0.05; - - int differences = 0; - int anomalies = 0; - - for (double[] point : testData.generateTestData(testSize, dimensions)) { - double score = forest.getAnomalyScore(point); - double score2 = forest2.getAnomalyScore(point); - - // we mostly care that points that are scored as an anomaly by one forest are - // also scored as an anomaly by the other forest - if (score > 1 || score2 > 1) { - anomalies++; - if (Math.abs(score - score2) > delta) { - differences++; - } - } - - forest.update(point); - forest2.update(point); - } - - // first validate that this was a nontrivial test - if (anomalies == 0) { - throw new IllegalStateException("test data did not produce any anomalies"); - } - - // validate that the two forests agree on anomaly scores - if (differences >= 0.01 * testSize) { - throw new IllegalStateException("restored forest does not agree with original forest"); - } - - System.out.println("Looks good!"); - } - - private byte[] serialize(Object model) { - try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) { - objectOutputStream.writeObject(model); - objectOutputStream.flush(); - return byteArrayOutputStream.toByteArray(); - } catch (IOException e) { - throw new RuntimeException("Failed to serialize model.", e.getCause()); - } - } - - private Object deserialize(byte[] modelBin) { - try (ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(modelBin))) { - return objectInputStream.readObject(); - } catch (IOException | ClassNotFoundException e) { - throw new RuntimeException("Failed to deserialize model.", e.getCause()); - } - } -} diff --git a/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/ThresholdedRandomCutForestState.java b/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/ThresholdedRandomCutForestState.java index 763d7273..09926c8e 100644 --- a/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/ThresholdedRandomCutForestState.java +++ b/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/ThresholdedRandomCutForestState.java @@ -17,8 +17,6 @@ import static com.amazon.randomcutforest.state.Version.V2_1; -import java.io.Serializable; - import lombok.Data; import com.amazon.randomcutforest.parkservices.state.preprocessor.PreprocessorState; @@ -27,9 +25,7 @@ import com.amazon.randomcutforest.state.returntypes.DiVectorState; @Data -public class ThresholdedRandomCutForestState implements Serializable { - private static final long serialVersionUID = 1L; - +public class ThresholdedRandomCutForestState { private String version = V2_1; RandomCutForestState forestState; private BasicThresholderState thresholderState; diff --git a/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/preprocessor/PreprocessorState.java b/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/preprocessor/PreprocessorState.java index a6bd9c01..f898af4e 100644 --- a/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/preprocessor/PreprocessorState.java +++ b/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/preprocessor/PreprocessorState.java @@ -17,15 +17,12 @@ import static com.amazon.randomcutforest.state.Version.V2_1; -import java.io.Serializable; - import lombok.Data; import com.amazon.randomcutforest.parkservices.state.statistics.DeviationState; @Data -public class PreprocessorState implements Serializable { - private static final long serialVersionUID = 1L; +public class PreprocessorState { private String version = V2_1; private double useImputedFraction; diff --git a/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/statistics/DeviationState.java b/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/statistics/DeviationState.java index 9e7273be..4b275835 100644 --- a/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/statistics/DeviationState.java +++ b/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/statistics/DeviationState.java @@ -15,14 +15,10 @@ package com.amazon.randomcutforest.parkservices.state.statistics; -import java.io.Serializable; - import lombok.Data; @Data -public class DeviationState implements Serializable { - private static final long serialVersionUID = 1L; - +public class DeviationState { private double discount; private double weight; diff --git a/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/threshold/BasicThresholderState.java b/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/threshold/BasicThresholderState.java index 9bdaa3dd..71a33fee 100644 --- a/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/threshold/BasicThresholderState.java +++ b/Java/parkservices/src/main/java/com/amazon/randomcutforest/parkservices/state/threshold/BasicThresholderState.java @@ -15,15 +15,12 @@ package com.amazon.randomcutforest.parkservices.state.threshold; -import java.io.Serializable; - import lombok.Data; import com.amazon.randomcutforest.parkservices.state.statistics.DeviationState; @Data -public class BasicThresholderState implements Serializable { - private static final long serialVersionUID = 1L; +public class BasicThresholderState { private long randomseed;