Skip to content

Commit

Permalink
Standardized MiningField element
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Sep 16, 2024
1 parent d0bb357 commit c7db6d3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
20 changes: 18 additions & 2 deletions pmml-model/src/main/java/org/jpmml/model/filters/ExportFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,24 @@ public Attributes filterAttributes(String localName, Attributes attributes){
if(("MiningField").equals(localName)){

if(target.compareTo(Version.PMML_4_3) <= 0){
String missingValueTreatment = getAttribute(attributes, "missingValueTreatment");
String invalidValueTreatment = getAttribute(attributes, "invalidValueTreatment");

attributes = renameAttribute(attributes, "invalidValueReplacement", "x-invalidValueReplacement");

String invalidValueTreatment = getAttribute(attributes, "invalidValueTreatment");
if(missingValueTreatment != null){

switch(missingValueTreatment){
case "returnInvalid":
{
attributes = setAttribute(attributes, "missingValueTreatment", "x-" + missingValueTreatment);
}
break;
default:
break;
}
} // End if

if(invalidValueTreatment != null){

switch(invalidValueTreatment){
Expand Down Expand Up @@ -98,10 +113,11 @@ public Attributes filterAttributes(String localName, Attributes attributes){
if(("Segmentation").equals(localName)){

if(target.compareTo(Version.PMML_4_3) <= 0){
String multipleModelMethod = getAttribute(attributes, "multipleModelMethod");

attributes = renameAttribute(attributes, "missingPredictionTreatment", "x-missingPredictionTreatment");
attributes = renameAttribute(attributes, "missingThreshold", "x-missingThreshold");

String multipleModelMethod = getAttribute(attributes, "multipleModelMethod");
if(multipleModelMethod != null){

switch(multipleModelMethod){
Expand Down
21 changes: 18 additions & 3 deletions pmml-model/src/main/java/org/jpmml/model/filters/ImportFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,22 @@ public Attributes filterAttributes(String localName, Attributes attributes){
} // End if

if(source.compareTo(Version.PMML_4_4) <= 0){
String missingValueTreatment = getAttribute(attributes, "missingValueTreatment");
String invalidValueTreatment = getAttribute(attributes, "invalidValueTreatment");

if(missingValueTreatment != null){

switch(missingValueTreatment){
case "x-returnInvalid":
{
attributes = setAttribute(attributes, "missingValueTreatment", missingValueTreatment.substring("x-".length()));
}
break;
default:
break;
}
} // End if

if(invalidValueTreatment != null){

switch(invalidValueTreatment){
Expand Down Expand Up @@ -115,10 +129,8 @@ public Attributes filterAttributes(String localName, Attributes attributes){
if(("Segmentation").equals(localName)){

if(source.compareTo(Version.PMML_4_3) <= 0){
attributes = renameAttribute(attributes, "x-missingPredictionTreatment", "missingPredictionTreatment");
attributes = renameAttribute(attributes, "x-missingThreshold", "missingThreshold");

String multipleModelMethod = getAttribute(attributes, "multipleModelMethod");

if(multipleModelMethod != null){

switch(multipleModelMethod){
Expand All @@ -132,6 +144,9 @@ public Attributes filterAttributes(String localName, Attributes attributes){
break;
}
}

attributes = renameAttribute(attributes, "x-missingPredictionTreatment", "missingPredictionTreatment");
attributes = renameAttribute(attributes, "x-missingThreshold", "missingThreshold");
}
} else

Expand Down
10 changes: 6 additions & 4 deletions pmml-model/src/test/java/org/dmg/pmml/MiningFieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public class MiningFieldTest extends SchemaUpdateTest {
public void transform() throws Exception {
byte[] original = ResourceUtil.getByteArray(MiningFieldTest.class);

checkMiningField(original, "asIs", new String[]{"0", null});
checkMiningField(original, "x-returnInvalid", "asIs", new String[]{"0", null});

byte[] latest = upgradeToLatest(original);

checkMiningField(latest, "asValue", new String[]{null, "0"});
checkMiningField(latest, "returnInvalid", "asValue", new String[]{null, "0"});

byte[] latestToOriginal = downgrade(latest, Version.PMML_4_3);

checkMiningField(latestToOriginal, "asIs", new String[]{"0", null});
checkMiningField(latestToOriginal, "x-returnInvalid", "asIs", new String[]{"0", null});
}

@Test
Expand All @@ -49,9 +49,11 @@ public void unmarshal() throws Exception {
}

static
private void checkMiningField(byte[] bytes, String invalidValueTreatment, String[] invalidValueReplacement) throws Exception {
private void checkMiningField(byte[] bytes, String missingValueTreatment, String invalidValueTreatment, String[] invalidValueReplacement) throws Exception {
Node node = DOMUtil.selectNode(bytes, "/:PMML/:RegressionModel/:MiningSchema/:MiningField");

assertEquals(missingValueTreatment, DOMUtil.getAttributeValue(node, "missingValueTreatment"));

assertEquals(invalidValueTreatment, DOMUtil.getAttributeValue(node, "invalidValueTreatment"));
assertArrayEquals(invalidValueReplacement, DOMUtil.getExtensionAttributeValues(node, "invalidValueReplacement"));
}
Expand Down
2 changes: 1 addition & 1 deletion pmml-model/src/test/resources/pmml/MiningFieldTest.pmml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</DataDictionary>
<RegressionModel>
<MiningSchema>
<MiningField name="x" invalidValueTreatment="asIs" x-invalidValueReplacement="0"/>
<MiningField name="x" missingValueTreatment="x-returnInvalid" invalidValueTreatment="asIs" x-invalidValueReplacement="0"/>
</MiningSchema>
<RegressionTable intercept="1">
<NumericPredictor name="x" coefficient="2"/>
Expand Down

0 comments on commit c7db6d3

Please sign in to comment.