Reader.cpp 3.04 KB
Newer Older
1
2
3
4
//
// Created by martin on 16.08.19.
//

5
6
7
#include <algorithm>
#include <cctype>

8
#include "MPIWrapper/mpi_wrapper.h"
9

10
#include "../exceptions.h"
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Reader.h"

lib4neuro::Reader::Reader(const std::string& file_path, bool ignore_first_line, std::string delimiter) {
    this->file_path = file_path;
    this->ignore_first_line = ignore_first_line;
    this->delimiter = delimiter;
    this->data_set = nullptr;
}

std::vector<std::vector<std::string>>* lib4neuro::Reader::get_data() {
    return &this->data;
}

void lib4neuro::Reader::print_data() {
25
26
27
28
    if( lib4neuro::mpi_rank > 0 ){
        return;
    }

29
30
31
32
33
34
35
36
37
    for (auto line : this->data) {
        for (auto e : line) {
            std::cout << e << " ";
        }
        std::cout << std::endl;
    }
}

void lib4neuro::Reader::read() {
38
39
    // TODO make filepath relative to an executable directory instead of the current one!
    // TODO https://www.boost.org/doc/libs/1_36_0/libs/filesystem/doc/reference.html#initial_path
40
41
42
43
44
45
46
47
48
49
    try {
        if (lib4neuro::mpi_rank == 0) {
            std::ifstream ifs(this->file_path);

            if (!ifs.is_open()) {
                MPI_INTERRUPT
                // throw std::logic_error("File could not be opened! Possibly due to the lack of access privileges or an incorrect path.\n");
                THROW_LOGIC_ERROR("File \'" + this->file_path +
                                  "\' could not be opened! Possibly due to the lack of access privileges or an incorrect path.\n")
            }
50

51
            std::string line;
52

53
54
55
            if (this->ignore_first_line) {
                std::getline(ifs,
                             line);
56
            }
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
            /* Read single line from the file */
            while (std::getline(ifs,
                                line)) {

                /* Ignore empty line */
                if (line == "") {
                    continue;
                }

                /* Separate elements of the line according to the delimiter */
                size_t                   last = 0;
                size_t                   next = 0;
                std::vector<std::string> separated_line;
                while ((next = line.find(this->delimiter,
                                         last)) != std::string::npos) {
                    separated_line.emplace_back(line.substr(last,
                                                            next - last));
                    last = next + 1;
                }
                separated_line.emplace_back(line.substr(last));

                /* Store the elements from the line to the vector with data */
                this->data.emplace_back(separated_line);
81
82
            }

83
            ifs.close();
84
        }
85
86
87
    } catch(...) {
        MPI_ERROR_CHECK
        THROW_LOGIC_ERROR("read() function error!")
88
    }
89

90
91
}

92
93
94
95
void lib4neuro::Reader::remove_white_characters_from_vector(std::vector<std::string>& v) {
    for(auto& e : v) {
        e.erase(std::remove_if(e.begin(), e.end(), [](int i){return std::isspace(i);}), e.end());
    }
96
97
    v.erase(std::remove(v.begin(), v.end(), ""), v.end());
}