Skip to content

Commit

Permalink
got rid of a reference to kmean cluster sizes in debug code as it was
Browse files Browse the repository at this point in the history
causing an incompatibility with the latest version of weka
  • Loading branch information
Marco Gillies committed May 24, 2015
1 parent b828836 commit 982a2ef
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 105 deletions.
3 changes: 2 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="classes" path="java"/>
<classpathentry kind="var" path="JRE_LIB" rootpath="JRE_SRCROOT" sourcepath="JRE_SRC"/>
<classpathentry kind="var" path="JRE_LIB" sourcepath="JRE_SRC"/>
<classpathentry kind="con" path="/Users/mas02mg/Documents/code/HMMWeka/build/classes"/>
<classpathentry kind="con" path="/Users/mas02mg/Documents/code/HMMWeka/build/testcases"/>
<classpathentry kind="lib" path="/Applications/eclipse/plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="build"/>
</classpath>
6 changes: 3 additions & 3 deletions Description.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
PackageName=HMM

# Version (required)
Version=0.1.0
Version=0.1.1

#Date (year-month-day)
Date=2010-10-01
Date=2015-05-23

# Title (required)
Title=Hidden Markov Model
Expand All @@ -26,7 +26,7 @@ Maintainer=Marco Gillies <m.gillies@gold.ac.uk>
License=Mozilla

# Description (required)
Description=A Package that supports Hidden Markov Model classification and modelling of sequence and time series data. It is still somewhat experimental, hence the release number < 1.0
Description=A Package that supports Hidden Markov Model classification and modelling of sequence and time series data.

# Package URL for obtaining the package archive (required)
PackageURL=https://github.com/downloads/marcogillies/HMMWeka/HMM.zip
Expand Down
35 changes: 35 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<property name="source.url" value="https://github.com/marcogillies/HMMWeka" />
<property name="source.repository" value="https://github.com/marcogillies/HMMWeka" />



<target name="init_all">
<!-- Create the time stamp -->
Expand Down Expand Up @@ -130,6 +131,7 @@
<mkdir dir="${src-test}"/>
</target>


<!-- compile the testcases -->
<target name="compile_tests" depends="init_tests, compile"
description="Compile unit tests into build/testcases">
Expand Down Expand Up @@ -159,6 +161,39 @@
basedir="${build}/testcases">
</jar>
</target>

<target name="run_tests" depends="jar_tests"
description="runs the junit tests">
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<fileset dir="${lib}">
<include name="*.jar"/>
<include name="*.zip"/>
</fileset>
<pathelement location="${build}/classes"/>
<pathelement location="${build}/testcases"/>
<pathelement path="${java.class.path}" />
</classpath>


<test name="weka.estimators.TestDiscreteHMMEstimator"
haltonfailure="no" outfile="TestDiscreteHMMEstimator.result">
<formatter type="plain"/>
</test>
<test name="weka.classifiers.bayes.TestHMMClassifiers"
haltonfailure="no" outfile="TestHMMClassifiers.result">
<formatter type="plain"/>
</test>
<test name="weka.estimators.TestMultivariateNormalEstimator"
haltonfailure="no" outfile="TestMultivariateNormalEstimator.result">
<formatter type="plain"/>
</test>
<!--test name="weka.estimators.TestMultivariateNormalHMMEstimator"
haltonfailure="no" outfile="TestMultivariateNormalHMMEstimator.result">
<formatter type="plain"/>
</test-->
</junit>
</target>

<!--
===========================================================================
Expand Down
Binary file modified lib/junit.jar
Binary file not shown.
63 changes: 58 additions & 5 deletions src/main/java/weka/classifiers/bayes/HMM.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,8 @@ protected double EMStep(Instances data) throws Exception
}
}



