Example usage for weka.gui.visualize PlotData2D getPlotInstances

List of usage examples for weka.gui.visualize PlotData2D getPlotInstances

Introduction

In this page you can find the example usage for weka.gui.visualize PlotData2D getPlotInstances.

Prototype

public Instances getPlotInstances() 

Source Link

Document

Returns the instances for this plot

Usage

From source file:adams.flow.sink.WekaClassifierErrors.java

License:Open Source License

/**
 * ClassifierErrorss the token (the panel and dialog have already been created at
 * this stage)./*from  w w  w.  j a va 2s . c  om*/
 *
 * @param token   the token to display
 */
@Override
protected void display(Token token) {
    DataGenerator generator;
    Evaluation eval;

    try {
        if (token.getPayload() instanceof WekaEvaluationContainer)
            eval = (Evaluation) ((WekaEvaluationContainer) token.getPayload())
                    .getValue(WekaEvaluationContainer.VALUE_EVALUATION);
        else
            eval = (Evaluation) token.getPayload();
        if (eval.predictions() == null) {
            getLogger().severe("No predictions available from Evaluation object!");
            return;
        }
        generator = new DataGenerator(eval, m_ErrorScaler);
        PlotData2D plotdata = generator.getPlotData();
        plotdata.setPlotName(generator.getPlotInstances().relationName());
        m_VisualizePanel.addPlot(plotdata);
        m_VisualizePanel.setColourIndex(plotdata.getPlotInstances().classIndex());
        if ((m_VisualizePanel.getXIndex() == 0) && (m_VisualizePanel.getYIndex() == 1)) {
            try {
                m_VisualizePanel.setXIndex(m_VisualizePanel.getInstances().classIndex()); // class
                m_VisualizePanel.setYIndex(m_VisualizePanel.getInstances().classIndex() - 1); // predicted class
            } catch (Exception e) {
                // ignored
            }
        }
    } catch (Exception e) {
        handleException("Failed to display token: " + token, e);
    }
}

From source file:adams.flow.sink.WekaClassifierErrors.java

License:Open Source License

/**
 * Creates a new panel for the token.//from  ww w. ja va  2s  .c om
 *
 * @param token   the token to display in a new panel, can be null
 * @return      the generated panel
 */
public AbstractDisplayPanel createDisplayPanel(Token token) {
    AbstractDisplayPanel result;
    String name;

    if (token != null)
        name = "Classifier errors (" + getEvaluation(token).getHeader().relationName() + ")";
    else
        name = "Classifier errors";

    result = new AbstractComponentDisplayPanel(name) {
        private static final long serialVersionUID = -7362768698548152899L;
        protected VisualizePanel m_VisualizePanel;

        @Override
        protected void initGUI() {
            super.initGUI();
            setLayout(new BorderLayout());
            m_VisualizePanel = new VisualizePanel();
            add(m_VisualizePanel, BorderLayout.CENTER);
        }

        @Override
        public void display(Token token) {
            try {
                Evaluation eval = getEvaluation(token);
                DataGenerator generator = new DataGenerator(eval, m_ErrorScaler);
                PlotData2D plotdata = generator.getPlotData();
                plotdata.setPlotName(generator.getPlotInstances().relationName());
                m_VisualizePanel.addPlot(plotdata);
                m_VisualizePanel.setColourIndex(plotdata.getPlotInstances().classIndex());
                if ((m_VisualizePanel.getXIndex() == 0) && (m_VisualizePanel.getYIndex() == 1)) {
                    try {
                        m_VisualizePanel.setXIndex(m_VisualizePanel.getInstances().classIndex()); // class
                        m_VisualizePanel.setYIndex(m_VisualizePanel.getInstances().classIndex() - 1); // predicted class
                    } catch (Exception e) {
                        // ignored
                    }
                }
            } catch (Exception e) {
                getLogger().log(Level.SEVERE, "Failed to display token: " + token, e);
            }
        }

        @Override
        public JComponent supplyComponent() {
            return m_VisualizePanel;
        }

        @Override
        public void clearPanel() {
            m_VisualizePanel.removeAllPlots();
        }

        public void cleanUp() {
            m_VisualizePanel.removeAllPlots();
        }
    };

    if (token != null)
        result.display(token);

    return result;
}

From source file:milk.visualize.MIPlot2D.java

License:Open Source License

/**
 * Renders this component/*from  w w w.  j  a va  2 s.  c o  m*/
 * @param gx the graphics context
 */
