PPCKO: Principal Predictive Components for Estimating an Autoregressive Operator
 
Loading...
Searching...
No Matches
data_reader.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 Andrea Enrico Franzoni (andreaenrico.franzoni@gmail.com)
2//
3// This file is part of PPCKO
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of PPCKO and associated documentation files (the PPCKO software), to deal
7// PPCKO without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of PPCKO, and to permit persons to whom PPCKO is
10// furnished to do so, subject to the following conditions:
11//
12// PPCKO IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17// OUT OF OR IN CONNECTION WITH PPCKO OR THE USE OR OTHER DEALINGS IN
18// PPCKO.
19
20#ifndef KO_READ_DATA_HPP
21#define KO_READ_DATA_HPP
22
23#include <RcppEigen.h>
24
25#include <algorithm>
26#include <iterator>
27#include <numeric>
28#include <cmath>
29#include <utility>
30#include <vector>
31#include <iostream>
32
33#include "traits_ko.hpp"
34#include "removing_nan.hpp"
36
37
38
44
45
53//
54// [[Rcpp::depends(RcppEigen)]]
55template<typename T>
56std::pair<KO_Traits::StoringMatrix,std::vector<int>>
57reader_data(Rcpp::NumericMatrix X,
58 REM_NAN MA_t)
59{
60 //taking the dimensions: n_row is the number of time series, n_col is the number of time istants
61 int n_row = X.nrow();
62 int n_col = X.ncol();
63
64 //needed only if the method is used to translate R containers into a coherent input matrix for surface's PPCKO verson. NOT usable by the user
65 if(MA_t == REM_NAN::NR)
66 {
67 KO_Traits::StoringMatrix x = Eigen::Map<KO_Traits::StoringMatrix>(X.begin(),n_row,n_col);
68 std::vector<int> row_removed;
69 return std::make_pair(x,row_removed);
70 }
71
72
73 //removing rows of all dummy NaNs
74 std::set<int> rows_to_be_retained = rows_entire_NaNs(X); //rows to be retained (position wrt the origianl matrix)
75
76 std::vector<int> temp;
77 temp.resize(X.nrow());
78 std::iota(temp.begin(),temp.end(),static_cast<int>(0));
79 std::set<int> all_rows(temp.begin(),temp.end());
80 temp.clear();
81
82 std::set<int> rows_to_be_removed; //rows to be removed (position wrt the origianl matrix)
83 std::set_difference(all_rows.begin(),all_rows.end(),
84 rows_to_be_retained.begin(),rows_to_be_retained.end(),
85 std::inserter(rows_to_be_removed,rows_to_be_removed.begin()));
86 all_rows.clear();
87
88 //remove rows of all dummy NaNs
89 Rcpp::NumericMatrix X_clean = removing_NaNS_entire_rows(X,rows_to_be_retained); //matrix without dummy NaNs
90 //mapping the position of the rows to be retained into a std::vector from a std::set
91 std::vector<int> rows_retained(rows_to_be_retained.begin(),rows_to_be_retained.end()); //AS IF INDECES START FROM 0
92
93
94 //Eigen::Map to map data into KO_Traits::StoringMatrix (Eigen::MatrixXd) (!!to be modified if the trait is not an Eigen object anymore!!)
95 KO_Traits::StoringMatrix x = Eigen::Map<KO_Traits::StoringMatrix>(X_clean.begin(),rows_to_be_retained.size(),n_col);
96
97 //check if there are other NaNs (NaNs due to missed measurements, not dummy)
98 auto check_nan = std::find_if(x.reshaped().cbegin(),x.reshaped().cend(),[](T el){return std::isnan(el);});
99
100 //if there are nans: remove them
101 if (check_nan!=x.reshaped().end())
102 {
103 if(MA_t == REM_NAN::MR) //replacing nans with the mean
104 {
105 removing_nan<T,REM_NAN::MR> data_clean(std::move(x));
106 data_clean.remove_nan();
107 return std::make_pair(data_clean.data(),rows_retained);
108 }
109 if(MA_t == REM_NAN::ZR) //replacing nans with 0s
110 {
111 removing_nan<T,REM_NAN::ZR> data_clean(std::move(x));
112 data_clean.remove_nan();
113 return std::make_pair(data_clean.data(),rows_retained);
114 }
115 }
116
117 return std::make_pair(x,rows_retained);
118}
119
120#endif /*KO_READ_DATA_HPP*/
Template class for removing NaNs from an Eigen::Matrix.
Definition removing_nan.hpp:64
void remove_nan()
Function to remove the row (dummy NaNs)
Definition removing_nan.hpp:115
KO_Traits::StoringMatrix data() const
Getter for the data matrix.
Definition removing_nan.hpp:103
std::pair< KO_Traits::StoringMatrix, std::vector< int > > reader_data(Rcpp::NumericMatrix X, REM_NAN MA_t)
Function that reads data from R containers, substitute actual NaNs, handle dummy NaNs,...
Definition data_reader.hpp:57
Contains methods to check and wrap R-inputs into PPCKO-coherent ones.
REM_NAN
The available strategy for removing non-dummy NaNs.
Definition parameters_wrapper.hpp:366
@ ZR
Replacing nans with 0s (could change the sd of the distribution)
Definition parameters_wrapper.hpp:369
@ MR
Replacing nans with mean (could change the mean of the distribution)
Definition parameters_wrapper.hpp:368
@ NR
Not replacing NaN: not to be used by the user, necessary for handling dummy NaNs.
Definition parameters_wrapper.hpp:367
Contains the class to remove (dummy and not dummy) NaNs.
std::set< int > rows_entire_NaNs(const Rcpp::NumericMatrix &x)
Identifying rows of only NaNs.
Definition removing_nan_cleaner_imp.hpp:54
Rcpp::NumericMatrix removing_NaNS_entire_rows(const Rcpp::NumericMatrix &x, const std::set< int > &rows_to_be_kept)
Removing rows of only NaNs.
Definition removing_nan_cleaner_imp.hpp:87
Eigen::MatrixXd StoringMatrix
Matrix data structure.
Definition traits_ko.hpp:49
Contains customized types and enumerator for customized template parameters, exploited in the algorit...