From: <jen...@us...> - 2011-01-31 19:37:43
|
Revision: 2635 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=2635&view=rev Author: jenslehmann Date: 2011-01-31 19:37:37 +0000 (Mon, 31 Jan 2011) Log Message: ----------- started unit tests for pos-neg learning measures Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/learningproblems/PosNegLPStandard.java trunk/components-core/src/test/java/org/dllearner/test/junit/HeuristicTests.java Modified: trunk/components-core/src/main/java/org/dllearner/learningproblems/PosNegLPStandard.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/learningproblems/PosNegLPStandard.java 2011-01-26 18:39:46 UTC (rev 2634) +++ trunk/components-core/src/main/java/org/dllearner/learningproblems/PosNegLPStandard.java 2011-01-31 19:37:37 UTC (rev 2635) @@ -313,18 +313,7 @@ @Override public double getAccuracyOrTooWeak(Description description, double noise) { // delegates to the appropriate methods - return useApproximations ? getAccuracyOrTooWeakApprox(description, noise) : getAccuracyOrTooWeakExact(description, noise); - /* - if(useApproximations) { - if(useFMeasure) { - return getFMeasureOrTooWeakApprox(description, noise); - } else { - throw new Error("approximating pred. acc not implemented"); - } - } else { - return getPredAccuracyOrTooWeakExact(description, noise); - } - */ + return useApproximations ? getAccuracyOrTooWeakApprox(description, noise) : getAccuracyOrTooWeakExact(description, noise); } public double getAccuracyOrTooWeakApprox(Description description, double noise) { @@ -386,6 +375,8 @@ return ret; } else if(heuristic.equals(HeuristicType.FMEASURE)) { + System.out.println("Testing " + description); + // we abort when there are too many uncovered positives int maxNotCovered = (int) Math.ceil(noise*positiveExamples.size()); int instancesCovered = 0; @@ -437,6 +428,8 @@ if(heuristic.equals(HeuristicType.PRED_ACC)) { return getPredAccuracyOrTooWeakExact(description, noise); } else if(heuristic.equals(HeuristicType.FMEASURE)) { + return getFMeasureOrTooWeakExact(description, noise); + /* // computing R(C) restricted to relevant instances int additionalInstances = 0; for(Individual ind : negativeExamples) { @@ -457,6 +450,7 @@ double precision = (additionalInstances + coveredInstances == 0) ? 0 : coveredInstances / (double) (coveredInstances + additionalInstances); return Heuristics.getFScore(recall, precision); + */ } else { throw new Error("Heuristic " + heuristic + " not implemented."); } @@ -518,7 +512,8 @@ double precision = (additionalInstances + coveredInstances == 0) ? 0 : coveredInstances / (double) (coveredInstances + additionalInstances); - return getFMeasure(recall, precision); +// return getFMeasure(recall, precision); + return Heuristics.getFScore(recall, precision); } // instead of using the standard operation, we use optimisation Modified: trunk/components-core/src/test/java/org/dllearner/test/junit/HeuristicTests.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/test/junit/HeuristicTests.java 2011-01-26 18:39:46 UTC (rev 2634) +++ trunk/components-core/src/test/java/org/dllearner/test/junit/HeuristicTests.java 2011-01-31 19:37:37 UTC (rev 2635) @@ -23,6 +23,8 @@ import java.net.MalformedURLException; import java.net.URL; +import java.util.Set; +import java.util.TreeSet; import org.dllearner.core.ComponentInitException; import org.dllearner.core.ComponentManager; @@ -37,9 +39,13 @@ import org.dllearner.kb.KBFile; import org.dllearner.learningproblems.ClassLearningProblem; import org.dllearner.learningproblems.Heuristics; +import org.dllearner.learningproblems.PosNegLPStandard; import org.dllearner.reasoning.OWLAPIReasoner; +import org.dllearner.utilities.Helper; import org.junit.Test; +import scala.actors.threadpool.Arrays; + /** * Tests for various heuristics employed in learning problems. * @@ -159,6 +165,60 @@ } @Test + public void posNegLPLearningTests() throws ComponentInitException { + // create artificial ontology + KB kb = new KB(); + String ns = "http://dl-learner.org/junit/"; + NamedClass[] nc = new NamedClass[5]; + for(int i=0; i<5; i++) { + nc[i] = new NamedClass(ns + "A" + i); + } + Individual[] ind = new Individual[100]; + for(int i=0; i<100; i++) { + ind[i] = new Individual(ns + "i" + i); + } + + // assert individuals to owl:Thing (such that they exist in the knowledge base) + for(int i=0; i<100; i++) { + kb.addAxiom(new ClassAssertionAxiom(Thing.instance,ind[i])); + } + + // A0 has 20 instances (i0 to i19) + for(int i=0; i<20; i++) { + kb.addAxiom(new ClassAssertionAxiom(nc[0],ind[i])); + } + + // A1 has 20 instances (i10 to i29) + for(int i=10; i<30; i++) { + kb.addAxiom(new ClassAssertionAxiom(nc[1],ind[i])); + } + + // A2 has 40 instances (i10 to i49) + for(int i=10; i<50; i++) { + kb.addAxiom(new ClassAssertionAxiom(nc[2],ind[i])); + } + + // A3 has 5 instances (i8 to i12) + for(int i=8; i<13; i++) { + kb.addAxiom(new ClassAssertionAxiom(nc[3],ind[i])); + } + + ComponentManager cm = ComponentManager.getInstance(); + KnowledgeSource ks = new KBFile(kb); + ReasonerComponent reasoner = cm.reasoner(OWLAPIReasoner.class, ks); + PosNegLPStandard problem = cm.learningProblem(PosNegLPStandard.class, reasoner); + ks.init(); + reasoner.init(); + + Individual[] pos1 = new Individual[] {ind[1], ind[2]}; + Individual[] neg1 = new Individual[] {ind[3], ind[4]}; + HeuristicTests.configurePosNegStandardLP(problem, pos1, neg1, "fmeasure", false); + + // TODO: continue + } + + + @Test public void approximationTests() { // perform F-Measure example in ontology engineering paper, which was computed on paper double[] approx1 = Heuristics.getFScoreApproximation(800, 0.8, 1, 10000, 41, 31); @@ -226,4 +286,19 @@ problem.init(); } + @SuppressWarnings("unchecked") + private static void configurePosNegStandardLP(PosNegLPStandard problem, Individual[] positiveExamples, Individual[] negativeExamples, String accuracyMethod, boolean useApproximations) throws ComponentInitException { + Set<Individual> s1 = new TreeSet<Individual>(Arrays.asList(positiveExamples)); + Set<Individual> s2 = new TreeSet<Individual>(Arrays.asList(negativeExamples)); + HeuristicTests.configurePosNegStandardLP(problem, s1, s2, accuracyMethod, useApproximations); + } + + // convencience method to set the learning problem to a desired configuration (approximations disabled) + private static void configurePosNegStandardLP(PosNegLPStandard problem, Set<Individual> positiveExamples, Set<Individual> negativeExamples, String accuracyMethod, boolean useApproximations) throws ComponentInitException { + problem.getConfigurator().setPositiveExamples(Helper.getStringSet(positiveExamples)); + problem.getConfigurator().setNegativeExamples(Helper.getStringSet(negativeExamples)); + problem.getConfigurator().setAccuracyMethod(accuracyMethod); + problem.getConfigurator().setUseApproximations(useApproximations); + problem.init(); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |