cMHN 1.0
C++ library for learning MHNs with pRC
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 {
29 pRC::Logging::warning(
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
35 pRC::Tensor<T, D, D> theta = pRC::zero();
36 pRC::Tensor<pRC::Bool, D, D> check = pRC::zero();
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 {
53 pRC::Logging::warning(
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
pRC::Float<> T
Definition: externs_nonTT.hpp:1
Definition: calculate_pTheta.hpp:15
static auto readTheta(std::string const &filename)
Reads a theta matrix from file, discarding its header.
Definition: read_theta.hpp:23