Skip to content

Commit b39182e

Browse files
more sanity checks
1 parent c5b0c91 commit b39182e

8 files changed

Lines changed: 141 additions & 128 deletions

File tree

components-core/src/main/java/org/dllearner/learningproblems/ClassAsInstanceLearningProblem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void init() throws ComponentInitException {
8989
if (!allClasses.containsAll(allExamples)) {
9090
Set<OWLClass> missing = Sets.difference(allExamples, allClasses);
9191
double percentage = (double) missing.size() / allExamples.size();
92-
percentage = Math.round(percentage * 1000) / 1000;
92+
percentage = Math.round(percentage * 1000.0) / 1000.0;
9393
String str =
9494
"The examples (" + (percentage * 100) + " % of total) " +
9595
"below are not contained in the knowledge base " +

components-core/src/main/java/org/dllearner/learningproblems/ClassExpressionLearningProblem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public void init() throws ComponentInitException {
154154
Collections.shuffle(classInstances, rand);
155155
Collections.shuffle(superClassInstances, rand);
156156

157+
157158
if (accuracyMethod == null) {
158159
accuracyMethod = new AccMethodPredAcc(true);
159160
}

components-core/src/main/java/org/dllearner/learningproblems/FuzzyPosNegLP.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@
1818
*/
1919
package org.dllearner.learningproblems;
2020

21+
import java.util.Map;
22+
import java.util.Set;
23+
import java.util.SortedSet;
24+
import java.util.TreeSet;
25+
import java.util.stream.Collectors;
26+
27+
import com.google.common.collect.Sets;
2128
import org.dllearner.core.AbstractClassExpressionLearningProblem;
2229
import org.dllearner.core.AbstractReasonerComponent;
30+
import org.dllearner.core.ComponentInitException;
2331
import org.dllearner.core.config.ConfigOption;
2432
import org.dllearner.core.owl.fuzzydll.FuzzyIndividual;
33+
import org.dllearner.utilities.Helper;
2534
import org.semanticweb.owlapi.model.OWLIndividual;
2635
import org.semanticweb.owlapi.model.OWLNamedIndividual;
2736

28-
import java.util.Map;
29-
import java.util.SortedSet;
30-
import java.util.TreeSet;
31-
3237
/**
3338
* @author Jens Lehmann
3439
*
@@ -44,15 +49,6 @@ public abstract class FuzzyPosNegLP extends AbstractClassExpressionLearningProbl
4449
@ConfigOption()
4550
protected SortedSet<FuzzyIndividual> fuzzyExamples;
4651

47-
public void setFuzzyExamples(Map<OWLIndividual, Double> fuzzyEx) {
48-
fuzzyExamples = new TreeSet<>();
49-
50-
for (OWLIndividual i : fuzzyEx.keySet()) {
51-
this.fuzzyExamples.add(new FuzzyIndividual(i.toStringID(), fuzzyEx.get(i).doubleValue()));
52-
}
53-
}
54-
55-
protected boolean useRetrievalForClassification = false;
5652
protected double percentPerLengthUnit = 0.05;
5753
protected double totalTruth = 0;
5854

@@ -68,10 +64,13 @@ public FuzzyPosNegLP(AbstractReasonerComponent reasoningService) {
6864
* @see org.dllearner.core.Component#init()
6965
*/
7066
@Override
71-
public void init() {
67+
public void init() throws ComponentInitException {
7268
// commented by Josue as now there's no need of + and - examples (more code need to be deleted in this sense)
7369
// allExamples = Helper.union(positiveExamples, negativeExamples);
74-
70+
71+
// sanity check whether examples are contained in KB
72+
Helper.checkIndividuals(reasoner, Sets.union(Sets.union(positiveExamples, negativeExamples), fuzzyExamples));
73+
7574
initialized = true;
7675
}
7776

@@ -102,5 +101,13 @@ public SortedSet<FuzzyIndividual> getFuzzyExamples() {
102101
public void setFuzzyExamples(SortedSet<FuzzyIndividual> fuzzyExamples) {
103102
this.fuzzyExamples = fuzzyExamples;
104103
}
104+
105+
public void setFuzzyExamples(Map<OWLIndividual, Double> fuzzyEx) {
106+
fuzzyExamples = fuzzyEx.entrySet().stream()
107+
.map(e -> new FuzzyIndividual(e.getKey().toStringID(), e.getValue()))
108+
.collect(Collectors.toCollection(TreeSet::new));
109+
}
110+
111+
105112

106113
}

components-core/src/main/java/org/dllearner/learningproblems/FuzzyPosNegLPStandard.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.dllearner.core.AbstractReasonerComponent;
2222
import org.dllearner.core.ComponentAnn;
23+
import org.dllearner.core.ComponentInitException;
2324
import org.dllearner.core.EvaluatedDescription;
2425
import org.dllearner.core.config.ConfigOption;
2526
import org.dllearner.core.owl.fuzzydll.FuzzyIndividual;
@@ -81,7 +82,7 @@ public FuzzyPosNegLPStandard(AbstractReasonerComponent reasoningService, SortedS
8182
}
8283

8384
@Override
84-
public void init() {
85+
public void init() throws ComponentInitException {
8586
super.init();
8687

8788
if(useApproximations && accuracyMethod.equals(HeuristicType.PRED_ACC)) {
@@ -97,7 +98,7 @@ public double getAccuracyOrTooWeak(OWLClassExpression description, double noise)
9798
return useApproximations ? getAccuracyOrTooWeakApprox(description, noise) : getAccuracyOrTooWeakExact(description, noise);
9899
}
99100

100-
public double getAccuracyOrTooWeakApprox(OWLClassExpression description, double noise) {
101+
private double getAccuracyOrTooWeakApprox(OWLClassExpression description, double noise) {
101102
if(heuristic.equals(HeuristicType.PRED_ACC)) {
102103
int maxNotCovered = (int) Math.ceil(noise*positiveExamples.size());
103104

@@ -144,7 +145,7 @@ public double getAccuracyOrTooWeakApprox(OWLClassExpression description, double
144145
}
145146

146147
// compute how accurate our current approximation is and return if it is sufficiently accurate
147-
double approx[] = Heuristics.getPredAccApproximation(positiveExamples.size(), negativeExamples.size(), 1, nrOfPosChecks, posClassifiedAsPos, nrOfNegChecks, negClassifiedAsNeg);
148+
double[] approx = Heuristics.getPredAccApproximation(positiveExamples.size(), negativeExamples.size(), 1, nrOfPosChecks, posClassifiedAsPos, nrOfNegChecks, negClassifiedAsNeg);
148149
if(approx[1]<approxDelta) {
149150
// System.out.println(approx[0]);
150151
return approx[0];
@@ -205,7 +206,7 @@ public double getAccuracyOrTooWeakApprox(OWLClassExpression description, double
205206
}
206207
}
207208

208-
public double getAccuracyOrTooWeakExact(OWLClassExpression description, double noise) {
209+
private double getAccuracyOrTooWeakExact(OWLClassExpression description, double noise) {
209210
if(heuristic.equals(HeuristicType.PRED_ACC)) {
210211
return getPredAccuracyOrTooWeakExact(description, noise);
211212
} else if(heuristic.equals(HeuristicType.FMEASURE)) {
@@ -240,7 +241,7 @@ public double getAccuracyOrTooWeakExact(OWLClassExpression description, double n
240241
/* (non-Javadoc)
241242
* @see org.dllearner.core.LearningProblem#getAccuracyOrTooWeak(org.dllearner.core.owl.Description, double)
242243
*/
243-
public double getPredAccuracyOrTooWeakExact(OWLClassExpression description, double noise) {
244+
private double getPredAccuracyOrTooWeakExact(OWLClassExpression description, double noise) {
244245

245246
// System.out.println(errorIndex++);
246247

@@ -259,7 +260,7 @@ public double getPredAccuracyOrTooWeakExact(OWLClassExpression description, doub
259260
// double negMembership = 0;
260261
double descriptionMembership = 0;
261262
// double accumulatedSingleMembership = 0;
262-
double nonAccumulativeDescriptionMembership = 0;
263+
double nonAccumulativeDescriptionMembership;
263264
double accumulativeDescriptionMembership = 0;
264265

265266
// System.out.println("noise = " + noise);
@@ -346,14 +347,14 @@ private double crispfMeasure(OWLClassExpression description, double noise) {
346347
return Heuristics.getFScore(recall, precision);
347348
}
348349

349-
public double getFMeasureOrTooWeakExact(OWLClassExpression description, double noise) {
350+
private double getFMeasureOrTooWeakExact(OWLClassExpression description, double noise) {
350351

351352
// added by Josue
352353
// fuzzy F-measure
353354
double coveredMembershipDegree = 0;
354355
double totalMembershipDegree = 0;
355356
double invertedCoveredMembershipDegree = 0;
356-
double lastMembershipDegree = 0;
357+
double lastMembershipDegree;
357358

358359
for (FuzzyIndividual ind: fuzzyExamples) {
359360
lastMembershipDegree = (1 - Math.abs(ind.getTruthDegree() - getReasoner().hasTypeFuzzyMembership(description, ind)));
@@ -394,7 +395,7 @@ public double getFMeasureOrTooWeakApprox(OWLClassExpression description, double
394395
int maxNotCovered = (int) Math.ceil(noise*positiveExamples.size());
395396
int instancesCovered = 0;
396397
int instancesNotCovered = 0;
397-
int total = 0;
398+
int total;
398399
boolean estimatedA = false;
399400

400401
double lowerBorderA = 0;

components-core/src/main/java/org/dllearner/learningproblems/PosNegLP.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.dllearner.core.ComponentInitException;
3030
import org.dllearner.core.config.ConfigOption;
3131
import org.dllearner.reasoning.SPARQLReasoner;
32+
import org.dllearner.utilities.Helper;
3233
import org.semanticweb.owlapi.model.OWLIndividual;
3334
import org.semanticweb.owlapi.model.OWLNamedIndividual;
3435
import org.springframework.beans.factory.annotation.Autowired;
@@ -107,24 +108,11 @@ public void init() throws ComponentInitException {
107108
}
108109

109110
// sanity check whether examples are contained in KB
110-
if(reasoner != null && !(reasoner instanceof SPARQLReasoner) && !reasoner.getIndividuals().containsAll(allExamples)) {
111-
Set<OWLIndividual> missing = Sets.difference(allExamples, reasoner.getIndividuals());
112-
double percentage = (double)missing.size() / allExamples.size();
113-
percentage = Math.round(percentage * 1000) / 1000;
114-
String str = "The examples (" + (percentage * 100) + " % of total) below are not contained in the knowledge base (check spelling and prefixes)\n";
115-
str += missing.toString();
116-
if(missing.size()==allExamples.size()) {
117-
throw new ComponentInitException(str);
118-
} if(percentage < 0.10) {
119-
logger.warn(str);
120-
} else {
121-
logger.error(str);
122-
}
123-
}
111+
Helper.checkIndividuals(reasoner, allExamples);
124112

125113
initialized = true;
126114
}
127-
115+
128116
public Set<OWLIndividual> getNegativeExamples() {
129117
return negativeExamples;
130118
}
Lines changed: 57 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,106 @@
11
/**
22
* Copyright (C) 2007 - 2016, Jens Lehmann
3-
*
3+
* <p>
44
* This file is part of DL-Learner.
5-
*
5+
* <p>
66
* DL-Learner is free software; you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License as published by
88
* the Free Software Foundation; either version 3 of the License, or
99
* (at your option) any later version.
10-
*
10+
* <p>
1111
* DL-Learner is distributed in the hope that it will be useful,
1212
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1313
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414
* GNU General Public License for more details.
15-
*
15+
* <p>
1616
* You should have received a copy of the GNU General Public License
1717
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919
package org.dllearner.learningproblems;
2020

21+
import com.google.common.collect.Sets;
2122
import org.dllearner.core.AbstractReasonerComponent;
2223
import org.dllearner.core.ComponentAnn;
2324
import org.dllearner.core.ComponentInitException;
2425
import org.dllearner.core.EvaluatedDescription;
2526
import org.dllearner.core.config.ConfigOption;
27+
import org.dllearner.utilities.Helper;
2628
import org.semanticweb.owlapi.model.OWLClassExpression;
2729
import org.semanticweb.owlapi.model.OWLIndividual;
2830

2931
import java.util.Set;
3032
import java.util.SortedSet;
3133
import java.util.TreeSet;
34+
3235
/**
3336
* A ternary learning problem (positive, negative and uncertain instances) to manage the problem of the Open World Assumption
3437
* typically employed for ontologies
3538
* @author Utente
3639
*
3740
*/
38-
@ComponentAnn(name="PosNegUndLP", shortName="posNegUndLP", version=1.0, description="A learning problem with uncertain-membership instances")
39-
public class PosNegUndLP extends PosNegLPStandard implements Cloneable{
40-
//private SortedSet<OWLIndividual> positiveExample;
41-
//private SortedSet<OWLIndividual> negativeExample;
42-
@ConfigOption(description = "the uncertain examples", required = true)
43-
private Set<OWLIndividual> uncertainExamples;
44-
45-
// getter and setters
46-
public Set<OWLIndividual> getPositiveExamples() {
47-
return new TreeSet<>(super.getPositiveExamples());
48-
}
41+
@ComponentAnn(name = "PosNegUndLP", shortName = "posNegUndLP", version = 1.0, description = "A learning problem with uncertain-membership instances")
42+
public class PosNegUndLP extends PosNegLPStandard implements Cloneable {
4943

50-
public void setPositiveExamples(SortedSet<OWLIndividual> positiveExample) {
51-
this.positiveExamples = positiveExample;
52-
}
44+
@ConfigOption(description = "the uncertain examples", required = true)
45+
private Set<OWLIndividual> uncertainExamples;
5346

54-
public Set<OWLIndividual> getNegativeExamples() {
55-
return new TreeSet<>(super.getNegativeExamples());//negativeExamples;
56-
}
47+
public PosNegUndLP() {}
5748

58-
public void setNegativeExamples(Set<OWLIndividual> negativeExample) {
59-
this.negativeExamples = negativeExample;
49+
public PosNegUndLP(AbstractReasonerComponent reasoner) {
50+
super(reasoner);
6051
}
6152

62-
public Set<OWLIndividual> getUncertainExamples() {
63-
return new TreeSet<>(uncertainExamples);
53+
public PosNegUndLP(AbstractReasonerComponent reasoningService,
54+
SortedSet<OWLIndividual> positiveExamples,
55+
SortedSet<OWLIndividual> negativeExamples,
56+
SortedSet<OWLIndividual> uncertainExamples) {
57+
super(reasoningService, positiveExamples, negativeExamples);
58+
this.uncertainExamples = uncertainExamples;
6459
}
6560

66-
public void setUncertainExamples(Set<OWLIndividual> uncertainExample) {
67-
this.uncertainExamples = uncertainExample;
68-
}
61+
public Set<OWLIndividual> getPositiveExamples() {
62+
return new TreeSet<>(super.getPositiveExamples());
63+
}
6964

70-
71-
72-
public PosNegUndLP(){
73-
74-
super();
75-
}
65+
public Set<OWLIndividual> getNegativeExamples() {
66+
return new TreeSet<>(super.getNegativeExamples());
67+
}
7668

77-
public PosNegUndLP(AbstractReasonerComponent reasoner){
78-
super(reasoner);
79-
80-
}
69+
public Set<OWLIndividual> getUncertainExamples() {
70+
return new TreeSet<>(uncertainExamples);
71+
}
8172

82-
public PosNegUndLP(AbstractReasonerComponent reasoningService, SortedSet<OWLIndividual> positiveExamples, SortedSet<OWLIndividual> negativeExamples, SortedSet<OWLIndividual> uncertainExamples){
83-
84-
// this.setReasoner(reasoningService);
85-
// this.positiveExamples=positiveExamples;
86-
// this.negativeExamples=negativeExamples;
87-
super(reasoningService, positiveExamples, negativeExamples);
88-
this.uncertainExamples=uncertainExamples;
89-
}
90-
91-
@Override
92-
public void init() throws ComponentInitException {
93-
initialized = true;
94-
}
73+
public void setUncertainExamples(Set<OWLIndividual> uncertainExample) {
74+
this.uncertainExamples = uncertainExample;
75+
}
9576

96-
@Override
97-
public ScorePosNeg computeScore(OWLClassExpression description) {
98-
// TODO Auto-generated method stub
99-
return super.computeScore(description);
100-
}
77+
@Override
78+
public void init() throws ComponentInitException {
79+
// sanity check whether examples are contained in KB
80+
Helper.checkIndividuals(reasoner, Sets.union(Sets.union(positiveExamples, negativeExamples), uncertainExamples));
10181

102-
@Override
103-
public EvaluatedDescription evaluate(OWLClassExpression description) {
104-
// TODO Auto-generated method stub
105-
return super.evaluate(description);
106-
}
82+
initialized = true;
83+
}
84+
85+
//TODO add two methods: the first one performs classification by inducing the derived concept definition (see the source code of PosNegstandard )
86+
// the second one performs classification with the induced algorithms.tree.models in order to deal with specific settings such as binary classification or missing values for TDTs and ETDTs
10787

108-
//TODO add two methods: the first one performs classification by inducing the derived concept definition (see the source code of PosNegstandard )
109-
// the second one performs classification with the induced algorithms.tree.models in order to deal with specific settings such as binary classification or missing values for TDTs and ETDTs
110-
/**
111-
* A method for binarizing a ternary learning problem. This is important to work if you want to run a method
112-
* such as CELOE starting from randomly generated queries
113-
* @return the pos/neg learning problem
114-
*/
115-
public PosNegLP getPosNegLP(){
116-
PosNegLPStandard binaryProblem= new PosNegLPStandard(getReasoner());
117-
binaryProblem.setPositiveExamples(getPositiveExamples());
118-
SortedSet<OWLIndividual> therestOfWorld= new TreeSet<>();
119-
//positive vs. the rest of world
120-
therestOfWorld.addAll(getNegativeExamples());
121-
therestOfWorld.addAll(uncertainExamples);
122-
binaryProblem.setNegativeExamples(therestOfWorld);
88+
/**
89+
* A method for binarizing a ternary learning problem. This is important to work if you want to run a method
90+
* such as CELOE starting from randomly generated queries
91+
* @return the pos/neg learning problem
92+
*/
93+
public PosNegLP getPosNegLP() {
94+
PosNegLP binaryProblem = new PosNegLPStandard(getReasoner());
95+
binaryProblem.setPositiveExamples(getPositiveExamples());
96+
SortedSet<OWLIndividual> therestOfWorld = new TreeSet<>();
97+
//positive vs. the rest of world
98+
therestOfWorld.addAll(getNegativeExamples());
99+
therestOfWorld.addAll(uncertainExamples);
100+
binaryProblem.setNegativeExamples(therestOfWorld);
123101
// System.out.println(getPositiveExamples().size()+" "+therestOfWorld.size());
124-
125-
return binaryProblem;
126-
}
102+
103+
return binaryProblem;
104+
}
127105

128106
}

0 commit comments

Comments
 (0)