org.dmg.pmml.MiningField Java Examples

The following examples show how to use org.dmg.pmml.MiningField. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AppPMMLUtilsTest.java    From oryx with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildMiningSchema() {
  MiningSchema miningSchema = AppPMMLUtils.buildMiningSchema(buildTestSchema());

  List<MiningField> miningFields = miningSchema.getMiningFields();
  assertEquals(4, miningFields.size());

  String[] fieldNames = { "foo", "bar", "baz", "bing" };
  for (int i = 0; i < fieldNames.length; i++) {
    assertEquals(fieldNames[i], miningFields.get(i).getName().getValue());
  }

  assertEquals(MiningField.UsageType.SUPPLEMENTARY, miningFields.get(0).getUsageType());
  assertEquals(MiningField.UsageType.PREDICTED, miningFields.get(1).getUsageType());
  assertEquals(MiningField.UsageType.SUPPLEMENTARY, miningFields.get(2).getUsageType());
  assertEquals(MiningField.UsageType.ACTIVE, miningFields.get(3).getUsageType());

  assertEquals(OpType.CATEGORICAL, miningFields.get(1).getOpType());
  assertEquals(OpType.CONTINUOUS, miningFields.get(3).getOpType());
}
 
Example #2
Source File: InputFieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public FieldValue prepareInputValue(Field<?> field, MiningField miningField, Object value){
	InputTypeInfo typeInfo = getTypeInfo(field, miningField);

	if(value instanceof Collection){
		Collection<?> rawValues = (Collection<?>)value;

		List<Object> pmmlValues = new ArrayList<>(rawValues.size());

		for(Object rawValue : rawValues){
			FieldValue fieldValue = prepareScalarInputValue(typeInfo, rawValue);

			pmmlValues.add(FieldValueUtil.getValue(fieldValue));
		}

		return createInputValue(typeInfo, pmmlValues);
	} else

	{
		return prepareScalarInputValue(typeInfo, value);
	}
}
 
Example #3
Source File: InputFieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private ScalarValue createInvalidInputValue(InputTypeInfo typeInfo, Object value){
	MiningField miningField = typeInfo.getMiningField();

	Object invalidValueReplacement = miningField.getInvalidValueReplacement();
	if(invalidValueReplacement != null){
		return (ScalarValue)createInputValue(typeInfo, invalidValueReplacement);
	}

	ScalarValue fieldValue = (ScalarValue)createInputValue(typeInfo, value);
	if(fieldValue.isValid()){
		fieldValue.setValid(false);
	}

	return fieldValue;
}
 
Example #4
Source File: ValueParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public VisitorAction visit(MiningField miningField){
	FieldName name = miningField.getName();
	if(name == null){
		throw new MissingAttributeException(miningField, PMMLAttributes.MININGFIELD_NAME);
	}

	DataType dataType = resolveDataType(name);
	if(dataType != null){
		Object missingValueReplacement = miningField.getMissingValueReplacement();
		if(missingValueReplacement != null){
			missingValueReplacement = safeParseOrCast(dataType, missingValueReplacement);

			miningField.setMissingValueReplacement(missingValueReplacement);
		}

		Object invalidValueReplacement = miningField.getInvalidValueReplacement();
		if(invalidValueReplacement != null){
			invalidValueReplacement = safeParseOrCast(dataType, invalidValueReplacement);

			miningField.setInvalidValueReplacement(invalidValueReplacement);
		}
	}

	return super.visit(miningField);
}
 
Example #5
Source File: ModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
protected boolean assessParentCompatibility(){
	List<InputField> inputFields = getInputFields();

	for(InputField inputField : inputFields){
		Field<?> field = inputField.getField();
		MiningField miningField = inputField.getMiningField();

		if(!(field instanceof DataField)){
			continue;
		} // End if

		if(!InputFieldUtil.isDefault(field, miningField)){
			return false;
		}
	}

	return true;
}
 
Example #6
Source File: ModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
protected boolean assessPurity(){
	List<InputField> inputFields = getInputFields();

	for(InputField inputField : inputFields){
		Field<?> field = inputField.getField();
		MiningField miningField = inputField.getMiningField();

		if(!InputFieldUtil.isDefault(field, miningField)){
			return false;
		}
	}

	if(hasLocalDerivedFields() || hasOutputFields()){
		return false;
	}

	return true;
}
 
Example #7
Source File: AppPMMLUtils.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * @param schema {@link InputSchema} whose information should be encoded in PMML
 * @param importances optional feature importances. May be {@code null}, or else the size
 *  of the array must match the number of predictors in the schema, which may be
 *  less than the total number of features.
 * @return a {@link MiningSchema} representing the information contained in an
 *  {@link InputSchema}
 */
