-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubset_df.cpp
490 lines (371 loc) · 13.9 KB
/
subset_df.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include <Rcpp.h>
//#include <RcppArmadillo.h>
#include <string> // for std::string and std::getline
using namespace Rcpp;
using namespace std;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
template <int RTYPE>
Vector<RTYPE> roll_vector_na(const Vector<RTYPE>& x) {
int n = x.size() ;
Vector<RTYPE> out = clone(x) ;
for( int i=1; i<n; i++){
//printf("elem %d", i);
if( Vector<RTYPE>::is_na( x[i] ) )
out[i] = out[i-1];
else
out[i] = x[i];
}
return out ;
}
// [[Rcpp::export(na_roll_cpp)]]
SEXP na_omit(SEXP x) {
switch(TYPEOF(x)) {
case INTSXP:
return roll_vector_na<INTSXP>(x);
case REALSXP:
return roll_vector_na<REALSXP>(x);
case LGLSXP:
return roll_vector_na<LGLSXP>(x);
default:
stop("unsupported data type");
}
}
// [[Rcpp::export]]
SEXP roll_vector_nas( SEXP x ){
RCPP_RETURN_VECTOR( roll_vector_na, x ) ;
}
*/
IntegerVector roll_vector_int(IntegerVector x) {
IntegerVector v(x) ;
int Nidx = v.size();
IntegerVector result = clone(v);
for( int i = 1; i < Nidx; i++){
if(IntegerVector::is_na( result[i] )) result[i] = result[i-1] ;
}
return result;
}
NumericVector roll_vector_num(NumericVector x) {
NumericVector v(x) ;
int Nidx = v.size();
NumericVector result = clone(v);
for( int i = 1; i < Nidx; i++){
if(NumericVector::is_na( result[i] )) result[i] = result[i-1] ;
}
return result;
}
CharacterVector roll_vector_char(CharacterVector x) {
CharacterVector v(x) ;
int Nidx = v.size();
CharacterVector result = clone(v);
for( int i = 1; i < Nidx; i++){
if(CharacterVector::is_na( result[i] )) result[i] = result[i-1] ;
}
return result;
}
LogicalVector roll_vector_log(LogicalVector x) {
LogicalVector v(x) ;
int Nidx = v.size();
LogicalVector result = clone(v);
for( int i = 1; i < Nidx; i++){
if(LogicalVector::is_na( result[i] )) result[i] = result[i-1] ;
}
return result;
}
// [[Rcpp::export(roll_vector_nas)]]
SEXP roll_vector_nas(SEXP x) {
switch(TYPEOF(x)) {
case INTSXP:
return roll_vector_int(x);
case REALSXP:
return roll_vector_num(x);
case LGLSXP:
return roll_vector_log(x);
case STRSXP:
return roll_vector_char(x);
default:
return x;
}
}
/*
// [[Rcpp::export]]
SEXP roll_vector_nas( SEXP x ){
RCPP_RETURN_VECTOR( roll_vector_na, x ) ;
}
*/
// [[Rcpp::export]]
List subsetCpp( DataFrame data, IntegerVector indxs){
// this function will subset a DataFrame, by the vector of indexes
// indxs.
// It returns a List, because returning a DataFrame takes aprox 100 x more.
//cout << "begin subset" << endl;
DataFrame df(data);
int ncol = Rf_length(df);
CharacterVector colNames(ncol);
SEXP names = Rf_getAttrib(df, R_NamesSymbol);
SEXP colData = VECTOR_ELT(df,0); // First column of data.
//int nrow = Rf_length(colData);
// IntegerVector indexes = IntegerVector::create(1,5,10);
IntegerVector indexes = clone(indxs);
indexes = indexes -1 ;
int Nidx = indexes.size();
List result(ncol);
for (int i=0; i < ncol; i++) {
colNames[i] = std::string(CHAR(STRING_ELT(names,i)));
SEXP colData = df(i);
bool isDateClass = false;
SEXP classname = Rf_getAttrib(colData, R_ClassSymbol);
if (classname != R_NilValue)
isDateClass = (strcmp(CHAR(STRING_ELT(classname,0)),"Date") == 0);
if (Rf_isReal(colData)) { // REAL
NumericVector subsetData(Nidx);
NumericVector v = as<NumericVector>(colData);
if (isDateClass) subsetData.attr("class") = "Date";
for (int j=0; j < Nidx; j++) {
int idx = indexes[j];
subsetData[j] = v[idx];
}
result[i] = subsetData;
//cout << "Real Col " << i << endl;
}
else if (Rf_isInteger(colData)) { // INTEGER
IntegerVector subsetData(Nidx);
IntegerVector v = as<IntegerVector>(colData);
if (isDateClass) subsetData.attr("class") = "Date";
for (int j=0; j < Nidx; j++) {
int idx = indexes[j];
subsetData[j] = v[idx];
}
result[i] = subsetData;
//cout << "Int Col " << i << endl;
}
else if (Rf_isString(colData)) { // Non-factor string column
CharacterVector subsetData(Nidx);
CharacterVector v = as<CharacterVector>(colData);
for (int j=0; j < Nidx; j++) {
int idx = indexes[j];
subsetData[j] = v[idx];
}
result[i] = subsetData;
}
else if (Rf_isFactor(colData)) { // Factor column.
IntegerVector subsetData(Nidx);
IntegerVector v = as<IntegerVector>(colData);
SEXP names = Rf_getAttrib(colData, R_LevelsSymbol);
int numLevels = Rf_length(names);
//std::string *levelNames = new std::string[numLevels];
CharacterVector levelNames = Rf_getAttrib(colData, R_LevelsSymbol);
//for (int k=0; k < numLevels; k++)
//levelNames[k] = std::string(CHAR(STRING_ELT(names,k)));
//for (int k=0; k < numLevels; k++) cout << levelNames[k] ;
//cout << endl;
//cout << " factor col" << endl;
subsetData.attr("levels") = levelNames;
subsetData.attr("class") = "factor";
for (int j=0; j < Nidx; j++) {
int idx = indexes[j];
subsetData[j] = v[idx];
}
result[i] = subsetData;
//delete [] levelNames;
}
else if (Rf_isLogical(colData)) { // LOGICAL
LogicalVector subsetData(Nidx);
LogicalVector v = as<LogicalVector>(colData);
for (int j=0; j < Nidx; j++) {
int idx = indexes[j];
subsetData[j] = v[idx];
}
result[i] = subsetData;
}
else { // WHEN COL IS NOT TREATED
CharacterVector vv(Nidx, "ERROR");
result[i] = vv;
}
}
result.attr("names") = colNames;
//result.attr("class") = "list";
//Rcpp::DataFrame res(result); // this makes it 100x slower than List ....
//cout << "end subset" << endl;
//return(res);
return(result);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
List getSubsetSplit(DataFrame Dsexp, List split_idx, int beg_index, int end_index, Function fun )
{
// get_split will apply function fun to each one of the split-indexes in the split_idx list.
// beg_index and end_index can be obtained from get_chunks_indexes_for_split_list,
// and will make possible to work with chunks of data.
// the function to apply MUST be working with a list of lists...
// so we should have like fu_apply <- function(x){ lapply(x, fu )}
// fu <- function(x) { c(min.date = min( x["date"] ) } for example....
DataFrame data = DataFrame(Dsexp);
Function fu = Function(fun);
List split_id = clone(split_idx);
int Napply = end_index - beg_index + 1 ;
int beg = (beg_index -1) ;
int end = (end_index) ;
List sol(Napply);
IntegerVector first_split = split_id[beg];
int first_index = first_split[0];
int lag_indexes = 0;
if(first_index != 1) { lag_indexes = first_index -1; };
//cout << "lag index for data : " << lag_indexes << endl;
int sol_index = 0;
for (int i=beg ; i < end ; i++){
IntegerVector idx = split_id(i) ;
IntegerVector idx_ = idx - lag_indexes; // take into account that the data can be subsetted , so first index of all is set to 1.
//cout << "f index " << idx_[0] << endl;
List sub_data = subsetCpp(data, idx_);
cout << "split " << i+1 << "/" << end << endl;
//indexes.push_back( idx );
sol[sol_index] = fu(sub_data);
sol_index += 1;
}
//sol = Lapply(indexes, fu);
//List examp = sol[0];
//CharacterVector names = Rf_getAttrib(examp, R_NamesSymbol);
//sol.attr("names") = names;
//DataFrame res(sol);
//return(res);
return(sol);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
Rcpp::List getSplitIndexes(Rcpp::DataFrame Dsexp, SEXP split_row_name)
{
// it will get the split indexes by same values of split_row_name.
// the result of this feeded into get_split will make a split-apply-combine.
Rcpp::DataFrame data = Rcpp::DataFrame(Dsexp);
//Rcpp::Function fu = Rcpp::Function(fun);
string split_name = Rcpp::as<string>(split_row_name);
Rcpp::IntegerVector ids = data[split_name];
int id_i = ids[0];
int N = ids.size();
Rcpp::IntegerVector indexes ;
std::set<int> s( ids.begin(), ids.end() );
int uniqueN = s.size();
// a way to call R functions::
//Environment base("package:base");
//Function rm = base["rm"];
//rm("s"","ids);
// another one ::
//Rcpp::Function length("length");
//int ll = Rcpp::as<int>(length(ids));
//cout << ll << endl;
printf("data size :: %d , uniques : %d \n", N, uniqueN);
Rcpp::List sol(uniqueN);
int nrows = 0;
int id_int = 0;
for (unsigned int i=0; i < (unsigned int) N; i++){
nrows += 1;
if(nrows%1000000 == 0) printf(" rows done :: %d\n", nrows);
int new_id = ids[i] ; // is there a new value for the split ??
if( new_id == id_i) {
indexes.push_back(i+1);
if(i == N-1 ){
sol[id_int] = indexes;
//sol[id_i-1] = indexes;
};
};
// change in id value..
if( new_id != id_i){
//printf(" id : %d\n", new_id);
int ll = indexes.size();
//printf(" indexes size : %d\n", ll);
sol[id_int] = indexes;
id_i = new_id;
id_int +=1;
Rcpp::IntegerVector indxx ;
indexes = indxx;
//indexes.clear();
indexes.push_back(i+1);
if(i == N-1) sol[id_int] = indexes;
};
}
return(sol);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
List Lapply(List data, Function fun){
List input(data);
Function f(fun);
List output(input.size());
transform( input.begin(), input.end(), output.begin(),f);
output.names() = input.names();
return output;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// [[Rcpp::export]]
DataFrame getChunksIndexesForSplitList(DataFrame data_, List split_indx, int chk_size){
// will make a DataFrame with info for indexes on split_indx list and DataFrame indexes
// to work on different chunks
// this is the output ::
//# chunk_number beg_split_index end_split_index beg_data_index end_data_index
//# 1 1 1 6586 1 100018
//# 2 2 6587 13372 100019 200001
List split_indexes(split_indx);
DataFrame data = data_;
int chuk_size = chk_size;
int chuk_row = chuk_size;
int Nsplits = ceil( data.nrows()/ ((double)chuk_size ) ) ;
// if(Nsplits == 0) Nsplits = 1;
//
printf("Nsplits %d , Nrows %d , chk_size %d \n", Nsplits,data.nrows(), chuk_size ) ;
int Nindexes = split_indexes.size();
IntegerVector chuk_split_index(Nsplits);
IntegerVector chuk_begin_data(Nsplits);
IntegerVector chuk_end_data(Nsplits);
//printf("Nsplits %d \n", Nsplits) ;
int split_number = 0 ;
//int last_i = 0 ;
for(int i = 0 ; i < Nindexes ; i++){
//printf("loop %d \n", i) ;
IntegerVector split = split_indexes(i) ;
int maxi = max(split);
if(maxi >= chuk_row ){
chuk_split_index[split_number] = i;
printf("split n %d chuk_row %d chuk_split_index %d \n" ,split_number, chuk_row, i);
chuk_row = chuk_row + chuk_size;
split_number = split_number + 1;
//last_i = i ;
}
}
//printf("done splits, split_number %d \n", split_number) ;
// if(split_number == Nsplits-1 & Nsplits != 1) {
if(split_number == Nsplits-1 ) {
chuk_split_index[split_number] = Nindexes -1; // case when we got until the end, but there were some remaining indexes ...
}
// make the vector of the begin indexes ::
IntegerVector chunk_number = seq_len(Nsplits);
IntegerVector chuk_beg(Nsplits);
chuk_beg[0] = 0 ;
for(int i = 1; i < Nsplits ; i++){
chuk_beg[i] = chuk_split_index[i-1] + 1;
}
// now make the vector of the beg and end data indexes :::
for(int i = 0; i < Nsplits; i++){
int chu = chuk_beg[i];
int idx = chuk_split_index[i];
//if(idx == )
IntegerVector spl_b = split_indexes(chu);
IntegerVector spl_e = split_indexes(idx);
chuk_begin_data[i] = min(spl_b);
chuk_end_data[i] = max(spl_e);
}
DataFrame DF = Rcpp::DataFrame::create(
Named("chunk_number")=chunk_number,
Named("beg_split_index")=chuk_beg +1 ,
Named("end_split_index")=chuk_split_index +1 ,
Named("beg_data_index")=chuk_begin_data,
Named("end_data_index")=chuk_end_data
);
//return( data.frame(chunk_number =chunk_number, beg_split_index = chuk_beg, end_split_index = chuk_split_index, beg_data_index = chuk_begin_data, end_data_index = chuk_end_data ))
return(DF);
}