<commit_before>#include "Session.h"

#include <string>

#include <SmurffCpp/Version.h>

#include <SmurffCpp/Utils/omp_util.h>
#include <SmurffCpp/Utils/Distribution.h>
#include <SmurffCpp/Utils/MatrixUtils.h>

#include <SmurffCpp/DataMatrices/MatrixDataFactory.h>
#include <SmurffCpp/Priors/PriorFactory.h>

#include <SmurffCpp/result.h>

using namespace smurff;

void Session::setFromConfig(const Config& cfg)
{
   // assign config

   cfg.validate(true);
   cfg.save(cfg.getSavePrefix() + ".ini");
   config = cfg;

   std::shared_ptr<Session> this_session = shared_from_this();

   // initialize pred

   if (config.classify)
      m_pred->setThreshold(config.threshold);

   m_pred->set(config.test);

   // initialize data

   data_ptr = MatrixDataFactory::create_matrix(this_session);

   // check if data is ScarceBinary
   /*
   if (0)
      if (!config.classify) {
            config.classify = true;
            config.threshold = 0.5;
            config.train.noise.name = "probit";
      }
   */

   // center mode

   data_ptr->setCenterMode(config.center_mode_type);

   // initialize priors

   this->addPrior(PriorFactory::create_prior(this_session, 0));

   this->addPrior(PriorFactory::create_prior(this_session, 1));
}

void Session::init()
{
   //init omp
   threads_init();

   //init random generator
   if(config.random_seed_set)
      init_bmrng(config.random_seed);
   else
      init_bmrng();

   //initialize train matrix (centring and noise model)
   data()->init();

   //initialize model (samples)
   m_model->init(config.num_latent, data()->dim(), config.model_init_type);

   //initialize priors (?)
   for( auto &p : m_priors)
      p->init();

   //write header to status file
   if (config.csv_status.size())
   {
      auto f = fopen(config.csv_status.c_str(), "w");
      fprintf(f, "phase;iter;phase_len;globmean_rmse;colmean_rmse;rmse_avg;rmse_1samp;train_rmse;auc_avg;auc_1samp;U0;U1;elapsed\n");
      fclose(f);
   }

   //write info to console
   if (config.verbose)
      info(std::cout, "");

   //restore session (model, priors)
   if (config.restore_prefix.size())
   {
      if (config.verbose)
         printf("-- Restoring model, predictions,... from '%s*%s'.\n", config.restore_prefix.c_str(), config.save_suffix.c_str());
      restore(config.restore_prefix, config.restore_suffix);
   }

   //print session status to console
   if (config.verbose)
   {
      printStatus(0);
      printf(" ====== Sampling (burning phase) ====== \n");
   }

   iter = 0;
   is_init = true;
}

void Session::run()
{
   init();
   while (iter < config.burnin + config.nsamples)
      step();
}

void Session::step()
{
   assert(is_init);

   if (config.verbose && iter == config.burnin)
   {
      printf(" ====== Burn-in complete, averaging samples ====== \n");
   }

   auto starti = tick();
   BaseSession::step();
   auto endi = tick();

   printStatus(endi - starti);
   save(iter - config.burnin + 1);
   iter++;
}

std::ostream& Session::info(std::ostream &os, std::string indent)
{
   BaseSession::info(os, indent);
   os << indent << "  Version: " << smurff::SMURFF_VERSION << "\n" ;
   os << indent << "  Iterations: " << config.burnin << " burnin + " << config.nsamples << " samples\n";

   if (config.save_freq != 0)
   {
      if (config.save_freq > 0) {
          os << indent << "  Save model: every " << config.save_freq << " iteration\n";
      } else {
          os << indent << "  Save model after last iteration\n";
      }
      os << indent << "  Save prefix: " << config.getSavePrefix() << "\n";
      os << indent << "  Save suffix: " << config.save_suffix << "\n";
   }
   else
   {
      os << indent << "  Save model: never\n";
   }

   if (config.restore_prefix.size())
   {
      os << indent << "  Restore prefix: " << config.restore_prefix << "\n";
      os << indent << "  Restore suffix: " << config.restore_suffix << "\n";
   }

   os << indent << "}\n";
   return os;
}

void Session::save(int isample)
{
   if (!config.save_freq || isample < 0) //do not save if (never save) mode is selected or if burnin
      return;

   //save_freq > 0: check modulo
   if (config.save_freq > 0 && ((isample + 1) % config.save_freq) != 0) //do not save if not a save iteration
      return;

   //save_freq < 0: save last iter
   if (config.save_freq < 0 && isample < config.nsamples) //do not save if (final model) mode is selected and not a final iteration
      return;

   std::string fprefix = config.getSavePrefix() + "-sample-" + std::to_string(isample);

   if (config.verbose)
      printf("-- Saving model, predictions,... into '%s*%s'.\n", fprefix.c_str(), config.save_suffix.c_str());

   BaseSession::save(fprefix, config.save_suffix);
}

