use of ambit2.base.data.PropertyAnnotations in project ambit-mirror by ideaconsult.
the class RDFPropertyIterator method parseRecord.
public Property parseRecord(RDFNode propertyEntry, Property property, int level) {
if (property == null)
property = createRecord();
if (propertyEntry.isLiteral()) {
property.setName(((Literal) propertyEntry).getString());
return property;
}
Resource newEntry = (Resource) propertyEntry;
Reference thisurl = null;
try {
String uri = getURI(newEntry);
thisurl = uri != null ? new Reference(uri) : null;
} catch (Exception x) {
}
parseObjectURI(newEntry, property);
if ((property.getId() > 0) && !thisurl.equals(reference)) {
// ours,
// let's
// retrieve
// what we
// have
RDFPropertyIterator iterator = null;
try {
iterator = new RDFPropertyIterator(thisurl, referer);
iterator.setCloseModel(true);
iterator.setBaseReference(getBaseReference());
while (iterator.hasNext()) {
property = iterator.next();
break;
}
if (!forceReadRDFLocalObjects)
return property;
// otherwise read what's in rdf as well
} catch (Exception x) {
} finally {
try {
iterator.close();
} catch (Exception x) {
}
}
}
// foreign, will live with what's available in RDF
String name = thisurl == null ? null : thisurl.toString();
String label = name;
try {
name = getTitle(newEntry);
} catch (Exception x) {
}
try {
Statement t = newEntry.getProperty(OWL.sameAs);
if (t != null) {
RDFNode resource = t.getObject();
if (resource.isLiteral())
label = ((Literal) resource).getString();
else
label = resource.isURIResource() ? ((Resource) resource).getURI() : resource.toString();
}
} catch (Exception x) {
label = Property.guessLabel(name);
}
Statement t = ((Resource) propertyEntry).getProperty(OTProperty.smarts.createProperty(jenaModel));
RDFNode smarts = t == null ? null : t.getObject();
String fragment = null;
if ((smarts != null) && smarts.isLiteral()) {
fragment = smarts.asLiteral().getString();
property.setUnits(fragment);
// TODO set ot:smarts property
property.setNominal(true);
property.setName(String.format("%s#%s", name, Reference.encode(fragment)));
} else
property.setName(name == null ? thisurl == null ? label : thisurl.toString() : name);
property.setLabel(label);
try {
property.setUnits(((Literal) newEntry.getProperty(OT.DataProperty.units.createProperty(jenaModel)).getObject()).getString());
} catch (Exception x) {
property.setUnits("");
}
Statement stmt = newEntry.getProperty(DC.creator);
String creator = (stmt == null) ? "Default" : (stmt.getObject() == null) ? "Default" : stmt.getObject().isLiteral() ? ((Literal) stmt.getObject()).getString() : stmt.getObject().toString();
String hasSource = creator;
property.setReference(processSource(newEntry, hasSource, creator));
// predicate =
// jenaModel.createProperty(String.format("http://www.opentox.org/api/1.1#%s",
property.setClazz(String.class);
StmtIterator it = null;
property.setNominal(false);
try {
it = jenaModel.listStatements(new SimpleSelector(newEntry, RDF.type, (RDFNode) null));
while (it.hasNext()) {
Statement st = it.next();
if (!st.getObject().isResource())
continue;
Resource resource = st.getResource();
if (resource.hasURI(OT.OTClass.Feature.getNS()))
continue;
if (resource.hasURI(OT.OTClass.NumericFeature.getNS()))
property.setClazz(Number.class);
else if (resource.hasURI(OT.OTClass.TupleFeature.getNS()))
property.setClazz(Dictionary.class);
else if (resource.hasURI(OT.OTClass.NominalFeature.getNS()))
property.setNominal(true);
else if (resource.hasURI(OT.OTClass.ModelConfidenceFeature.getNS())) {
// TODO
} else if (resource.hasURI(OT.OTClass.ModelPredictionFeature.getNS())) {
// TODO
}
}
} catch (Exception x) {
} finally {
try {
it.close();
} catch (Exception x) {
}
}
// confidence
try {
t = newEntry.getProperty(OTProperty.confidenceOf.createProperty(jenaModel));
if (t != null) {
PropertyAnnotation pa = null;
Property theFeature = null;
RDFNode resource = t.getObject();
if (resource == newEntry) {
// link to itself, ignore
} else if (resource.isAnon()) {
try {
PropertyAnnotation<Property> ppa = new PropertyAnnotation<Property>();
if (confidenceLinks != null)
theFeature = confidenceLinks.get(resource);
if (theFeature == null) {
theFeature = parseRecord(resource, null, level + 1);
if (theFeature != null) {
if (confidenceLinks == null)
confidenceLinks = new Hashtable<RDFNode, Property>();
confidenceLinks.put(resource, theFeature);
}
}
if (theFeature != null) {
ppa.setObject(theFeature);
pa = ppa;
}
} catch (Exception x) {
pa = null;
}
} else {
PropertyAnnotation<String> psa = new PropertyAnnotation<String>();
if (resource.isLiteral())
psa.setObject(((Literal) resource).getString());
else
psa.setObject(resource.isURIResource() ? ((Resource) resource).getURI() : resource.toString());
pa = psa;
}
pa.setPredicate(OTProperty.confidenceOf.getURI());
pa.setType(OTClass.ModelConfidenceFeature.name());
if (property.getAnnotations() == null)
property.setAnnotations(new PropertyAnnotations());
property.getAnnotations().add(pa);
}
} catch (Exception x) {
x.printStackTrace();
} finally {
t = null;
}
if (property.getName() == null)
property.setName("Unnamed");
return property;
}
use of ambit2.base.data.PropertyAnnotations in project ambit-mirror by ideaconsult.
the class WafflesModelBuilder method process.
@Override
public ModelQueryResults process(Algorithm algorithm) throws AmbitException {
ModelQueryResults model = new ModelQueryResults();
File dataset = trainingData;
if ((dataset == null) || !dataset.exists())
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Empty dataset!");
ShellWafflesLearn waffles = null;
try {
Class clazz = this.getClass().getClassLoader().loadClass(algorithm.getContent().toString());
waffles = (ShellWafflesLearn) clazz.newInstance();
} catch (Exception x) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, algorithm.getContent().toString(), x);
}
// return the model representation as string, not file, so we store it
// into the DB
waffles.setOutputFile(null);
waffles.setOutProperty(WafflesLearnOption.model.name());
Properties in = new Properties();
in.put(WafflesLearnOption.command.name(), WafflesLearnCommand.train.name());
if ((parameters != null) && (parameters.length > 0))
in.put(WafflesLearnOption.alg_opts.name(), parameters[0]);
in.put(WafflesLearnOption.dataset.name(), dataset.getAbsolutePath());
in.put(WafflesLearnOption.algorithm.name(), waffles.getAlgorithm().name());
if (dataOptions != null)
in.put(WafflesLearnOption.data_opts.name(), dataOptions);
try {
Properties out = waffles.runShell(in);
if ((out == null) || (out.getProperty(waffles.getOutProperty()) == null))
throw new AmbitException(String.format("Model not generated", waffles.getOutProperty()));
if (!"0".equals(out.getProperty(ShellWafflesLearn.getExitcodeProperty())))
throw new AmbitException(String.format("Model not generated [exit code = %s] %s", out.getProperty(ShellWafflesLearn.getExitcodeProperty()), out.getProperty(waffles.getOutProperty())));
String name = String.format("%s.%s.%s", waffles.getAlgorithm().name(), UUID.randomUUID().toString(), waffles.getClass().getName());
model.setContentMediaType(algorithm.getFormat().getMediaType());
model.setParameters(parameters);
model.setId(null);
model.setName(name);
model.setAlgorithm(alg_reporter.getURI(algorithm));
/**
* Variables
*/
AlgorithmURIReporter r = new AlgorithmURIReporter();
LiteratureEntry entry = new LiteratureEntry(name, algorithm == null ? waffles.getClass().getName() : r.getURI(applicationRootReference.toString(), algorithm));
LiteratureEntry prediction = new LiteratureEntry(model.getName(), model_reporter.getURI(applicationRootReference.toString(), model));
prediction.setType(_type.Model);
Template predictors = null;
Template dependent = null;
PredictedVarsTemplate predicted = null;
dependent = new Template(name + "#Dependent");
predicted = new PredictedVarsTemplate(name + "#Predicted");
predictors = new Template(name + "#Independent");
StringBuilder labels = null;
for (int i = 0; i < header.numAttributes(); i++) {
boolean isTarget = false;
for (String t : getTargetURI()) if (header.attribute(i).name().equals(t)) {
header.setClassIndex(header.attribute(i).index());
// but could be multiple targets
if (labels == null)
labels = new StringBuilder();
else
labels.append(",");
labels.append(i);
// dependent property
Property property = createPropertyFromReference(new Reference(header.attribute(i).name()), entry, referer);
property.setOrder(i + 1);
dependent.add(property);
// predicted property
Property predictedProperty = new Property(property.getName(), prediction);
predictedProperty.setOrder(i + 1);
predictedProperty.setLabel(property.getLabel());
predictedProperty.setUnits(property.getUnits());
predictedProperty.setClazz(property.getClazz());
predictedProperty.setNominal(property.isNominal());
predictedProperty.setEnabled(true);
predicted.add(predictedProperty);
// link to dependent - necessary for multilabel
PropertyAnnotations annotations = new PropertyAnnotations();
PropertyAnnotation annotation = new PropertyAnnotation();
annotation.setObject(header.attribute(i).name());
annotation.setPredicate("predictionOf");
annotation.setType("Feature");
annotations.add(annotation);
predictedProperty.setAnnotations(annotations);
if (header.attribute(i).isNominal()) {
Enumeration e = header.attribute(i).enumerateValues();
while (e.hasMoreElements()) {
String value = e.nextElement().toString();
predictedProperty.addAllowedValue(value);
annotation = new PropertyAnnotation();
annotation.setObject(value);
annotation.setPredicate("acceptValue");
annotation.setType("Feature");
annotations.add(annotation);
}
predictedProperty.setAnnotations(annotations);
}
isTarget = true;
break;
}
if (!isTarget) {
Property property = createPropertyFromReference(new Reference(header.attribute(i).name()), entry, referer);
property.setOrder(i + 1);
predictors.add(property);
}
}
model.setContent(serializeModel(out.getProperty(waffles.getOutProperty()), header));
// if (supportsDistribution(classifier)) {}
if (labels != null)
setDataOptions(String.format("-labels %s", labels));
model.setPredictors(predictors);
model.setDependent(dependent);
model.setPredicted(predicted);
// model.setParameters(new String[] {"-labels",labels.toString()});
return model;
} catch (AmbitException x) {
throw x;
} catch (Exception x) {
throw new AmbitException(x);
}
}
use of ambit2.base.data.PropertyAnnotations in project ambit-mirror by ideaconsult.
the class PropertyAnnotationTest method testMany.
@Test
public void testMany() throws Exception {
PropertyAnnotations as = new PropertyAnnotations();
PropertyAnnotation a1 = new PropertyAnnotation();
a1.setObject("o");
a1.setPredicate("p");
a1.setType("t");
as.add(a1);
PropertyAnnotation a2 = new PropertyAnnotation();
a2.setObject("o1");
a2.setPredicate("p1");
a2.setType("t1");
as.add(a2);
Assert.assertEquals("\n\t{\t\"type\" : \"t\",\t\"p\" : \"p\",\t\"o\" : \"o\"},\n\t{\t\"type\" : \"t1\",\t\"p\" : \"p1\",\t\"o\" : \"o1\"}", as.toJSON());
}
use of ambit2.base.data.PropertyAnnotations in project ambit-mirror by ideaconsult.
the class CoverageModelBuilder method process.
public ModelQueryResults process(Algorithm algorithm) throws AmbitException {
Instances instances = trainingData;
if ((instances == null) || (instances.numInstances() == 0) || (instances.numAttributes() == 0))
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Empty dataset!");
try {
RemoveWithValues removeMissingValues = new RemoveWithValues();
String[] options = new String[1];
options[0] = "-M";
removeMissingValues.setOptions(options);
removeMissingValues.setInputFormat(instances);
Instances newInstances = Filter.useFilter(instances, removeMissingValues);
instances = newInstances;
} catch (Exception x) {
// use unfiltered
}
// int numAttr = 0;
// for (int j=0; j < instances.numAttributes();j++)
// if (instances.attribute(j).isNumeric()) numAttr++;
// if (numAttr==0) throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,"No numeric attributes!");
Matrix matrix = new Matrix(instances.numInstances(), instances.numAttributes() - 1);
for (int i = 0; i < instances.numInstances(); i++) for (int j = 1; j < instances.numAttributes(); j++) try {
double value = instances.instance(i).value(j);
if (Double.isNaN(value))
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, String.format("Missing value %s in record %s", instances.attribute(j), instances.instance(i)));
matrix.set(i, j - 1, value);
} catch (ResourceException x) {
throw x;
} catch (Exception x) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x.getMessage(), x);
}
DataCoverage coverage = null;
try {
Class clazz = this.getClass().getClassLoader().loadClass(algorithm.getContent().toString());
coverage = (DataCoverage) clazz.newInstance();
} catch (Exception x) {
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, x.getMessage(), x);
}
String name = String.format("%s.%s", UUID.randomUUID().toString(), coverage.getName());
ModelQueryResults m = new ModelQueryResults();
m.setParameters(parameters);
m.setId(null);
m.setContentMediaType(AlgorithmFormat.WEKA.getMediaType());
m.setName(name);
m.setAlgorithm(alg_reporter.getURI(algorithm));
AlgorithmURIReporter r = new AlgorithmURIReporter();
LiteratureEntry entry = new LiteratureEntry(name, algorithm == null ? coverage.getClass().getName() : r.getURI(applicationRootReference.toString(), algorithm));
LiteratureEntry prediction = new LiteratureEntry(m.getName(), model_reporter.getURI(applicationRootReference.toString(), m));
prediction.setType(_type.Model);
Template predictors = null;
Template dependent = null;
PredictedVarsTemplate predicted = null;
if (coverage != null) {
coverage.build(matrix);
predicted = new PredictedVarsTemplate(name + "#ApplicabilityDomain");
Property property = new Property(coverage.getMetricName(), prediction);
property.setEnabled(true);
property.setLabel(String.format("http://www.opentox.org/api/1.1#%s", coverage.getMetricName()));
predicted.add(property);
property = new Property(coverage.getDomainName(), prediction);
property.setLabel(Property.opentox_ConfidenceFeature);
property.setClazz(Number.class);
property.setEnabled(true);
// this is a confidence feature
if (predictedFeatureURI != null) {
PropertyAnnotation<String> a = new PropertyAnnotation<String>();
a.setType(OT.OTClass.ModelConfidenceFeature.name());
a.setPredicate(OT.OTProperty.confidenceOf.name());
a.setObject(predictedFeatureURI);
PropertyAnnotations aa = new PropertyAnnotations();
aa.add(a);
property.setAnnotations(aa);
}
predicted.add(property);
dependent = new Template("Empty");
predictors = new Template(name + "#Independent");
for (int i = 1; i < instances.numAttributes(); i++) {
property = createPropertyFromReference(new Reference(instances.attribute(i).name()), entry, referer);
property.setOrder(i + 1);
predictors.add(property);
}
}
m.setPredictors(predictors);
m.setDependent(dependent);
m.setPredicted(predicted);
try {
serializeModel(coverage, instances, m);
} catch (IOException x) {
throw new AmbitException(x);
}
m.setContentMediaType(AlgorithmFormat.COVERAGE_SERIALIZED.getMediaType());
return m;
}
use of ambit2.base.data.PropertyAnnotations in project ambit-mirror by ideaconsult.
the class DXParser method process.
@Override
public IStructureRecord process(IStructureRecord record) throws AmbitException {
cleanPropertyRefs();
ILiteratureEntry reference = null;
Map<Property, Object> properties = new Hashtable<Property, Object>();
PropertyAnnotations pa = new PropertyAnnotations();
for (Property p : record.getRecordProperties()) {
Object value = record.getRecordProperty(p);
JsonNode strucProperty = dxRoot.get(json_fields.StructureProperties.name()).get(p.getName());
if (strucProperty != null) {
JsonNode propertyRef = strucProperty.get(json_fields.referenceFor.name());
if (propertyRef != null) {
JsonNode prop = dxRoot.get(json_fields.StructureProperties.name()).get(propertyRef.textValue());
if (prop != null && (prop instanceof ObjectNode))
((ObjectNode) prop).put(json_fields.reference.name(), value.toString());
}
continue;
}
JsonNode reportProperty = dxRoot.get(json_fields.Report.name()).get(p.getName());
if (reportProperty == null)
continue;
if (reportProperty.get("import").asBoolean())
try {
importas ias = importas.valueOf(reportProperty.get("importas").textValue());
switch(ias) {
case reference:
{
reference = LiteratureEntry.getDXReference(value.toString());
break;
}
case annotation:
{
PropertyAnnotation a = new PropertyAnnotation();
a.setPredicate(p.getName());
a.setObject(value.toString());
pa.add(a);
break;
}
default:
{
}
}
} catch (Exception x) {
}
}
if (reference == null)
reference = LiteratureEntry.getDXReference();
for (Property p : record.getRecordProperties()) {
Object value = record.getRecordProperty(p);
JsonNode reportProperty = dxRoot.get(json_fields.Report.name()).get(p.getName());
JsonNode strucProperty = dxRoot.get(json_fields.StructureProperties.name()).get(p.getName());
if (reportProperty != null)
continue;
if (strucProperty != null) {
ILiteratureEntry propReference = reference;
if (strucProperty.get(json_fields.referenceFor.name()) != null)
continue;
if (strucProperty.get(json_fields.reference.name()) != null) {
try {
propReference = LiteratureEntry.getDXReference(strucProperty.get(json_fields.reference.name()).textValue());
} catch (Exception x) {
}
}
if (strucProperty.get(json_fields.ot.name()) != null)
try {
p.setLabel(strucProperty.get(json_fields.ot.name()).textValue());
} catch (Exception x) {
}
p.setReference(propReference);
properties.put(p, value);
} else if ("Superendpoints".equals(p.getName())) {
verifySuperEndpoints(record, p);
} else if (p.getName().startsWith(DXPrefix)) {
Property dx = verifyDXProperty(p, value, reference);
if (dx != null) {
properties.put(dx, value);
for (PropertyAnnotation a : pa) {
dx.getAnnotations().add(a);
}
}
} else
properties.put(p, value);
}
record.clearProperties();
record.addRecordProperties(properties);
return record;
}
Aggregations