cMHN 1.0
C++ library for learning MHNs with pRC
read_data.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef cMHN_UTILITY_READ_DATA_H
4#define cMHN_UTILITY_READ_DATA_H
5
6#include <fstream>
7#include <map>
8#include <sstream>
9#include <string>
10
11#include <prc.hpp>
12
13namespace cMHN
14{
25 template<class T, pRC::Size D>
26 static inline auto readData(std::string const &filename)
27 {
28 std::ifstream file(filename);
29
30 using Subscripts =
31 decltype(expand(pRC::makeConstantSequence<pRC::Size, D, 2>(),
32 [](auto const... seq)
33 {
34 return pRC::Subscripts<seq...>();
35 }));
36
37 if(!file.is_open())
38 {
39 pRC::Logging::error("Unable to open input file!");
40 }
41
42 std::map<Subscripts, T> pD;
43
44 // number of samples
45 pRC::UnsignedInteger<64> sum = 0;
46
47 std::string line;
48
49 // first line contains header (event names)
50 std::getline(file, line);
51
52 // write samples to pD
53 while(std::getline(file, line))
54 {
55 // turn commas into spaces
56 std::replace(line.begin(), line.end(), ',', ' ');
57
58 std::istringstream iss(line);
59
60 // store event data for current sample in 'bits'
61 Subscripts bits;
62 std::size_t i = 0;
63 unsigned v;
64 while(iss >> v)
65 {
66 bits[i++] = v;
67 }
68 if constexpr(pRC::cDebugLevel >= pRC::DebugLevel::Low)
69 {
70 if(i != D)
71 {
72 pRC::Logging::error(
73 "Number of events differs for input file and binary. "
74 "File:",
75 i, "Binary:", D);
76 }
77 }
78
79 pD.try_emplace(bits, pRC::zero<T>());
80 pD[bits] += pRC::unit<T>();
81
82 sum += pRC::unit<decltype(sum)>();
83 }
84
85 // normalize pD
86 for(auto &[k, v] : pD)
87 {
88 v /= sum;
89 }
90
91 return pD;
92 }
93} // namespace cMHN
94
95#endif // cMHN_UTILITY_READ_DATA_H
pRC::Size const D
Definition: CalculatePThetaTests.cpp:9
Definition: calculate_pTheta.hpp:15
static auto readData(std::string const &filename)
Reads a dataset from file, where the first line is the header (containing event names) and all subseq...
Definition: read_data.hpp:26