use of com.oracle.labs.mlrg.olcut.config.PropertyException in project tribuo by oracle.
the class DemoLabelDataSource method postConfig.
/**
* Configures the class. Should be called in sub-classes' postConfigs
* after they've validated their parameters.
*/
@Override
public void postConfig() {
if (numSamples < 1) {
throw new PropertyException("", "numSamples", "Number of samples must be positive, found " + numSamples);
}
this.rng = new Random(seed);
this.examples = Collections.unmodifiableList(generate());
}
use of com.oracle.labs.mlrg.olcut.config.PropertyException in project tribuo by oracle.
the class BinaryResponseProcessor method postConfig.
@Override
public void postConfig() {
/*
* Canonically all internal logic is driven by fieldNames and positiveResponses, so this method takes values
* populated in fieldName and positiveResponse and sets them appropriately.
*/
boolean bothFieldNamesPopulated = fieldName != null && fieldNames != null;
boolean neitherFieldNamesPopulated = fieldName == null && fieldNames == null;
boolean multipleFieldNamesPopulated = fieldNames != null;
boolean singleFieldNamePopulated = fieldName != null;
boolean bothPositiveResponsesPopulated = positiveResponses != null && positiveResponse != null;
boolean neitherPositiveResponsesPopulated = positiveResponse == null && positiveResponses == null;
boolean multiplePositiveResponsesPopulated = positiveResponses != null;
boolean singlePositiveResponsePopulated = positiveResponse != null;
if (bothFieldNamesPopulated || neitherFieldNamesPopulated) {
throw new PropertyException(configName, "fieldName, FieldNames", "exactly one of fieldName or fieldNames must be populated");
} else if (bothPositiveResponsesPopulated || neitherPositiveResponsesPopulated) {
throw new PropertyException(configName, "positiveResponse, positiveResponses", "exactly one of positiveResponse or positiveResponses must be populated");
} else if (multipleFieldNamesPopulated && multiplePositiveResponsesPopulated && fieldNames.size() != positiveResponses.size()) {
// sizes don't match
throw new PropertyException(configName, "positiveResponses", "must match the length of fieldNames");
} else if (multipleFieldNamesPopulated && singlePositiveResponsePopulated) {
positiveResponses = Collections.nCopies(fieldNames.size(), positiveResponse);
positiveResponse = null;
} else if (singleFieldNamePopulated && multiplePositiveResponsesPopulated) {
throw new PropertyException(configName, "positiveResponses", "if fieldName is populated, positiveResponses must be blank");
} else if (singleFieldNamePopulated && singlePositiveResponsePopulated) {
fieldNames = Collections.singletonList(fieldName);
fieldName = null;
positiveResponses = Collections.singletonList(positiveResponse);
positiveResponse = null;
}
// the case where both positiveResponses and fieldNames are populated and their sizes match requires no action
}
use of com.oracle.labs.mlrg.olcut.config.PropertyException in project tribuo by oracle.
the class FieldResponseProcessor method postConfig.
@Override
public void postConfig() {
/*
* Canonically all internal logic is driven by
* fieldNames and defaultValues, so this method takes values populated in fieldName and defaultValue and sets them
* appropriately.
*/
boolean bothFieldNamesPopulated = fieldName != null && fieldNames != null;
boolean neitherFieldNamesPopulated = fieldName == null && fieldNames == null;
boolean multipleFieldNamesPopulated = fieldNames != null;
boolean singleFieldNamePopulated = fieldName != null;
boolean bothDefaultValuesPopulated = defaultValues != null && defaultValue != null;
boolean neitherDefaultValuesPopulated = defaultValue == null && defaultValues == null;
boolean multipleDefaultValuesPopulated = defaultValues != null;
boolean singleDefaultValuePopulated = defaultValue != null;
if (bothFieldNamesPopulated || neitherFieldNamesPopulated) {
throw new PropertyException(configName, "fieldName, FieldNames", "exactly one of fieldName or fieldNames must be populated");
} else if (bothDefaultValuesPopulated || neitherDefaultValuesPopulated) {
throw new PropertyException(configName, "defaultValue, defaultValues", "exactly one of defaultValue or defaultValues must be populated");
} else if (multipleFieldNamesPopulated && multipleDefaultValuesPopulated && fieldNames.size() != defaultValues.size()) {
// sizes don't match
throw new PropertyException(configName, "defaultValues", "must match the length of fieldNames");
} else if (multipleFieldNamesPopulated && singleDefaultValuePopulated) {
defaultValues = Collections.nCopies(fieldNames.size(), defaultValue);
defaultValue = null;
} else if (singleFieldNamePopulated && multipleDefaultValuesPopulated) {
throw new PropertyException(configName, "defaultValues", "if fieldName is populated, defaultValues must be blank");
} else if (singleFieldNamePopulated && singleDefaultValuePopulated) {
fieldNames = Collections.singletonList(fieldName);
fieldName = null;
defaultValues = Collections.singletonList(defaultValue);
defaultValue = null;
}
// the case where both defaultValues and fieldNames are populated and their sizes match requires no action
}
use of com.oracle.labs.mlrg.olcut.config.PropertyException in project tribuo by oracle.
the class IDXDataSourceTest method testFileNotFound.
@Test
public void testFileNotFound() throws IOException {
// First check that the configuration system throws out of postConfig
ConfigurationManager cm = new ConfigurationManager("/org/tribuo/datasource/config.xml");
try {
// this config file is in the tests, we know the type
@SuppressWarnings("unchecked") IDXDataSource<MockOutput> tmp = (IDXDataSource<MockOutput>) cm.lookup("train");
fail("Should have thrown PropertyException");
} catch (PropertyException e) {
if (!e.getMessage().contains("Failed to load from path - ")) {
fail("Incorrect exception message", e);
}
} catch (RuntimeException e) {
fail("Incorrect exception thrown", e);
}
// Next check the constructor throws
MockOutputFactory factory = new MockOutputFactory();
try {
IDXDataSource<MockOutput> tmp = new IDXDataSource<>(Paths.get("these-features-dont-exist"), Paths.get("these-outputs-dont-exist"), factory);
fail("Should have thrown FileNotFoundException");
} catch (FileNotFoundException e) {
if (!e.getMessage().contains("Failed to load from path - ")) {
fail("Incorrect exception message", e);
}
}
}
use of com.oracle.labs.mlrg.olcut.config.PropertyException in project tribuo by oracle.
the class GaussianDataSource method postConfig.
/**
* Used by the OLCUT configuration system, and should not be called by external code.
*/
@Override
public void postConfig() {
// We use java.util.Random here because SplittableRandom doesn't have nextGaussian yet.
Random rng = new Random(seed);
List<Example<Regressor>> examples = new ArrayList<>(numSamples);
if (xMax <= xMin) {
throw new PropertyException("", "xMax", "xMax must be greater than xMin, found xMax = " + xMax + ", xMin = " + xMin);
}
if (variance <= 0.0) {
throw new PropertyException("", "variance", "Variance must be positive, found variance = " + variance);
}
double range = xMax - xMin;
for (int i = 0; i < numSamples; i++) {
double input = (rng.nextDouble() * range) + xMin;
Regressor output = new Regressor("Y", (rng.nextGaussian() * variance) + ((slope * input) + intercept));
ArrayExample<Regressor> e = new ArrayExample<>(output, new String[] { "X" }, new double[] { input });
examples.add(e);
}
this.examples = Collections.unmodifiableList(examples);
}
Aggregations