cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
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 <algorithm>
7#include <fstream>
8#include <map>
9#include <sstream>
10#include <string>
11
12#include <prc.hpp>
13
14namespace cMHN
15{
26 template<class T, pRC::Size D>
27 static inline auto readData(std::string const &filename)
28 {
29 std::ifstream file(filename);
30
31 using Subscripts =
33 [](auto const... seq)
34 {
35 return pRC::Subscripts<seq...>();
36 }));
37
38 if(!file.is_open())
39 {
40 pRC::Logging::error("Unable to open input file!");
41 }
42
43 std::map<Subscripts, T> pD;
44
45 // number of samples
47
48 std::string line;
49
50 // first line contains header (event names)
51 std::getline(file, line);
52
53 // write samples to pD
54 while(std::getline(file, line))
55 {
56 // turn commas into spaces
57 std::replace(line.begin(), line.end(), ',', ' ');
58
59 std::istringstream iss(line);
60
61 // store event data for current sample in 'bits'
62 Subscripts bits;
63 std::size_t i = 0;
64 unsigned v;
65 while(iss >> v)
66 {
67 bits[i++] = v;
68 }
70 {
71 if(i != D)
72 {
74 "Number of events differs for input file and binary. "
75 "File:",
76 i, "Binary:", D);
77 }
78 }
79
80 pD.try_emplace(bits, pRC::zero<T>());
81 pD[bits] += pRC::unit<T>();
82
84 }
85
86 // normalize pD
87 for(auto &[k, v] : pD)
88 {
89 v /= sum;
90 }
91
92 return pD;
93 }
94} // namespace cMHN
95
96#endif // cMHN_UTILITY_READ_DATA_H
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
Definition value.hpp:15
Definition subscripts.hpp:21
int i
Definition gmock-matchers-comparisons_test.cc:603
Definition calculate_pTheta.hpp:20
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:27
static void error(Xs &&...args)
Definition log.hpp:14
static constexpr auto unit()
Definition unit.hpp:13
static constexpr auto makeConstantSequence()
Definition sequence.hpp:444
constexpr auto cDebugLevel
Definition config.hpp:48
static constexpr auto zero()
Definition zero.hpp:12