<commit_before>/*
 * Copyright 2016, Simula Research Laboratory
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#include <algorithm>
#include "Regression.h"

static void RemoveAllFiles(const boost::filesystem::path& dirPath);
static std::vector<boost::filesystem::path> CollectFiles(const boost::filesystem::path& dirPath);
static bool SortTags(FrameLog& log);

/////////////////////////////////////////////////////////////////////////////

TestRunner::TestRunner(const std::string& inputDir, const std::string& outputDir, boost::optional<bool> useCuda) :
  _inputDirPath(inputDir), _outputDirPath(outputDir), _useCuda(useCuda)
{
  if (!exists(_inputDirPath) || !is_directory(_inputDirPath))
    throw std::runtime_error("TestRunner: inputDir is not a directory");
  if (!exists(_outputDirPath) || !is_directory(_outputDirPath))
    throw std::runtime_error("TestRunner: outputDir is not a directory");
  RemoveAllFiles(_outputDirPath);
  _inputFilePaths = CollectFiles(_inputDirPath);
}

void TestRunner::adjustParameters(cctag::Parameters& parameters)
{
  if (_useCuda)
    parameters._useCuda = *_useCuda;
}

// Input directory must contain images.
// NB! parameters is by-val since we may need to adjust them.
void TestRunner::generateReferenceResults(cctag::Parameters parameters)
{
  adjustParameters(parameters);
  size_t i = 1, count = _inputFilePaths.size();
  for (const auto& inputFilePath: _inputFilePaths) {
    std::clog << "Processing file " << i++ << "/" << count << ": " << inputFilePath << std::endl;
    FileLog fileLog = FileLog::detect(inputFilePath.native(), parameters);
    auto outputPath = _outputDirPath / inputFilePath.filename().replace_extension(".xml");
    fileLog.save(outputPath.native());
  }
}

// Input directory must contain XML files; parameters and input file will be read from those.
void TestRunner::generateTestResults()
{
  size_t i = 1, count = _inputFilePaths.size();
  for (const auto& inputFilePath: _inputFilePaths)
  if (inputFilePath.extension() == ".xml") {
    std::clog << "Processing file " << i++ << "/" << count << ": " << inputFilePath << std::endl;
    FileLog fileLog;
    fileLog.load(inputFilePath.native());
    adjustParameters(fileLog.parameters);
    fileLog = FileLog::detect(fileLog.filename, fileLog.parameters);
    auto outputPath = _outputDirPath / inputFilePath.filename();
    fileLog.save(outputPath.native());
  }
}

/////////////////////////////////////////////////////////////////////////////
// TestChecker assumption: all IDs in the frame are different.

TestChecker::TestChecker(const std::string& referenceDir, const std::string& testDir, float epsilon) :
  _referenceDirPath(referenceDir), _testDirPath(testDir), _epsilon(epsilon), _failed(false)
{
  if (!exists(_referenceDirPath) || !is_directory(_referenceDirPath))
    throw std::runtime_error("TestChecker: referenceDir is not a directory");
  if (!exists(_testDirPath) || !is_directory(_testDirPath))
    throw std::runtime_error("TestChecker: testDir is not a directory");
  _referenceFilePaths = CollectFiles(_referenceDirPath);
  _testFilePaths = CollectFiles(_testDirPath);
}

boost::filesystem::path TestChecker::testToReferencePath(const boost::filesystem::path& testPath)
{
  const auto testFilename = testPath.filename();
  auto it = std::find_if(_referenceFilePaths.begin(), _referenceFilePaths.end(),
    [testFilename](const boost::filesystem::path& p) { return p.filename() == testFilename; });
  return it != _referenceFilePaths.end() ? *it : boost::filesystem::path();
}

bool TestChecker::check()
{
  size_t i = 1, count = _testFilePaths.size();
  for (const auto& testFilePath: _testFilePaths) {
    std::clog << "Processing file " << i++ << "/" << count << ": " << testFilePath << std::endl;
    try {
      check(testFilePath);
    }
    catch (check_error& e) {
      std::clog << "  FAILED: " << e.what() << std::endl;
      _failed = true;
    }
  }
  return !_failed;
}

void TestChecker::check(const boost::filesystem::path& testFilePath)
{
  auto referenceFilePath = testToReferencePath(testFilePath);
  if (referenceFilePath.empty())
    throw check_error("reference file not found");

  FileLog referenceLog, testLog;
  referenceLog.load(referenceFilePath.native());
  testLog.load(testFilePath.native());
  compare(referenceLog, testLog);
}
<commit_msg>[applications] using string() instead of native()<commit_after>/*
 * Copyright 2016, Simula Research Laboratory
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#include <algorithm>
#include "Regression.h"

static void RemoveAllFiles(const boost::filesystem::path& dirPath);
static std::vector<boost::filesystem::path> CollectFiles(const boost::filesystem::path& dirPath);
static bool SortTags(FrameLog& log);

/////////////////////////////////////////////////////////////////////////////

TestRunner::TestRunner(const std::string& inputDir, const std::string& outputDir, boost::optional<bool> useCuda) :
  _inputDirPath(inputDir), _outputDirPath(outputDir), _useCuda(useCuda)
{
  if (!exists(_inputDirPath) || !is_directory(_inputDirPath))
    throw std::runtime_error("TestRunner: inputDir is not a directory");
  if (!exists(_outputDirPath) || !is_directory(_outputDirPath))
    throw std::runtime_error("TestRunner: outputDir is not a directory");
  RemoveAllFiles(_outputDirPath);
  _inputFilePaths = CollectFiles(_inputDirPath);
}

void TestRunner::adjustParameters(cctag::Parameters& parameters)
{
  if (_useCuda)
    parameters._useCuda = *_useCuda;
}

// Input directory must contain images.
// NB! parameters is by-val since we may need to adjust them.
void TestRunner::generateReferenceResults(cctag::Parameters parameters)
{
  adjustParameters(parameters);
  size_t i = 1, count = _inputFilePaths.size();
  for (const auto& inputFilePath: _inputFilePaths) {
    std::clog << "Processing file " << i++ << "/" << count << ": " << inputFilePath << std::endl;
    FileLog fileLog = FileLog::detect(inputFilePath.string(), parameters);
    const auto outputPath = _outputDirPath / inputFilePath.filename().replace_extension(".xml");
    fileLog.save(outputPath.string());
  }
}

// Input directory must contain XML files; parameters and input file will be read from those.
void TestRunner::generateTestResults()
{
  size_t i = 1, count = _inputFilePaths.size();
  for (const auto& inputFilePath: _inputFilePaths)
  if (inputFilePath.extension() == ".xml") {
    std::clog << "Processing file " << i++ << "/" << count << ": " << inputFilePath << std::endl;
    FileLog fileLog;
    fileLog.load(inputFilePath.string());
    adjustParameters(fileLog.parameters);
    fileLog = FileLog::detect(fileLog.filename, fileLog.parameters);
    auto outputPath = _outputDirPath / inputFilePath.filename();
    fileLog.save(outputPath.string());
  }
}

/////////////////////////////////////////////////////////////////////////////
// TestChecker assumption: all IDs in the frame are different.

TestChecker::TestChecker(const std::string& referenceDir, const std::string& testDir, float epsilon) :
  _referenceDirPath(referenceDir), _testDirPath(testDir), _epsilon(epsilon), _failed(false)
{
  if (!exists(_referenceDirPath) || !is_directory(_referenceDirPath))
    throw std::runtime_error("TestChecker: referenceDir is not a directory");
  if (!exists(_testDirPath) || !is_directory(_testDirPath))
    throw std::runtime_error("TestChecker: testDir is not a directory");
  _referenceFilePaths = CollectFiles(_referenceDirPath);
  _testFilePaths = CollectFiles(_testDirPath);
}

boost::filesystem::path TestChecker::testToReferencePath(const boost::filesystem::path& testPath)
{
  const auto testFilename = testPath.filename();
  auto it = std::find_if(_referenceFilePaths.begin(), _referenceFilePaths.end(),
    [testFilename](const boost::filesystem::path& p) { return p.filename() == testFilename; });
  return it != _referenceFilePaths.end() ? *it : boost::filesystem::path();
}

bool TestChecker::check()
{
  size_t i = 1, count = _testFilePaths.size();
  for (const auto& testFilePath: _testFilePaths) {
    std::clog << "Processing file " << i++ << "/" << count << ": " << testFilePath << std::endl;
    try {
      check(testFilePath);
    }
    catch (check_error& e) {
      std::clog << "  FAILED: " << e.what() << std::endl;
      _failed = true;
    }
  }
  return !_failed;
}

void TestChecker::check(const boost::filesystem::path& testFilePath)
{
  auto referenceFilePath = testToReferencePath(testFilePath);
  if (referenceFilePath.empty())
    throw check_error("reference file not found");

  FileLog referenceLog, testLog;
  referenceLog.load(referenceFilePath.string());
  testLog.load(testFilePath.string());
  compare(referenceLog, testLog);
}

{'fraction_non_alphanumeric': 0.1675183292601644, 'fraction_numerical': 0.002443901355254388, 'mean_word_length': 4.374925373134328, 'pattern_counts': {'":': 6, '<': 105, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6635436', 'n_tokens_mistral': 2596, 'n_tokens_neox': 2578, 'n_words': 636}