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.log4j.ConsoleAppender; 038import org.apache.log4j.FileAppender; 039import org.apache.log4j.Level; 040import org.apache.log4j.Logger; 041import org.apache.log4j.PatternLayout; 042import org.openimaj.io.IOUtils; 043import org.openimaj.ml.linear.learner.BilinearLearnerParameters; 044 045public abstract class BilinearExperiment { 046 private static final String EXPERIMENT_NAME = "%s/%s/%s_%s"; 047 private static final String PARAMS_NAME = ".paramsascii"; 048 private static final String PARAMS_DATA_NAME = ".params"; 049 050 private static final String BILL_DATA_ROOT = "%s/TrendMiner/deliverables/year2-18month/Austrian Data/"; 051 private static final String BILL_DATA = "%s/data.mat"; 052 053 Logger logger = Logger.getLogger(getClass()); 054 private static long experimentTime = -1; 055 056 protected void prepareExperimentLog(BilinearLearnerParameters params) throws IOException { 057 ConsoleAppender console = new ConsoleAppender(); //create appender 058 //configure the appender 059 String PATTERN = "[%p->%C{1}] %m%n"; 060 console.setLayout(new PatternLayout(PATTERN)); 061 console.setThreshold(Level.DEBUG); 062 console.activateOptions(); 063 // add appender to any Logger (here is root) 064 Logger.getRootLogger().addAppender(console); 065 File expRoot = prepareExperimentRoot(); 066 067 IOUtils.write(params, new DataOutputStream(new FileOutputStream(new File(expRoot,PARAMS_DATA_NAME)))); 068 IOUtils.writeASCII(new File(expRoot,PARAMS_NAME), params); 069 070 File logFile = new File(expRoot,"log"); 071 if(logFile.exists())logFile.delete(); 072 FileAppender file = new FileAppender(new PatternLayout(PATTERN), logFile.getAbsolutePath()); 073 file.setThreshold(Level.DEBUG); 074 file.activateOptions(); 075 Logger.getRootLogger().addAppender(file ); 076 077 } 078 079 public File prepareExperimentRoot() throws IOException { 080 String experimentRoot = String.format(EXPERIMENT_NAME,DATA_ROOT(),getExperimentSetName(),getExperimentName(),""+currentExperimentTime()); 081 File expRoot = new File(experimentRoot); 082 if(expRoot.exists() && expRoot.isDirectory()) return expRoot; 083 logger.debug("Experiment root: " + expRoot); 084 if(!expRoot.mkdirs()) throw new IOException("Couldn't prepare experiment output"); 085 return expRoot; 086 } 087 088 private long currentExperimentTime() { 089 if(experimentTime==-1){ 090 experimentTime = System.currentTimeMillis(); 091 } 092 return experimentTime; 093 } 094 protected String FOLD_ROOT(int fold) throws IOException { 095 File foldRoot = new File(prepareExperimentRoot(),String.format("fold_%d",fold)); 096 if(!foldRoot.exists() && !foldRoot.mkdirs()) 097 throw new IOException("Failed creating the fold directory: " + foldRoot); 098 return foldRoot.getAbsolutePath(); 099 } 100 101 protected String MATLAB_DATA() { 102 103 return String.format(BILL_DATA,DATA_ROOT()); 104 } 105 106 protected String MATLAB_DATA(String data) { 107 108 return String.format(data,DATA_ROOT()); 109 } 110 111 protected String DATA_ROOT() { 112 113 return String.format(BILL_DATA_ROOT,DROPBOX_HOME()); 114 } 115 116 private String DROPBOX_HOME() { 117 String home = System.getProperty("user.home"); 118 119 return String.format("%s/Dropbox",home); 120 } 121 122 public abstract void performExperiment() throws Exception; 123 124 public String getExperimentName() { 125 return "experiment"; 126 } 127 128 public String getExperimentSetName() { 129 return "streamingExperiments"; 130 } 131}