Skip to content

Commit

Permalink
Merge branch 'rqa'
Browse files Browse the repository at this point in the history
  • Loading branch information
constantino-garcia committed Dec 4, 2023
2 parents 1e311c2 + ed39c3d commit d95a8f4
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 67 deletions.
5 changes: 4 additions & 1 deletion .Rproj.user/shared/notebooks/paths
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/home/gaussllego/code/r_packages/nonlinearTseries/src/ANN.cpp="2D966DFE"
/home/gaussllego/code/r_packages/nonlinearTseries/DESCRIPTION="485829EC"
/home/gaussllego/code/r_packages/nonlinearTseries/NAMESPACE="41E46569"
/home/gaussllego/code/r_packages/nonlinearTseries/R/RcppExports.R="E0DB4424"
/home/gaussllego/code/r_packages/nonlinearTseries/cran-comments.md="DEDBE334"
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ Suggests:
rmarkdown,
testthat
VignetteBuilder: knitr
RoxygenNote: 7.1.2
RoxygenNote: 7.2.3
Encoding: UTF-8
16 changes: 13 additions & 3 deletions R/RQA.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ neighbourListToCsparseNeighbourMatrix = function(neighs) {
}



calculateVerticalParameters = function(ntakens, numberRecurrencePoints,
vmin = 2, verticalLinesHistogram) {
#begin parameter computations
Expand Down Expand Up @@ -292,12 +293,21 @@ calculateDiagonalParameters = function(ntakens, numberRecurrencePoints,
}

getHistograms = function(neighs, ntakens, lmin, vmin){
# update neighs:
# 1) add each vector to its own neigbourhood (i is a neighbour of i)
# 2) sort
# 3) substract 1 to get C-like indexes for neighbors
cneighs = mapply(
function(ith_neighs, i) as.integer(sort(c(i, ith_neighs) - 1)),
neighs,
seq_along(neighs)
)
# the neighbours are labeled from 0 to ntakens-1
c.matrix = neighbourListToCsparseNeighbourMatrix(neighs)
# auxiliar variables
.Call("_nonlinearTseries_get_rqa_histograms",
neighs = c.matrix$neighs, nneighs = c.matrix$nneighs,
ntakens = as.integer(ntakens), vmin = as.integer(vmin),
neighs = cneighs,
ntakens = as.integer(ntakens),
vmin = as.integer(vmin),
lmin = as.integer(lmin),
PACKAGE = "nonlinearTseries")
}
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ poincare_map <- function(timeSeries, hiperplanePoint, normalVector) {
.Call('_nonlinearTseries_poincare_map', PACKAGE = 'nonlinearTseries', timeSeries, hiperplanePoint, normalVector)
}

get_rqa_histograms <- function(neighs, nneighs, ntakens, vmin, lmin) {
.Call('_nonlinearTseries_get_rqa_histograms', PACKAGE = 'nonlinearTseries', neighs, nneighs, ntakens, vmin, lmin)
get_rqa_histograms <- function(neighs, ntakens, vmin, lmin) {
.Call('_nonlinearTseries_get_rqa_histograms', PACKAGE = 'nonlinearTseries', neighs, ntakens, vmin, lmin)
}

space_time_plot <- function(phaseSpace, radii, nTimeSteps, timeStep, nPercentages) {
Expand Down
2 changes: 1 addition & 1 deletion nonlinearTseries.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ AutoAppendNewline: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageInstallArgs: --no-multiarch --with-keep.source --no-build-vignettes --ignore-vignettes
PackageCheckArgs: --as-cran
PackageRoxygenize: rd,namespace,vignette
11 changes: 5 additions & 6 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,16 @@ BEGIN_RCPP
END_RCPP
}
// get_rqa_histograms
List get_rqa_histograms(IntegerMatrix& neighs, IntegerVector& nneighs, int ntakens, int vmin, int lmin);
RcppExport SEXP _nonlinearTseries_get_rqa_histograms(SEXP neighsSEXP, SEXP nneighsSEXP, SEXP ntakensSEXP, SEXP vminSEXP, SEXP lminSEXP) {
List get_rqa_histograms(List& neighs, int ntakens, int vmin, int lmin);
RcppExport SEXP _nonlinearTseries_get_rqa_histograms(SEXP neighsSEXP, SEXP ntakensSEXP, SEXP vminSEXP, SEXP lminSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< IntegerMatrix& >::type neighs(neighsSEXP);
Rcpp::traits::input_parameter< IntegerVector& >::type nneighs(nneighsSEXP);
Rcpp::traits::input_parameter< List& >::type neighs(neighsSEXP);
Rcpp::traits::input_parameter< int >::type ntakens(ntakensSEXP);
Rcpp::traits::input_parameter< int >::type vmin(vminSEXP);
Rcpp::traits::input_parameter< int >::type lmin(lminSEXP);
rcpp_result_gen = Rcpp::wrap(get_rqa_histograms(neighs, nneighs, ntakens, vmin, lmin));
rcpp_result_gen = Rcpp::wrap(get_rqa_histograms(neighs, ntakens, vmin, lmin));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -249,7 +248,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_nonlinearTseries_getAllNeighbours", (DL_FUNC) &_nonlinearTseries_getAllNeighbours, 3},
{"_nonlinearTseries_nonlinear_noise_reduction", (DL_FUNC) &_nonlinearTseries_nonlinear_noise_reduction, 4},
{"_nonlinearTseries_poincare_map", (DL_FUNC) &_nonlinearTseries_poincare_map, 3},
{"_nonlinearTseries_get_rqa_histograms", (DL_FUNC) &_nonlinearTseries_get_rqa_histograms, 5},
{"_nonlinearTseries_get_rqa_histograms", (DL_FUNC) &_nonlinearTseries_get_rqa_histograms, 4},
{"_nonlinearTseries_space_time_plot", (DL_FUNC) &_nonlinearTseries_space_time_plot, 5},
{"_nonlinearTseries_neighsList2Sparse", (DL_FUNC) &_nonlinearTseries_neighsList2Sparse, 2},
{"_nonlinearTseries_neighsList2SparseRCreator", (DL_FUNC) &_nonlinearTseries_neighsList2SparseRCreator, 3},
Expand Down
98 changes: 45 additions & 53 deletions src/rqa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ using namespace Rcpp;
// n-th Takens' vector. n is also present as neighbour. The n-th position of the
// nneighs vector has the number of neighs of the n-th takens' vector.
// verticalHistogram: vector with ntakens elements
void get_vertical_histogram(IntegerMatrix& neighs, IntegerVector& nneighs,
int ntakens, int vmin,
void get_vertical_histogram(List& neighs, int ntakens, int vmin,
IntegerVector& verticalHistogram){
for (int i = 0; i < ntakens; i++){
verticalHistogram[i] = 0;
}
// find vertical lines in every column/row
for (int i = 0; i < ntakens; i++){
// count number of neighbours + itself
int lne = nneighs[i];
int j=1;
IntegerVector ith_takens_neighs = as<IntegerVector>(neighs[i]);
int lne = ith_takens_neighs.length();
int j = 1;
while (j < lne){
int count = 1;
// while there is a vertical line, update the length (count)
while( (j < lne) && (neighs(i, j) == (neighs(i, j - 1) + 1)) ){
while( (j < lne) && (ith_takens_neighs[j] == (ith_takens_neighs[j - 1] + 1)) ) {
j++;
count++;
}
Expand All @@ -35,29 +32,28 @@ void get_vertical_histogram(IntegerMatrix& neighs, IntegerVector& nneighs,
}
}

bool is_contained_in_neighbourhood(int possibleNeigh, int i, IntegerMatrix& neighs,
int ntakens, int nneighs){
bool is_contained_in_neighbourhood(int possibleNeigh, int i, List& neighs){
bool found = false;
IntegerVector ith_takens_neighs = as<IntegerVector>(neighs[i]);
int nneighs = ith_takens_neighs.length();
for (int iter = 0; iter < nneighs; iter++ ){
if (possibleNeigh == neighs(i,iter)){
if (possibleNeigh == ith_takens_neighs[iter]){
found = true;
break;
}
}
return found;

}

void update_length_histogram(int i, int j, IntegerMatrix& neighs,
IntegerVector& nneighs, int ntakens,
void update_length_histogram(int i, int j, List& neighs, int ntakens,
IntegerVector& diagonalHistogram, int lmin){
// update diagonalHistogram
int actualLength = 1;
// advance in diagonal direction
i++;
j++;
// advance while there exist diagonal
while ( (j < ntakens) && (is_contained_in_neighbourhood(j, i, neighs, ntakens, nneighs[i])) ){
while ( (j < ntakens) && (is_contained_in_neighbourhood(j, i, neighs)) ){
// update the length of the diagonal
actualLength++;
// advance
Expand All @@ -67,82 +63,78 @@ void update_length_histogram(int i, int j, IntegerMatrix& neighs,
// update the Histogram
if (actualLength >= lmin){
// diagonal lengths of 1 in position 0, 2 in position 1, etc.
diagonalHistogram[actualLength - 1]++;
// +=2 to take into account that the matrix is symmetric
diagonalHistogram[actualLength - 1] += 2;
}
}

//diagonalHistogram and recurrenceHistogram have ntakens elements
// compute the diagonal histogram and the recurrenceHistogram
void get_diagonal_recurrence_histogram(
IntegerMatrix& neighs, IntegerVector& nneighs, int ntakens, int lmin,
List& neighs, int ntakens, int lmin,
IntegerVector& diagonalHistogram, IntegerVector& recurrenceHistogram){
for (int i = 0; i < ntakens ;i++){
diagonalHistogram[i]=0;
recurrenceHistogram[i]=0;
}
// Treat the first row separately: It will be easier if we find diagonals that
// "originate" at the first row separately
for (int j = 0; j < nneighs[0]; j++){
int currentNeigh = neighs(0,j);
IntegerVector first_takens_neighs = as<IntegerVector>(neighs[0]);
int nneighs = first_takens_neighs.length();
for (int j = 0; j < nneighs; j++){
int currentNeigh = first_takens_neighs[j];
// update recurrenceHistogram: the recurrence histogram takes into account
// the distance between neigbours. In this case: currentNeigh - 0, and we store
// it in tre previous position (position 0 stores distance 1)
recurrenceHistogram[currentNeigh - 1]++;
// it in the previous position (position 0 stores distance 1).
// +=2 to take into account that the RQA matrix is symmetric
recurrenceHistogram[currentNeigh - 1] += 2;
// update diagonal histogram
update_length_histogram(0, currentNeigh, neighs, nneighs,
ntakens, diagonalHistogram, lmin);
update_length_histogram(0, currentNeigh, neighs,
ntakens, diagonalHistogram, lmin);
}
// find diagonals that "originate" at other rows
int lastPosition = ntakens - lmin;
for (int i = 1; i < lastPosition; i++){
for (int j = 0; j < nneighs[i]; j++){
int currentNeigh = neighs(i,j);
//check if we have already took into acount the pair i,j
IntegerVector ith_takens_neighs = as<IntegerVector>(neighs[i]);
nneighs = ith_takens_neighs.length();
for (int j = 0; j < nneighs; j++){
int currentNeigh = ith_takens_neighs[j];
//check if we have already took into account the pair i,j
if (currentNeigh <= i) continue;
// update recurrenceHistogram
recurrenceHistogram[currentNeigh - i - 1]++;
// +=2 to take into account that the RQA matrix is symmetric
recurrenceHistogram[currentNeigh - i - 1] += 2;
//check if this diagonal has its origin in any row before
if (is_contained_in_neighbourhood(currentNeigh - 1, i - 1, neighs,
ntakens, nneighs[i-1])) {
if (is_contained_in_neighbourhood(currentNeigh - 1, i - 1, neighs)) {
continue;
} else{
//update the diagonalHistogram and the recurrenceHistogram
update_length_histogram(i,currentNeigh,neighs,nneighs, ntakens,
update_length_histogram(i,currentNeigh,neighs, ntakens,
diagonalHistogram, lmin);
}
}
}
//complete the recurrence vector if needed
if (lastPosition < ntakens){
for (int i = lastPosition; i < ntakens; i++){
for (int j = 0; j < nneighs[i]; j++){
int currentNeigh = neighs(i,j);
IntegerVector ith_takens_neighs = as<IntegerVector>(neighs[i]);
nneighs = ith_takens_neighs.length();
for (int j = 0; j < nneighs; j++){
int currentNeigh = ith_takens_neighs[j];
if (currentNeigh <= i) continue;
// update recurrenceHistogram
recurrenceHistogram[currentNeigh - i - 1]++;
// +=2 to take into account that the matrix is symmetric
recurrenceHistogram[currentNeigh - i - 1] += 2;
}
}
}

// take into account that the matrix is symmetric
for (int i = 0; i < ntakens; i++){
diagonalHistogram[i] = 2 * diagonalHistogram[i];
recurrenceHistogram[i] = 2 * recurrenceHistogram[i];
}
// Add the main diagonal, of length ntakens
// Add the main diagonal, of length ntakens (there is only one!)
diagonalHistogram[ntakens - 1] = 1;
}

// [[Rcpp::export]]
List get_rqa_histograms(IntegerMatrix& neighs, IntegerVector& nneighs,
int ntakens, int vmin, int lmin){
List get_rqa_histograms(List& neighs, int ntakens, int vmin, int lmin){
// auxiliar variables
IntegerVector verticalHistogram(ntakens, 0.0);
IntegerVector diagonalHistogram(ntakens, 0.0);
IntegerVector recurrenceHistogram(ntakens, 0.0);
IntegerVector verticalHistogram(ntakens, 0);
IntegerVector diagonalHistogram(ntakens, 0);
IntegerVector recurrenceHistogram(ntakens, 0);

get_vertical_histogram(neighs, nneighs, ntakens, vmin, verticalHistogram);
get_diagonal_recurrence_histogram(neighs, nneighs, ntakens, lmin,
get_vertical_histogram(neighs, ntakens, vmin, verticalHistogram);
get_diagonal_recurrence_histogram(neighs, ntakens, lmin,
diagonalHistogram, recurrenceHistogram);
List ret;
ret["diagonalHist"] = diagonalHistogram;
Expand Down

0 comments on commit d95a8f4

Please sign in to comment.