void Session::printStatus(double elapsedi)
{
   m_pred->update(m_model, data(), iter < config.burnin);
<commit_msg>move m_pred->update to session::step<commit_after>#include "Session.h"

#include <string>

#include <SmurffCpp/Version.h>

#include <SmurffCpp/Utils/omp_util.h>
#include <SmurffCpp/Utils/Distribution.h>
#include <SmurffCpp/Utils/MatrixUtils.h>

#include <SmurffCpp/DataMatrices/MatrixDataFactory.h>
#include <SmurffCpp/Priors/PriorFactory.h>

#include <SmurffCpp/result.h>

using namespace smurff;

void Session::setFromConfig(const Config& cfg)
{
   // assign config

   cfg.validate(true);
   cfg.save(cfg.getSavePrefix() + ".ini");
   config = cfg;

   std::shared_ptr<Session> this_session = shared_from_this();

   // initialize pred

   if (config.classify)
      m_pred->setThreshold(config.threshold);

   m_pred->set(config.test);

   // initialize data

   data_ptr = MatrixDataFactory::create_matrix(this_session);

   // check if data is ScarceBinary
   /*
   if (0)
      if (!config.classify) {
            config.classify = true;
            config.threshold = 0.5;
            config.train.noise.name = "probit";
      }
   */

   // center mode

   data_ptr->setCenterMode(config.center_mode_type);

   // initialize priors

   this->addPrior(PriorFactory::create_prior(this_session, 0));

   this->addPrior(PriorFactory::create_prior(this_session, 1));
}

void Session::init()
{
   //init omp
   threads_init();

   //init random generator
   if(config.random_seed_set)
      init_bmrng(config.random_seed);
   else
      init_bmrng();

   //initialize train matrix (centring and noise model)
   data()->init();

   //initialize model (samples)
   m_model->init(config.num_latent, data()->dim(), config.model_init_type);

   //initialize priors (?)
   for( auto &p : m_priors)
      p->init();

   //write header to status file
   if (config.csv_status.size())
   {
      auto f = fopen(config.csv_status.c_str(), "w");
      fprintf(f, "phase;iter;phase_len;globmean_rmse;colmean_rmse;rmse_avg;rmse_1samp;train_rmse;auc_avg;auc_1samp;U0;U1;elapsed\n");
      fclose(f);
   }

   //write info to console
   if (config.verbose)
      info(std::cout, "");

   //restore session (model, priors)
   if (config.restore_prefix.size())
   {
      if (config.verbose)
         printf("-- Restoring model, predictions,... from '%s*%s'.\n", config.restore_prefix.c_str(), config.save_suffix.c_str());
      restore(config.restore_prefix, config.restore_suffix);
   }

   //print session status to console
   if (config.verbose)
   {
      printStatus(0);
      printf(" ====== Sampling (burning phase) ====== \n");
   }

   iter = 0;
   is_init = true;
}

void Session::run()
{
   init();
   while (iter < config.burnin + config.nsamples)
      step();
}

void Session::step()
{
   assert(is_init);

   if (config.verbose && iter == config.burnin)
   {
      printf(" ====== Burn-in complete, averaging samples ====== \n");
   }

   auto starti = tick();
   BaseSession::step();
   auto endi = tick();

   //WARNING: update is an expensive operation because of sort (when calculating AUC)
   m_pred->update(m_model, data(), iter < config.burnin);

   printStatus(endi - starti);
   save(iter - config.burnin + 1);
   iter++;
}

std::ostream& Session::info(std::ostream &os, std::string indent)
{
   BaseSession::info(os, indent);
   os << indent << "  Version: " << smurff::SMURFF_VERSION << "\n" ;
   os << indent << "  Iterations: " << config.burnin << " burnin + " << config.nsamples << " samples\n";

   if (config.save_freq != 0)
   {
      if (config.save_freq > 0) {
          os << indent << "  Save model: every " << config.save_freq << " iteration\n";
      } else {
          os << indent << "  Save model after last iteration\n";
      }
      os << indent << "  Save prefix: " << config.getSavePrefix() << "\n";
      os << indent << "  Save suffix: " << config.save_suffix << "\n";
   }
   else
   {
      os << indent << "  Save model: never\n";
   }

   if (config.restore_prefix.size())
   {
      os << indent << "  Restore prefix: " << config.restore_prefix << "\n";
      os << indent << "  Restore suffix: " << config.restore_suffix << "\n";
   }

   os << indent << "}\n";
   return os;
}

void Session::save(int isample)
{
   if (!config.save_freq || isample < 0) //do not save if (never save) mode is selected or if burnin
      return;

   //save_freq > 0: check modulo
   if (config.save_freq > 0 && ((isample + 1) % config.save_freq) != 0) //do not save if not a save iteration
      return;

   //save_freq < 0: save last iter
   if (config.save_freq < 0 && isample < config.nsamples) //do not save if (final model) mode is selected and not a final iteration
      return;

   std::string fprefix = config.getSavePrefix() + "-sample-" + std::to_string(isample);

   if (config.verbose)
      printf("-- Saving model, predictions,... into '%s*%s'.\n", fprefix.c_str(), config.save_suffix.c_str());

   BaseSession::save(fprefix, config.save_suffix);
}

void Session::printStatus(double elapsedi)
{
{'fraction_non_alphanumeric': 0.16145566376217324, 'fraction_numerical': 0.004305484366991286, 'mean_word_length': 3.034739454094293, 'pattern_counts': {'":': 0, '<': 177, '<?xml version=': 0, '>': 48, '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': '4546002', 'n_tokens_mistral': 3394, 'n_tokens_neox': 3262, 'n_words': 731}