Skip to content

Commit

Permalink
Improved code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Aug 14, 2022
1 parent 9e94081 commit b0916d2
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2022 Villu Ruusmann
*
* This file is part of JPMML-Evaluator
*
* JPMML-Evaluator is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Evaluator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Evaluator. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jpmml.evaluator.mining;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import org.dmg.pmml.mining.Segmentation;
import org.jpmml.evaluator.MissingFieldValueException;
import org.jpmml.evaluator.ModelEvaluator;
import org.jpmml.evaluator.ModelEvaluatorTest;
import org.junit.Test;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class GradientBoosterTest extends ModelEvaluatorTest {

@Test
public void evaluate() throws Exception {
ModelEvaluator<?> evaluator = createModelEvaluator();

checkResultFields(Arrays.asList("y"), Arrays.asList("probability(event)", "probability(no event)"), evaluator);

Map<String, ?> arguments = Collections.emptyMap();

Map<String, ?> results;

try {
results = evaluator.evaluate(arguments);

fail();
} catch(MissingFieldValueException mfve){
// Ignored
}

evaluator = createModelEvaluator(new MissingPredictionTreatmentTransformer(Segmentation.MissingPredictionTreatment.RETURN_MISSING));

results = evaluator.evaluate(arguments);

assertTrue(results.containsKey("y"));
assertNull(results.get("y"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2022 Villu Ruusmann
*
* This file is part of JPMML-Evaluator
*
* JPMML-Evaluator is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Evaluator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Evaluator. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jpmml.evaluator.mining;

import org.dmg.pmml.PMML;
import org.dmg.pmml.Visitor;
import org.dmg.pmml.VisitorAction;
import org.dmg.pmml.mining.Segmentation;
import org.jpmml.evaluator.PMMLTransformer;
import org.jpmml.model.visitors.AbstractVisitor;

public class MissingPredictionTreatmentTransformer implements PMMLTransformer<RuntimeException> {

private Segmentation.MissingPredictionTreatment missingPredictionTreatment;


public MissingPredictionTreatmentTransformer(Segmentation.MissingPredictionTreatment missingPredictionTreatment){
setMissingPredictionTreatment(missingPredictionTreatment);
}

@Override
public PMML apply(PMML pmml){
Visitor visitor = new AbstractVisitor(){

@Override
public VisitorAction visit(Segmentation segmentation){
segmentation.setMissingPredictionTreatment(getMissingPredictionTreatment());

return super.visit(segmentation);
}
};

visitor.applyTo(pmml);

return pmml;
}

public Segmentation.MissingPredictionTreatment getMissingPredictionTreatment(){
return this.missingPredictionTreatment;
}

private void setMissingPredictionTreatment(Segmentation.MissingPredictionTreatment missingPredictionTreatment){
this.missingPredictionTreatment = missingPredictionTreatment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.Map;

import org.dmg.pmml.mining.Segmentation;
import org.jpmml.evaluator.ModelEvaluator;
import org.jpmml.evaluator.ModelEvaluatorTest;
import org.junit.Test;
Expand Down Expand Up @@ -56,5 +57,18 @@ public void evaluate() throws Exception {
assertNotNull(results.get("y1"));
assertNotNull(results.get("y2"));
assertNotNull(results.get("y3"));

evaluator = createModelEvaluator(new MissingPredictionTreatmentTransformer(Segmentation.MissingPredictionTreatment.RETURN_MISSING));

arguments = Collections.emptyMap();

results = evaluator.evaluate(arguments);

assertTrue(results.containsKey("y1"));
assertNull(results.get("y1"));
assertTrue(results.containsKey("y2"));
assertNull(results.get("y2"));
assertTrue(results.containsKey("y3"));
assertNull(results.get("y3"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" ?>
<PMML xmlns="http://www.dmg.org/PMML-4_4" version="4.4">
<DataDictionary>
<DataField name="x" optype="continuous" dataType="double"/>
<DataField name="y" optype="categorical" dataType="string">
<Value value="event"/>
<Value value="no event"/>
</DataField>
</DataDictionary>
<MiningModel functionName="classification">
<MiningSchema>
<MiningField name="x"/>
<MiningField name="y" usageType="target"/>
</MiningSchema>
<Segmentation multipleModelMethod="modelChain">
<Segment>
<True/>
<MiningModel functionName="regression">
<MiningSchema>
<MiningField name="x"/>
</MiningSchema>
<Output>
<OutputField name="decisionFunction(event)" feature="predictedValue"/>
</Output>
<Segmentation multipleModelMethod="sum">
<Segment>
<True/>
<RegressionModel functionName="regression">
<MiningSchema>
<MiningField name="x"/>
</MiningSchema>
<RegressionTable intercept="0.0">
<NumericPredictor name="x" coefficient="2"/>
</RegressionTable>
</RegressionModel>
</Segment>
<Segment>
<True/>
<RegressionModel functionName="regression">
<MiningSchema>
<MiningField name="x"/>
</MiningSchema>
<RegressionTable intercept="0.0">
<NumericPredictor name="x" coefficient="1"/>
</RegressionTable>
</RegressionModel>
</Segment>
<Segment>
<True/>
<RegressionModel functionName="regression">
<MiningSchema>
<MiningField name="x"/>
</MiningSchema>
<RegressionTable intercept="0.0">
<NumericPredictor name="x" coefficient="0.5"/>
</RegressionTable>
</RegressionModel>
</Segment>
</Segmentation>
</MiningModel>
</Segment>
<Segment>
<True/>
<RegressionModel functionName="classification">
<MiningSchema>
<MiningField name="decisionFunction(event)"/>
<MiningField name="y" usageType="target"/>
</MiningSchema>
<Output>
<OutputField name="probability(event)" optype="continuous" dataType="double" feature="probability" value="event"/>
<OutputField name="probability(no event)" optype="continuous" dataType="double" feature="probability" value="no event"/>
</Output>
<RegressionTable targetCategory="event" intercept="0.0">
<NumericPredictor field="decisionFunction(event)" coefficient="1"/>
</RegressionTable>
<RegressionTable targetCategory="no event" intercept="0.0"/>
</RegressionModel>
</Segment>
</Segmentation>
</MiningModel>
</PMML>


0 comments on commit b0916d2

Please sign in to comment.