<commit_before>
#include "clustering.hpp"

#include <omp.h>

#include "tools.hpp"
#include "density_clustering.hpp"
#include "mpp.hpp"
#include "logger.hpp"

template <typename NUM>
std::vector<NUM>
read_single_column(std::string filename) {
  std::vector<NUM> dat;
  std::ifstream ifs(filename);
  if (ifs.fail()) {
    std::cerr << "error: cannot open file '" << filename << "'" << std::endl;
    exit(EXIT_FAILURE);
  } else {
    while (ifs.good()) {
      NUM buf;
      ifs >> buf;
      if ( ! ifs.fail()) {
        dat.push_back(buf);
      }
    }
  }
  return dat;
}

template <typename NUM>
void
write_single_column(std::string filename, std::vector<NUM> dat) {
  std::ofstream ofs(filename);
  if (ofs.fail()) {
    std::cerr << "error: cannot open file '" << filename << "' for writing." << std::endl;
    exit(EXIT_FAILURE);
  }
  for (NUM i: dat) {
    ofs << i << "\n";
  }
}

std::vector<std::size_t>
read_clustered_trajectory(std::string filename) {
  return read_single_column<std::size_t>(filename);
}

std::vector<float>
read_free_energies(std::string filename) {
  return read_single_column<float>(filename);
}

template <typename KEY, typename VAL>
void
write_map(std::string filename, std::map<KEY, VAL> mapping) {
  std::ofstream ofs(filename);
  if (ofs.fail()) {
    std::cerr << "error: cannot open file '" << filename << "' for writing." << std::endl;
    exit(EXIT_FAILURE);
  }
  for (auto key_val: mapping) {
    ofs << key_val.first << " " << key_val.second << "\n";
  }
}

std::map<std::size_t, std::size_t>
microstate_populations(std::vector<std::size_t> traj) {
  std::map<std::size_t, std::size_t> populations;
  for (std::size_t state: traj) {
    if (populations.count(state) == 0) {
      populations[state] = 1;
    } else {
      ++populations[state];
    }
  }
  return populations;
}

