PPCKO: Principal Predictive Components for Estimating an Autoregressive Operator
 
Loading...
Searching...
No Matches
removing_nan_cleaner_imp.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#include "removing_nan.hpp"
21#include <RcppEigen.h>
22
23
29
30
37inline
38bool
39is_row_all_nan(const Rcpp::NumericMatrix::ConstRow& row, int tot_cols)
40{
41 return std::count_if(row.cbegin(),row.cend(),[](auto el){return std::isnan(el);}) == tot_cols ? true : false;
42}
43
44
51//
52// [[Rcpp::depends(RcppEigen)]]
53std::set<int>
54rows_entire_NaNs(const Rcpp::NumericMatrix &x)
55{
56 if (x.nrow() == 0 || x.ncol() == 0)
57 {
58 std::string error_message1 = "Empty data matrix";
59 throw std::invalid_argument(error_message1);
60 }
61
62 int tot_cols = x.ncol();
63 std::set<int> rows_to_be_kept;
64 for (int i = 0; i < x.nrow(); ++i) { if (!is_row_all_nan(x.row(i),tot_cols)) { rows_to_be_kept.insert(i);}}
65
66 if (rows_to_be_kept.empty())
67 {
68 std::string error_message2 = "Only-NaNs data matrix";
69 throw std::invalid_argument(error_message2);
70 }
71
72 return rows_to_be_kept;
73}
74
75
76// function to remove the all-NaNs rows
84//
85// [[Rcpp::depends(RcppEigen)]]
86Rcpp::NumericMatrix
87removing_NaNS_entire_rows(const Rcpp::NumericMatrix &x,const std::set<int> &rows_to_be_kept)
88{
89 Rcpp::NumericMatrix x_clean(rows_to_be_kept.size(),x.ncol());
90 int counter_row_clean = 0;
91 std::for_each(rows_to_be_kept.cbegin(),rows_to_be_kept.cend(),[&x,&x_clean,&counter_row_clean](auto el){x_clean.row(counter_row_clean)=x.row(el); counter_row_clean++;});
92
93 return x_clean;
94}
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
bool is_row_all_nan(const Rcpp::NumericMatrix::ConstRow &row, int tot_cols)
Function checking if a row is entirely made by NaNs.
Definition removing_nan_cleaner_imp.hpp:39
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