001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.ml.linear.experiments.sinabill;
031
032import java.io.DataOutputStream;
033import java.io.File;
034import java.io.FileOutputStream;
035import java.io.IOException;
036
037import org.apache.logging.log4j.Logger;
038import org.apache.logging.log4j.LogManager;
039
040import org.openimaj.io.IOUtils;
041import org.openimaj.ml.linear.learner.BilinearLearnerParameters;
042
043public abstract class BilinearExperiment {
044        private static final String EXPERIMENT_NAME = "%s/%s/%s_%s";
045        private static final String PARAMS_NAME = ".paramsascii";
046        private static final String PARAMS_DATA_NAME = ".params";
047
048        private static final String BILL_DATA_ROOT = "%s/TrendMiner/deliverables/year2-18month/Austrian Data/";
049        private static final String BILL_DATA = "%s/data.mat";
050        
051        Logger logger = LogManager.getLogger(getClass());
052        private static long experimentTime = -1;
053        
054        protected void prepareExperimentLog(BilinearLearnerParameters params) throws IOException {
055                // ConsoleAppender console = new ConsoleAppender(); //create appender
056                // //configure the appender
057                // String PATTERN = "[%p->%C{1}] %m%n";
058                // console.setLayout(new PatternLayout(PATTERN)); 
059                // console.setThreshold(Level.DEBUG);
060                // console.activateOptions();
061         //     // add appender to any Logger (here is root)
062                // Logger.getRootLogger().addAppender(console);
063                // File expRoot = prepareExperimentRoot();
064                
065                // IOUtils.write(params, new DataOutputStream(new FileOutputStream(new File(expRoot,PARAMS_DATA_NAME))));
066                // IOUtils.writeASCII(new File(expRoot,PARAMS_NAME), params);
067                
068                // File logFile = new File(expRoot,"log");
069                // if(logFile.exists())logFile.delete();
070                // FileAppender file = new FileAppender(new PatternLayout(PATTERN), logFile.getAbsolutePath()); 
071                // file.setThreshold(Level.DEBUG);
072                // file.activateOptions();
073                // Logger.getRootLogger().addAppender(file );
074                
075        }
076        
077        public File prepareExperimentRoot() throws IOException {
078                String experimentRoot = String.format(EXPERIMENT_NAME,DATA_ROOT(),getExperimentSetName(),getExperimentName(),""+currentExperimentTime());
079                File expRoot = new File(experimentRoot);
080                if(expRoot.exists() && expRoot.isDirectory()) return expRoot;
081                logger.debug("Experiment root: " + expRoot);
082                if(!expRoot.mkdirs()) throw new IOException("Couldn't prepare experiment output");
083                return expRoot;
084        }
085
086        private long currentExperimentTime() {
087                if(experimentTime==-1){
088                        experimentTime = System.currentTimeMillis();
089                }
090                return experimentTime;
091        }
092        protected String FOLD_ROOT(int fold) throws IOException {
093                File foldRoot = new File(prepareExperimentRoot(),String.format("fold_%d",fold));
094                if(!foldRoot.exists() && !foldRoot.mkdirs()) 
095                        throw new IOException("Failed creating the fold directory: " + foldRoot);
096                return foldRoot.getAbsolutePath();
097        }
098        
099        protected String MATLAB_DATA() {
100                
101                return String.format(BILL_DATA,DATA_ROOT());
102        }
103        
104        protected String MATLAB_DATA(String data) {
105                
106                return String.format(data,DATA_ROOT());
107        }
108        
109        protected String DATA_ROOT() {
110                
111                return String.format(BILL_DATA_ROOT,DROPBOX_HOME());
112        }
113        
114        private String DROPBOX_HOME() {
115                String home = System.getProperty("user.home");
116                
117                return String.format("%s/Dropbox",home);
118        }
119        
120        public abstract void performExperiment() throws Exception;
121
122        public String getExperimentName() {
123                return "experiment";
124        }
125        
126        public String getExperimentSetName() {
127                return "streamingExperiments";
128        }
129}