void density_main(boost::program_options::variables_map args) {
  using namespace Clustering::Density;
  const std::string input_file = args["file"].as<std::string>();
  const float radius = args["radius"].as<float>();
  // setup coords
  float* coords;
  std::size_t n_rows;
  std::size_t n_cols;
  Clustering::logger(std::cout) << "reading coords" << std::endl;
  std::tie(coords, n_rows, n_cols) = read_coords<float>(input_file);
  #ifdef DC_USE_OPENCL
  DC_OpenCL::setup(coords, n_rows, n_cols);
  #endif
  //// free energies
  std::vector<float> free_energies;
  if (args.count("free-energy-input")) {
    Clustering::logger(std::cout) << "re-using free energy data." << std::endl;
    free_energies = read_free_energies(args["free-energy-input"].as<std::string>());
  } else if (args.count("free-energy") || args.count("output")) {
    Clustering::logger(std::cout) << "calculating free energies" << std::endl;
    #ifdef DC_USE_OPENCL
    free_energies = calculate_free_energies(
                      DC_OpenCL::calculate_populations(radius));
    #else
    free_energies = calculate_free_energies(
                      calculate_populations(coords, n_rows, n_cols, radius));
    #endif
    if (args.count("free-energy")) {
      std::ofstream ofs(args["free-energy"].as<std::string>());
      ofs << std::scientific;
      for (float f: free_energies) {
        ofs << f << "\n";
      }
    }
  }
  //// nearest neighbors
  Neighborhood nh;
  Neighborhood nh_high_dens;<commit_msg>now populations are really written to file<commit_after>
#include "clustering.hpp"

#include <omp.h>

#include "tools.hpp"
#include "density_clustering.hpp"
#include "mpp.hpp"
#include "logger.hpp"

template <typename NUM>
std::vector<NUM>
read_single_column(std::string filename) {
  std::vector<NUM> dat;
  std::ifstream ifs(filename);
  if (ifs.fail()) {
    std::cerr << "error: cannot open file '" << filename << "'" << std::endl;
    exit(EXIT_FAILURE);
  } else {
    while (ifs.good()) {
      NUM buf;
      ifs >> buf;
      if ( ! ifs.fail()) {
        dat.push_back(buf);
      }
    }
  }
  return dat;
}

template <typename NUM>
void
write_single_column(std::string filename, std::vector<NUM> dat) {
  std::ofstream ofs(filename);
  if (ofs.fail()) {
    std::cerr << "error: cannot open file '" << filename << "' for writing." << std::endl;
    exit(EXIT_FAILURE);
  }
  for (NUM i: dat) {
    ofs << i << "\n";
  }
}

std::vector<std::size_t>
read_clustered_trajectory(std::string filename) {
  return read_single_column<std::size_t>(filename);
}

std::vector<float>
read_free_energies(std::string filename) {
  return read_single_column<float>(filename);
}

template <typename KEY, typename VAL>
void
write_map(std::string filename, std::map<KEY, VAL> mapping) {
  std::ofstream ofs(filename);
  if (ofs.fail()) {
    std::cerr << "error: cannot open file '" << filename << "' for writing." << std::endl;
    exit(EXIT_FAILURE);
  }
  for (auto key_val: mapping) {
    ofs << key_val.first << " " << key_val.second << "\n";
  }
}

std::map<std::size_t, std::size_t>
microstate_populations(std::vector<std::size_t> traj) {
  std::map<std::size_t, std::size_t> populations;
  for (std::size_t state: traj) {
    if (populations.count(state) == 0) {
      populations[state] = 1;
    } else {
      ++populations[state];
    }
  }
  return populations;
}

void density_main(boost::program_options::variables_map args) {
  using namespace Clustering::Density;
  const std::string input_file = args["file"].as<std::string>();
  const float radius = args["radius"].as<float>();
  // setup coords
  float* coords;
  std::size_t n_rows;
  std::size_t n_cols;
  Clustering::logger(std::cout) << "reading coords" << std::endl;
  std::tie(coords, n_rows, n_cols) = read_coords<float>(input_file);
  #ifdef DC_USE_OPENCL
  DC_OpenCL::setup(coords, n_rows, n_cols);
  #endif
  //// free energies
  std::vector<float> free_energies;
  if (args.count("free-energy-input")) {
    Clustering::logger(std::cout) << "re-using free energy data." << std::endl;
    free_energies = read_free_energies(args["free-energy-input"].as<std::string>());
  } else if (args.count("free-energy") || args.count("population") || args.count("output")) {
    Clustering::logger(std::cout) << "calculating populations" << std::endl;
    #ifdef DC_USE_OPENCL
      std::vector<std::size_t> pops = DC_OpenCL::calculate_poputions(radius);
    #else
      std::vector<std::size_t> pops = calculate_populations(coords, n_rows, n_cols, radius);
    #endif
    if (args.count("population")) {
      std::ofstream ofs(args["population"].as<std::string>());
      for (std::size_t p: pops) {
        ofs << p << "\n";
      }
    }
    Clustering::logger(std::cout) << "calculating free energies" << std::endl;
    free_energies = calculate_free_energies(pops);
    if (args.count("free-energy")) {
      std::ofstream ofs(args["free-energy"].as<std::string>());
      ofs << std::scientific;
      for (float f: free_energies) {
        ofs << f << "\n";
      }
    }
  }
  //// nearest neighbors
  Neighborhood nh;
  Neighborhood nh_high_dens;
{'fraction_non_alphanumeric': 0.17469011242432977, 'fraction_numerical': 0.0005765350245027386, 'mean_word_length': 4.010108303249098, 'pattern_counts': {'":': 0, '<': 164, '<?xml version=': 0, '>': 52, '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': '7410494', 'n_tokens_mistral': 2505, 'n_tokens_neox': 2435, 'n_words': 485}