Logo Search packages:      
Sourcecode: weka version File versions

ResultMatrix.java

/*
 *    This program 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.
 *
 *    This program 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 this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * ResultMatrix.java
 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
 *
 */

package weka.experiment;

import weka.core.RevisionHandler;
import weka.core.Utils;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.Vector;

/**
 * This matrix is a container for the datasets and classifier setups and 
 * their statistics. Derived classes output the data in different formats.
 * Derived classes need to implement the following methods:
 * <ul>
 *   <li><code>toStringMatrix()</code></li>
 *   <li><code>toStringKey()</code></li>
 *   <li><code>toStringHeader()</code></li>
 *   <li><code>toStringSummary()</code></li>
 *   <li><code>toStringRanking()</code></li>
 * </ul>
 *
 *
 * @author FracPete (fracpete at waikato dot ac dot nz)
 * @version $Revision: 1.9 $
 * @see #toStringMatrix()
 * @see #toStringKey()
 * @see #toStringHeader()
 * @see #toStringSummary()
 * @see #toStringRanking()
 */
00053 public abstract class ResultMatrix
  implements Serializable, RevisionHandler {

  /** for serialization */
00057   private static final long serialVersionUID = 4487179306428209739L;
  
  /** tie */
00060   public final static int SIGNIFICANCE_TIE = 0;

  /** win */
00063   public final static int SIGNIFICANCE_WIN = 1;

  /** loss */
00066   public final static int SIGNIFICANCE_LOSS = 2;

  /** tie string */
00069   public String TIE_STRING = " ";

  /** win string */
00072   public String WIN_STRING = "v";

  /** loss string */
00075   public String LOSS_STRING = "*";

  /** the left parentheses for enumerating cols/rows */
00078   public String LEFT_PARENTHESES = "(";

  /** the right parentheses for enumerating cols/rows */
00081   public String RIGHT_PARENTHESES = ")";

  /** the column names */
00084   protected String[] m_ColNames = null;

  /** the row names */
00087   protected String[] m_RowNames = null;

  /** whether a column is hidden */
00090   protected boolean[] m_ColHidden = null;
  
  /** whether a row is hidden */
00093   protected boolean[] m_RowHidden = null;
  
  /** the significance */
00096   protected int[][] m_Significance = null;

  /** the values */
00099   protected double[][] m_Mean = null;

  /** the standard deviation */
00102   protected double[][] m_StdDev = null;

  /** the counts for the different datasets */
00105   protected double[] m_Counts = null;

  /** the standard mean precision */
00108   protected int m_MeanPrec;

  /** the standard std. deviation preicision */
00111   protected int m_StdDevPrec;

  /** whether std. deviations are printed as well */
00114   protected boolean m_ShowStdDev;

  /** whether the average for each column should be printed */
00117   protected boolean m_ShowAverage;
  
  /** whether the names or numbers are output as column declarations */
00120   protected boolean m_PrintColNames;

  /** whether the names or numbers are output as row declarations */
00123   protected boolean m_PrintRowNames;

  /** whether a "(x)" is printed before each column name with "x" as the
   * index */
00127   protected boolean m_EnumerateColNames;

  /** whether a "(x)" is printed before each row name with "x" as the index */
00130   protected boolean m_EnumerateRowNames;

  /** the size of the names of the columns */
00133   protected int m_ColNameWidth;

  /** the size of the names of the rows */
00136   protected int m_RowNameWidth;

  /** the size of the mean columns */
00139   protected int m_MeanWidth;

  /** the size of the std dev columns */
00142   protected int m_StdDevWidth;

  /** the size of the significance columns */
00145   protected int m_SignificanceWidth;

  /** the size of the counts */
00148   protected int m_CountWidth;

  /** contains the keys for the header */
00151   protected Vector m_HeaderKeys = null;

  /** contains the values for the header */
00154   protected Vector m_HeaderValues = null;

  /** the non-significant wins */
00157   protected int[][] m_NonSigWins = null;

  /** the significant wins */
00160   protected int[][] m_Wins = null;

  /** the wins in ranking */
00163   protected int[] m_RankingWins = null;

  /** the losses in ranking */
00166   protected int[] m_RankingLosses = null;

  /** the difference between wins and losses */
00169   protected int[] m_RankingDiff = null;

  /** the ordering of the rows */
00172   protected int[] m_RowOrder = null;

  /** the ordering of the columns */
00175   protected int[] m_ColOrder = null;

  /** whether to remove the filter name from the dataaset name */
00178   protected boolean m_RemoveFilterName = false;
  
  /**
   * initializes the matrix as 1x1 matrix
   */
00183   public ResultMatrix() {
    this(1, 1);
  }
  
  /**
   * initializes the matrix with the given dimensions
   */
00190   public ResultMatrix(int cols, int rows) {
    setSize(cols, rows);
    clear();
  }

  /**
   * initializes the matrix with the values from the given matrix
   * @param matrix      the matrix to get the values from
   */
00199   public ResultMatrix(ResultMatrix matrix) {
    assign(matrix);
  }

  /**
   * returns the name of the output format
   */
  public abstract String getDisplayName();

  /**
   * acquires the data from the given matrix
   */
00211   public void assign(ResultMatrix matrix) {
    int         i;
    int         n;
    
    setSize(matrix.getColCount(), matrix.getRowCount());
    
    // output parameters
    TIE_STRING          = matrix.TIE_STRING;
    WIN_STRING          = matrix.WIN_STRING;
    LOSS_STRING         = matrix.LOSS_STRING;
    LEFT_PARENTHESES    = matrix.LEFT_PARENTHESES;
    RIGHT_PARENTHESES   = matrix.RIGHT_PARENTHESES;
    m_MeanPrec          = matrix.m_MeanPrec;
    m_StdDevPrec        = matrix.m_StdDevPrec;
    m_ShowStdDev        = matrix.m_ShowStdDev;
    m_ShowAverage       = matrix.m_ShowAverage;
    m_PrintColNames     = matrix.m_PrintColNames;
    m_PrintRowNames     = matrix.m_PrintRowNames;
    m_EnumerateColNames = matrix.m_EnumerateColNames;
    m_EnumerateRowNames = matrix.m_EnumerateRowNames;
    m_RowNameWidth      = matrix.m_RowNameWidth;
    m_MeanWidth         = matrix.m_MeanWidth;
    m_StdDevWidth       = matrix.m_StdDevWidth;
    m_SignificanceWidth = matrix.m_SignificanceWidth;
    m_CountWidth        = matrix.m_CountWidth;
    m_RemoveFilterName  = matrix.m_RemoveFilterName;
    
    // header
    m_HeaderKeys   = (Vector) matrix.m_HeaderKeys.clone();
    m_HeaderValues = (Vector) matrix.m_HeaderValues.clone();

    // matrix
    for (i = 0; i < matrix.m_Mean.length; i++) {
      for (n = 0; n < matrix.m_Mean[i].length; n++) {
        m_Mean[i][n]         = matrix.m_Mean[i][n];
        m_StdDev[i][n]       = matrix.m_StdDev[i][n];
        m_Significance[i][n] = matrix.m_Significance[i][n];
      }
    }

    for (i = 0; i < matrix.m_ColNames.length; i++) {
      m_ColNames[i]  = matrix.m_ColNames[i];
      m_ColHidden[i] = matrix.m_ColHidden[i];
    }

    for (i = 0; i < matrix.m_RowNames.length; i++) {
      m_RowNames[i]  = matrix.m_RowNames[i];
      m_RowHidden[i] = matrix.m_RowHidden[i];
    }

    for (i = 0; i < matrix.m_Counts.length; i++)
      m_Counts[i] = matrix.m_Counts[i];

    // summary
    if (matrix.m_NonSigWins != null) {
      m_NonSigWins = new int[matrix.m_NonSigWins.length][];
      m_Wins       = new int[matrix.m_NonSigWins.length][];
      for (i = 0; i < matrix.m_NonSigWins.length; i++) {
        m_NonSigWins[i] = new int[matrix.m_NonSigWins[i].length];
        m_Wins[i]       = new int[matrix.m_NonSigWins[i].length];

        for (n = 0; n < matrix.m_NonSigWins[i].length; n++) {
          m_NonSigWins[i][n] = matrix.m_NonSigWins[i][n];
          m_Wins[i][n]       = matrix.m_Wins[i][n];
        }
      }
    }

    // ranking
    if (matrix.m_RankingWins != null) {
      m_RankingWins   = new int[matrix.m_RankingWins.length];
      m_RankingLosses = new int[matrix.m_RankingWins.length];
      m_RankingDiff   = new int[matrix.m_RankingWins.length];
      for (i = 0; i < matrix.m_RankingWins.length; i++) {
        m_RankingWins[i]   = matrix.m_RankingWins[i];
        m_RankingLosses[i] = matrix.m_RankingLosses[i];
        m_RankingDiff[i]   = matrix.m_RankingDiff[i];
      }
    }
  }

  /**
   * removes the stored data and the ordering, but retains the dimensions of
   * the matrix
   */
00296   public void clear() {
    m_MeanPrec          = 2;
    m_StdDevPrec        = 2;
    m_ShowStdDev        = false;
    m_ShowAverage       = false;
    m_PrintColNames     = true;
    m_PrintRowNames     = true;
    m_EnumerateColNames = true;
    m_EnumerateRowNames = false;
    m_RowNameWidth      = 0;
    m_ColNameWidth      = 0;
    m_MeanWidth         = 0;
    m_StdDevWidth       = 0;
    m_SignificanceWidth = 0;
    m_CountWidth        = 0;

    setSize(getColCount(), getRowCount());
  }

  /**
   * clears the content of the matrix and sets the new size
   * @param cols        the number of mean columns
   * @param rows        the number of mean rows
   */
00320   public void setSize(int cols, int rows) {
    int       i;
    int       n;

    m_ColNames     = new String[cols];
    m_RowNames     = new String[rows];
    m_Counts       = new double[rows];
    m_ColHidden    = new boolean[cols];
    m_RowHidden    = new boolean[rows];
    m_Mean         = new double[rows][cols];
    m_Significance = new int[rows][cols];
    m_StdDev       = new double[rows][cols];
    m_ColOrder     = null;
    m_RowOrder     = null;

    // NaN means that there exists no value! -> toArray()
    for (i = 0; i < m_Mean.length; i++) {
      for (n = 0; n < m_Mean[i].length; n++)
        m_Mean[i][n]   = Double.NaN;
    }

    for (i = 0; i < m_ColNames.length; i++)
      m_ColNames[i] = "col" + i;
    for (i = 0; i < m_RowNames.length; i++)
      m_RowNames[i] = "row" + i;

    clearHeader();
    clearSummary();
    clearRanking();
  }

  /**
   * sets the precision for the means
   */
00354   public void setMeanPrec(int prec) {
    if (prec >= 0)
      m_MeanPrec = prec;
  }

  /**
   * returns the current precision for the means
   */
00362   public int getMeanPrec() {
    return m_MeanPrec;
  }

  /**
   * sets the precision for the standard deviation
   */
00369   public void setStdDevPrec(int prec) {
    if (prec >= 0)
      m_StdDevPrec = prec;
  }

  /**
   * returns the current standard deviation precision
   */
00377   public int getStdDevPrec() {
    return m_StdDevPrec;
  }

  /**
   * sets the width for the column names (0 = optimal)
   */
00384   public void setColNameWidth(int width) {
    if (width >= 0)
      m_ColNameWidth = width;
  }

  /**
   * returns the current width for the column names
   */
00392   public int getColNameWidth() {
    return m_ColNameWidth;
  }

  /**
   * sets the width for the row names (0 = optimal)
   */
00399   public void setRowNameWidth(int width) {
    if (width >= 0)
      m_RowNameWidth = width;
  }

  /**
   * returns the current width for the row names
   */
00407   public int getRowNameWidth() {
    return m_RowNameWidth;
  }

  /**
   * sets the width for the mean (0 = optimal)
   */
00414   public void setMeanWidth(int width) {
    if (width >= 0)
      m_MeanWidth = width;
  }

  /**
   * returns the current width for the mean
   */
00422   public int getMeanWidth() {
    return m_MeanWidth;
  }

  /**
   * sets the width for the std dev (0 = optimal)
   */
00429   public void setStdDevWidth(int width) {
    if (width >= 0)
      m_StdDevWidth = width;
  }

  /**
   * returns the current width for the std dev
   */
00437   public int getStdDevWidth() {
    return m_StdDevWidth;
  }

  /**
   * sets the width for the significance (0 = optimal)
   */
00444   public void setSignificanceWidth(int width) {
    if (width >= 0)
      m_SignificanceWidth = width;
  }

  /**
   * returns the current width for the significance
   */
00452   public int getSignificanceWidth() {
    return m_SignificanceWidth;
  }

  /**
   * sets the width for the counts (0 = optimal)
   */
00459   public void setCountWidth(int width) {
    if (width >= 0)
      m_CountWidth = width;
  }

  /**
   * returns the current width for the counts
   */
00467   public int getCountWidth() {
    return m_CountWidth;
  }

  /**
   * sets whether to display the std deviations or not
   */
00474   public void setShowStdDev(boolean show) {
    m_ShowStdDev = show;
  }

  /**
   * returns whether std deviations are displayed or not
   */
00481   public boolean getShowStdDev() {
    return m_ShowStdDev;
  }

  /**
   * sets whether to display the average per column or not
   */
00488   public void setShowAverage(boolean show) {
    m_ShowAverage = show;
  }

  /**
   * returns whether average per column is displayed or not
   */
00495   public boolean getShowAverage() {
    return m_ShowAverage;
  }

  /**
   * sets whether to remove the filter classname from the dataset name
   */
00502   public void setRemoveFilterName(boolean remove) {
    m_RemoveFilterName = remove;
  }

  /**
   * returns whether the filter classname is removed from the dataset name
   */
00509   public boolean getRemoveFilterName() {
    return m_RemoveFilterName;
  }

  /**
   * sets whether the column names or numbers instead are printed.
   * deactivating automatically sets m_EnumerateColNames to TRUE.
   * @see #setEnumerateColNames(boolean)
   */
00518   public void setPrintColNames(boolean print) {
    m_PrintColNames = print;
    if (!print)
      setEnumerateColNames(true);
  }

  /**
   * returns whether column names or numbers instead are printed
   */
00527   public boolean getPrintColNames() {
    return m_PrintColNames;
  }

  /**
   * sets whether the row names or numbers instead are printed
   * deactivating automatically sets m_EnumerateColNames to TRUE.
   * @see #setEnumerateRowNames(boolean)
   */
00536   public void setPrintRowNames(boolean print) {
    m_PrintRowNames = print;
    if (!print)
      setEnumerateRowNames(true);
  }

  /**
   * returns whether row names or numbers instead are printed
   */
00545   public boolean getPrintRowNames() {
    return m_PrintRowNames;
  }

  /**
   * sets whether the column names are prefixed with "(x)" where "x" is
   * the index
   */
00553   public void setEnumerateColNames(boolean enumerate) {
    m_EnumerateColNames = enumerate;
  }

  /**
   * returns whether column names or numbers instead are enumerateed
   */
00560   public boolean getEnumerateColNames() {
    return m_EnumerateColNames;
  }

  /**
   * sets whether to the row names or numbers instead are enumerateed
   */
00567   public void setEnumerateRowNames(boolean enumerate) {
    m_EnumerateRowNames = enumerate;
  }

  /**
   * returns whether row names or numbers instead are enumerateed
   */
00574   public boolean getEnumerateRowNames() {
    return m_EnumerateRowNames;
  }

  /**
   * returns the number of columns
   */
00581   public int getColCount() {
    return m_ColNames.length;
  }

  /**
   * returns the number of visible columns
   */
00588   public int getVisibleColCount() {
    int         cols;
    int         i;
    
    cols = 0;
    for (i = 0; i < getColCount(); i++) {
      if (!getColHidden(i))
        cols++;
    }

    return cols;
  }

  /**
   * returns the number of rows
   */
00604   public int getRowCount() {
    return m_RowNames.length;
  }

  /**
   * returns the number of visible rows
   */
00611   public int getVisibleRowCount() {
    int         rows;
    int         i;
    
    rows= 0;
    for (i = 0; i < getRowCount(); i++) {
      if (!getRowHidden(i))
        rows++;
    }

    return rows;
  }
  
  /**
   * sets the name of the column (if the index is valid)
   * @param index     the index of the column
   * @param name      the name of the column
   */
00629   public void setColName(int index, String name) {
    if ( (index >= 0) && (index < getColCount()) )
      m_ColNames[index] = name;
  }

  /**
   * returns the name of the row, if the index is valid, otherwise null.
   * if getPrintColNames() is FALSE then an empty string is returned or if
   * getEnumerateColNames() is TRUE then the 1-based index surrounded by
   * parentheses.
   * @see #setPrintColNames(boolean)
   * @see #getPrintColNames()
   * @see #setEnumerateColNames(boolean)
   * @see #getEnumerateColNames()
   */
00644   public String getColName(int index) {
    String        result;
    
    result = null;
    
    if ( (index >= 0) && (index < getColCount()) ) {
      if (getPrintColNames())
        result = m_ColNames[index];
      else
        result = "";

      if (getEnumerateColNames()) {
        result =   LEFT_PARENTHESES 
                 + Integer.toString(index + 1) 
                 + RIGHT_PARENTHESES
                 + " " + result;
        result = result.trim();
      }
    }

    return result;
  }

  /**
   * sets the name of the row (if the index is valid)
   * @param index     the index of the row
   * @param name      the name of the row
   */
00672   public void setRowName(int index, String name) {
    if ( (index >= 0) && (index < getRowCount()) )
      m_RowNames[index] = name;
  }

  /**
   * returns the name of the row, if the index is valid, otherwise null.
   * if getPrintRowNames() is FALSE then an empty string is returned or if
   * getEnumerateRowNames() is TRUE then the 1-based index surrounded by
   * parentheses.
   * @see #setPrintRowNames(boolean)
   * @see #getPrintRowNames()
   * @see #setEnumerateRowNames(boolean)
   * @see #getEnumerateRowNames()
   */
00687   public String getRowName(int index) {
    String        result;
    
    result = null;
    
    if ( (index >= 0) && (index < getRowCount()) ) {
      if (getPrintRowNames())
        result = m_RowNames[index];
      else
        result = "";

      if (getEnumerateRowNames()) {
        result =   LEFT_PARENTHESES 
                 + Integer.toString(index + 1) 
                 + RIGHT_PARENTHESES
                 + " " + result;
        result = result.trim();
      }
    }
    
    return result;
  }

  /**
   * sets the hidden status of the column (if the index is valid)
   * @param index       the index of the column
   * @param hidden      the hidden status of the column
   */
00715   public void setColHidden(int index, boolean hidden) {
    if ( (index >= 0) && (index < getColCount()) )
      m_ColHidden[index] = hidden;
  }

  /**
   * returns the hidden status of the column, if the index is valid, otherwise
   * false
   */
00724   public boolean getColHidden(int index) {
    if ( (index >= 0) && (index < getColCount()) )
      return m_ColHidden[index];
    else
      return false;
  }

  /**
   * sets the hidden status of the row (if the index is valid)
   * @param index       the index of the row
   * @param hidden      the hidden status of the row
   */
00736   public void setRowHidden(int index, boolean hidden) {
    if ( (index >= 0) && (index < getRowCount()) )
      m_RowHidden[index] = hidden;
  }

  /**
   * returns the hidden status of the row, if the index is valid, otherwise
   * false
   */
00745   public boolean getRowHidden(int index) {
    if ( (index >= 0) && (index < getRowCount()) )
      return m_RowHidden[index];
    else
      return false;
  }

  /**
   * sets the count for the row (if the index is valid)
   * @param index     the index of the row
   * @param count     the count for the row
   */
00757   public void setCount(int index, double count) {
    if ( (index >= 0) && (index < getRowCount()) )
      m_Counts[index] = count;
  }

  /**
   * returns the count for the row. if the index is invalid then 0.
   * @param index     the index of the row
   * @return          the count for the row
   */
00767   public double getCount(int index) {
    if ( (index >= 0) && (index < getRowCount()) )
      return m_Counts[index];
    else
      return 0;
  }

  /**
   * sets the mean at the given position (if the position is valid)
   * @param col     the column of the mean
   * @param row     the row of the mean
   * @param value   the value of the mean
   */
00780   public void setMean(int col, int row, double value) {
    if (    (col >= 0) && (col < getColCount()) 
         && (row >= 0) && (row < getRowCount()) )
      m_Mean[row][col] = value;
  }

  /**
   * returns the mean at the given position, if the position is valid,
   * otherwise 0
   */
00790   public double getMean(int col, int row) {
    if (    (col >= 0) && (col < getColCount()) 
         && (row >= 0) && (row < getRowCount()) )
      return m_Mean[row][col];
    else
      return 0;
  }

  /**
   * returns the average of the mean at the given position, if the position is
   * valid, otherwise 0
   */
00802   public double getAverage(int col) {
    int       i;
    double    avg;
    int       count;

    if ( (col >= 0) && (col < getColCount()) ) {
      avg   = 0;
      count = 0;

      for (i = 0; i < getRowCount(); i++) {
        if (!Double.isNaN(getMean(col, i))) {
          avg += getMean(col, i);
          count++;
        }
      }
      
      return avg / (double) count;
    }
    else {
      return 0;
    }
  }

  /**
   * sets the std deviation at the given position (if the position is valid)
   * @param col     the column of the std. deviation
   * @param row     the row of the std deviation
   * @param value   the value of the std deviation
   */
00831   public void setStdDev(int col, int row, double value) {
    if (    (col >= 0) && (col < getColCount()) 
         && (row >= 0) && (row < getRowCount()) )
      m_StdDev[row][col] = value;
  }

  /**
   * returns the std deviation at the given position, if the position is valid,
   * otherwise 0
   */
00841   public double getStdDev(int col, int row) {
    if (    (col >= 0) && (col < getColCount()) 
         && (row >= 0) && (row < getRowCount()) )
      return m_StdDev[row][col];
    else
      return 0;
  }

  /**
   * sets the significance at the given position (if the position is valid)
   * @param col     the column of the significance
   * @param row     the row of the significance
   * @param value   the value of the significance
   */
00855   public void setSignificance(int col, int row, int value) {
    if (    (col >= 0) && (col < getColCount()) 
         && (row >= 0) && (row < getRowCount()) )
      m_Significance[row][col] = value;
  }

  /**
   * returns the significance at the given position, if the position is valid,
   * otherwise SIGNIFICANCE_ATIE
   */
00865   public int getSignificance(int col, int row) {
    if (    (col >= 0) && (col < getColCount()) 
         && (row >= 0) && (row < getRowCount()) )
      return m_Significance[row][col];
    else
      return SIGNIFICANCE_TIE;
  }

  /**
   * counts the occurrences of the given significance type in the given
   * column.
   * @param col       the columnn to gather the information from
   * @param type      the significance type, WIN/TIE/LOSS
   */
00879   public int getSignificanceCount(int col, int type) {
    int       result;
    int       i;

    result = 0;

    if ( (col >= 0) && (col < getColCount()) ) {
      for (i = 0; i < getRowCount(); i++) {
        if (getRowHidden(i))
          continue;

        // no value?
        if (Double.isNaN(getMean(col, i)))
          continue;

        if (getSignificance(col, i) == type)
          result++;
      }
    }

    return result;
  }

  /**
   * sets the ordering of the rows, null means default
   * @param order       the new order of the rows
   */
00906   public void setRowOrder(int[] order) {
    int         i;
    
    // default order?
    if (order == null) {
      m_RowOrder = null;
    }
    else {
      if (order.length == getRowCount()) {
        m_RowOrder = new int[order.length];
        for (i = 0; i < order.length; i++)
          m_RowOrder[i] = order[i];
      }
      else {
        System.err.println("setRowOrder: length does not match (" 
            + order.length + " <> " + getRowCount() + ") - ignored!");
      }
    }
  }

  /**
   * returns the current order of the rows, null means the default order
   * @return        the current order of the rows
   */
00930   public int[] getRowOrder() {
    return m_RowOrder;
  }

  /**
   * returns the displayed index of the given row, depending on the order of
   * rows, returns -1 if index out of bounds
   * @param index         the row to get the displayed index for
   * @return              the real index of the row
   */
00940   public int getDisplayRow(int index) {
    if ( (index >= 0) && (index < getRowCount()) ) {
      if (getRowOrder() == null)
        return index;
      else
        return getRowOrder()[index];
    }
    else {
      return -1;
    }
  }

  /**
   * sets the ordering of the columns, null means default
   * @param order       the new order of the columns
   */
00956   public void setColOrder(int[] order) {
    int         i;
    
    // default order?
    if (order == null) {
      m_ColOrder = null;
    }
    else {
      if (order.length == getColCount()) {
        m_ColOrder = new int[order.length];
        for (i = 0; i < order.length; i++)
          m_ColOrder[i] = order[i];
      }
      else {
        System.err.println("setColOrder: length does not match (" 
            + order.length + " <> " + getColCount() + ") - ignored!");
      }
    }
  }

  /**
   * returns the current order of the columns, null means the default order
   * @return        the current order of the columns
   */
00980   public int[] getColOrder() {
    return m_ColOrder;
  }

  /**
   * returns the displayed index of the given col, depending on the order of
   * columns, returns -1 if index out of bounds
   * @param index         the column to get the displayed index for
   * @return              the real index of the column
   */
00990   public int getDisplayCol(int index) {
    if ( (index >= 0) && (index < getColCount()) ) {
      if (getColOrder() == null)
        return index;
      else
        return getColOrder()[index];
    }
    else {
      return -1;
    }
  }

  /**
   * returns the given number as string rounded to the given number of
   * decimals. additional necessary 0's are added
   * @param d       the number to format
   * @param prec    the number of decimals after the point
   * @return        the formatted number
   */
01009   protected String doubleToString(double d, int prec) {
    String        result;
    int           currentPrec;
    int           i;

    result = Utils.doubleToString(d, prec);

    // decimal point?
    if (result.indexOf(".") == -1)
      result += ".";
    
    // precision so far?
    currentPrec = result.length() - result.indexOf(".") - 1;
    for (i = currentPrec; i < prec; i++)
      result += "0";
    
    return result;
  }

  /**
   * trims the given string down to the given length if longer, otherwise
   * leaves it unchanged. a length of "0" leaves the string always 
   * unchanged.
   * @param s       the string to trim (if too long)
   * @param length  the max. length (0 means infinity)
   * @return        the trimmed string
   */
01036   protected String trimString(String s, int length) {
    if ( (length > 0) && (s.length() > length) )
      return s.substring(0, length);
    else
      return s;
  }

  /**
   * pads the given string on the right until it reaches the given length, if
   * longer cuts it down. if length is 0 then nothing is done.
   * @param s         the string to pad
   * @param length    the max. length of the string
   * @return          the padded string
   */
01050   protected String padString(String s, int length) {
    return padString(s, length, false);
  }

  /**
   * pads the given string until it reaches the given length, if longer cuts
   * it down. if length is 0 then nothing is done.
   * @param s         the string to pad
   * @param length    the max. length of the string
   * @param left      whether to pad left or right
   * @return          the padded string
   */
01062   protected String padString(String s, int length, boolean left) {
    String      result;
    int         i;

    result = s;

    // pad with blanks
    for (i = s.length(); i < length; i++) {
      if (left)
        result = " " + result;
      else
        result = result + " ";
    }
      
    // too long?
    if ( (length > 0) && (result.length() > length) )
      result = result.substring(0, length);

    return result;
  }

  /**
   * returns the length of the longest cell in the given column
   * @param data    the data to base the calculation on
   * @param col     the column to check
   * @return        the maximum length
   */
01089   protected int getColSize(String[][] data, int col) {
    return getColSize(data, col, false, false);
  }

  /**
   * returns the length of the longest cell in the given column
   * @param data        the data to base the calculation on
   * @param col         the column to check
   * @param skipFirst   whether to skip the first row
   * @param skipLast    whether to skip the last row
   * @return            the maximum length
   */
01101   protected int getColSize( String[][] data, int col, 
                            boolean skipFirst, boolean skipLast ) {
    int       result;
    int       i;

    result = 0;

    if ( (col >= 0) && (col < data[0].length) ) {
      for (i = 0; i < data.length; i++) {
        // skip first?
        if ( (i == 0) && (skipFirst) )
          continue;

        // skip last?
        if ( (i == data.length - 1) && (skipLast) )
          continue;
        
        if (data[i][col].length() > result)
          result = data[i][col].length();
      }
    }

    return result;
  }

  /**
   * removes the filter classname from the given string if it should be 
   * removed, otherwise leaves the string alone
   * @see     #getRemoveFilterName()
   */
01131   protected String removeFilterName(String s) {
    if (getRemoveFilterName())
      return s.replaceAll("-weka\\.filters\\..*", "")
              .replaceAll("-unsupervised\\..*",   "")
              .replaceAll("-supervised\\..*",     "");
    else
      return s;
  }

  /**
   * returns a 2-dimensional array with the prepared data. includes the column
   * and row names. hidden cols/rows are already excluded. <br>
   * first row: column names<br>
   * last  row: wins/ties/losses<br>
   * first col: row names<br>
   */
01147   protected String[][] toArray() {
    int               i;
    int               n;
    int               ii;
    int               nn;
    int               x;
    int               y;
    String[][]        result;
    String[][]        tmpResult;
    int               cols;
    int               rows;
    int[]             widths;
    boolean           valueExists;

    // determine visible cols/rows
    rows = getVisibleRowCount();
    if (getShowAverage())
      rows++;
    cols = getVisibleColCount();
    if (getShowStdDev())
      cols = cols*3;   // mean + stddev + sign.
    else
      cols = cols*2;   // mean + stddev

    result = new String[rows + 2][cols + 1];

    // col names
    result[0][0] = trimString("Dataset", getRowNameWidth());
    x = 1;
    for (ii = 0; ii < getColCount(); ii++) {
      i = getDisplayCol(ii);
      if (getColHidden(i))
        continue;
      
      result[0][x] = trimString(
          removeFilterName(getColName(i)), getColNameWidth());
      x++;
      // std dev
      if (getShowStdDev()) {
        result[0][x] = "";
        x++;
      }
      // sign.
      result[0][x] = "";
      x++;
    }

    // row names
    y = 1;
    for (ii = 0; ii < getRowCount(); ii++) {
      i = getDisplayRow(ii);
      if (!getRowHidden(i)) {
        result[y][0] = trimString(
            removeFilterName(getRowName(i)), getRowNameWidth());
        y++;
      }
    }

    // fill in mean/std dev
    y = 1;
    for (ii = 0; ii < getRowCount(); ii++) {
      i = getDisplayRow(ii);
      if (getRowHidden(i))
        continue;

      x = 1;
      for (nn = 0; nn < getColCount(); nn++) {
        n = getDisplayCol(nn);
        if (getColHidden(n))
          continue;

        // do we have a value in the matrix?
        valueExists = (!Double.isNaN(getMean(n, i)));

        // mean
        if (!valueExists)
          result[y][x] = "";
        else
          result[y][x] = doubleToString(getMean(n, i), getMeanPrec());
        x++;
        
        // stddev
        if (getShowStdDev()) {
          if (!valueExists)
            result[y][x] = "";
          else if (Double.isInfinite(getStdDev(n, i)))
            result[y][x] = "Inf";
          else
            result[y][x] = doubleToString(getStdDev(n, i), getStdDevPrec());
          x++;
        }
        
        // significance
        if (!valueExists) {
          result[y][x] = "";
        }
        else {
          switch (getSignificance(n, i)) {
            case SIGNIFICANCE_TIE:
              result[y][x] = TIE_STRING;
              break;
            case SIGNIFICANCE_WIN:
              result[y][x] = WIN_STRING;
              break;
            case SIGNIFICANCE_LOSS:
              result[y][x] = LOSS_STRING;
              break;
          }
        }
        x++;
      }

      y++;
    }

    // the average
    if (getShowAverage()) {
      y = result.length - 2;
      x = 0;
      result[y][0] = "Average";
      x++;
      for (ii = 0; ii < getColCount(); ii++) {
        i = getDisplayCol(ii);
        if (getColHidden(i))
          continue;

        // mean-average
        result[y][x] = doubleToString(getAverage(i), getMeanPrec());
        x++;

        // std dev.
        if (getShowStdDev()) {
          result[y][x] = "";
          x++;
        }

        // significance
        result[y][x] = "";
        x++;
      }
    }

    // wins/ties/losses
    y = result.length - 1;
    x = 0;
    result[y][0] =   LEFT_PARENTHESES 
                   + WIN_STRING + "/" 
                   + TIE_STRING + "/" 
                   + LOSS_STRING 
                   + RIGHT_PARENTHESES;
    x++;
    for (ii = 0; ii < getColCount(); ii++) {
      i = getDisplayCol(ii);
      if (getColHidden(i))
        continue;

      // mean
      result[y][x] = "";
      x++;

      // std dev.
      if (getShowStdDev()) {
        result[y][x] = "";
        x++;
      }

      // significance
      result[y][x] =   LEFT_PARENTHESES 
                     + getSignificanceCount(i, SIGNIFICANCE_WIN) + "/" 
                     + getSignificanceCount(i, SIGNIFICANCE_TIE) + "/" 
                     + getSignificanceCount(i, SIGNIFICANCE_LOSS) 
                     + RIGHT_PARENTHESES;
      x++;
    }

    // base column has no significance -> remove these columns
    tmpResult = new String[result.length][result[0].length - 1];

    x = 0;
    for (i = 0; i < result[0].length; i++) {
      // significance
      if (    ((i == 3) && ( getShowStdDev()))
           || ((i == 2) && (!getShowStdDev())) )
        continue;
      
      for (n = 0; n < result.length; n++)
        tmpResult[n][x] = result[n][i];

      x++;
    }
    result = tmpResult;

    return result;
  }

  /**
   * returns true if the index (in the array produced by toArray(boolean))
   * is the row name
   */
01346   protected boolean isRowName(int index) {
    return  (index == 0);
  }

  /**
   * returns true if the index (in the array produced by toArray(boolean))
   * contains a mean
   */
01354   protected boolean isMean(int index) {
    index--;   // dataset
    if (index == 0) {
      return true;   // base column
    }
    else {
      index--;   // base column

      if (index < 0)
        return false;
      
      if (getShowStdDev())
        return (index % 3 == 1);
      else
        return (index % 2 == 0);
    }
  }

  /**
   * returns true if the row index (in the array produced by toArray(boolean))
   * contains the average row
   */
01376   protected boolean isAverage(int rowIndex) {
    if (getShowAverage())
      return (getVisibleRowCount() + 1 == rowIndex);
    else
      return false;
  }

  /**
   * returns true if the index (in the array produced by toArray(boolean))
   * contains a std deviation
   */
01387   protected boolean isStdDev(int index) {
    index--;   // dataset
    index--;   // base column

    if (getShowStdDev()) {
      if (index == 0) {
        return true;   // stddev of base column
      }
      else {
        index--;   // stddev of base column

        if (index < 0)
          return false;
      
        return (index % 3 == 1);
      }
    }
    else
      return false;
  }

  /**
   * returns true if the index (in the array produced by toArray(boolean))
   * contains a significance column
   */
01412   protected boolean isSignificance(int index) {
    index--;   // dataset
    index--;   // base column
    if (getShowStdDev()) {
      index--;   // stddev of base column

      if (index < 0)
        return false;
      
      return (index % 3 == 2);
    }
    else {
      if (index < 0)
        return false;
      
      return (index % 2 == 1);
    }
  }

  /**
   * returns the matrix as a string
   */
  public abstract String toStringMatrix();

  /**
   * returns the matrix as a string
   * @see #toStringMatrix()
   */
01440   public String toString() {
    return toStringMatrix();
  }

  /**
   * removes all the header information
   */
01447   public void clearHeader() {
    m_HeaderKeys   = new Vector();
    m_HeaderValues = new Vector();
  }

  /**
   * adds the key-value pair to the header
   * @param key       the name of the header value
   * @param value     the value of the header value
   */
01457   public void addHeader(String key, String value) {
    int         pos;
    
    pos = m_HeaderKeys.indexOf(key);
    if (pos > -1) {
      m_HeaderValues.set(pos, value);
    }
    else {
      m_HeaderKeys.add(key);
      m_HeaderValues.add(value);
    }
  }

  /**
   * returns the value associated with the given key, null if if cannot be
   * found
   * @param key       the key to retrieve the value for
   * @return          the associated value
   */
01476   public String getHeader(String key) {
    int       pos;

    pos = m_HeaderKeys.indexOf(key);
    if (pos == 0)
      return null;
    else
      return (String) m_HeaderKeys.get(pos);
  }

  /**
   * returns an enumeration of the header keys
   * @return all stored keys
   */
01490   public Enumeration headerKeys() {
    return m_HeaderKeys.elements();
  }
  
  /**
   * returns the header of the matrix as a string
   * @see #m_HeaderKeys
   * @see #m_HeaderValues
   */
  public abstract String toStringHeader();

  /**
   * returns returns a key for all the col names, for better readability if
   * the names got cut off
   */
  public abstract String toStringKey();

  /**
   * clears the current summary data
   */
01510   public void clearSummary() {
    m_NonSigWins = null;
    m_Wins       = null;
  }

  /**
   * sets the non-significant and significant wins of the resultsets
   * @param nonSigWins      the non-significant wins
   * @param wins         the significant wins
   */
01520   public void setSummary(int[][] nonSigWins, int[][] wins) {
    int         i;
    int         n;
    
    m_NonSigWins = new int[nonSigWins.length][nonSigWins[0].length];
    m_Wins       = new int[wins.length][wins[0].length];

    for (i = 0; i < m_NonSigWins.length; i++) {
      for (n = 0; n < m_NonSigWins[i].length; n++) {
        m_NonSigWins[i][n] = nonSigWins[i][n];
        m_Wins[i][n]       = wins[i][n];
      }
    }
  }

  /**
   * returns the character representation of the given column
   */
01538   protected String getSummaryTitle(int col) {
    return "" + (char) ((int) 'a' + col % 26);
  }

  /**
   * returns the summary as string
   */
  public abstract String toStringSummary();

  /**
   * clears the currently stored ranking data
   */
01550   public void clearRanking() {
    m_RankingWins   = null;
    m_RankingLosses = null;
    m_RankingDiff   = null;
  }

  /**
   * sets the ranking data based on the wins
   * @param wins      the wins 
   */
01560   public void setRanking(int[][] wins) {
    int         i;
    int         j;
    
    m_RankingWins   = new int[wins.length];
    m_RankingLosses = new int[wins.length];
    m_RankingDiff   = new int[wins.length];

    for (i = 0; i < wins.length; i++) {
      for (j = 0; j < wins[i].length; j++) {
      m_RankingWins[j]   += wins[i][j];
      m_RankingDiff[j]   += wins[i][j];
      m_RankingLosses[i] += wins[i][j];
      m_RankingDiff[i]   -= wins[i][j];
      }
    }
  }

  /**
   * returns the ranking in a string representation
   */
  public abstract String toStringRanking();
}

Generated by  Doxygen 1.6.0   Back to index