/*
* Copyright (c) 2011, Yahoo! Inc. All rights reserved.
*
* Redistribution and use of this software in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Yahoo! Inc. nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission of Yahoo!
* Inc.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.yahoo.labs.taxomo.learn;
import junit.framework.TestCase;
import org.apache.log4j.Level;
import org.junit.Before;
import org.junit.Test;
import com.yahoo.labs.taxomo.LearnModel;
import com.yahoo.labs.taxomo.Model;
import com.yahoo.labs.taxomo.util.SymbolTransitionFrequencies;
import com.yahoo.labs.taxomo.util.Taxonomy;
public class LearnModelTest extends TestCase {
public final String TAXOFILE1 = "test1.taxo";
public final String SEQFILE1 = "test1.seq";
Taxonomy taxo1;
SymbolTransitionFrequencies sym1;
Class<SearchStrategy> strategy;
@Before
public void setUp() throws Exception {
taxo1 = new Taxonomy(TAXOFILE1, LearnModelTest.class.getResourceAsStream(TAXOFILE1));
sym1 = new SymbolTransitionFrequencies(taxo1);
sym1.processStream(LearnModelTest.class.getResourceAsStream(SEQFILE1));
LearnModel.logger.setLevel(Level.ERROR);
strategy = getStrategy(CloserToOrigin.class.getName());
}
@Test
public void testLearnFromLeaf() throws ClassNotFoundException {
Candidate initialCandidate = Candidate.createLeafCandidate(taxo1);
assertEquals("(A B C D E F G H)", initialCandidate.toBriefString());
assertEquals(
"# Valid states\ns ABCD EF GH\n\n# Transition probabilities\nt ^ ABCD 0.905\nt ^ EF 0.054\nt ^ GH 0.041\nt ABCD ABCD 0.6757206208425721\nt ABCD EF 0.0689209164818921\nt ABCD GH 0.07058388765705839\nt ABCD $ 0.18477457501847747\nt EF ABCD 0.9122055674518201\nt EF EF 0.047109207708779445\nt EF GH 0.04068522483940043\nt GH ABCD 0.9118279569892473\nt GH EF 0.03870967741935484\nt GH GH 0.04946236559139785\n\n# Emission probabilities\no ^ ^ 1.0\no ABCD a 0.05007390983000739\no ABCD b 0.45805617147080563\no ABCD c 0.17165558019216556\no ABCD d 0.3202143385070214\no EF e 0.5117773019271948\no EF f 0.48822269807280516\no GH g 0.4946236559139785\no GH h 0.5053763440860215\no $ $ 1.0\n",
learnBestModelFull(initialCandidate, 1.0));
}
@SuppressWarnings("unchecked")
Class<SearchStrategy> getStrategy(String className) throws ClassNotFoundException {
return (Class<SearchStrategy>) Class.forName(className);
}
String learnBestModelFull(Candidate initialCandidate, double d) {
return learnBestModel(initialCandidate,d).toString(Model.PrintMode.FULL);
}
String learnBestModelStates(Candidate initialCandidate, double d) {
return learnBestModel(initialCandidate,d).toString(Model.PrintMode.STATES_ONLY);
}
Model learnBestModel(Candidate initialCandidate, double d) {
LearnModel learner = new LearnModel(taxo1, sym1, initialCandidate, strategy, d);
learner.learn();
return learner.getBestModel();
}
String learnBestModelFull(Candidate initialCandidate, int batchSize, int maxIterations) {
return learnBestModel(initialCandidate, batchSize, maxIterations).toString(Model.PrintMode.FULL);
}
String learnBestModelStates(Candidate initialCandidate, int batchSize, int maxIterations) {
return learnBestModel(initialCandidate, batchSize, maxIterations).toString(Model.PrintMode.STATES_ONLY);
}
Model learnBestModel(Candidate initialCandidate, int batchSize, int numIterations ) {
LearnModel learner = new LearnModel(taxo1, sym1, initialCandidate, strategy, 1.0);
learner.setBatchSize(batchSize);
learner.setMaxIterations(numIterations);
learner.learn();
return learner.getBestModel();
}
Candidate getLeafCandidate() {
return Candidate.createLeafCandidate(taxo1);
}
}