Github user takuti commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/107#discussion_r134151609 --- Diff: core/src/test/java/hivemall/evaluation/FMeasureUDAFTest.java --- @@ -0,0 +1,355 @@ +package hivemall.evaluation; + +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator; +import org.apache.hadoop.hive.ql.udf.generic.SimpleGenericUDAFParameterInfo; +import org.apache.hadoop.hive.serde2.io.DoubleWritable; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils; +import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + + +public class FMeasureUDAFTest { + FMeasureUDAF fmeasure; + GenericUDAFEvaluator evaluator; + ObjectInspector[] inputOIs; + FMeasureUDAF.FMeasureAggregationBuffer agg; + + @Before + public void setUp() throws Exception { + fmeasure = new FMeasureUDAF(); + inputOIs = new ObjectInspector[] { + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), + ObjectInspectorUtils.getConstantObjectInspector( + PrimitiveObjectInspectorFactory.javaStringObjectInspector, "-beta 1.")}; + + evaluator = fmeasure.getEvaluator(new SimpleGenericUDAFParameterInfo(inputOIs, false, false)); + + agg = (FMeasureUDAF.FMeasureAggregationBuffer) evaluator.getNewAggregationBuffer(); + } + + private void setUpWithArguments(double beta, String average) throws Exception { + fmeasure = new FMeasureUDAF(); + inputOIs = new ObjectInspector[] { + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), + ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableLongObjectInspector), + ObjectInspectorUtils.getConstantObjectInspector( + PrimitiveObjectInspectorFactory.javaStringObjectInspector, "-beta " + beta + + " -average " + average)}; + + evaluator = fmeasure.getEvaluator(new SimpleGenericUDAFParameterInfo(inputOIs, false, false)); + agg = (FMeasureUDAF.FMeasureAggregationBuffer) evaluator.getNewAggregationBuffer(); + } + + private void binarySetUp(Object actual, Object predicted, double beta, String average) + throws Exception { + fmeasure = new FMeasureUDAF(); + inputOIs = new ObjectInspector[3]; + + String actualClassName = actual.getClass().getName(); + if (actualClassName.equals("java.lang.Integer")) { + inputOIs[0] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.INT); + } else if (actualClassName.equals("java.lang.Boolean")) { + inputOIs[0] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.BOOLEAN); + } else if ((actualClassName.equals("java.lang.String"))) { + inputOIs[0] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING); + } + + String predicatedClassName = predicted.getClass().getName(); + if (predicatedClassName.equals("java.lang.Integer")) { + inputOIs[1] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.INT); + } else if (predicatedClassName.equals("java.lang.Boolean")) { + inputOIs[1] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.BOOLEAN); + } else if ((predicatedClassName.equals("java.lang.String"))) { + inputOIs[1] = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING); + } + + inputOIs[2] = ObjectInspectorUtils.getConstantObjectInspector( + PrimitiveObjectInspectorFactory.javaStringObjectInspector, "-beta " + beta + + " -average " + average); + + evaluator = fmeasure.getEvaluator(new SimpleGenericUDAFParameterInfo(inputOIs, false, false)); + agg = (FMeasureUDAF.FMeasureAggregationBuffer) evaluator.getNewAggregationBuffer(); + } + + @Test + public void testBinaryMultiSamplesAverageBinary() throws Exception { + final int[] actual = {0, 1, 0, 0, 0, 1, 0, 0}; + final int[] predicted = {1, 0, 0, 1, 0, 1, 0, 1}; + double beta = 1.; + String average = "binary"; + binarySetUp(actual[0], predicted[0], beta, average); + + evaluator.init(GenericUDAFEvaluator.Mode.PARTIAL1, inputOIs); + evaluator.reset(agg); + + for (int i = 0; i < actual.length; i++) { + evaluator.iterate(agg, new Object[] {actual[i], predicted[i]}); + } + + // should equal to turi's result + // https://turi.com/learn/userguide/evaluation/classification.html#fscores-f1-fbeta- + Assert.assertEquals(0.3333d, agg.get(), 1e-4); + } + + @Test(expected = UnsupportedOperationException.class) --- End diff -- Expect `HiveException` for the reasons that I mentioned in the review of `FMeasureUDAF.java`
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---