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