66
77#include " tick/preprocessing/longitudinal_features_lagger.h"
88
9+
910LongitudinalFeaturesLagger::LongitudinalFeaturesLagger (
10- const SBaseArrayDouble2dPtrList1D &features, const SArrayULongPtr n_lags)
11- : n_intervals(features[0 ]->n_rows ()),
12- n_lags(n_lags),
13- n_samples(features.size()),
14- n_observations(n_samples * n_intervals),
15- n_features(features[0 ]->n_cols ()),
16- n_lagged_features(n_lags->sum () + n_lags->size()) {
17- col_offset = ArrayULong (n_lags->size ());
18- col_offset.init_to_zero ();
19- if (n_features != n_lags->size ()) {
20- TICK_ERROR (" Features matrix column number should match n_lags length." );
21- }
22- if ((*n_lags)[0 ] >= n_intervals) {
23- TICK_ERROR (" n_lags elements must be between 0 and (n_intervals - 1)." );
24- }
11+ ulong n_intervals,
12+ SArrayULongPtr _n_lags)
13+ : n_intervals(n_intervals),
14+ n_lags(_n_lags),
15+ n_features(_n_lags->size ()),
16+ n_lagged_features(_n_lags->size () + _n_lags->sum()) {
17+ if (n_lags != nullptr ) compute_col_offset (n_lags);
18+ }
19+
20+ void LongitudinalFeaturesLagger::compute_col_offset (const SArrayULongPtr n_lags) {
21+ ArrayULong col_offset_temp = ArrayULong (n_lags->size ());
22+ col_offset_temp.init_to_zero ();
2523 for (ulong i (1 ); i < n_lags->size (); i++) {
2624 if ((*n_lags)[i] >= n_intervals) {
2725 TICK_ERROR (" n_lags elements must be between 0 and (n_intervals - 1)." );
2826 }
29- col_offset [i] = col_offset [i - 1 ] + (*n_lags)[i - 1 ] + 1 ;
27+ col_offset_temp [i] = col_offset_temp [i - 1 ] + (*n_lags)[i- 1 ] + 1 ;
3028 }
29+ col_offset = col_offset_temp.as_sarray_ptr ();
3130}
3231
3332void LongitudinalFeaturesLagger::dense_lag_preprocessor (ArrayDouble2d &features,
3433 ArrayDouble2d &out,
3534 ulong censoring) const {
35+ if (n_intervals != features.n_rows ()) {
36+ TICK_ERROR (" Features matrix rows count should match n_intervals." );
37+ }
38+ if (n_features != features.n_cols ()) {
39+ TICK_ERROR (" Features matrix column count should match n_lags length." );
40+ }
3641 if (out.n_cols () != n_lagged_features) {
3742 TICK_ERROR (
3843 " n_columns of &out should be equal to n_features + sum(n_lags)." );
@@ -46,8 +51,9 @@ void LongitudinalFeaturesLagger::dense_lag_preprocessor(ArrayDouble2d &features,
4651 n_cols_feature = (*n_lags)[feature] + 1 ;
4752 for (ulong j = 0 ; j < n_intervals; j++) {
4853 row = j;
49- col = col_offset[feature];
50- value = features (row, feature);
54+ col = (*col_offset)[feature];
55+ // use view_row instead of (row, feature) to be const
56+ value = view_row (features, row)[feature];
5157 max_col = col + n_cols_feature;
5258 if (value != 0 ) {
5359 while (row < censoring && col < max_col) {
@@ -60,17 +66,22 @@ void LongitudinalFeaturesLagger::dense_lag_preprocessor(ArrayDouble2d &features,
6066 }
6167}
6268
63- void LongitudinalFeaturesLagger::sparse_lag_preprocessor (
64- ArrayULong &row, ArrayULong &col, ArrayDouble &data, ArrayULong &out_row,
65- ArrayULong &out_col, ArrayDouble &out_data, ulong censoring) const {
69+ void LongitudinalFeaturesLagger::sparse_lag_preprocessor (ArrayULong &row,
70+ ArrayULong &col,
71+ ArrayDouble &data,
72+ ArrayULong &out_row,
73+ ArrayULong &out_col,
74+ ArrayDouble &out_data,
75+ ulong censoring) const {
76+ // TODO: add checks here ? Or do them in Python ?
6677 ulong j (0 ), r, c, offset, new_col, max_col;
6778 double value;
6879
6980 for (ulong i = 0 ; i < data.size (); i++) {
7081 value = data[i];
7182 r = row[i];
7283 c = col[i];
73- offset = col_offset[c];
84+ offset = (* col_offset) [c];
7485 max_col = offset + (*n_lags)[c] + 1 ;
7586 new_col = offset;
7687
0 commit comments