cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
read_theta.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef cMHN_UTILITY_READ_THETA_H
4#define cMHN_UTILITY_READ_THETA_H
5
6#include <fstream>
7#include <iomanip>
8#include <string>
9
10#include <prc.hpp>
11
12namespace cMHN
13{
22 template<class T, pRC::Size D>
23 static inline auto readTheta(std::string const &filename)
24 {
25 std::ifstream file(filename);
26
27 if(!file.is_open())
28 {
30 "Unable to open file containing previous theta!");
31 pRC::Logging::info("Returning pRC::zero()");
32 return eval(pRC::zero<pRC::Tensor<T, D, D>>());
33 }
34
37
38 std::string line;
39
40 // First line contains header
41 std::getline(file, line);
42
43 pRC::Index i = 0;
44 while(std::getline(file, line))
45 {
46 std::istringstream iss(line);
47 pRC::Index j = 0;
48 T v;
49 while(iss >> v())
50 {
51 if(j >= D)
52 {
54 "Number of events in file and binary do not match "
55 "(#File > #Binary)");
56 pRC::Logging::info("Returning pRC::zero()");
57 return eval(pRC::zero<decltype(theta)>());
58 }
59 theta(i, j) = v;
60 check(i, j) = true;
61 ++j;
62 }
63 ++i;
64 }
65
66 if(check)
67 {
68 return theta;
69 }
70 else
71 {
72 pRC::Logging::warning("Failed to read thetas from:", filename);
73 pRC::Logging::warning("Probably file had fewer events than binary");
74 pRC::Logging::info("Returning pRC::zero()");
75 return eval(pRC::zero<decltype(theta)>());
76 }
77 }
78}
79
80#endif // CMHN_UTILITY_READ_THETA_H
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
Definition value.hpp:12
Definition tensor.hpp:25
int i
Definition gmock-matchers-comparisons_test.cc:603
Definition calculate_pTheta.hpp:20
static auto readTheta(std::string const &filename)
Reads a theta matrix from file, discarding its header.
Definition read_theta.hpp:23
static void info(Xs &&...args)
Definition log.hpp:27
static void warning(Xs &&...args)
Definition log.hpp:21
Size Index
Definition basics.hpp:32
static constexpr auto zero()
Definition zero.hpp:12