public static MiningSchema buildMiningSchema(InputSchema schema, double[] importances) {
  Preconditions.checkArgument(
      importances == null || (importances.length == schema.getNumPredictors()));
  List<String> featureNames = schema.getFeatureNames();
  List<MiningField> miningFields = new ArrayList<>();
  for (int featureIndex = 0; featureIndex < featureNames.size(); featureIndex++) {
    String featureName = featureNames.get(featureIndex);
    MiningField field = new MiningField(FieldName.create(featureName));
    if (schema.isNumeric(featureName)) {
      field.setOpType(OpType.CONTINUOUS);
      field.setUsageType(MiningField.UsageType.ACTIVE);
    } else if (schema.isCategorical(featureName)) {
      field.setOpType(OpType.CATEGORICAL);
      field.setUsageType(MiningField.UsageType.ACTIVE);
    } else {
      // ID, or ignored
      field.setUsageType(MiningField.UsageType.SUPPLEMENTARY);
    }
    if (schema.hasTarget() && schema.isTarget(featureName)) {
      // Override to PREDICTED
      field.setUsageType(MiningField.UsageType.PREDICTED);
    }
    // Will be active if and only if it's a predictor
    if (field.getUsageType() == MiningField.UsageType.ACTIVE && importances != null) {
      int predictorIndex = schema.featureToPredictorIndex(featureIndex);
      field.setImportance(importances[predictorIndex]);
    }
    miningFields.add(field);
  }
  return new MiningSchema(miningFields);
}
 
Example #8
Source File: ModelEvaluationContext.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected FieldValue prepare(FieldName name, Object value){
	ModelEvaluator<?> modelEvaluator = getModelEvaluator();

	DataField dataField = modelEvaluator.getDataField(name);
	if(dataField == null){
		throw new MissingFieldException(name);
	}

	MiningField miningField = modelEvaluator.getMiningField(name);
	if(miningField == null){
		throw new InvisibleFieldException(name);
	}

	MiningField.UsageType usageType = miningField.getUsageType();
	switch(usageType){
		case ACTIVE:
		case GROUP:
		case ORDER:
			{
				return InputFieldUtil.prepareInputValue(dataField, miningField, value);
			}
		case PREDICTED:
		case TARGET:
			{
				return InputFieldUtil.prepareResidualInputValue(dataField, miningField, value);
			}
		default:
			throw new UnsupportedAttributeException(miningField, usageType);
	}
}
 
Example #9
Source File: ModelEvaluationContext.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private FieldValue inheritOrPrepareInputValue(Field<?> field, MiningField miningField, FieldValue value){

	if(InputFieldUtil.isDefault(field, miningField)){
		return value;
	}

	return InputFieldUtil.prepareInputValue(field, miningField, FieldValueUtil.getValue(value));
}
 
Example #10
Source File: AssociationModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public List<InputField> getGroupFields(){

	if(this.groupInputFields == null){
		this.groupInputFields = createInputFields(MiningField.UsageType.GROUP);
	}

	return this.groupInputFields;
}
 
Example #11
Source File: TargetCategoryParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private void processModel(Model model){
	MiningSchema miningSchema = model.getMiningSchema();
	if(miningSchema == null){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(model.getClass()) + "/" + XPathUtil.formatElement(MiningSchema.class)), model);
	}

	Map<FieldName, DataType> targetDataTypes = new LinkedHashMap<>();

	if(miningSchema.hasMiningFields()){
		List<MiningField> miningFields = miningSchema.getMiningFields();

		for(MiningField miningField : miningFields){
			FieldName name = miningField.getName();
			if(name == null){
				throw new MissingAttributeException(miningField, PMMLAttributes.MININGFIELD_NAME);
			}

			MiningField.UsageType usageType = miningField.getUsageType();
			switch(usageType){
				case PREDICTED:
				case TARGET:
					DataType dataType = resolveTargetDataType(name);

					targetDataTypes.put(name, dataType);
					break;
				default:
					break;
			}
		}
	}

	this.targetDataTypes.push(targetDataTypes);

	this.dataType = getDataType();
}
 
Example #12
Source File: MiningFieldInterner.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public VisitorAction visit(MiningSchema miningSchema){

	if(miningSchema.hasMiningFields()){
		List<MiningField> miningFields = miningSchema.getMiningFields();

		for(ListIterator<MiningField> it = miningFields.listIterator(); it.hasNext(); ){
			it.set(intern(it.next()));
		}
	}

	return super.visit(miningSchema);
}
 
Example #13
Source File: MiningFieldInterner.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private MiningField intern(MiningField miningField){

		if(miningField == null || miningField.hasExtensions()){
			return miningField;
		} // End if

		if(!InputFieldUtil.isDefault(null, miningField)){
			return miningField;
		}

		return this.cache.intern(miningField);
	}
 
Example #14
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
protected List<InputField> createInputFields(MiningField.UsageType usageType){
	M model = getModel();

	MiningSchema miningSchema = model.getMiningSchema();

	List<InputField> inputFields = new ArrayList<>();

	if(miningSchema.hasMiningFields()){
		List<MiningField> miningFields = miningSchema.getMiningFields();

		for(MiningField miningField : miningFields){
			FieldName name = miningField.getName();

			if(!(miningField.getUsageType()).equals(usageType)){
				continue;
			}

			Field<?> field = getDataField(name);
			if(field == null){
				field = new VariableField(name);
			}

			InputField inputField = new InputField(field, miningField);

			inputFields.add(inputField);
		}
	}

	return ImmutableList.copyOf(inputFields);
}
 
Example #15
Source File: InputFieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private ScalarValue createMissingInputValue(InputTypeInfo typeInfo){
	MiningField miningField = typeInfo.getMiningField();

	Object missingValueReplacement = miningField.getMissingValueReplacement();
	if(missingValueReplacement != null){
		return (ScalarValue)createInputValue(typeInfo, missingValueReplacement);
	}

	return (ScalarValue)FieldValues.MISSING_VALUE;
}
 
Example #16
Source File: FieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public OpType getOpType(Field<?> field, MiningField miningField){
	OpType opType = field.getOpType();

	// "A MiningField overrides a (Data)Field"
	if(miningField != null){
		opType = miningField.getOpType(opType);
	}

	return opType;
}
 
Example #17
Source File: FieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public OpType getOpType(Field<?> field, MiningField miningField, Target target){
	OpType opType = field.getOpType();

	// "A MiningField overrides a (Data)Field, and a Target overrides a MiningField"
	if(miningField != null){
		opType = miningField.getOpType(opType);

		if(target != null){
			opType = target.getOpType(opType);
		}
	}

	return opType;
}
 
Example #18
Source File: InputFieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private ScalarValue performMissingValueTreatment(InputTypeInfo typeInfo){
	MiningField miningField = typeInfo.getMiningField();

	MissingValueTreatmentMethod missingValueTreatmentMethod = miningField.getMissingValueTreatment();
	if(missingValueTreatmentMethod == null){
		missingValueTreatmentMethod = MissingValueTreatmentMethod.AS_IS;
	}

	switch(missingValueTreatmentMethod){
		case AS_IS:
		case AS_MEAN:
		case AS_MODE:
		case AS_MEDIAN:
		case AS_VALUE:
			return createMissingInputValue(typeInfo);
		case RETURN_INVALID:
			Field<?> field = typeInfo.getField();

			Object missingValueReplacement = miningField.getMissingValueReplacement();
			if(missingValueReplacement != null){
				throw new MisplacedAttributeException(miningField, PMMLAttributes.MININGFIELD_MISSINGVALUEREPLACEMENT, missingValueReplacement);
			}

			throw new InvalidResultException("Field " + PMMLException.formatKey(field.getName()) + " requires user input value", miningField);
		default:
			throw new UnsupportedAttributeException(miningField, missingValueTreatmentMethod);
	}
}
 
Example #19
Source File: InputFieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private ScalarValue performInvalidValueTreatment(InputTypeInfo typeInfo, Object value){
	MiningField miningField = typeInfo.getMiningField();

	InvalidValueTreatmentMethod invalidValueTreatmentMethod = miningField.getInvalidValueTreatment();
	switch(invalidValueTreatmentMethod){
		case AS_IS:
			break;
		case AS_MISSING:
		case RETURN_INVALID:
			Object invalidValueReplacement = miningField.getInvalidValueReplacement();
			if(invalidValueReplacement != null){
				throw new MisplacedAttributeException(miningField, PMMLAttributes.MININGFIELD_INVALIDVALUEREPLACEMENT, invalidValueReplacement);
			}
			break;
		default:
			throw new UnsupportedAttributeException(miningField, invalidValueTreatmentMethod);
	} // End switch

	switch(invalidValueTreatmentMethod){
		case RETURN_INVALID:
			Field<?> field = typeInfo.getField();

			throw new InvalidResultException("Field " + PMMLException.formatKey(field.getName()) + " cannot accept user input value " + PMMLException.formatValue(value), miningField);
		case AS_IS:
			return createInvalidInputValue(typeInfo, value);
		case AS_MISSING:
			return createMissingInputValue(typeInfo);
		default:
			throw new UnsupportedAttributeException(miningField, invalidValueTreatmentMethod);
	}
}
 
Example #20
Source File: InputField.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public InputField(Field<?> field, MiningField miningField){
	super(field);

	setMiningField(Objects.requireNonNull(miningField));

	if(!Objects.equals(field.getName(), miningField.getName())){
		throw new IllegalArgumentException();
	}
}
 
