diff --git a/src/shogun/preprocessor/DensePreprocessor.cpp b/src/shogun/preprocessor/DensePreprocessor.cpp index f8c9831d687..eb235eee4fe 100644 --- a/src/shogun/preprocessor/DensePreprocessor.cpp +++ b/src/shogun/preprocessor/DensePreprocessor.cpp @@ -92,12 +92,26 @@ CFeatures* CDensePreprocessor::apply(CFeatures* features, bool inplace) "has to be of C_DENSE (%d) class!\n", features->get_feature_class(), C_DENSE); - SGMatrix feat_matrix=apply_to_feature_matrix(features); - CDenseFeatures* preprocessed=new CDenseFeatures(feat_matrix); + auto matrix = features->as>()->get_feature_matrix(); + if (!inplace) + matrix = matrix.clone(); + auto feat_matrix = apply_to_matrix(matrix); + auto preprocessed = new CDenseFeatures(feat_matrix); SG_REF(preprocessed); return preprocessed; } +template +SGMatrix +CDensePreprocessor::apply_to_feature_matrix(CFeatures* features) +{ + auto simple_features = features->as>(); + auto feature_matrix = + apply_to_matrix(simple_features->get_feature_matrix()); + simple_features->set_feature_matrix(feature_matrix); + return feature_matrix; +} + template class CDensePreprocessor; template class CDensePreprocessor; template class CDensePreprocessor; diff --git a/src/shogun/preprocessor/DensePreprocessor.h b/src/shogun/preprocessor/DensePreprocessor.h index 689501e814a..6d10805ceae 100644 --- a/src/shogun/preprocessor/DensePreprocessor.h +++ b/src/shogun/preprocessor/DensePreprocessor.h @@ -44,11 +44,14 @@ template class CDensePreprocessor : public CPreprocessor /// apply preproc on feature matrix /// result in feature matrix /// return pointer to feature_matrix, i.e. f->get_feature_matrix(); - virtual SGMatrix apply_to_feature_matrix(CFeatures* features)=0; + // remove after cleaning up codebase + [[deprecated]] virtual SGMatrix + apply_to_feature_matrix(CFeatures* features); /// apply preproc on single feature vector /// result in feature matrix - virtual SGVector apply_to_feature_vector(SGVector vector)=0; + [[deprecated]] virtual SGVector + apply_to_feature_vector(SGVector vector) = 0; /// return that we are dense features (just fixed size matrices) virtual EFeatureClass get_feature_class(); @@ -58,6 +61,13 @@ template class CDensePreprocessor : public CPreprocessor /// return a type of preprocessor virtual EPreprocessorType get_type() const; + protected: + /** Apply preprocessor on matrix. Subclasses should try to apply in + * place to avoid copying. + * @param matrix the input feature matrix + * @return the matrix after applying the preprocessor + */ + virtual SGMatrix apply_to_matrix(SGMatrix matrix) = 0; }; }