001/**
002 * Copyright (C) 2007-2011, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019
020package org.dllearner.experiments;
021
022import java.io.File;
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Random;
027import java.util.SortedSet;
028import java.util.TreeSet;
029
030import org.apache.log4j.Logger;
031import org.dllearner.utilities.Files;
032import org.dllearner.utilities.JamonMonitorLogger;
033
034import com.jamonapi.Monitor;
035
036public class Table implements Serializable{
037        private static final long serialVersionUID = -7191672899557577952L;
038
039        private static final Logger logger = Logger.getLogger(Table.class);
040
041        boolean replaceCommaByPoints = true;
042        
043        enum Formats {
044                LATEX, GNUPLOT
045        }
046
047        private SortedSet<String> experimentNames = new TreeSet<>();
048        private SortedSet<String> labels = new TreeSet<>();
049        
050        private List<TableRowColumn> tableRowColumns = new ArrayList<>();
051        private int length;
052
053        public static void main(String[] args) {
054                int tablesize = 10;
055                int nrOfMonitors = 10;
056                Table t = new Table();
057                
058                Random r = new Random();
059                for (int i = 0; i < tablesize; i++) {
060                        Monitor[] m = new Monitor[nrOfMonitors];
061                        for (int a = 0; a < nrOfMonitors; a++) {
062                                m[a] = JamonMonitorLogger.getStatisticMonitor("test" + i + "" + a,
063                                                (a==0)?"":JamonMonitorLogger.PERCENTAGE);
064                                for (int b = 0; b < 2; b++) {
065                                        m[a].add(r.nextDouble());
066
067                                }
068                        }
069                        TableRowColumn trc = new TableRowColumn(m, "Test","entry_" + i);
070                        trc.useStdDev=false;
071                        t.addTableRowColumn(trc);
072                }
073                
074                for (int i = 0; i < tablesize; i++) {
075                        Monitor[] m = new Monitor[nrOfMonitors];
076                        for (int a = 0; a < nrOfMonitors; a++) {
077                                m[a] = JamonMonitorLogger.getStatisticMonitor("test" + i + "" + a,
078                                                (a==0)?"":JamonMonitorLogger.PERCENTAGE);
079                                for (int b = 0; b < 2; b++) {
080                                        m[a].add(r.nextDouble());
081                                        
082                                }
083                        }
084                        TableRowColumn trc = new TableRowColumn(m, "Whatever","bentry_" + i);
085                        trc.useStdDev=false;
086                        t.addTableRowColumn(trc);
087                }
088                
089                
090                System.out.println(t.getLatexAsColumn(true));
091                t.sortByLabel();
092                System.out.println(t.getLatexAsColumn(true));
093                t.sortByExperimentName();
094                System.out.println(t.getGnuPlotAsColumn(true));
095//              System.out.println(t.getLatexAsRows());
096//              System.out.println(t.getGnuPlotAsColumn(true));
097//              System.out.println(t.getGnuPlotAsRows());
098//              System.out.println(MonProxyFactory.);
099//              System.out.println( MonitorFactory.getReport());
100//              JamonMonitorLogger.writeHTMLReport("log/tiger.html");
101        }
102
103        
104        
105        /**
106         * passes each it TableRowColumn one by one to addTableRowColumn
107         * @param t
108         */
109        public void addTable(Table t){
110                for (TableRowColumn  trc : t.tableRowColumns) {
111                        addTableRowColumn(trc);
112                }
113        }
114        
115        /**
116         * passes it one by one to addTableRowColumn
117         * @param trcs
118         */
119        public void addTableRowColumns(List<TableRowColumn> trcs) {
120                for (TableRowColumn tableRowColumn : trcs) {
121                        addTableRowColumn(tableRowColumn);
122                }
123        }
124        
125        public void addTableRowColumn(TableRowColumn trc) {
126                labels.add(trc.getLabel());
127                experimentNames.add(trc.getExperimentName());
128                try{
129                        trc.toLatexRow();
130                }catch (NullPointerException e) {
131                        logger.error("TableRowColumn was not initialized, ignoring it: "+trc);
132                        e.printStackTrace();
133                }
134                
135                if (tableRowColumns.isEmpty()) {
136                        length = trc.size();
137                }
138                
139                if (trc.size() != length) {
140                        
141                        logger.error("Added TableRowColumn does not match previous set length (" + length + ") but has size "
142                                        + trc.size() + "), \nignoring it: "+trc);
143                        
144                        
145                }
146                tableRowColumns.add(trc);
147        }
148
149        public String getLatexAsRows() {
150                return getAsRows(Formats.LATEX);
151        }
152
153        public String getLatexAsColumn() {
154                return getLatexAsColumn(false);
155        }
156        public String getLatexAsColumn(boolean addNumbersInFront) {
157                return getAsColums(Formats.LATEX, addNumbersInFront);
158        }
159
160        public String getGnuPlotAsRows() {
161                return getAsRows(Formats.GNUPLOT);
162        }
163
164        public String getGnuPlotAsColumn() {
165                return getGnuPlotAsColumn(false);
166        }
167        public String getGnuPlotAsColumn(boolean addNumbersInFront) {
168                return getAsColums(Formats.GNUPLOT, addNumbersInFront);
169        }
170
171        private String getAsColums(Formats f, boolean addNumbersInFront) {
172                String[] rows = new String[length+1];
173                for (int i = 0; i < length+1; i++) {
174                        rows[i]="";
175                }
176                
177                for (int a = 0; a < tableRowColumns.size(); a++) {
178                        TableRowColumn trc = tableRowColumns.get(a);
179                        String header = trc.getExperimentName()+" "+trc.getLabel();
180                        boolean firstColumn = (a==0);
181                        boolean lastColumn = (a + 1 == tableRowColumns.size());
182                        for (int i = 1; i < length+1; i++) {
183                                
184                                boolean firstRow = (i==1);
185                                switch (f) {
186                                        case LATEX:
187                                                rows[0] += ((firstColumn&&firstRow&&addNumbersInFront)?TableRowColumn.latexSep:"");
188                                                rows[0] += (firstRow?header+TableRowColumn.latexSep:"");
189                                                rows[i] += ((firstColumn&&addNumbersInFront)?i+TableRowColumn.latexSep:"");
190                                                rows[i] += trc.getLatexEntry(i-1)+ ((lastColumn) ? TableRowColumn.latexSep : TableRowColumn.latexSep);
191                                                break;
192                                        case GNUPLOT:
193                                                rows[0] += (firstRow?"#"+header+"\t":"");
194                                                rows[i] += (firstColumn&&addNumbersInFront?i+"\t":"");
195                                                rows[i] += trc.getGnuPlotEntry(i-1) + ((lastColumn) ?""  : "\t");
196                                                break;
197                                        }
198                        }
199                }
200                String ret = "";
201                for (int i = 0; i < length+1; i++) {
202                        ret += rows[i]+"\n";
203                }
204                
205                return (replaceCommaByPoints)?ret.replace(",","."):ret;
206        }
207
208        private String getAsRows(Formats f) {
209                String ret = "";
210                for (TableRowColumn trc : tableRowColumns) {
211                        switch (f) {
212                        case LATEX:
213                                ret += trc.toLatexRow()+"\n";
214                                break;
215                        case GNUPLOT:
216                                ret += trc.toGnuPlotRow()+"\n";
217                                break;
218                        default:
219                        }
220                }
221                return (replaceCommaByPoints)?ret.replace(",","."):ret;
222        }
223        
224        public void sortByExperimentName(){
225                _sortByLabel();
226                _sortByExperimentName();
227                
228        }
229        public void sortByLabel(){
230                _sortByExperimentName();
231                _sortByLabel();
232        }
233        
234        private void _sortByLabel(){
235                List<String> l = new ArrayList<>(labels);
236                List<TableRowColumn> newTrc = new ArrayList<>();
237                
238                for (String s : l) {
239                        for (TableRowColumn trc : tableRowColumns) {
240                                if(trc.getLabel().startsWith(s) && trc.getLabel().contains(s)){
241                                        newTrc.add(trc);
242                                }
243                        }
244                }
245                tableRowColumns = newTrc;
246        }
247        private void _sortByExperimentName(){
248                List<String> l = new ArrayList<>(experimentNames);
249                List<TableRowColumn> newTrc = new ArrayList<>();
250                
251                for (String s : l) {
252                        for (TableRowColumn trc : tableRowColumns) {
253                                if(trc.getExperimentName().equals(s)){
254                                        newTrc.add(trc);
255                                }
256                        }
257                }
258                tableRowColumns = newTrc;
259        }
260        
261        /**
262         * Writes all possible outputs to the given folder, i.e. 
263         * GNu plot rows and Columns and Latex tables
264         * @param folder
265         * @param fileprefix
266         */
267        public void write(String folder, String fileprefix){
268                logger.info("Writing results to "+folder+fileprefix);
269                Files.mkdir(folder);
270                Files.createFile(new File(folder+fileprefix+"_GNU_ROWS"), getGnuPlotAsRows());
271                Files.createFile(new File(folder+fileprefix+"_GNU_COLUMNS_I"), getGnuPlotAsColumn(true));
272                Files.createFile(new File(folder+fileprefix+"_GNU_COLUMNS"), getGnuPlotAsColumn());
273                
274                
275                Files.createFile(new File(folder+fileprefix+"_LATEX_ROWS"), getLatexAsRows());
276                Files.createFile(new File(folder+fileprefix+"_LATEX_COLUMNS"), getLatexAsColumn());
277                Files.createFile(new File(folder+fileprefix+"_LATEX_COLUMNS_I"), getLatexAsColumn(true));
278        
279                serialize(folder+fileprefix+".ser");
280                
281        }
282        
283        public void serialize(String filename){
284                Files.writeObjectToFile(this, new File(filename));
285        }
286        public static Table deserialize(String filename){
287                return (Table)Files.readObjectfromFile(new File(filename));
288        }
289
290}