/*
* 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;
import java.io.IOException;
import java.util.ArrayList;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import com.yahoo.labs.taxomo.util.State;
import com.yahoo.labs.taxomo.util.StateSet;
import com.yahoo.labs.taxomo.util.Taxonomy;
import com.yahoo.labs.taxomo.util.UtilTest;
public class ModelTest extends TestCase {
public final String TREEFILE1 = "example.taxo";
public final String MODELFILE1 = "example.mod";
public final String SEQFILE1 = "example.seq";
StateSet set1;
Model model1;
@Before
public void setUp() throws Exception {
set1 = new StateSet(UtilTest.class.getResourceAsStream(MODELFILE1), new Taxonomy(TREEFILE1, UtilTest.class.getResourceAsStream(TREEFILE1)));
model1 = new Model(UtilTest.class.getResourceAsStream(MODELFILE1), set1);
assertEquals(5, model1.nbStates());
}
@Test
public void testGetAij() {
assertEquals(0.3, model1.getAij(set1.getStateNumber("AB"), set1.getStateNumber("C")));
assertEquals(0.9, model1.getAij(set1.getStateNumber("C"), set1.getStateNumber("D")));
assertEquals(0.0, model1.getAij(set1.getStateNumber("D"), set1.getStateNumber("AB")));
}
@Test
public void testGetEmissionProbability() {
assertEquals(0.8, model1.getEmissionProbability(set1.getStateNumber("AB"), set1.getSymbolNumber("a")));
}
@Test
public void testLogProb() throws IOException {
assertEquals(-19.45830894, model1.logProb(UtilTest.class.getResourceAsStream(SEQFILE1)));
}
@Test
public void testSequence() {
ArrayList<String> seq = model1.sequence();
assertTrue(seq.size() > 0);
for (int i = 0; i < seq.size(); i++) {
assertTrue(set1.getSymbolNumber(seq.get(i)) < set1.numSymbols());
assertTrue(set1.getSymbolNumber(seq.get(i)) > 0);
}
}
@Test
public void testToString() {
assertEquals(
"# Valid states\ns AB C D\n\n# Transition probabilities\nt ^ AB 0.5\nt ^ C 0.3\nt ^ D 0.2\nt AB AB 0.5\nt AB C 0.3\nt AB D 0.2\nt C D 0.9\nt C $ 0.1\nt D C 0.5\nt D $ 0.5\n\n# Emission probabilities\no ^ ^ 1.0\no AB a 0.8\no AB b 0.2\no C c 1.0\no D d 1.0\no $ $ 1.0\n",
model1.toString());
}
@Test
public void testToStringPrintMode() {
assertEquals(
"# Valid states\ns AB C D\n\n# Transition probabilities\nt ^ AB 0.5\nt ^ C 0.3\nt ^ D 0.2\nt AB AB 0.5\nt AB C 0.3\nt AB D 0.2\nt C D 0.9\nt C $ 0.1\nt D C 0.5\nt D $ 0.5\n\n# Emission probabilities\no ^ ^ 1.0\no AB a 0.8\no AB b 0.2\no C c 1.0\no D d 1.0\no $ $ 1.0\n",
model1.toString(Model.PrintMode.FULL));
assertEquals( "AB C D", model1.toString(Model.PrintMode.STATES_ONLY) );
}
@Test
public void testViterbiCalculate() {
ArrayList<String> seq1 = new ArrayList<String>();
seq1.add("a");
seq1.add("b");
seq1.add("b");
seq1.add("c");
seq1.add("d");
assertEquals( -7.523941418, model1.viterbiCalculate(seq1));
}
@Test
public void testViterbiCalculateNonOverlap() {
ArrayList<String> seq1 = new ArrayList<String>();
seq1.add("a");
seq1.add("b");
seq1.add("b");
seq1.add("c");
seq1.add("d");
assertEquals( -7.523941418, model1.viterbiCalculateNonOverlap(seq1));
ArrayList<String> seq2 = new ArrayList<String>();
seq2.add("a");
seq2.add("b");
seq2.add("c");
seq2.add("b");
seq2.add("d");
assertTrue( model1.viterbiCalculateNonOverlap(seq2) == Double.NEGATIVE_INFINITY );
}
@Test
public void testViterbiPath() {
ArrayList<String> seq1 = new ArrayList<String>();
seq1.add("a");
seq1.add("b");
seq1.add("b");
seq1.add("c");
seq1.add("d");
State[] states = model1.viterbiPath(seq1);
assertEquals( "^", states[0].name() );
assertEquals( "AB", states[1].name() );
assertEquals( "AB", states[2].name() );
assertEquals( "AB", states[3].name() );
assertEquals( "C", states[4].name() );
assertEquals( "D", states[5].name() );
assertEquals( "$", states[6].name() );
}
public void assertEquals(double d1, double d2) {
assertEquals(d1, d2, 1e-6);
}
}