public void paintComponent(Graphics gx) {

    //if(!isEnabled())
    //    return;

    super.paintComponent(gx);

    if (plotExemplars != null) {
        gx.setColor(m_axisColour);
        // Draw the axis name
        String xname = plotExemplars.attribute(m_xIndex).name(),
                yname = plotExemplars.attribute(m_yIndex).name();
        gx.drawString(yname, m_XaxisStart + m_labelMetrics.stringWidth("M"),
                m_YaxisStart + m_labelMetrics.getAscent() / 2 + m_tickSize);
        gx.drawString(xname, m_XaxisEnd - m_labelMetrics.stringWidth(yname) + m_tickSize,
                (int) (m_YaxisEnd - m_labelMetrics.getAscent() / 2));

        // Draw points
        Attribute classAtt = plotExemplars.classAttribute();
        for (int j = 0; j < m_plots.size(); j++) {
            PlotData2D temp_plot = (PlotData2D) (m_plots.elementAt(j));
            Instances instances = temp_plot.getPlotInstances();

            StringTokenizer st = new StringTokenizer(
                    instances.firstInstance().stringValue(plotExemplars.idIndex()), "_");

            //////////////////// TLD stuff /////////////////////////////////
            /*
            double[] mu = new double[plotExemplars.numAttributes()],
                sgm = new double[plotExemplars.numAttributes()];
            st.nextToken(); // Squeeze first element
            int p=0;
            while(p<mu.length){
                if((p==plotExemplars.idIndex()) || (p==plotExemplars.classIndex()))
               p++;
                if(p<mu.length){
               mu[p] = Double.parseDouble(st.nextToken());
               sgm[p] = Double.parseDouble(st.nextToken());
               p++;
                }
            }
            Instance ins = instances.firstInstance();
            gx.setColor((Color)m_colorList.elementAt((int)ins.classValue()));
            double mux=mu[m_xIndex], muy=mu[m_yIndex],
                sgmx=sgm[m_xIndex], sgmy=sgm[m_yIndex];
            double xs = convertToPanelX(mux-3*sgmx), xe = convertToPanelX(mux+3*sgmx),
                xleng = Math.abs(xe-xs);
            double ys = convertToPanelY(muy+3*sgmy), ye = convertToPanelY(muy-3*sgmy),
                yleng = Math.abs(ye-ys);
            // Draw oval
            gx.drawOval((int)xs,(int)ys,(int)xleng,(int)yleng);
            // Draw a dot
            gx.fillOval((int)convertToPanelX(mux)-2, (int)convertToPanelY(muy)-2, 4, 4);
            */
            //////////////////// TLD stuff /////////////////////////////////

            //////////////////// instance-based stuff /////////////////////////////////
            /*
              double[] core = new double[plotExemplars.numAttributes()],
                range=new double[plotExemplars.numAttributes()];
            st.nextToken(); // Squeeze first element
            int p=0;
            while(p<range.length){
                if((p==plotExemplars.idIndex()) || (p==plotExemplars.classIndex()))
               p++;
                if(p<range.length)
               range[p++] = Double.parseDouble(st.nextToken());
            }
                    
            p=0;
            while(st.hasMoreTokens()){
                if((p==plotExemplars.idIndex()) || (p==plotExemplars.classIndex()))
               p++;
                core[p++] = Double.parseDouble(st.nextToken());
            }
                    
            Instance ins = instances.firstInstance();
            gx.setColor((Color)m_colorList.elementAt((int)ins.classValue()));
            double rgx=range[m_xIndex], rgy=range[m_yIndex];
            double x1 = convertToPanelX(core[m_xIndex]-rgx/2),
                y1 = convertToPanelY(core[m_yIndex]-rgy/2),
                x2 = convertToPanelX(core[m_xIndex]+rgx/2),
                y2 = convertToPanelY(core[m_yIndex]+rgy/2),
                x = convertToPanelX(core[m_xIndex]),
                y = convertToPanelY(core[m_yIndex]);
                    
            // Draw a rectangle
            gx.drawLine((int)x1, (int)y1, (int)x2, (int)y1);
            gx.drawLine((int)x1, (int)y1, (int)x1, (int)y2);
            gx.drawLine((int)x2, (int)y1, (int)x2, (int)y2);
            gx.drawLine((int)x1, (int)y2, (int)x2, (int)y2);
                    
            // Draw a dot
            gx.fillOval((int)x-3, (int)y-3, 6, 6);
                    
            // Draw string
            StringBuffer text =new StringBuffer(temp_plot.getPlotName()+":"+instances.numInstances());      
            gx.drawString(text.toString(), (int)x1, (int)y2+m_labelMetrics.getHeight());
            */
            //////////////////// instance-based stuff /////////////////////////////////

            //////////////////// normal graph /////////////////////////////////

            // Paint numbers
            for (int i = 0; i < instances.numInstances(); i++) {
                Instance ins = instances.instance(i);
                if (!ins.isMissing(m_xIndex) && !ins.isMissing(m_yIndex)) {
                    if (classAtt.isNominal())
                        gx.setColor((Color) m_colorList.elementAt((int) ins.classValue()));
                    else {
                        double r = (ins.classValue() - m_minC) / (m_maxC - m_minC);
                        r = (r * 240) + 15;
                        gx.setColor(new Color((int) r, 150, (int) (255 - r)));
                    }

                    double x = convertToPanelX(ins.value(m_xIndex));
                    double y = convertToPanelY(ins.value(m_yIndex));

                    String id = temp_plot.getPlotName();
                    gx.drawString(id, (int) (x - m_labelMetrics.stringWidth(id) / 2),
                            (int) (y + m_labelMetrics.getHeight() / 2));
                }
            }

            //////////////////// normal graph /////////////////////////////////   
        }
    }

    //////////////////// TLD stuff /////////////////////////////////
    // Draw two Guassian contour with 3 stdDev
    // (-1, -1) with stdDev 1, 2
    // (1, 1) with stdDev 2, 1
    /*gx.setColor(Color.black);
    double mu=-1.5, sigmx, sigmy; // class 0
    if(m_xIndex == 1)
        sigmx = 1;          
    else
        sigmx = 2;
    if(m_yIndex == 1)
        sigmy = 1;          
    else
        sigmy = 2;
            
    double x1 = convertToPanelX(mu-3*sigmx), x2 = convertToPanelX(mu+3*sigmx),
        xlen = Math.abs(x2-x1);
    double y1 = convertToPanelY(mu+3*sigmy), y2 = convertToPanelY(mu-3*sigmy),
        ylen = Math.abs(y2-y1);
    // Draw heavy oval
    gx.drawOval((int)x1,(int)y1,(int)xlen,(int)ylen);
    gx.drawOval((int)x1-1,(int)y1-1,(int)xlen+2,(int)ylen+2);
    gx.drawOval((int)x1+1,(int)y1+1,(int)xlen-2,(int)ylen-2);
    // Draw a dot
    gx.fillOval((int)convertToPanelX(mu)-3, (int)convertToPanelY(mu)-3, 6, 6);
            
    mu=1.5; // class 1
    if(m_xIndex == 1)
        sigmx = 1;          
    else
        sigmx = 2;
    if(m_yIndex == 1)
        sigmy = 1;          
    else
        sigmy = 2;
            
    x1 = convertToPanelX(mu-3*sigmx);
    x2 = convertToPanelX(mu+3*sigmx);
    xlen = Math.abs(x2-x1);
    y1 = convertToPanelY(mu+3*sigmy);
    y2 = convertToPanelY(mu-3*sigmy);
    ylen = Math.abs(y2-y1);
    // Draw heavy oval
    gx.drawOval((int)x1,(int)y1,(int)xlen,(int)ylen);
    gx.drawOval((int)x1-1,(int)y1-1,(int)xlen+2,(int)ylen+2);
    gx.drawOval((int)x1+1,(int)y1+1,(int)xlen-2,(int)ylen-2);
    // Draw a dot
    gx.fillOval((int)convertToPanelX(mu)-3, (int)convertToPanelY(mu)-3, 6, 6);
    */
    //////////////////// TLD stuff /////////////////////////////////

    //////////////////// instance-based stuff /////////////////////////////////
    /*
    // Paint a log-odds line: 1*x0+2*x1=0
    double xstart, xend, ystart, yend, xCoeff, yCoeff;
    if(m_xIndex == 1)
        xCoeff = 1;   
    else
        xCoeff = 2;   
    if(m_yIndex == 1)
        yCoeff = 1;   
    else
        yCoeff = 2;   
            
    xstart = m_minX;
    ystart = -xstart*xCoeff/yCoeff;
    if(ystart > m_maxY){
        ystart = m_maxY;
        xstart = -ystart*yCoeff/xCoeff;
    }   
    yend = m_minY;
    xend = -yend*yCoeff/xCoeff;
    if(xend > m_maxX){
        xend = m_maxX;
        yend = -xend*xCoeff/yCoeff;
    }
            
    // Draw a heavy line
    gx.setColor(Color.black);
    gx.drawLine((int)convertToPanelX(xstart), (int)convertToPanelY(ystart),
           (int)convertToPanelX(xend), (int)convertToPanelY(yend));
    gx.drawLine((int)convertToPanelX(xstart)+1, (int)convertToPanelY(ystart)+1,
           (int)convertToPanelX(xend)+1, (int)convertToPanelY(yend)+1);
    gx.drawLine((int)convertToPanelX(xstart)-1, (int)convertToPanelY(ystart)-1,
           (int)convertToPanelX(xend)-1, (int)convertToPanelY(yend)-1);
    */
    //////////////////// instance-based stuff /////////////////////////////////
}