Skip to content

Commit ff1fdb0

Browse files
committed
oops
1 parent 1596d9e commit ff1fdb0

6 files changed

Lines changed: 300 additions & 269 deletions

lib/cpp/preprocessing/longitudinal_features_lagger.cpp

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,38 @@
66

77
#include "tick/preprocessing/longitudinal_features_lagger.h"
88

9+
910
LongitudinalFeaturesLagger::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

3332
void 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

lib/cpp/preprocessing/longitudinal_features_lagger_mp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ std::vector<ArrayDouble2d> LongitudinalFeaturesLagger_MP::transform(
175175
if (features.size() != censoring.size())
176176
TICK_ERROR("features size and censoring size doesn\'t match");
177177

178-
std::pair<size_t, size_t> base_shape = {features[0].n_rows(), features[0].n_cols()};
178+
std::pair<ulong, ulong> base_shape = {features[0].n_rows(), features[0].n_cols()};
179179
for (ArrayDouble2d f : features)
180180
if (f.n_rows() != base_shape.first || f.n_cols() != base_shape.second)
181181
TICK_ERROR("All the elements of features should have the same shape");
@@ -223,7 +223,7 @@ std::vector<SSparseArrayDouble2dPtr> LongitudinalFeaturesLagger_MP::transform(
223223
if (features.size() != censoring.size())
224224
TICK_ERROR("features size and censoring size doesn\'t match");
225225

226-
std::pair<size_t, size_t> base_shape = {features[0]->n_rows(), features[0]->n_cols()};
226+
std::pair<ulong, ulong> base_shape = {features[0]->n_rows(), features[0]->n_cols()};
227227
n_intervals = base_shape.first;
228228
for (SSparseArrayDouble2dPtr f : features)
229229
if (f->n_rows() != base_shape.first || f->n_cols() != base_shape.second)

lib/cpp/preprocessing/sparse_longitudinal_features_product.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
#include "tick/preprocessing/sparse_longitudinal_features_product.h"
88
#include <map>
99

10-
SparseLongitudinalFeaturesProduct::SparseLongitudinalFeaturesProduct(
11-
const SBaseArrayDouble2dPtrList1D &features)
12-
: n_features(features[0]->n_cols()) {}
13-
1410
ulong SparseLongitudinalFeaturesProduct::get_feature_product_col(
1511
ulong col1, ulong col2, ulong n_cols) const {
1612
if (col1 > col2) { // ensure we have the right order as the following formula

lib/include/tick/preprocessing/longitudinal_features_lagger.h

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ class LongitudinalFeaturesLagger {
1515
protected:
1616
ulong n_intervals;
1717
SArrayULongPtr n_lags;
18-
ArrayULong col_offset;
19-
ulong n_samples;
20-
ulong n_observations;
2118
ulong n_features;
2219
ulong n_lagged_features;
20+
SArrayULongPtr col_offset;
2321

2422
public:
25-
LongitudinalFeaturesLagger(const SBaseArrayDouble2dPtrList1D &features,
26-
const SArrayULongPtr n_lags);
23+
// This exists solely for cereal/swig
24+
LongitudinalFeaturesLagger() = default;
2725

28-
void dense_lag_preprocessor(ArrayDouble2d &features, ArrayDouble2d &out,
26+
LongitudinalFeaturesLagger(ulong n_intervals,
27+
SArrayULongPtr n_lags);
28+
29+
void compute_col_offset(SArrayULongPtr n_lags);
30+
31+
void dense_lag_preprocessor(ArrayDouble2d &features,
32+
ArrayDouble2d &out,
2933
ulong censoring) const;
3034

3135
void sparse_lag_preprocessor(ArrayULong &row, ArrayULong &col,
@@ -34,14 +38,26 @@ class LongitudinalFeaturesLagger {
3438
ulong censoring) const;
3539

3640
template <class Archive>
37-
void serialize(Archive &ar) {
41+
void load(Archive &ar) {
42+
ar(CEREAL_NVP(n_intervals));
43+
ar(CEREAL_NVP(n_features));
44+
ar(CEREAL_NVP(n_lagged_features));
45+
46+
Array<ulong> temp_n_lags, temp_col_offset;
47+
ar(cereal::make_nvp("n_lags", temp_n_lags));
48+
49+
n_lags = temp_n_lags.as_sarray_ptr();
50+
col_offset = temp_col_offset.as_sarray_ptr();
51+
}
52+
53+
54+
template <class Archive>
55+
void save(Archive &ar) const {
3856
ar(CEREAL_NVP(n_intervals));
39-
ar(CEREAL_NVP(n_lags));
40-
ar(CEREAL_NVP(col_offset));
41-
ar(CEREAL_NVP(n_samples));
42-
ar(CEREAL_NVP(n_observations));
4357
ar(CEREAL_NVP(n_features));
4458
ar(CEREAL_NVP(n_lagged_features));
59+
ar(cereal::make_nvp("n_lags", *n_lags));
60+
ar(cereal::make_nvp("col_offset", *col_offset));
4561
}
4662
};
4763

0 commit comments

Comments
 (0)