⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hmmcomponent.java

📁 java实现的隐马尔科夫模型
💻 JAVA
字号:
/* jahmm package - v0.3.1 *//* *  Copyright (c) 2004, Jean-Marc Francois. * *  This file is part of Jahmm. *  Jahmm is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  Jahmm is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with Jahmm; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */package be.ac.ulg.montefiore.run.jahmm.gui;import java.util.*;import java.text.*;import java.awt.*;import java.awt.font.*;import java.awt.event.*;import edu.uci.ics.jung.utils.*;import edu.uci.ics.jung.graph.*;import edu.uci.ics.jung.graph.decorators.*;import edu.uci.ics.jung.graph.impl.*;import edu.uci.ics.jung.visualization.*;import edu.uci.ics.jung.visualization.graphdraw.*;import edu.uci.ics.jung.visualization.contrib.*;import be.ac.ulg.montefiore.run.jahmm.*;/** * This class implements a graphical representation of a HMM. * State transitions labelled with a very low probability are not drawn. */public class HmmComponent    extends GraphDraw implements GuiComponent {        /**     * The HMM represented by this object.  This HMM must not be modified     * except using this object, otherwise those representations will not be     * synchronized anymore.     */    final protected Hmm hmm;    // Maps a vertex to a state number and vice-versa    final private Hashtable<Vertex, Integer> mapToState;    final private Hashtable<Integer, Vertex> mapToVertex;        static private double MIN_DRAWN_PROBABILITY = 0.01;        /**     * Builds a new graphical representation of a HMM. This HMM must not be      * modified except using this object, otherwise this representation will     * not be synchronized anymore.     *     * @param hmm The HMM to draw.     **/    public HmmComponent(Hmm hmm) {	super(new DirectedSparseGraph());		hmmToGraph(hmm, (DirectedSparseGraph) graph, 		   mapToState = new Hashtable<Vertex, Integer>(),		   mapToVertex = new Hashtable<Integer, Vertex>());	this.hmm = hmm;	NumberEdgeValue edgeWeight = 	    new UserDatumNumberEdgeValue("probability");	NumberEdgeValueStringer edgeStringer = 	    new NumberEdgeValueStringer(edgeWeight);	NumberVertexValue stateNb = new UserDatumNumberVertexValue("stateNb");	NumberVertexValueStringer vertexStringer = 	    new NumberVertexValueStringer(stateNb);		PluggableRenderer sr = new PluggableRenderer();	sr.setEdgeStringer(edgeStringer);	sr.setVertexStringer(vertexStringer);	sr.setVertexShapeFunction(new VertexShaper());		Hashtable<TextAttribute, Float> vertexTextAttributes = 	    new Hashtable<TextAttribute, Float>();	vertexTextAttributes.put(TextAttribute.WEIGHT, 				 TextAttribute.WEIGHT_BOLD);	Font vertexFont = new Font(vertexTextAttributes);	sr.setVertexFontFunction(new ConstantVertexFontFunction(vertexFont));		setGraphLayout(new CircleLayout(graph));	setRenderer(sr);	getVisualizationViewer().	    setToolTipListener(new VertexListener(this, 10.));    }            /**     * Returns the associated HMM.     *     * @return The HMM associated to this object.     */    public Hmm embeddedObject() {	return hmm;    }            /*     * Returns a graph representing a given HMM.     */    private DirectedSparseGraph hmmToGraph(Hmm hmm, DirectedSparseGraph graph,					   Hashtable<Vertex, Integer> 					   mapToState,					   Hashtable<Integer, Vertex> 					   mapToVertex) {	StringLabeller labeller = StringLabeller.getLabeller(graph);		for (int i = 0; i < hmm.nbStates(); i++) {	    SimpleDirectedSparseVertex v = new SimpleDirectedSparseVertex();	    graph.addVertex(v);	    v.addUserDatum("stateNb", new Integer(i), UserData.SHARED);	    v.addUserDatum("hmm", hmm, UserData.SHARED);	    mapToState.put(v, i);	    mapToVertex.put(i, v);	}		for (int o = 0; o < hmm.nbStates(); o++)	    for (int d = 0; d < hmm.nbStates(); d++)		if (hmm.getAij(o, d) >= MIN_DRAWN_PROBABILITY) {		    DirectedSparseEdge edge = 			new DirectedSparseEdge(mapToVertex.get(o), 					       mapToVertex.get(d));		    		    graph.addEdge(edge);		    edge.addUserDatum("probability", 				      new Double(hmm.getAij(o, d)),				      UserData.SHARED);		}		return graph;    }}class VertexListener     implements VisualizationViewer.ToolTipListener {        // How close to the vertex in order to fire the tooltip    private double proximity;        // The visual component holding the graph's rendering engine    private GraphDraw gd;    static private NumberFormat numberFormat;        /**     * create an instance with passed parameters     * @param gd the GraphDraw for this graph     * @param proximity how close to the vertex     */    public VertexListener(GraphDraw gd, double proximity) {	this.gd = gd;	this.proximity = proximity;	numberFormat = NumberFormat.getInstance();	numberFormat.setMaximumFractionDigits(3);    }            /**     * Evaluate the mouse event position and prepare a ToolTip     * for the Vertex that is within 'proximity' of the event.     * @param event the MouseEvent where the mouse pointer is dwelling     */    public String getToolTipText(MouseEvent event) {	Layout layout = gd.getGraphLayout();	VisualizationViewer vv = gd.getVisualizationViewer();		double scalex = vv.getScaleX();	double scaley = vv.getScaleY();	double offsetx = vv.getOffsetX();	double offsety = vv.getOffsetY();		Vertex v = layout.getVertex(event.getX() / scalex + offsetx, 				    event.getY() / scaley + offsety, proximity);	if (v != null) {	    Hmm hmm = (Hmm) v.getUserDatum("hmm");	    int stateNb = (Integer) v.getUserDatum("stateNb");	    String s = "State " + stateNb;	    	    s += " - Pi: " + numberFormat.format(hmm.getPi(stateNb));	    s += " - OPDF: [ " + hmm.getOpdf(stateNb).toString(numberFormat) +		" ]";	    	    return s;	} else 	    return null;    }}class VertexShaper    extends EllipseVertexShapeFunction {            public VertexShaper() {	super(new VertexSizer(), new VertexSizer());    }}class VertexSizer    implements VertexSizeFunction, VertexAspectRatioFunction {        public float getAspectRatio(Vertex v) {	return 1.F;    }        public int getSize(Vertex v) {	Hmm hmm = (Hmm) v.getUserDatum("hmm");	int stateNb = (Integer) v.getUserDatum("stateNb");		double size = 80. * hmm.getPi(stateNb);	return (int) ((size > 15.) ? size : 15.);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -