@Test public final void testInvalidClass() throws URISyntaxException, ExecutionException, InterruptedException { final Path modelPath = Paths.get(getClass().getResource("/dummy_model").toURI()); final String id = "classificationModel"; final List<FieldSchema> fields = ImmutableList.of(FIELD_SCHEMA, CATEGORICAL_FIELD_SCHEMA); final String illegalTargetValue = "those"; final DatasetSchema schema = new DatasetSchema(1, fields); final Random random = new Random(); this.jepInstance.submitEvaluation(jep -> { jep.eval("import sys"); jep.eval(String.format("sys.path.append(\"%s\")", modelPath.toAbsolutePath())); jep.eval("from classifier import Classifier"); jep.eval(String.format("%s = Classifier('%s')", id, illegalTargetValue)); return null; }).get(); final ClassificationPythonModel model = new ClassificationPythonModel(this.jepInstance, schema, id, "classify", "getClassDistribution"); assertThatThrownBy(() -> model.classify(new MockInstance(schema, random))) .as("A classifier that does not return a valid target value will fail with a null pointer exception") .isInstanceOf(NullPointerException.class); }
@Override public int classify(final Instance instance) { final String classValue = invokeFunction(instance, this.classifyFunctionName, "str(%s[0])"); final int asNotNullable; try { asNotNullable = this.classToIndexConverter.apply(classValue); } catch (final NullPointerException e) { final AbstractValueSchema targetVarSchema = this.schema.getTargetFieldSchema() .map(FieldSchema::getValueSchema) .get(); final Function<CategoricalValueSchema, String> block = targetSchema -> String.format( "Unexpected class provided by model: %s. Expected values: %s", classValue, targetSchema.getNominalValues() ); final String msg = ClassificationDatasetSchemaUtil.withCategoricalValueSchema(targetVarSchema, block) .orElseThrow(() -> new RuntimeException("The target variable is not a categorical value: " + targetVarSchema)); logger.error(msg, e); throw e; } return asNotNullable; }
ClassificationPythonModel implements ClassificationMLModel { @Override public int classify(final Instance instance) { final String classValue = invokeFunction(instance, this.classifyFunctionName, "str(%s[0])"); final int asNotNullable; try { asNotNullable = this.classToIndexConverter.apply(classValue); } catch (final NullPointerException e) { final AbstractValueSchema targetVarSchema = this.schema.getTargetFieldSchema() .map(FieldSchema::getValueSchema) .get(); final Function<CategoricalValueSchema, String> block = targetSchema -> String.format( "Unexpected class provided by model: %s. Expected values: %s", classValue, targetSchema.getNominalValues() ); final String msg = ClassificationDatasetSchemaUtil.withCategoricalValueSchema(targetVarSchema, block) .orElseThrow(() -> new RuntimeException("The target variable is not a categorical value: " + targetVarSchema)); logger.error(msg, e); throw e; } return asNotNullable; } }
ClassificationPythonModel implements ClassificationMLModel { @Override public int classify(final Instance instance) { final String classValue = invokeFunction(instance, this.classifyFunctionName, "str(%s[0])"); final int asNotNullable; try { asNotNullable = this.classToIndexConverter.apply(classValue); } catch (final NullPointerException e) { final AbstractValueSchema targetVarSchema = this.schema.getTargetFieldSchema() .map(FieldSchema::getValueSchema) .get(); final Function<CategoricalValueSchema, String> block = targetSchema -> String.format( "Unexpected class provided by model: %s. Expected values: %s", classValue, targetSchema.getNominalValues() ); final String msg = ClassificationDatasetSchemaUtil.withCategoricalValueSchema(targetVarSchema, block) .orElseThrow(() -> new RuntimeException("The target variable is not a categorical value: " + targetVarSchema)); logger.error(msg, e); throw e; } return asNotNullable; } ClassificationPythonModel(final JepInstance jepInstance,
                                     final DatasetSchema schema,
                                     final String id,
                                     final String classifyFunctionName,
                                     final String getClassDistributionFunctionName);  ClassificationPythonModel(final JepInstance jepInstance,
                                     final DatasetSchema schema,
                                     final String id); }
ClassificationPythonModel implements ClassificationMLModel { @Override public int classify(final Instance instance) { final String classValue = invokeFunction(instance, this.classifyFunctionName, "str(%s[0])"); final int asNotNullable; try { asNotNullable = this.classToIndexConverter.apply(classValue); } catch (final NullPointerException e) { final AbstractValueSchema targetVarSchema = this.schema.getTargetFieldSchema() .map(FieldSchema::getValueSchema) .get(); final Function<CategoricalValueSchema, String> block = targetSchema -> String.format( "Unexpected class provided by model: %s. Expected values: %s", classValue, targetSchema.getNominalValues() ); final String msg = ClassificationDatasetSchemaUtil.withCategoricalValueSchema(targetVarSchema, block) .orElseThrow(() -> new RuntimeException("The target variable is not a categorical value: " + targetVarSchema)); logger.error(msg, e); throw e; } return asNotNullable; } ClassificationPythonModel(final JepInstance jepInstance,
                                     final DatasetSchema schema,
                                     final String id,
                                     final String classifyFunctionName,
                                     final String getClassDistributionFunctionName);  ClassificationPythonModel(final JepInstance jepInstance,
                                     final DatasetSchema schema,
                                     final String id); @Override boolean save(final Path dir, final String name); @Override DatasetSchema getSchema(); @Override double[] getClassDistribution(final Instance instance); @Override int classify(final Instance instance); void validate(final JepInstance jepInstance, final String id); @Override void close(); }
ClassificationPythonModel implements ClassificationMLModel { @Override public int classify(final Instance instance) { final String classValue = invokeFunction(instance, this.classifyFunctionName, "str(%s[0])"); final int asNotNullable; try { asNotNullable = this.classToIndexConverter.apply(classValue); } catch (final NullPointerException e) { final AbstractValueSchema targetVarSchema = this.schema.getTargetFieldSchema() .map(FieldSchema::getValueSchema) .get(); final Function<CategoricalValueSchema, String> block = targetSchema -> String.format( "Unexpected class provided by model: %s. Expected values: %s", classValue, targetSchema.getNominalValues() ); final String msg = ClassificationDatasetSchemaUtil.withCategoricalValueSchema(targetVarSchema, block) .orElseThrow(() -> new RuntimeException("The target variable is not a categorical value: " + targetVarSchema)); logger.error(msg, e); throw e; } return asNotNullable; } ClassificationPythonModel(final JepInstance jepInstance,
                                     final DatasetSchema schema,
                                     final String id,
                                     final String classifyFunctionName,
                                     final String getClassDistributionFunctionName);  ClassificationPythonModel(final JepInstance jepInstance,
                                     final DatasetSchema schema,
                                     final String id); @Override boolean save(final Path dir, final String name); @Override DatasetSchema getSchema(); @Override double[] getClassDistribution(final Instance instance); @Override int classify(final Instance instance); void validate(final JepInstance jepInstance, final String id); @Override void close(); static final String DEFAULT_CLASSIFY_FUNCTION_NAME; static final String DEFAULT_GETCLASSDISTRIBUTION_FUNCTION_NAME; }