package ie.dcu.apps.ist.exp; import ie.dcu.eval.Evaluator; import ie.dcu.apps.ist.*; import ie.dcu.segment.Segmenter; import java.io.*; import java.util.*; public class Experiment { public static final int MAX_TASKS = 2500; public static final int MAX_SEGMENTERS = 50; public static final int MAX_EVALUATORS = 100; private String name; private File dir; private File outputFile; private int time; private List segmenters; private List evaluators; private List tasks; private StorageSelection saveUserMasks; private File saveUserMasksDir; Experiment() { } public String getName() { return name; } public File getDirectory() { return dir; } public File getOutputFile() { return outputFile; } public int getTime() { return time; } public StorageSelection getStorageSelection() { return saveUserMasks; } public File getStorageDirectory() { return saveUserMasksDir; } public List getSegmenters() { return Collections.unmodifiableList(segmenters); } public List getEvaluators() { return Collections.unmodifiableList(evaluators); } public List getTasks() { return Collections.unmodifiableList(tasks); } public boolean using(Segmenter segmenter) { for (Segmenter s : segmenters) { if (s.equals(segmenter)) { return true; } } return false; } public void load(File file) throws IOException, FormatException { InputStream in = null; try { in = new FileInputStream(file); load(in); } finally { if (in != null) { in.close(); } } } public void load(InputStream in) throws IOException, FormatException { // Create properties object Properties props = new Properties(); props.load(in); // Load name name = props.getProperty("name", "Experiment"); // Load directory dir = getDir(props, "dir"); // Load output file outputFile = getOutputFile(props); // Load task time time = getTimeout(props, "time"); // Get save user masks selection saveUserMasks = getStorageSelection(props, "save-user-masks"); // .. and directory saveUserMasksDir = getUserMaskDir(props, "save-user-masks.dir"); // Load segmenters segmenters = new ArrayList(); for (int i = 1; i <= MAX_SEGMENTERS; i++) { String key = "segmenters." + i; String val = props.getProperty(key); if (val == null) { break; } Segmenter segmenter = findSegmenter(val); if (segmenter != null) { segmenters.add(segmenter); } else { exception("Segmenter not found: <%s>", val); } } // Load evaluators evaluators = new ArrayList(); for (int i = 1; i <= MAX_EVALUATORS; i++) { String key = "evaluators." + i; String val = props.getProperty(key); if (val == null) { break; } Evaluator evaluator = findEvaluator(val); if (evaluator != null) { evaluators.add(evaluator); } else { exception("Evaluator not found: <%s>", val); } } // Load tasks tasks = new ArrayList(); for (int i = 1; i <= MAX_TASKS; i++) { // Form keys String imkey = String.format("task.%d.im", i); String gtkey = String.format("task.%d.gt", i); String desckey = String.format("task.%d.description", i); // Check if we're done if (props.getProperty(imkey) == null) { break; } // Get image and ground truth file File im = getFile(props, imkey); File gt = getFile(props, gtkey); // Get description String desc = getDescription(props, desckey); // Add task tasks.add(new Task(im, gt, desc)); } } private static StorageSelection getStorageSelection( Properties props, String key ) { String prop = props.getProperty(key); if (prop != null) { StorageSelection selection = StorageSelection.parse(prop); if (selection != null) { return selection; } } return StorageSelection.None; } private File getUserMaskDir(Properties props, String key) throws FormatException { String fname = props.getProperty(key); if (fname == null) { fname = "user"; } File file = new File(fname); if (!file.isAbsolute()) { file = new File(dir, fname); } if (!file.exists()) { // Try and create it if (!file.mkdirs()) { String path = file.getAbsolutePath(); exception("Couldn't create directory %s", path); } } else if (!file.isDirectory()) { String path = file.getAbsolutePath(); exception("%s is not a directory", path); } return file; } private static Segmenter findSegmenter(String name) { return SegmenterRegistry.getInstance().find(name); } private static Evaluator findEvaluator(String name) { return EvaluatorRegistry.getInstance().find(name); } private File getOutputFile(Properties props) { String fname = props.getProperty("output", "output.txt"); File file = new File(fname); if (file.isAbsolute()) { return file; } return new File(dir, fname); } private static String getDescription(Properties props, String key) throws FormatException { String desc = props.getProperty(key); if (desc == null) { exception("Expected description property for %s", key); } return desc; } private File getFile(Properties props, String key) throws FormatException { String fname = props.getProperty(key); if (fname == null) { exception("Expected file property for %s", key); } File file = new File(fname); if (file.isAbsolute()) { if (file.isFile()) { return file; } } file = new File(dir, fname); if (!file.isFile()) { exception("File not found %s", file.getAbsolutePath()); } return file; } private static File getDir(Properties props, String key) throws FormatException { String prop = props.getProperty(key); if (prop == null) { exception("Required directory element not found: %a",key); } File file = new File(prop); if (!file.isDirectory()) { exception("%s is not a directory", prop); } return file; } private static int getTimeout(Properties props, String key) throws FormatException { String prop = props.getProperty(key, "120"); try { return Integer.parseInt(prop); } catch (NumberFormatException ex) { exception("%s is not an integer", key); } return 0; } private static void exception(String format, Object ... args) throws FormatException { throw new FormatException(String.format(format, args)); } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("name=<").append(name).append('>'); sb.append(" dir=<").append(dir.getAbsolutePath()).append('>'); sb.append(" output=<").append(outputFile.getAbsolutePath()).append('>'); sb.append(" time=<").append(time).append('>'); // Evaluators sb.append(" evaluators={ "); for (Evaluator e : evaluators) { sb.append(e.getName()).append(' '); } sb.append('}'); // Tasks sb.append(" tasks={ "); for (Task t : tasks) { sb.append(t).append(' '); } sb.append('}'); return sb.toString(); } }