Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for issue 305 #309

Merged
merged 2 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@

package com.amazon.randomcutforest.state.store;

import static com.amazon.randomcutforest.CommonUtils.checkArgument;
import static com.amazon.randomcutforest.CommonUtils.checkNotNull;
import static com.amazon.randomcutforest.CommonUtils.toDoubleArray;
import static com.amazon.randomcutforest.CommonUtils.toFloatArray;

import lombok.Getter;
import lombok.Setter;

import com.amazon.randomcutforest.config.Precision;
import com.amazon.randomcutforest.state.IStateMapper;
import com.amazon.randomcutforest.state.Version;
import com.amazon.randomcutforest.store.PointStore;
import com.amazon.randomcutforest.store.PointStoreLarge;
import com.amazon.randomcutforest.util.ArrayPacking;
import lombok.Getter;
import lombok.Setter;

import static com.amazon.randomcutforest.CommonUtils.checkArgument;
import static com.amazon.randomcutforest.CommonUtils.checkNotNull;
import static com.amazon.randomcutforest.CommonUtils.toDoubleArray;
import static com.amazon.randomcutforest.CommonUtils.toFloatArray;

@Getter
@Setter
Expand All @@ -55,7 +54,12 @@ public PointStore toModel(PointStoreState state, long seed) {
int[] locationList = new int[indexCapacity];
int[] tempList = ArrayPacking.unpackInts(state.getLocationList(), state.isCompressed());
if (!state.getVersion().equals(Version.V3_0)) {
System.arraycopy(tempList, 0, locationList, 0, tempList.length);
int shingleSize = state.getShingleSize();
int baseDimension = dimensions / shingleSize;
for (int i = 0; i < tempList.length; i++) {
checkArgument(tempList[i] % baseDimension == 0, "the location field should be a multiple of dimension/shingle size for versions before 3.0");
locationList[i] = tempList[i] / baseDimension;
}
} else {
int[] duplicateRefs = null;
if (state.getDuplicateRefs() != null) {
Expand Down
6 changes: 6 additions & 0 deletions Java/parkservices/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.parkservices.state;

import lombok.Getter;

@Getter
public enum V2TRCFJsonResource {

TRCF_1("state_1.json"), TRCF_2("state_2.json");

private final String resource;

V2TRCFJsonResource(String resource) {
this.resource = resource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.parkservices.state;

import com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.fail;

public class V2TRCFToV3StateConverterTest {

@ParameterizedTest
@EnumSource(V2TRCFJsonResource.class)
public void test(V2TRCFJsonResource jsonResource) {

try (InputStream is = V2TRCFToV3StateConverterTest.class.getResourceAsStream(jsonResource.getResource());
BufferedReader rr = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));) {

StringBuilder b = new StringBuilder();
String line;
while ((line = rr.readLine()) != null) {
b.append(line);
}

String json = b.toString();

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
ThresholdedRandomCutForestState state = mapper.readValue(json, ThresholdedRandomCutForestState.class);

ThresholdedRandomCutForestMapper mapper1 = new ThresholdedRandomCutForestMapper();
ThresholdedRandomCutForest forest = mapper1.toModel(state);

} catch (IOException e) {
fail("Unable to load JSON resource");
}
}

}
Loading