jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
anomaly.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2011,2012 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #include "anomaly.hpp"
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "../anomaly/anomaly_factory.hpp"
24 #include "../anomaly/anomaly_base.hpp"
25 #include "../common/vector_util.hpp"
26 #include "../fv_converter/datum.hpp"
27 #include "../fv_converter/datum_to_fv_converter.hpp"
28 #include "../fv_converter/converter_config.hpp"
29 #include "../fv_converter/revert.hpp"
30 #include "../storage/storage_factory.hpp"
31 
32 using std::string;
33 using std::vector;
34 using std::pair;
37 using jubatus::util::lang::shared_ptr;
38 
39 namespace jubatus {
40 namespace core {
41 namespace driver {
42 
44  shared_ptr<jubatus::core::anomaly::anomaly_base> anomaly_method,
45  shared_ptr<fv_converter::datum_to_fv_converter> converter)
46  : converter_(converter),
47  anomaly_(anomaly_method),
48  wm_(mixable_weight_manager::model_ptr(new weight_manager)) {
49  vector<framework::mixable*> mixables = anomaly_->get_mixables();
50  for (size_t i = 0; i < mixables.size(); i++) {
51  register_mixable(mixables[i]);
52  }
54 
55  converter_->set_weight_manager(wm_.get_model());
56 }
57 
59 }
60 
61 bool anomaly::is_updatable() const {
62  return anomaly_->is_updatable();
63 }
64 
65 void anomaly::clear_row(const std::string& id) {
66  anomaly_->clear_row(id);
67 }
68 
69 pair<string, float> anomaly::add(
70  const string& id,
71  const fv_converter::datum& d) {
72  if (anomaly_->is_updatable()) {
73  return make_pair(id, this->update(id, d));
74  } else {
75  return make_pair(id, this->overwrite(id, d));
76  }
77 }
78 
79 float anomaly::update(const string& id, const fv_converter::datum& d) {
81  converter_->convert_and_update_weight(d, v);
82 
83  anomaly_->update_row(id, v);
84  return anomaly_->calc_anomaly_score(id);
85 }
86 
87 float anomaly::overwrite(const string& id, const fv_converter::datum& d) {
89  converter_->convert_and_update_weight(d, v);
90 
91  anomaly_->set_row(id, v);
92  return anomaly_->calc_anomaly_score(id);
93 }
94 
96  anomaly_->clear();
97  converter_->clear_weights();
98 }
99 
102  converter_->convert(d, v);
103  return anomaly_->calc_anomaly_score(v);
104 }
105 
106 vector<string> anomaly::get_all_rows() const {
107  vector<string> ids;
108  anomaly_->get_all_row_ids(ids);
109  return ids;
110 }
111 
112 uint64_t anomaly::find_max_int_id() const {
113  return anomaly_->find_max_int_id();
114 }
115 
117  pk.pack_array(2);
118  anomaly_->pack(pk);
119  wm_.get_model()->pack(pk);
120 }
121 
122 void anomaly::unpack(msgpack::object o) {
123  if (o.type != msgpack::type::ARRAY || o.via.array.size != 2) {
124  throw msgpack::type_error();
125  }
126 
127  // clear before load
128  anomaly_->clear();
129  converter_->clear_weights();
130  anomaly_->unpack(o.via.array.ptr[0]);
131  wm_.get_model()->unpack(o.via.array.ptr[1]);
132 }
133 
134 } // namespace driver
135 } // namespace core
136 } // namespace jubatus
fv_converter::mixable_weight_manager wm_
Definition: anomaly.hpp:71
void unpack(msgpack::object o)
Definition: anomaly.cpp:122
jubatus::util::lang::shared_ptr< fv_converter::datum_to_fv_converter > converter_
Definition: anomaly.hpp:69
float calc_score(const fv_converter::datum &d) const
Definition: anomaly.cpp:100
jubatus::util::lang::shared_ptr< core::anomaly::anomaly_base > anomaly_
Definition: anomaly.hpp:70
uint64_t find_max_int_id() const
Definition: anomaly.cpp:112
std::pair< std::string, float > add(const std::string &id, const fv_converter::datum &d)
Definition: anomaly.cpp:69
framework::linear_mixable_helper< weight_manager, versioned_weight_diff > mixable_weight_manager
float overwrite(const std::string &id, const fv_converter::datum &d)
Definition: anomaly.cpp:87
anomaly(jubatus::util::lang::shared_ptr< core::anomaly::anomaly_base > anomaly_method, jubatus::util::lang::shared_ptr< fv_converter::datum_to_fv_converter > converter)
Definition: anomaly.cpp:43
void clear_row(const std::string &id)
Definition: anomaly.cpp:65
std::vector< T > v(size)
void pack(framework::packer &pk) const
Definition: anomaly.cpp:116
std::vector< std::string > get_all_rows() const
Definition: anomaly.cpp:106
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
void register_mixable(framework::mixable *mixable)
Definition: driver.cpp:242
float update(const std::string &id, const fv_converter::datum &d)
Definition: anomaly.cpp:79
storage::mixable_lsh_index_storage::model_ptr model_ptr
Definition: euclid_lsh.cpp:90