Skip to content

Commit

Permalink
Merge pull request #37 from stscl/dev
Browse files Browse the repository at this point in the history
add grid data cpp src
  • Loading branch information
SpatLyu authored Dec 11, 2024
2 parents 7c4a1bf + 7bb9cbd commit cffa41b
Show file tree
Hide file tree
Showing 10 changed files with 515 additions and 106 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ LinkingTo:
Imports:
dplyr,
sdsfun (>= 0.6.0),
terra,
tibble
Suggests:
ggplot2,
knitr,
Rcpp,
RcppThread,
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ RcppGenGridEmbeddings <- function(mat, E) {
.Call(`_spEDM_RcppGenGridEmbeddings`, mat, E)
}

RcppGCCM4Grid <- function(xMatrix, yMatrix, lib_sizes, pred, E) {
.Call(`_spEDM_RcppGCCM4Grid`, xMatrix, yMatrix, lib_sizes, pred, E)
}

RcppConfidence <- function(r, n, level = 0.05) {
.Call(`_spEDM_RcppConfidence`, r, n, level)
}
Expand Down
122 changes: 33 additions & 89 deletions src/CppGridUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,105 +3,49 @@
#include <cmath>
#include <limits>

/**
* Expands a matrix by adding rows and columns of NaN values based on the lag number.
*
* @param mat The input matrix represented as a 2D vector of doubles.
* @param lagNum The number of times the matrix should be expanded. Must be a non-negative integer.
* @return The expanded matrix.
*/
std::vector<std::vector<double>> ExpandMat(std::vector<std::vector<double>> mat,
int lagNum) {
// If lagNum is negative, return the original matrix as no expansion is needed.
if (lagNum < 0) {
return mat;
}

// If lagNum is greater than 1, recursively call ExpandMat to expand the matrix.
if (lagNum > 1) {
mat = ExpandMat(mat, lagNum - 1);
}

// Get the number of columns and rows in the matrix.
int numCols = mat[0].size();
// int numRows = mat.size();

// Add a row of NaN values at the top and bottom of the matrix.
std::vector<double> nanRow(numCols, std::numeric_limits<double>::quiet_NaN());
mat.insert(mat.begin(), nanRow); // Add at the top
mat.push_back(nanRow); // Add at the bottom

// Add a column of NaN values at the left and right of the matrix.
for (auto& row : mat) {
row.insert(row.begin(), std::numeric_limits<double>::quiet_NaN()); // Add at the left
row.push_back(std::numeric_limits<double>::quiet_NaN()); // Add at the right
}

return mat;
}

/**
* Generates a 2D matrix of lagged variables based on the input matrix and lag number.
*
* @param mat The input matrix represented as a 2D vector of doubles.
* @param lagNum The lag number used to generate the lagged variables.
* @return A 2D matrix of lagged variables.
*/
// Note that the return value is the value of the lagged order position, not the index.
std::vector<std::vector<double>> CppLaggedVar4Grid(
std::vector<std::vector<double>> mat,
int lagNum) {
// Get the number of columns and rows in the input matrix
int numCols = mat[0].size();
const std::vector<std::vector<double>>& mat,
int lagNum
) {
int numRows = mat.size();
int numCols = mat[0].size();
int totalElements = numRows * numCols;
std::vector<std::vector<double>> result(totalElements, std::vector<double>(8, std::numeric_limits<double>::quiet_NaN()));

// Expand the matrix using the ExpandMat function
mat = ExpandMat(mat, lagNum);

// Initialize the lagged variable matrix with dimensions (numRows * numCols) x (8 * lagNum)
int laggedVarRows = numRows * numCols;
int laggedVarCols = 8 * lagNum;
std::vector<std::vector<double>> laggedVar(laggedVarRows, std::vector<double>(laggedVarCols, std::numeric_limits<double>::quiet_NaN()));

// Iterate over each row and column of the original matrix
for (int r = 1; r <= numRows; ++r) {
for (int c = 1; c <= numCols; ++c) {
int item = 0; // Index for the laggedVar matrix

// Generate the sequence of column offsets
int colsAddStart = -std::trunc((3 + (lagNum - 1) * 2) / 2);
int colsAddEnd = std::trunc((3 + (lagNum - 1) * 2) / 2);
for (int la = colsAddStart; la <= colsAddEnd; ++la) {
laggedVar[(r - 1) * numCols + c - 1][item] = mat[r - 1 + lagNum][c - 1 + la + lagNum];
item++;
}

// Generate the sequence of row offsets
for (int ra = -(lagNum - 1); ra <= (lagNum - 1); ++ra) {
laggedVar[(r - 1) * numCols + c - 1][item] = mat[r - 1 + ra + lagNum][c - 1 - lagNum + lagNum];
item++;

laggedVar[(r - 1) * numCols + c - 1][item] = mat[r - 1 + ra + lagNum][c - 1 + lagNum + lagNum];
item++;
}
// Directions for Moore neighborhood (8 directions)
const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

// Generate the sequence of column offsets again
for (int la = colsAddStart; la <= colsAddEnd; ++la) {
laggedVar[(r - 1) * numCols + c - 1][item] = mat[r - 1 + lagNum + lagNum][c - 1 + la + lagNum];
item++;
for (int r = 0; r < numRows; ++r) {
for (int c = 0; c < numCols; ++c) {
int elementIndex = r * numCols + c;
int neighborIndex = 0;

// Calculate positions that are exactly 'lagNum' units away
for (int dir = 0; dir < 8; ++dir) {
int dr = dx[dir] * lagNum;
int dc = dy[dir] * lagNum;
int rNeighbor = r + dr;
int cNeighbor = c + dc;

// Check if the neighbor position is within bounds
if (rNeighbor >= 0 && rNeighbor < numRows && cNeighbor >= 0 && cNeighbor < numCols) {
result[elementIndex][neighborIndex] = mat[rNeighbor][cNeighbor];
}
// Else, it remains NaN

neighborIndex++;
if (neighborIndex >= 8) {
break;
}
}
}
}

return laggedVar;
return result;
}

/**
* Generates a list of grid embeddings for the input matrix and embedding dimension.
*
* @param mat The input matrix represented as a 2D vector of doubles.
* @param E The embedding dimension, which determines the number of embeddings to generate.
* @return A vector of 2D vectors, where each element is an embedding of the input matrix.
*/
std::vector<std::vector<std::vector<double>>> GenGridEmbeddings(
const std::vector<std::vector<double>>& mat, int E) {
// Initialize a vector to store the embeddings
Expand Down
8 changes: 5 additions & 3 deletions src/CppGridUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
#include <limits>

std::vector<std::vector<double>> CppLaggedVar4Grid(
std::vector<std::vector<double>> mat,
int lagNum);
const std::vector<std::vector<double>>& mat,
int lagNum
);

std::vector<std::vector<std::vector<double>>> GenGridEmbeddings(
const std::vector<std::vector<double>>& mat,
int E);
int E
);

#endif // CppGridUtils_H
1 change: 1 addition & 0 deletions src/CppLatticeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ std::vector<std::vector<int>> nb2vec(Rcpp::List nb) {
}

// Function to compute lagged neighborhoods for a given lag number
// **Note that the return value corresponds to the cumulative neighbor indices for lagNum**
std::vector<std::vector<int>> CppLaggedVar4Lattice(std::vector<std::vector<int>> spNeighbor,
int lagNum) {
// If lagNum is less than 1, return an empty vector
Expand Down
Loading

0 comments on commit cffa41b

Please sign in to comment.