<commit_before>#include <cstdio>
#include <fstream>

#include <boost/filesystem/operations.hpp>

#include "catch.hpp"

#include <SmurffCpp/Types.h>

#include <SmurffCpp/Configs/Config.h>
#include <SmurffCpp/Sessions/TrainSession.h>
#include <SmurffCpp/Predict/PredictSession.h>
#include <SmurffCpp/Utils/MatrixUtils.h>
#include <SmurffCpp/Utils/StateFile.h>
#include <SmurffCpp/result.h>

#include "Tests.h"

#define TAG_MATRIX_TESTS "[matrix][random]"

namespace fs = boost::filesystem;

namespace smurff {
namespace test {

static Config& prepareResultDir(Config &config, const std::string &dir)
{
  fs::path output_dir("tests_output");
  fs::create_directory(output_dir);

  std::string save_dir(dir);
  save_dir.erase(std::remove_if(save_dir.begin(), save_dir.end(),
                           [](char c) {
                              const std::string special_chars("\\/:*\"<>|");
                              return special_chars.find(c) != std::string::npos;
                           }
            ), save_dir.end());
  
  output_dir /= fs::path(save_dir);
  fs::path output_filename = output_dir / "output.h5";
 
  config.setSaveFreq(1);
  config.setSaveName(output_filename.string());
  fs::remove_all(output_dir);
  fs::create_directory(output_dir);
  return config;
}

TEST_CASE("OverwriteHDF5")
{
  Config config = genConfig(trainDenseMatrix, testSparseMatrix, {PriorTypes::normal, PriorTypes::normal});
  prepareResultDir(config, Catch::getResultCapture().getCurrentTestName() + "_train");

  // this one should work just fine
  TrainSession(config).run();

  // the second run should complain -- trying to create th HDF5 file that already exists
  TrainSession(config).run();
}

TEST_CASE("PredictSession/BPMF")
{
  Config config = genConfig(trainDenseMatrix, testSparseMatrix, {PriorTypes::normal, PriorTypes::normal});
  prepareResultDir(config, Catch::getResultCapture().getCurrentTestName() + "_train");
  std::string model_file = config.getSaveName();

  std::shared_ptr<ISession> trainSession = std::make_shared<TrainSession>(config);
  trainSession->run();

  // std::cout << "Prediction from TrainSession RMSE: " << trainSession->getRmseAvg() <<
  // std::endl;

  {
    prepareResultDir(config, Catch::getResultCapture().getCurrentTestName() + "_predict");
    PredictSession s(model_file); // restore from file just generated by TrainSession

    // test predict from TensorConfig
    auto result = s.predict(config.getTest());

    // std::cout << "Prediction from StateFile RMSE: " << result->rmse_avg <<
    // std::endl;
    checkValue(trainSession->getRmseAvg(), result->rmse_avg, rmse_epsilon);
  }
}

//=================================================================
<commit_msg>ENH: TensorPredict test<commit_after>#include <cstdio>
#include <fstream>

#include <boost/filesystem/operations.hpp>

#include "catch.hpp"

#include <SmurffCpp/Types.h>

#include <SmurffCpp/Configs/Config.h>
#include <SmurffCpp/Sessions/TrainSession.h>
#include <SmurffCpp/Predict/PredictSession.h>
#include <SmurffCpp/Utils/MatrixUtils.h>
#include <SmurffCpp/Utils/StateFile.h>
#include <SmurffCpp/result.h>

#include "Tests.h"

#define TAG_MATRIX_TESTS "[matrix][random]"

namespace fs = boost::filesystem;

namespace smurff {
namespace test {

static Config& prepareResultDir(Config &config, const std::string &dir)
{
  fs::path output_dir("tests_output");
  fs::create_directory(output_dir);

  std::string save_dir(dir);
  save_dir.erase(std::remove_if(save_dir.begin(), save_dir.end(),
                           [](char c) {
                              const std::string special_chars("\\/:*\"<>|");
                              return special_chars.find(c) != std::string::npos;
                           }
            ), save_dir.end());
  
  output_dir /= fs::path(save_dir);
  fs::path output_filename = output_dir / "output.h5";
 
  config.setSaveFreq(1);
  config.setSaveName(output_filename.string());
  fs::remove_all(output_dir);
  fs::create_directory(output_dir);
  return config;
}

TEST_CASE("OverwriteHDF5")
{
  Config config = genConfig(trainDenseMatrix, testSparseMatrix, {PriorTypes::normal, PriorTypes::normal});
  prepareResultDir(config, Catch::getResultCapture().getCurrentTestName() + "_train");

  // this one should work just fine
  TrainSession(config).run();

  // the second run should complain -- trying to create th HDF5 file that already exists
  TrainSession(config).run();
}

TEST_CASE("PredictSession/BPMF")
{
  Config config = genConfig(trainDenseMatrix, testSparseMatrix, {PriorTypes::normal, PriorTypes::normal});
  prepareResultDir(config, Catch::getResultCapture().getCurrentTestName() + "_train");
  std::string model_file = config.getSaveName();

  std::shared_ptr<ISession> trainSession = std::make_shared<TrainSession>(config);
  trainSession->run();

  // std::cout << "Prediction from TrainSession RMSE: " << trainSession->getRmseAvg() <<
  // std::endl;

  {
    prepareResultDir(config, Catch::getResultCapture().getCurrentTestName() + "_predict");
    PredictSession s(model_file); // restore from file just generated by TrainSession

    // test predict from TensorConfig
    auto result = s.predict(config.getTest());

    // std::cout << "Prediction from StateFile RMSE: " << result->rmse_avg <<
    // std::endl;
    checkValue(trainSession->getRmseAvg(), result->rmse_avg, rmse_epsilon);
  }
}


TEST_CASE("TrainSession/TensorBPMF")
{
  Config config = genConfig(trainSparseTensor3d, testSparseTensor3d, {PriorTypes::normal, PriorTypes::normal, PriorTypes::normal});
  prepareResultDir(config, Catch::getResultCapture().getCurrentTestName() + "_train");
  std::string model_file = config.getSaveName();

  std::shared_ptr<ISession> trainSession = std::make_shared<TrainSession>(config);
  trainSession->run();

  // std::cout << "Prediction from TrainSession RMSE: " << trainSession->getRmseAvg() <<
  // std::endl;

  {
    prepareResultDir(config, Catch::getResultCapture().getCurrentTestName() + "_predict");
    PredictSession s(model_file); // restore from file just generated by TrainSession

    // test predict from TensorConfig
    auto result = s.predict(config.getTest());

    // std::cout << "Prediction from StateFile RMSE: " << result->rmse_avg <<
    // std::endl;
    checkValue(trainSession->getRmseAvg(), result->rmse_avg, rmse_epsilon);
  }
}

//=================================================================

{'fraction_non_alphanumeric': 0.16404179011383127, 'fraction_numerical': 0.001559332605644784, 'mean_word_length': 4.524547803617571, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5971325', 'n_tokens_mistral': 2038, 'n_tokens_neox': 2003, 'n_words': 364}