Turi Create  4.0
transform_to_flat_dictionary.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_UNITY_TOOLKITS_TRANSFORM_TO_FLAT_DICT_HPP
7 #define TURI_UNITY_TOOLKITS_TRANSFORM_TO_FLAT_DICT_HPP
8 
9 #include <string>
10 #include <model_server/lib/toolkit_class_macros.hpp>
11 #include <toolkits/feature_engineering/dict_transform_utils.hpp>
12 #include <core/export.hpp>
13 #include <toolkits/feature_engineering/transformer_base.hpp>
14 
15 namespace turi {
16 namespace sdk_model {
17 namespace feature_engineering {
18 
19 class EXPORT transform_to_flat_dictionary : public transformer_base {
20 
21  static constexpr size_t TRANSFORM_TO_FLAT_DICTIONARY_VERSION = 0;
22  std::map<std::string, flex_type_enum> feature_types;
23  std::vector<std::string> feature_columns;
24  flexible_type unprocessed_features; // Input provided by the user.
25  bool exclude = false;
26  bool fitted = false;
27 
28  public:
29 
30  /**
31  * Methods that must be implemented in a new transformer model.
32  * -------------------------------------------------------------------------
33  */
34 
35  virtual inline ~transform_to_flat_dictionary() {}
36 
37  /**
38  * Set one of the options in the model. Use the option manager to set
39  * these options. If the option does not satisfy the conditions that the
40  * option manager has imposed on it. Errors will be thrown.
41  *
42  * \param[in] options Options to set
43  */
44  void init_options(const std::map<std::string, flexible_type>&_options) override;
45 
46  /**
47  * Get a version for the object.
48  */
49  size_t get_version() const override;
50 
51  /**
52  * Save the object using Turi's oarc.
53  */
54  void save_impl(turi::oarchive& oarc) const override;
55 
56  /**
57  * Load the object using Turi's iarc.
58  */
59  void load_version(turi::iarchive& iarc, size_t version) override;
60 
61 
62  /**
63  * Initialize the transformer.
64  */
65  void init_transformer(const std::map<std::string,
66  flexible_type>& _options) override;
67 
68  /**
69  * Set constant.
70  *
71  * \param[in] data (SFrame of data)
72  */
73  void fit(gl_sframe data) override;
74 
75  /**
76  * Transform the given data.
77  *
78  * \param[in] data (SFrame of data)
79  *
80  * Python side interface
81  * ------------------------
82  * This function directly interfaces with "transform" in python.
83  *
84  */
85  gl_sframe transform(gl_sframe data) override;
86 
87  /**
88  * Fit and transform the given data. Intended as an optimization because
89  * fit and transform are usually always called together. The default
90  * implementaiton calls fit and then transform.
91  *
92  * \param[in] data (SFrame of data)
93  */
94  gl_sframe fit_transform(gl_sframe data) {
95  fit(data);
96  return transform(data);
97  }
98 
99  // Functions that all transformers need to register. Can be copied verbatim
100  // for other classes.
101  // --------------------------------------------------------------------------
102  BEGIN_CLASS_MEMBER_REGISTRATION("_TransformToFlatDictionary")
103  REGISTER_CLASS_MEMBER_FUNCTION(transform_to_flat_dictionary::init_transformer, "_options");
104  REGISTER_CLASS_MEMBER_FUNCTION(transform_to_flat_dictionary::fit, "data");
105  REGISTER_CLASS_MEMBER_FUNCTION(transform_to_flat_dictionary::fit_transform, "data");
106  REGISTER_CLASS_MEMBER_FUNCTION(transform_to_flat_dictionary::transform, "data");
107  REGISTER_CLASS_MEMBER_FUNCTION(transform_to_flat_dictionary::get_current_options);
108  REGISTER_CLASS_MEMBER_FUNCTION(transform_to_flat_dictionary::list_fields);
109  REGISTER_NAMED_CLASS_MEMBER_FUNCTION("_get_default_options",
110  transform_to_flat_dictionary::get_default_options);
112  transform_to_flat_dictionary::get_value_from_state,
113  "key");
115 
116 };
117 
118 } // feature_engineering
119 } // sdk_model
120 } // turicreate
121 #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
#define END_CLASS_MEMBER_REGISTRATION
#define REGISTER_NAMED_CLASS_MEMBER_FUNCTION(name, function,...)
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