All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.mallet.grmm.test.TestUndirectedModel Maven / Gradle / Ivy

Go to download

MALLET is a Java-based package for statistical natural language processing, document classification, clustering, topic modeling, information extraction, and other machine learning applications to text.

The newest version!
/* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept.
   This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
   http://www.cs.umass.edu/~mccallum/mallet
   This software is provided under the terms of the Common Public License,
   version 1.0, as published by http://www.opensource.org.  For further
   information, see the file `LICENSE' included with this distribution. */
package cc.mallet.grmm.test;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import org._3pq.jgrapht.UndirectedGraph;
import org._3pq.jgrapht.GraphHelper;

import cc.mallet.grmm.inference.RandomGraphs;
import cc.mallet.grmm.types.*;
import cc.mallet.grmm.util.Graphs;
import cc.mallet.util.ArrayUtils;

/**
 * Created: Mar 17, 2005
 *
 * @author  2 aren't now cached.
        default:
          break;
      }
    }
  }

  // Verify that potentialOfVertex and potentialOfEdge (which use
  // caches) are consistent with the potentials set even if a vertex is removed.
  public void testUndirectedCachesAfterRemove ()
  {
    List models = TestInference.createTestModels ();
    for (Iterator mdlIt = models.iterator (); mdlIt.hasNext ();) {
      FactorGraph mdl = (FactorGraph) mdlIt.next ();
      mdl = (FactorGraph) mdl.duplicate ();
      mdl.remove (mdl.get (0));

      // Verify that indexing correct
      for (Iterator it = mdl.variablesIterator (); it.hasNext ();) {
        Variable var = (Variable) it.next ();
        int idx = mdl.getIndex (var);
        assertTrue (idx >= 0);
        assertTrue (idx < mdl.numVariables ());
      }

      // Verify that caches consistent
      verifyCachesConsistent (mdl);
    }
  }

  public void testMdlToGraph ()
  {
    List models = TestInference.createTestModels ();
    for (Iterator mdlIt = models.iterator (); mdlIt.hasNext ();) {
      UndirectedModel mdl = (UndirectedModel) mdlIt.next ();
      UndirectedGraph g = Graphs.mdlToGraph (mdl);
      Set vertices = g.vertexSet ();

      // check the number of vertices
      assertEquals (mdl.numVariables (), vertices.size ());

      // check the number of edges
      int numEdgePtls = 0;
      for (Iterator factorIt = mdl.factors ().iterator (); factorIt.hasNext ();) {
        Factor factor =  (Factor) factorIt.next ();
        if (factor.varSet ().size() == 2) numEdgePtls++;
      }
      assertEquals (numEdgePtls, g.edgeSet ().size ());

      // check that the neighbors of each vertex contain at least some of what they're supposed to
      Iterator it = vertices.iterator ();
      while (it.hasNext ()) {
        Variable var = (Variable) it.next ();
        assertTrue (vertices.contains (var));
        Set neighborsInG = new HashSet (GraphHelper.neighborListOf (g, var));
        neighborsInG.add (var);

        Iterator factorIt = mdl.allFactorsContaining (var).iterator ();
        while (factorIt.hasNext ()) {
          Factor factor = (Factor) factorIt.next ();
          assertTrue (neighborsInG.containsAll (factor.varSet ()));
        }
      }
    }
  }

  public void testFactorOfSet ()
  {
    Variable[] vars = new Variable [3];
    for (int i = 0; i < vars.length; i++) {
      vars[i] = new Variable (2);
    }
    Factor factor = new TableFactor (vars, new double[] { 0, 1, 2, 3, 4, 5, 6, 7 });
    
    FactorGraph fg = new FactorGraph (vars);
    fg.addFactor (factor);

    assertTrue (factor == fg.factorOf (factor.varSet ()));

    HashSet set = new HashSet (factor.varSet ());
    assertTrue (factor == fg.factorOf (set));
    set.remove (vars[0]);
    assertTrue (null == fg.factorOf (set));
  }

  public static Test suite ()
  {
    return new TestSuite (TestUndirectedModel.class);
  }

  public static void main (String[] args) throws Throwable
  {
    TestSuite theSuite;
    if (args.length > 0) {
      theSuite = new TestSuite ();
      for (int i = 0; i < args.length; i++) {
        theSuite.addTest (new TestUndirectedModel (args[i]));
      }
    } else {
      theSuite = (TestSuite) suite ();
    }

    junit.textui.TestRunner.run (theSuite);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy