-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgetNB_fast.cpp
135 lines (112 loc) · 3.39 KB
/
getNB_fast.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]
#include "idrsc2.h"
using namespace Rcpp;
using namespace arma;
using namespace std;
// [[Rcpp::export]]
arma::sp_umat get_fixedNumber_neighbors(const MATTYPE x, int number=6) {
// Get the fixed number of neighbors for each spot.
int i,j, jj, N = x.n_rows;
int number4 = 4* number+1;
arma::sp_umat D(N, N);
//float dis;
uvec idx, idx2, idx_all;
for (j = 0; j < N; ++j)
{
//Rprintf("good R!\n");
idx_all = sort_index(abs(x(j,0) - x.col(0)));
idx = idx_all.subvec(0, number4);
//Rprintf("good R %d !\n",j);
idx2 = find(idx != j);
int p = idx2.n_elem;
vec dis(p);
for (i = 0; i < p; ++i)
{
dis(i) = norm(x.row(idx(idx2(i))) - x.row(j), 2);
}
uvec idx3 = sort_index(dis);
for(i=0; i<number; ++i){
jj = idx(idx2(idx3(i)));
// D(jj,j) = 1;
D(jj,j) = 1;
}
}
return D;
}
//' @title
//' getneighborhood_fast
//' @description
//' an efficient function to find the neighborhood based on the matrix of position and a pre-defined cutoff
//'
//' @param x is a n-by-2 matrix of position.
//' @param radius is a threashold of Euclidean distance to decide whether a spot is an neighborhood of another spot. For example, if the Euclidean distance between spot A and B is less than cutoff, then A is taken as the neighbourhood of B.
//' @return A sparse matrix containing the neighbourhood
//'
//' @export
// [[Rcpp::export]]
arma::sp_umat getneighborhood_fast(const MATTYPE x, float radius) {
int N = x.n_rows;
arma::sp_umat D(N, N);
float dis;
uvec idx, idx2;
for (int j = 0; j < N-1; ++j)
{
idx = find(abs(x(j,0) - x.col(0))<radius);
idx2 = find(idx>j);
int p = idx2.n_elem;
for (int i = 0; i < p; ++i)
{
dis = norm(x.row(idx(idx2(i))) - x.row(j), 2);
if (dis < radius){
D(idx(idx2(i)),j) = 1;
D(j,idx(idx2(i))) = 1;
}
}
}
return D;
}
//' Calculate column-wise or row-wise mean
//' @param sp_data A sparse matrix
//' @param rowMeans A boolean value, whether to calculate row-wise mean
//' @return A n x 1 or p x 1 matrix
//' @export
// [[Rcpp::export]]
arma::vec sp_means_Rcpp(arma::sp_mat sp_data, bool rowMeans = false) {
arma::sp_mat norm_col_sums;
mat tmp_mat;
mat tmp_mat2;
if (rowMeans) {
norm_col_sums = arma::mean(sp_data, 1);
tmp_mat = arma::conv_to< mat >::from(norm_col_sums.col(0));
tmp_mat2 = arma::conv_to< mat >::from(tmp_mat);
}
else {
norm_col_sums = arma::mean(sp_data, 0);
tmp_mat = arma::conv_to< mat >::from(norm_col_sums.row(0).t());
tmp_mat2 = arma::conv_to< mat >::from(tmp_mat);
}
return tmp_mat2;
}
//' Calculate column-wise or row-wise sum
//' @param sp_data A sparse matrix
//' @param rowSums A boolean value, whether to calculate row-wise sum
//' @return A n x 1 or p x 1 matrix
//' @export
// [[Rcpp::export]]
arma::vec sp_sums_Rcpp(arma::sp_mat sp_data, bool rowSums = false) {
mat tmp_mat;
mat tmp_mat2;
arma::sp_mat norm_col_sums;
if (rowSums) {
norm_col_sums = arma::sum(sp_data, 1);
tmp_mat = arma::conv_to< mat >::from(norm_col_sums.col(0));
tmp_mat2 = arma::conv_to< mat >::from(tmp_mat);
}
else {
norm_col_sums = arma::sum(sp_data, 0);
tmp_mat = arma::conv_to< mat >::from(norm_col_sums.row(0).t());
tmp_mat2 = arma::conv_to< mat >::from(tmp_mat);
}
return tmp_mat2;
}