Example #21
Source File: AppPMMLUtils.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * @param miningSchema {@link MiningSchema} from a model
 * @return index of the {@link MiningField.UsageType#PREDICTED} feature
 */
public static Integer findTargetIndex(MiningSchema miningSchema) {
  List<MiningField> miningFields = miningSchema.getMiningFields();
  for (int i = 0; i < miningFields.size(); i++) {
    if (miningFields.get(i).getUsageType() == MiningField.UsageType.PREDICTED) {
      return i;
    }
  }
  return null;
}
 
Example #22
Source File: AbstractAppMLlibIT.java    From oryx with Apache License 2.0 5 votes vote down vote up
protected static void checkMiningSchema(InputSchema schema, MiningSchema miningSchema) {
  assertNotNull(miningSchema);
  List<MiningField> miningFields = miningSchema.getMiningFields();
  List<String> expectedFeatureNames = schema.getFeatureNames();
  assertEquals("Wrong number of features",
               expectedFeatureNames.size(), miningFields.size());
  for (int i = 0; i < expectedFeatureNames.size(); i++) {
    MiningField miningField = miningFields.get(i);
    String expectedFeature = expectedFeatureNames.get(i);
    String featureName = miningField.getName().getValue();
    assertEquals("Wrong feature at position " + i, expectedFeature, featureName);
    if (schema.isNumeric(expectedFeature) || schema.isCategorical(expectedFeature)) {
      assertEquals("Wrong op type for feature + " + featureName,
                   schema.isNumeric(expectedFeature) ? OpType.CONTINUOUS : OpType.CATEGORICAL,
                   miningField.getOpType());
      if (schema.isTarget(expectedFeature)) {
        assertEquals("Wrong usage type for feature " + featureName,
                     MiningField.UsageType.PREDICTED,
                     miningField.getUsageType());
      } else {
        assertEquals("Wrong usage type for feature " + featureName,
                     MiningField.UsageType.ACTIVE,
                     miningField.getUsageType());
        assertRange(miningField.getImportance().doubleValue(), 0.0, 1.0);
      }
    } else {
      assertEquals("Wrong usage type for feature " + featureName,
                   MiningField.UsageType.SUPPLEMENTARY,
                   miningField.getUsageType());
    }

  }
}
 
Example #23
Source File: ModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected List<InputField> createInputFields(MiningField.UsageType usageType){
	InputMapper inputMapper = getInputMapper();

	List<InputField> inputFields = super.createInputFields(usageType);

	if(inputMapper != null){
		inputFields = updateNames(inputFields, inputMapper);
	}

	return inputFields;
}
 
Example #24
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public MiningField getMiningField(FieldName name){

		if(Objects.equals(Evaluator.DEFAULT_TARGET_NAME, name)){
			return null;
		}

		return this.miningFields.get(name);
	}
 
Example #25
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public List<InputField> getActiveFields(){

		if(this.activeInputFields == null){
			this.activeInputFields = createInputFields(MiningField.UsageType.ACTIVE);
		}

		return this.activeInputFields;
	}
 
Example #26
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
private DerivedFieldLoader(FieldName name, String column, DerivedField derivedField, MiningField miningField){
	super(name, column);

	setDerivedField(derivedField);
	setMiningField(miningField);
}
 
Example #27
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public FieldValue prepare(Object value){
	DerivedField derivedField = getDerivedField();
	MiningField miningField = getMiningField();

	if(miningField != null){
		return InputFieldUtil.prepareInputValue(derivedField, miningField, value);
	}

	TypeInfo typeInfo = new TypeInfo(){

		@Override
		public DataType getDataType(){
			DataType dataType = derivedField.getDataType();
			if(dataType == null){
				throw new MissingAttributeException(derivedField, org.dmg.pmml.PMMLAttributes.DERIVEDFIELD_DATATYPE);
			}

			return dataType;
		}

		@Override
		public OpType getOpType(){
			OpType opType = derivedField.getOpType();
			if(opType == null){
				throw new MissingAttributeException(derivedField, org.dmg.pmml.PMMLAttributes.DERIVEDFIELD_OPTYPE);
			}

			return opType;
		}

		@Override
		public List<?> getOrdering(){
			List<?> ordering = FieldUtil.getValidValues(derivedField);

			return ordering;
		}
	};

	return FieldValueUtil.create(typeInfo, value);
}
 
Example #28
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
private DataFieldLoader(FieldName name, String column, DataField dataField, MiningField miningField){
	super(name, column);

	setDataField(dataField);
	setMiningField(miningField);
}
 
Example #29
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public MiningField getMiningField(){
	return this.miningField;
}
 
Example #30
Source File: TargetField.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public TargetField(DataField dataField, MiningField miningField, Target target){
	super(dataField);

	setMiningField(miningField);
	setTarget(target);
}