RobotsIO
Loading...
Searching...
No Matches
DatasetDataStream.cpp
1/*
2 * Copyright (C) 2020 Istituto Italiano di Tecnologia (IIT)
3 *
4 * This software may be modified and distributed under the terms of the
5 * BSD 3-Clause license. See the accompanying LICENSE file for details.
6 */
7
8#include <RobotsIO/Utils/DatasetDataStream.h>
9#include <RobotsIO/Utils/FileToEigen.h>
10
11using namespace Eigen;
12using namespace RobotsIO::Utils;
13
14
15DatasetDataStream::DatasetDataStream(const std::string& file_path, const std::size_t& skip_rows, const std::size_t& skip_cols, const std::size_t& expected_cols, const int rx_time_index, const int tx_time_index)
16{
17 bool valid_file;
18 MatrixXd data_all;
19 std::tie(valid_file, data_all) = file_to_eigen(file_path, 0, skip_cols, expected_cols);
20
21 if (!valid_file)
22 throw(std::runtime_error(log_name_ + "::ctor. Error cannot read data from file " + file_path + "."));
23
24 if (rx_time_index >= data_all.cols())
25 throw(std::runtime_error(log_name_ + "::ctor. Specified rx time index " + std::to_string(rx_time_index) + " is out of range."));
26
27 if (tx_time_index >= data_all.cols())
28 throw(std::runtime_error(log_name_ + "::ctor. Specified tx time index " + std::to_string(tx_time_index) + " is out of range."));
29
30 std::size_t data_time_rows = 0;
31 if (rx_time_index != -1)
32 {
33 data_rx_time_ = data_all.row(rx_time_index);
34 data_time_rows++;
35 }
36
37 if (tx_time_index != -1)
38 {
39 data_tx_time_ = data_all.row(tx_time_index);
40 data_time_rows++;
41 }
42
43 data_.resize(data_all.rows() - data_time_rows, data_all.cols());
44 std::size_t j = 0;
45 for (std::size_t i = 0; i < data_all.rows(); i++)
46 {
47 if ((i == rx_time_index) || (i == tx_time_index))
48 continue;
49 data_.row(j) = data_all.row(i);
50
51 j++;
52 }
53}
54
55
56DatasetDataStream::~DatasetDataStream()
57{}
58
59
60double DatasetDataStream::rx_time()
61{
62 if (data_rx_time_.size() != 0)
63 return data_rx_time_(get_head());
64
65 return 0.0;
66}
67
68
69double DatasetDataStream::tx_time()
70{
71 if (data_tx_time_.size() != 0)
72 return data_tx_time_(get_head());
73
74 return 0.0;
75}
76
77
78VectorXd DatasetDataStream::data()
79{
80 return data_.col(get_head());
81}
82
83
84bool DatasetDataStream::freeze()
85{
86 return set_head(get_head() + 1);
87}
88
89
90int DatasetDataStream::get_head()
91{
92 return head_;
93}
94
95
96bool DatasetDataStream::set_head(const int& value)
97{
98 if (value >= data_.cols())
99 return false;
100
101 head_ = value;
102
103 return true;
104}
105
106
107VectorXd DatasetDataStream::data(const int& index)
108{
109 if (index < 0 || index >= data_.cols())
110 throw(std::runtime_error(log_name_ + "::data(const int& index). Error: invalid index provided (index = " + std::to_string(index) + ")."));
111
112 return data_.col(index);
113}