001    /*
002     * BaseCoOccurrenceFrequencyMatrix
003     * 
004     * Copyright (c) 2001, 2002, 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.color.data;
009    
010    /**
011     * This abstract class encapsulates all data of a co-occurrence
012     * frequency matrix except for the frequency values.
013     * The method computeStatistics is implemented.
014     * Any class extending this class only has to
015     * deal with storing the frequency values ({@link MemoryCoOccurrenceFrequencyMatrix}
016     * does this by using a one-dimensional array internally).
017     * @author Marco Schmidt
018     */
019    public abstract class BaseCoOccurrenceFrequencyMatrix implements CoOccurrenceFrequencyMatrix
020    {
021            /** co-occurrence frequency mean $\mu_{C(j)}$ */
022            private double[] cofMean;
023            /** co-occurrence frequency standard deviation $\sigma_{C(j)}$ */
024            private double[] cofStddev;
025            /** self co-occurrence frequency mean $\mu_S$ */
026            private double scofMean;
027            /** self co-occurrence frequency standard deviation $\sigma_S$ */
028            private double scofStddev;
029            /** equals scofMean + scofStddev */
030            private double scofSum;
031    
032            private void computeCoOccurrenceFrequencyMeanValues()
033            {
034                    cofMean = new double[getDimension()];
035                    for (int j = 0; j < getDimension(); j++)
036                    {
037                            double result = 0.0;
038                            for (int i = 0; i < getDimension(); i++)
039                            {
040                                    result += getValue(i, j);
041                            }
042                            cofMean[j] = result / ((double)getDimension());
043                    }
044                    //System.out.println("DEBUG: done computing cofm mean values");
045            }
046    
047            private void computeCoOccurrenceFrequencyStandardDeviationValues()
048            {
049                    cofStddev = new double[getDimension()];
050                    for (int j = 0; j < getDimension(); j++)
051                    {
052                            double result = 0.0;
053                            for (int i = 0; i < getDimension(); i++)
054                            {
055                                    double value = getValue(i, j) - cofMean[j];
056                                    result += (value * value);
057                            }
058                            cofStddev[j] = Math.sqrt(result);
059                    }
060                    //System.out.println("DEBUG: done computing cofm stddev values");
061            }
062    
063            private void computeSelfCoOccurrenceFrequencyMeanValue()
064            {
065                    double sum = 0.0;
066                    for (int i = 0; i < getDimension(); i++)
067                    {
068                            sum += getValue(i, i);
069                    }
070                    scofMean = sum / (getDimension());
071                    //System.out.println("DEBUG: scof mean=" + scofMean);
072            }
073    
074            private void computeSelfCoOccurrenceFrequencyStandardDeviationValue()
075            {
076                    double result = 0.0;
077                    for (int i = 0; i < getDimension(); i++)
078                    {
079                            double value = getValue(i, i) - getScofMean();
080                            result += (value * value);
081                    }
082                    scofStddev = Math.sqrt(result);
083                    //System.out.println("DEBUG: scof stddev=" + scofStddev);
084            }
085    
086            /**
087             * Assumes that the co-occurrence frequency values have been initialized.
088             * Computes mean and standard deviation for co-occurrence and self co-occurrence
089             * frequency values.
090             */
091            public void computeStatistics()
092            {
093                    // we must keep this order because stddev needs mean!
094                    computeSelfCoOccurrenceFrequencyMeanValue();
095                    computeSelfCoOccurrenceFrequencyStandardDeviationValue();
096                    scofSum = getScofMean() + getScofStddev();
097                    //System.out.println("DEBUG: scof sum=" + scofSum);
098                    computeCoOccurrenceFrequencyMeanValues();
099                    computeCoOccurrenceFrequencyStandardDeviationValues();
100            }
101    
102            /**
103             * Prints co-occurrence frequency values to standard output, one line
104             * per matrix row j.
105             * Calls getValue(i, j) with each column i for each row j.
106             */
107            private void dump()
108            {
109                    //System.out.println("\nCo-occurrence frequency values (dimension of matrix=" + getDimension() + "):");
110                    for (int j = 0; j < getDimension(); j++)
111                    {
112                            for (int i = 0; i < getDimension(); i++)
113                            {
114                                    System.out.print(getValue(i, j) + " ");
115                            }
116                            System.out.println("");
117                    }
118            }
119    
120            /**
121             * Prints self co-occurrence frequency values to standard output, eight
122             * values per row.
123             * Calls getValue(i, i) with each value i from 0 to getDimension() - 1.
124             */
125            private void dumpScofValues()
126            {
127                    //System.out.println("\nSelf co-occurrence frequency values " + "(dimension of matrix=" + getDimension() + "):");
128                    for (int j = 0; j < getDimension(); j++)
129                    {
130                            System.out.print(getValue(j) + " ");
131                            if (j % 8 == 0 && j > 0)
132                            {
133                                    System.out.println("");
134                            }
135                    }
136                    System.out.println("");
137            }
138    
139            /**
140             * Returns the mean of the co-occurrence frequency values.
141             */
142            public double getMean(int index)
143            {
144                    return cofMean[index];
145            }
146    
147            public double getStddev(int index)
148            {
149                    return cofStddev[index];
150            }
151    
152            /**
153             * Returns the mean of all self co-occurrence frequency values.
154             * This value is called $\mu_S$ in Shufelt's paper.
155             * This value is determined once within computeStatistics().
156             */
157            public double getScofMean()
158            {
159                    return scofMean;
160            }
161    
162            /**
163             * Returns the standard deviation of all self co-occurrence frequency
164             * values.
165             * This value is called $\sigma_S$ in Shufelt's paper.
166             * This value is determined once within a call to computeStatistics().
167             */
168            public double getScofStddev()
169            {
170                    return scofStddev;
171            }
172    
173            /**
174             * Return the sum of mean and standard deviation of the self
175             * co-occurrence frequency values.
176             * Assumes that {@link #computeStatistics} has been called already.
177             * @return sum of mean and standard deviation of the self co-occurrence
178             *  frequency values
179             */
180            public double getScofSum()
181            {
182                    return scofSum;
183            }
184    }