You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@systemds.apache.org by GitBox <gi...@apache.org> on 2021/02/27 09:48:09 UTC

[GitHub] [systemds] OlgaOvcharenko commented on a change in pull request #1192: Encoder serialization test

OlgaOvcharenko commented on a change in pull request #1192:
URL: https://github.com/apache/systemds/pull/1192#discussion_r584097932



##########
File path: src/test/java/org/apache/sysds/test/functions/transform/EncoderSerializationTest.java
##########
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License 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 org.apache.sysds.test.functions.transform;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import org.apache.sysds.common.Types;
+import org.apache.sysds.runtime.matrix.data.FrameBlock;
+import org.apache.sysds.runtime.transform.decode.Decoder;
+import org.apache.sysds.runtime.transform.decode.DecoderComposite;
+import org.apache.sysds.runtime.transform.decode.DecoderFactory;
+import org.apache.sysds.runtime.transform.encode.Encoder;
+import org.apache.sysds.runtime.transform.encode.EncoderComposite;
+import org.apache.sysds.runtime.transform.encode.EncoderFactory;
+import org.apache.sysds.runtime.util.UtilFunctions;
+import org.apache.sysds.test.AutomatedTestBase;
+import org.apache.sysds.test.TestUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class EncoderSerializationTest extends AutomatedTestBase
+{
+	private final static int rows = 2791;
+	private final static int cols = 8;
+
+	private final static Types.ValueType[] schemaStrings = new Types.ValueType[]{
+		Types.ValueType.STRING, Types.ValueType.STRING, Types.ValueType.STRING, Types.ValueType.STRING,
+		Types.ValueType.STRING, Types.ValueType.STRING, Types.ValueType.STRING, Types.ValueType.STRING};
+	private final static Types.ValueType[] schemaMixed = new Types.ValueType[]{
+		Types.ValueType.STRING, Types.ValueType.FP64, Types.ValueType.INT64, Types.ValueType.BOOLEAN,
+		Types.ValueType.STRING, Types.ValueType.FP64, Types.ValueType.INT64, Types.ValueType.BOOLEAN};
+
+
+	public enum TransformType {
+		RECODE,
+		DUMMY
+	}
+
+	@Override
+	public void setUp() {
+		TestUtils.clearAssertionInformation();
+	}
+
+	@Test
+	public void testComposite1() { runTransformSerTest(TransformType.DUMMY, schemaStrings); }
+
+	@Test
+	public void testComposite2() { runTransformSerTest(TransformType.RECODE, schemaMixed); }
+
+	@Test
+	public void testComposite3() { runTransformSerTest(TransformType.RECODE, schemaStrings); }
+
+	@Test
+	public void testComposite4() { runTransformSerTest(TransformType.DUMMY, schemaMixed); }
+
+
+	private void runTransformSerTest(TransformType type, Types.ValueType[] schema) {
+		//data generation
+		double[][] A = getRandomMatrix(rows, cols, -10, 10, 0.9, 8234);
+
+		//init data frame
+		FrameBlock frame = new FrameBlock(schema);
+
+		//init data frame
+		Object[] row = new Object[schema.length];
+		for( int i=0; i < rows; i++) {
+			for( int j=0; j<schema.length; j++ )
+				A[i][j] = UtilFunctions.objectToDouble(schema[j],
+					row[j] = UtilFunctions.doubleToObject(schema[j], A[i][j]));
+			frame.appendRow(row);
+		}
+
+		String spec = "";
+		if(type == TransformType.DUMMY)
+			spec = "{\n \"ids\": true\n, \"dummycode\":[ 2, 7, 8, 1 ]\n\n}";
+		else if(type == TransformType.RECODE)
+			spec = "{\n \"ids\": true\n, \"recode\":[ 2, 7, 1, 8 ]\n\n}";
+
+
+		frame.setSchema(schema);
+		String[] cnames = frame.getColumnNames();
+
+		Encoder encoderIn = EncoderFactory.createEncoder(spec, cnames, frame.getNumColumns(), null);
+		EncoderComposite encoderOut;
+
+		// serialization and deserialization
+		encoderOut = (EncoderComposite) serializeDeserialize(encoderIn);
+		// compare
+		Assert.assertArrayEquals(encoderIn.getColList(), encoderOut.getColList());
+		Assert.assertEquals(encoderIn.getNumCols(), encoderOut.getNumCols());
+
+		List<Encoder> eListIn = ((EncoderComposite) encoderIn).getEncoders();
+		List<Encoder> eListOut = encoderOut.getEncoders();
+		for(int i = 0; i < eListIn.size();  i++) {
+			Assert.assertThat(eListIn.get(i).getColList(), is(eListOut.get(i).getColList()));
+			Assert.assertThat(eListIn.get(i).getNumCols(), is(eListOut.get(i).getNumCols()));

Review comment:
       Hi @Baunsgaard, I used it to compare arrays and long values (like "assertThat(eListIn.get(i).getColList(), equalTo(eListOut.get(i).getColList())))"), but now replaced by simple "assertArrayEquals" and "assertEquals".




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org