Turi Create  4.0
sample_transformer.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_SAMPLE_TRANSFORMER_H_
7 #define TURI_SAMPLE_TRANSFORMER_H_
8 #include <string>
9 #include <model_server/lib/toolkit_class_macros.hpp>
10 #include <toolkits/feature_engineering/transformer_base.hpp>
11 #include <core/export.hpp>
12 
13 namespace turi {
14 namespace sdk_model {
15 namespace feature_engineering {
16 
17 /**
18  * transformer_base example toolkit
19  * ---------------------------------------------
20  *
21  * This example class also serves as test cases for the transformer
22  * base class. Copy the class verbatim and change things for your
23  * transformer goodness.
24  *
25  * Methods that must be implemented
26  * -----------------------------------------
27  *
28  * Functions that should always be implemented.
29  *
30  * init_transformers
31  * fit
32  * transform
33  * init_options
34  * get_version
35  * save_impl
36  * load_version
37  *
38  * This class interfaces with the TransformerBaseNative class in Python and works
39  * end to end once the following set of fuctions are implemented by the user.
40  *
41  *
42  * Example Class
43  * -----------------------------------------------------------------------------
44  *
45  * This class does the wonderously complicated task of transforming your data
46  * to a constant, no matter what you give it.
47  *
48  */
49 class EXPORT sample_transformer : public transformer_base {
50 
51  static constexpr size_t SAMPLE_TRANSFORMER_VERSION = 0;
52  double constant = 0; /**< Transformation */
53 
54  public:
55 
56  /**
57  * Methods that must be implemented in a new transformer model.
58  * -------------------------------------------------------------------------
59  */
60 
61  virtual inline ~sample_transformer() {}
62 
63  /**
64  * Set one of the options in the model. Use the option manager to set
65  * these options. If the option does not satisfy the conditions that the
66  * option manager has imposed on it. Errors will be thrown.
67  *
68  * \param[in] options Options to set
69  */
70  void init_options(const std::map<std::string, flexible_type>&_options) override;
71 
72  /**
73  * Get a version for the object.
74  */
75  size_t get_version() const override;
76 
77  /**
78  * Save the object using Turi's oarc.
79  */
80  void save_impl(turi::oarchive& oarc) const override;
81 
82  /**
83  * Load the object using Turi's iarc.
84  */
85  void load_version(turi::iarchive& iarc, size_t version) override;
86 
87 
88  /**
89  * Initialize the transformer.
90  */
91  void init_transformer(const std::map<std::string,
92  flexible_type>& _options) override;
93 
94  /**
95  * Set constant.
96  *
97  * \param[in] data (SFrame of data)
98  */
99  void fit(gl_sframe data) override;
100 
101  /**
102  * Transform the given data.
103  *
104  * \param[in] data (SFrame of data)
105  *
106  * Python side interface
107  * ------------------------
108  * This function directly interfaces with "transform" in python.
109  *
110  */
111  gl_sframe transform(gl_sframe data) override;
112 
113  // Helper functions.
114  // --------------------------------------------------------------------------
115  inline double get_constant() { return constant;}
116 
117  // Functions that all transformers need to register. Can be copied verbatim
118  // for other classes.
119  // --------------------------------------------------------------------------
120  BEGIN_CLASS_MEMBER_REGISTRATION("_SampleTransformer")
127  REGISTER_NAMED_CLASS_MEMBER_FUNCTION("_get_default_options",
131  "key");
133 
134 };
135 
136 
137 } // feature_engineering
138 } // sdk_model
139 } // turicreate
140 #endif
#define BEGIN_CLASS_MEMBER_REGISTRATION(python_facing_classname)
#define REGISTER_CLASS_MEMBER_FUNCTION(function,...)
The serialization input archive object which, provided with a reference to an istream, will read from the istream, providing deserialization capabilities.
Definition: iarchive.hpp:60
const std::map< std::string, flexible_type > & get_current_options() const
#define END_CLASS_MEMBER_REGISTRATION
void init_transformer(const std::map< std::string, flexible_type > &_options) override
const variant_type & get_value_from_state(std::string key)
std::vector< std::string > list_fields()
#define REGISTER_NAMED_CLASS_MEMBER_FUNCTION(name, function,...)
std::map< std::string, flexible_type > get_default_options() const
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
Definition: oarchive.hpp:80
void transform(S &&input, T &&output, TransformFn transformfn, std::set< size_t > constraint_segments=std::set< size_t >())
Definition: algorithm.hpp:64