Example #1
0
  /**
   * Given confusion matrix, it writes it in CSV and LaTeX form to the tasks output directory, and
   * also prints evaluations (F-measure, Precision, Recall)
   *
   * @param context task context
   * @param confusionMatrix confusion matrix
   * @param filePrefix prefix of output files
   * @throws java.io.IOException
   */
  public static void writeOutputResults(
      TaskContext context, ConfusionMatrix confusionMatrix, String filePrefix) throws IOException {
    // storing the results as latex confusion matrix
    String confMatrixFileTex = (filePrefix != null ? filePrefix : "") + "confusionMatrix.tex";
    File matrixFolderTex =
        context.getFolder(Constants.TEST_TASK_OUTPUT_KEY, StorageService.AccessMode.READWRITE);
    File evaluationFileLaTeX = new File(matrixFolderTex, confMatrixFileTex);
    FileUtils.writeStringToFile(evaluationFileLaTeX, confusionMatrix.toStringLatex());

    // as CSV confusion matrix

    String confMatrixFileCsv = (filePrefix != null ? filePrefix : "") + "confusionMatrix.csv";
    File matrixFolder =
        context.getFolder(Constants.TEST_TASK_OUTPUT_KEY, StorageService.AccessMode.READWRITE);
    File evaluationFileCSV = new File(matrixFolder, confMatrixFileCsv);

    CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(evaluationFileCSV), CSVFormat.DEFAULT);
    csvPrinter.printRecords(confusionMatrix.toStringMatrix());
    IOUtils.closeQuietly(csvPrinter);

    // and results
    File evalFolder =
        context.getFolder(Constants.TEST_TASK_OUTPUT_KEY, StorageService.AccessMode.READWRITE);
    String evalFileName =
        new SVMHMMAdapter()
            .getFrameworkFilename(TCMachineLearningAdapter.AdapterNameEntries.evaluationFile);
    File evaluationFile = new File(evalFolder, evalFileName);

    PrintWriter pw = new PrintWriter(evaluationFile);
    pw.println(confusionMatrix.printNiceResults());
    pw.println(confusionMatrix.printLabelPrecRecFm());
    pw.println(confusionMatrix.printClassDistributionGold());
    IOUtils.closeQuietly(pw);
  }
 @Override
 public TaskContext getContext(String aInstanceId) {
   if (contexts.containsKey(aInstanceId)) {
     return contexts.get(aInstanceId);
   } else if (getStorageService().containsContext(aInstanceId)) {
     TaskContextMetadata metadata = getStorageService().getContext(aInstanceId);
     TaskContext ctx = createContext(metadata);
     contexts.put(ctx.getId(), ctx);
     return ctx;
   } else {
     return null;
   }
 }
  protected void resolveImports(TaskContext aContext) {
    for (Entry<String, String> e : aContext.getMetadata().getImports().entrySet()) {
      URI uri = URI.create(e.getValue());
      // Try resolving by type
      if (LATEST_CONTEXT_SCHEME.equals(uri.getScheme())
          || CONTEXT_ID_SCHEME.equals(uri.getScheme())) {
        String uuid;
        uuid = aContext.resolve(uri).getId();
        if (!getStorageService().containsKey(uuid, uri.getPath())) {
          throw new UnresolvedImportException(aContext, e.getKey(), e.getValue(), "Key not found");
        }

        String resolvedUri = CONTEXT_ID_SCHEME + "://" + uuid + uri.getPath();
        log.debug("Resolved import [" + e.getValue() + "] -> [" + resolvedUri + "]");
        e.setValue(resolvedUri);
      }
    }
  }
 public void registerContext(TaskContext aContext) {
   contexts.put(aContext.getId(), aContext);
 }
 public void unregisterContext(TaskContext aContext) {
   contexts.remove(aContext.getId());
 }