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.vis.ternary; 031 032import java.io.File; 033import java.io.FileNotFoundException; 034import java.io.FileReader; 035import java.io.IOException; 036import java.text.AttributedCharacterIterator.Attribute; 037import java.util.ArrayList; 038import java.util.HashSet; 039import java.util.List; 040import java.util.Map; 041import java.util.Map.Entry; 042import java.util.Set; 043 044import org.openimaj.image.DisplayUtilities; 045import org.openimaj.image.ImageUtilities; 046import org.openimaj.image.MBFImage; 047import org.openimaj.image.colour.ColourMap; 048import org.openimaj.image.colour.RGBColour; 049import org.openimaj.image.typography.FontStyle; 050import org.openimaj.image.typography.mathml.MathMLFont; 051import org.openimaj.image.typography.mathml.MathMLFontStyle; 052import org.openimaj.util.pair.IndependentPair; 053import org.openimaj.vis.ternary.TernaryPlot.TernaryData; 054 055import com.google.gson.Gson; 056import com.google.gson.GsonBuilder; 057import com.google.gson.JsonIOException; 058import com.google.gson.JsonSyntaxException; 059 060/** 061 * 062 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 063 */ 064public class SED2013Ternary { 065 066 public static void main(String[] args) throws JsonSyntaxException, JsonIOException, IOException { 067 final String[] fileroots = new String[] { 068 "/Users/ss/Experiments/sed2013/long_regen_weights_taken_posted_geo.div", 069 // "/Users/ss/Experiments/sed2013/long_regen_weights_taken_posted_tag.div", 070 "/Users/ss/Experiments/sed2013/long_regen_weights_desc_tags_title.div", 071 "/Users/ss/Experiments/sed2013/long_regen_weights_taken_desc_tag.div", 072 // "/Users/ss/Experiments/sed2013/long_regen_weights_geo_desc_tag.div" 073 }; 074 final boolean[] correctRange = new boolean[] { 075 true, 076 // false, 077 true, 078 true, 079 // true 080 }; 081 for (int i = 0; i < correctRange.length; i++) { 082 final String fileroot = fileroots[i]; 083 084 ternaryPlotJSONF(new File(fileroot + ".json"), new File(fileroot.replace(".", "_") + ".png"), correctRange[i]); 085 } 086 087 ternaryPlotSearchGrid(new File("/Users/ss/Experiments/sed2013/searchgrid.png")); 088 089 } 090 091 private static void ternaryPlotSearchGrid(File file) throws IOException { 092 final List<IndependentPair<TernaryData, String>> labels = new ArrayList<IndependentPair<TernaryData, String>>(); 093 final TernaryParams params = new TernaryParams(); 094 final List<TernaryData> data = new ArrayList<TernaryData>(); 095 data.add(new TernaryData(0.3f, 0.3f, 0.3f, 1f)); 096 final TernaryPlot plot = preparePlot(params, data); 097 params.put(TernaryParams.DRAW_SCALE, false); 098 099 for (int i = 0; i < 3; i++) { 100 for (int j = 0; j < 3; j++) { 101 for (int k = 0; k < 3; k++) { 102 final float sum = i + j + k; 103 if (sum == 0) 104 continue; 105 final float ip = i / sum; 106 final float jp = j / sum; 107 final float kp = k / sum; 108 final TernaryData point = new TernaryData(ip, jp, kp, 10); 109 final String label = String.format("(%2.1f,%2.1f,%2.1f)", ip, jp, kp); 110 final IndependentPair<TernaryData, String> pair = IndependentPair.pair(point, label); 111 labels.add(pair); 112 } 113 } 114 } 115 116 // labels.add(IndependentPair.pair(new TernaryData(1f,0,0,0),"f_1")); 117 // labels.add(IndependentPair.pair(new TernaryData(0,1,0,0),"f_2")); 118 // labels.add(IndependentPair.pair(new TernaryData(0,0,1,0),"f_3")); 119 params.put(TernaryParams.LABELS, labels); 120 final Map<Attribute, Object> fontAttrs = params.getTyped(TernaryParams.LABEL_FONT); 121 fontAttrs.put(MathMLFontStyle.TEXT_MODE, false); 122 fontAttrs.put(MathMLFontStyle.FONT_SIZE, 12); 123 final MBFImage draw = plot.draw(params); 124 ImageUtilities.write(draw, file); 125 DisplayUtilities.display(draw); 126 } 127 128 private static void ternaryPlotJSONF(File json, File outfile, boolean correctRange) 129 throws FileNotFoundException, IOException 130 { 131 final Gson gson = new GsonBuilder().create(); 132 @SuppressWarnings("unchecked") 133 final Map<String, Object> obj = gson.fromJson(new FileReader(json), Map.class); 134 final TernaryParams params = new TernaryParams(); 135 final List<TernaryData> data = prepareData(params, obj, correctRange); 136 final TernaryPlot plot = preparePlot(params, data); 137 final MBFImage draw = plot.draw(params); 138 ImageUtilities.write(draw, outfile); 139 DisplayUtilities.display(draw); 140 } 141 142 private static TernaryPlot preparePlot(TernaryParams params, List<TernaryData> data) { 143 final TernaryPlot plot = new TernaryPlot(500, data); 144 145 params.put(TernaryParams.COLOUR_MAP, ColourMap.Greys9); 146 params.put(TernaryParams.PADDING, 60); 147 148 params.put(TernaryParams.TRIANGLE_BORDER, true); 149 params.put(TernaryParams.TRIANGLE_BORDER_THICKNESS, 3); 150 params.put(TernaryParams.TRIANGLE_BORDER_TICKS, true); 151 params.put(TernaryParams.TRIANGLE_BORDER_COLOUR, RGBColour.WHITE); 152 Map<Attribute, Object> fontAttrs = params.getTyped(TernaryParams.LABEL_FONT); 153 fontAttrs.put(FontStyle.COLOUR, RGBColour.WHITE); 154 fontAttrs.put(FontStyle.FONT_SIZE, 20); 155 fontAttrs.put(FontStyle.FONT, new MathMLFont()); 156 fontAttrs.put(MathMLFontStyle.TEXT_MODE, true); 157 158 fontAttrs = params.getTyped(TernaryParams.SCALE_FONT); 159 fontAttrs.put(FontStyle.COLOUR, RGBColour.WHITE); 160 fontAttrs.put(FontStyle.FONT_SIZE, 16); 161 fontAttrs.put(FontStyle.FONT, new MathMLFont()); 162 163 // params.put(TernaryParams.BG_COLOUR, RGBColour.WHITE); 164 params.put(TernaryParams.BG_COLOUR, RGBColour.BLACK); 165 166 params.put(TernaryParams.DRAW_SCALE, true); 167 // params.put(TernaryParams.LABEL_BACKGROUND, RGBColour.WHITE); 168 // params.put(TernaryParams.LABEL_BORDER, RGBColour.BLACK); 169 params.put(TernaryParams.LABEL_BACKGROUND, RGBColour.BLACK); 170 params.put(TernaryParams.LABEL_BORDER, RGBColour.WHITE); 171 params.put(TernaryParams.LABEL_PADDING, 3); 172 return plot; 173 } 174 175 @SuppressWarnings("unchecked") 176 private static List<TernaryData> prepareData(TernaryParams params, Map<String, Object> obj, boolean correctRange) { 177 final List<String> exps = new ArrayList<String>(obj.keySet()); 178 final Set<String> weightedFeatures = new HashSet<String>(); 179 180 double f1min = 1; 181 double f1max = 0; 182 for (final String exp : exps) { 183 final Map<String, Object> expVars = (Map<String, Object>) obj.get(exp); 184 final Map<String, Double> expFeatureWeights = (Map<String, Double>) expVars.get("weights"); 185 for (final Entry<String, Double> fw : expFeatureWeights.entrySet()) { 186 if (fw.getValue() != 0) { 187 weightedFeatures.add(fw.getKey()); 188 } 189 } 190 final double avgf1 = ((Double) expVars.get("averageF1")); 191 f1min = Math.min(avgf1, f1min); 192 f1max = Math.max(avgf1, f1max); 193 } 194 if (correctRange) 195 f1min = f1min + (((f1max - f1min) / 8) * 7); 196 final List<String> weightedFeaturesL = new ArrayList<String>(weightedFeatures); 197 final List<TernaryData> data = new ArrayList<TernaryData>(); 198 final double f1rng = f1max - f1min; 199 final List<IndependentPair<TernaryData, String>> labels = new ArrayList<IndependentPair<TernaryData, String>>(); 200 labels.add(IndependentPair.pair(new TernaryData(1f, 0, 0, 0), correctLabel(weightedFeaturesL.get(0)))); 201 labels.add(IndependentPair.pair(new TernaryData(0, 1, 0, 0), correctLabel(weightedFeaturesL.get(1)))); 202 labels.add(IndependentPair.pair(new TernaryData(0, 0, 1, 0), correctLabel(weightedFeaturesL.get(2)))); 203 float maxa = 0, maxb = 0, maxc = 0; 204 // double maxv = 0; 205 for (final String exp : exps) { 206 final Map<String, Object> expmap = (Map<String, Object>) obj.get(exp); 207 final Map<String, Double> weights = (Map<String, Double>) ((Map<String, Object>) obj.get(exp)).get("weights"); 208 final double a = weights.get(weightedFeaturesL.get(0)); 209 final double b = weights.get(weightedFeaturesL.get(1)); 210 final double c = weights.get(weightedFeaturesL.get(2)); 211 final double af1 = (Double) expmap.get("averageF1"); 212 float value = (float) ((af1 - f1min) / f1rng); 213 value = Math.max(value, 0); 214 if (value >= af1) { 215 // maxv = af1; 216 maxa = (float) a; 217 maxb = (float) b; 218 maxc = (float) c; 219 } 220 data.add(new TernaryData((float) a, (float) b, (float) c, value)); 221 } 222 labels.add(IndependentPair.pair(new TernaryData(maxa, maxb, maxc, 5), String.format("max_{F_1}(%2.2f)", f1max))); 223 params.put(TernaryParams.LABELS, labels); 224 params.put(TernaryParams.SCALE_MIN, String.format("\\leq div_{F_1}(%2.2f)", f1min)); 225 params.put(TernaryParams.SCALE_MAX, String.format("div_{F_1}(%2.2f)", f1max)); 226 return data; 227 } 228 229 private static String correctLabel(String string) { 230 if (string.contains("Geo")) 231 return "Geo"; 232 if (string.contains("TAKEN")) 233 return "TimeTaken"; 234 if (string.contains("POSTED")) 235 return "TimePosted"; 236 if (string.contains("Title")) 237 return "Title"; 238 if (string.contains("Tag")) 239 return "Tags"; 240 if (string.contains("Descrip")) 241 return "Desc"; 242 return string; 243 } 244 245}