int numS0 = 0;
int numS1 = 0;
for (int i = 0; i < data.numInstances(); i++)
Expand Down Expand Up @@ -1043,6 +1045,32 @@ protected void initGaussianOutputProbsAllData(int numClasses, Instances data, Do
for (int i = 0; i < numClasses; i++)
ests[i] = new MultivariateNormalEstimator();

m_SeqAttr = -1;
m_NumOutputs = 0;
for(int i = 0; i < data.numAttributes(); i++)
{
Attribute attr = data.attribute(i);
if(attr.isRelationValued())
{
if(attr.relation().attribute(0).isNominal())
{
m_SeqAttr = attr.index();
assert(m_SeqAttr == i);
//m_NumOutputs = attr.relation().numDistinctValues(0);
}
if(attr.relation().attribute(0).isNumeric())
{
m_SeqAttr = attr.index();
assert(m_SeqAttr == i);
this.setNumeric(true);
//m_NumOutputs = -1;
//m_OutputDimension = attr.relation().numAttributes();
//break;
}
break;
}
}

for (int i = 0; i < data.numInstances(); i++)
{
Instance inst = data.instance(i);
Expand Down Expand Up @@ -1088,6 +1116,31 @@ protected void initGaussianOutputProbsAllData(int numClasses, Instances data, Do
protected void initGaussianOutputProbsCluster(int numClasses, Instances data, DoubleVector outputMeans[][], Matrix outputVars[][]) throws Exception
{

m_SeqAttr = -1;
m_NumOutputs = 0;
for(int i = 0; i < data.numAttributes(); i++)
{
Attribute attr = data.attribute(i);
if(attr.isRelationValued())
{
if(attr.relation().attribute(0).isNominal())
{
m_SeqAttr = attr.index();
assert(m_SeqAttr == i);
//m_NumOutputs = attr.relation().numDistinctValues(0);
}
if(attr.relation().attribute(0).isNumeric())
{
m_SeqAttr = attr.index();
assert(m_SeqAttr == i);
//this.setNumeric(true);
//m_NumOutputs = -1;
//m_OutputDimension = attr.relation().numAttributes();
//break;
}
break;
}
}

Instances [] flatdata = new Instances[numClasses];
for (int i = 0; i < data.numInstances(); i++)
Expand Down Expand Up @@ -1122,11 +1175,11 @@ protected void initGaussianOutputProbsCluster(int numClasses, Instances data, Do
kmeans[i].setNumClusters(getNumStates());
kmeans[i].setDisplayStdDevs(true);
kmeans[i].buildClusterer(flatdata[i]);
System.out.print("Kmeans cluster " + i + " sizes ");
int [] clusterSizes = kmeans[i].getClusterSizes();
for (int j = 0; j < clusterSizes.length; j++)
System.out.print(clusterSizes[j] + " ");
System.out.println("");
//System.out.print("Kmeans cluster " + i + " sizes ");
//int [] clusterSizes = kmeans[i].getClusterSizes();
//for (int j = 0; j < clusterSizes.length; j++)
// System.out.print(clusterSizes[j] + " ");
//System.out.println("");
}


Expand Down
135 changes: 81 additions & 54 deletions src/test/java/TestDiscreteHMMEstimator.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,59 +173,85 @@ public void testGetProbabilityDoubleDoubleDoubleVector() {
@Test
public void testAddValueDoubleDoubleDoubleDouble() {

DiscreteHMMEstimator dhe1 = new DiscreteHMMEstimator(m_rand.nextInt(20), m_rand.nextInt(20), false);
double weights[][][] = new double[dhe1.getNumStates()][dhe1.getNumStates()][dhe1.getNumOutputs()];
double sum = 0;
for(int s = 0; s < dhe1.getNumStates(); s++)
for(int testRun = 0; testRun < 10; testRun++)
{
sum = 0.0;
for(int s1 = 0; s1 < dhe1.getNumStates(); s1++)
for (int o = 0; o < dhe1.getNumOutputs(); o++)
{
weights[s][s1][o] = m_rand.nextDouble();
sum += weights[s][s1][o];
dhe1.addValue(s, s1, o, weights[s][s1][o]);
}
for(int s1 = 0; s1 < dhe1.getNumStates(); s1++)
for (int o = 0; o < dhe1.getNumOutputs(); o++)
{
weights[s][s1][o] /= sum;
}
}
for(int s = 0; s < dhe1.getNumStates(); s ++)
for(int s1 = 0; s1 < dhe1.getNumStates(); s1++)
for (int o = 0; o < dhe1.getNumOutputs(); o++)
DiscreteHMMEstimator dhe1 = new DiscreteHMMEstimator(m_rand.nextInt(20), m_rand.nextInt(20), false);
double weights[][][] = new double[dhe1.getNumStates()][dhe1.getNumStates()][dhe1.getNumOutputs()];
double sum = 0;
double stateSum[] = new double[dhe1.getNumStates()];
double stateProb[][] = new double[dhe1.getNumStates()][dhe1.getNumStates()];
double outputSum[] = new double[dhe1.getNumStates()];
double outputProb[][] = new double[dhe1.getNumStates()][dhe1.getNumOutputs()];
for(int s = 0; s < dhe1.getNumStates(); s++)
{
//sum = 0.0;
for(int s1 = 0; s1 < dhe1.getNumStates(); s1++)
{
System.out.println(dhe1.getProbability(s, s1, o) + " " + weights[s][s1][o]);
//assertEquals(dhe1.getProbability(s, s1, o), weights[s][s1][o], 0.001);

for (int o = 0; o < dhe1.getNumOutputs(); o++)
{

weights[s][s1][o] = m_rand.nextDouble();
sum += weights[s][s1][o];
dhe1.addValue(s, s1, o, weights[s][s1][o]);

stateSum[s] += weights[s][s1][o];
stateProb[s][s1] += weights[s][s1][o];
outputSum[s1] += weights[s][s1][o];
outputProb[s1][o] += weights[s][s1][o];
}
}

dhe1 = new DiscreteHMMEstimator(m_rand.nextInt(20), m_rand.nextInt(20), false);
weights = new double[dhe1.getNumStates()][dhe1.getNumStates()][dhe1.getNumOutputs()];
sum = 0;
for(int s = 0; s < dhe1.getNumStates(); s ++)
{
sum = 0.0;
for(int s1 = 0; s1 < dhe1.getNumStates(); s1 ++)
for (int o = 0; o < dhe1.getNumOutputs(); o++)
{
weights[s][s1][o] = m_rand.nextInt(100);
sum += weights[s][s1][o];
for(int i = 0; i < weights[s][s1][o]; i++)
dhe1.addValue(s, s1, o, 1.0);
//for(int s1 = 0; s1 < dhe1.getNumStates(); s1++)
// for (int o = 0; o < dhe1.getNumOutputs(); o++)
// {
// weights[s][s1][o] /= sum;
// }
}
for(int s1 = 0; s1 < dhe1.getNumStates(); s1 ++)
for (int o = 0; o < dhe1.getNumOutputs(); o++)



for(int s = 0; s < dhe1.getNumStates(); s ++)
{
weights[s][s1][o] /= sum;
for(int s1 = 0; s1 < dhe1.getNumStates(); s1++)
{
for (int o = 0; o < dhe1.getNumOutputs(); o++)
{
System.out.println(dhe1.getProbability(s, s1, o) + " " + weights[s][s1][o]);
double prob = stateProb[s][s1]/stateSum[s];
prob *= outputProb[s1][o]/outputSum[s1];
assertEquals("probability " + s + " " + s1 + " " + o + " " + stateSum[s],
dhe1.getProbability(s, s1, o), prob, 0.001);
}
}
}
}
for(int s = 0; s < dhe1.getNumStates(); s ++)
for(int s1 = 0; s1 < dhe1.getNumStates(); s1 ++)
for (int o = 0; o < dhe1.getNumOutputs(); o++)
{
assertEquals(dhe1.getProbability(s, s1, o), weights[s][s1][o], 0.001);
}

// dhe1 = new DiscreteHMMEstimator(m_rand.nextInt(20), m_rand.nextInt(20), false);
// weights = new double[dhe1.getNumStates()][dhe1.getNumStates()][dhe1.getNumOutputs()];
// sum = 0;
// for(int s = 0; s < dhe1.getNumStates(); s ++)
// {
// sum = 0.0;
// for(int s1 = 0; s1 < dhe1.getNumStates(); s1 ++)
// for (int o = 0; o < dhe1.getNumOutputs(); o++)
// {
// weights[s][s1][o] = m_rand.nextInt(100);
// sum += weights[s][s1][o];
// for(int i = 0; i < weights[s][s1][o]; i++)
// dhe1.addValue(s, s1, o, 1.0);
// }
// //for(int s1 = 0; s1 < dhe1.getNumStates(); s1 ++)
// // for (int o = 0; o < dhe1.getNumOutputs(); o++)
// //{
// // weights[s][s1][o] /= sum;
// //}
// }
// for(int s = 0; s < dhe1.getNumStates(); s ++)
// for(int s1 = 0; s1 < dhe1.getNumStates(); s1 ++)
// for (int o = 0; o < dhe1.getNumOutputs(); o++)
// {
// assertEquals(dhe1.getProbability(s, s1, o), weights[s][s1][o], 0.001);
// }
}

/**
Expand Down Expand Up @@ -325,18 +351,19 @@ public void testGetProbabilityDoubleDoubleDouble() {
/**
* Test method for {@link weka.estimators.DiscreteHMMEstimator#Sample0(weka.core.Instances, java.util.Random)}.
*/
@Test
public void testSample0() {
fail("Not yet implemented");
}

// @Test
// public void testSample0() {
// fail("Not yet implemented");
// }

/**
* Test method for {@link weka.estimators.DiscreteHMMEstimator#Sample(weka.core.Instances, int, java.util.Random)}.
*/
@Test
public void testSample() {
fail("Not yet implemented");
}
// @Test
// public void testSample() {
// fail("Not yet implemented");
// }

/**
* Test method for {@link weka.estimators.AbstractHMMEstimator#setNumStates(int)}.
Expand Down
Loading

0 comments on commit 982a2ef

Please sign in to comment.