jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
Classes | Public Member Functions | Static Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
jubatus::core::anomaly::lof_storage Class Reference

#include <lof_storage.hpp>

Collaboration diagram for jubatus::core::anomaly::lof_storage:
Collaboration graph

Classes

struct  config
 

Public Member Functions

void clear ()
 
float collect_lrds (const common::sfv_t &query, jubatus::util::data::unordered_map< std::string, float > &neighbor_lrd) const
 
float collect_lrds (const std::string &id, jubatus::util::data::unordered_map< std::string, float > &neighbor_lrd) const
 
void get_all_row_ids (std::vector< std::string > &ids) const
 
void get_diff (lof_table_t &diff) const
 
float get_kdist (const std::string &row) const
 
float get_lrd (const std::string &row) const
 
storage::version get_version () const
 
bool has_row (const std::string &row) const
 
 lof_storage ()
 
 lof_storage (jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine)
 
 lof_storage (const config &config, jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine)
 
void mix (const lof_table_t &lhs, lof_table_t &rhs) const
 
 MSGPACK_DEFINE (lof_table_, lof_table_diff_, neighbor_num_, reverse_nn_num_)
 
std::string name () const
 
void pack (framework::packer &packer) const
 
bool put_diff (const lof_table_t &mixed_diff)
 
void remove_row (const std::string &row)
 
void set_nn_engine (jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine)
 
void unpack (msgpack::object o)
 
void update_all ()
 
void update_row (const std::string &row, const common::sfv_t &diff)
 
virtual ~lof_storage ()
 

Static Public Attributes

static const uint32_t DEFAULT_NEIGHBOR_NUM = 10
 
static const uint32_t DEFAULT_REVERSE_NN_NUM = 30
 

Private Member Functions

float collect_lrds_from_neighbors (const std::vector< std::pair< std::string, float > > &neighbors, jubatus::util::data::unordered_map< std::string, float > &neighbor_lrd) const
 
void collect_neighbors (const std::string &row, jubatus::util::data::unordered_set< std::string > &nn) const
 
void update_entries (const jubatus::util::data::unordered_set< std::string > &rows)
 
void update_kdist (const std::string &row)
 
void update_kdist_with_neighbors (const std::string &row, const std::vector< std::pair< std::string, float > > &neighbors)
 
void update_lrd (const std::string &row)
 
void update_lrd_with_neighbors (const std::string &row, const std::vector< std::pair< std::string, float > > &neighbors)
 

Static Private Member Functions

static bool is_removed (const lof_entry &entry)
 
static void mark_removed (lof_entry &entry)
 

Private Attributes

lof_table_t lof_table_
 
lof_table_t lof_table_diff_
 
uint32_t neighbor_num_
 
jubatus::util::lang::shared_ptr< core::recommender::recommender_basenn_engine_
 
uint32_t reverse_nn_num_
 

Detailed Description

Definition at line 52 of file lof_storage.hpp.

Constructor & Destructor Documentation

jubatus::core::anomaly::lof_storage::lof_storage ( )

Definition at line 59 of file lof_storage.cpp.

63  "euclid_lsh",
64  common::jsonconfig::config(jubatus::util::text::json::to_json(
65  recommender::euclid_lsh::config())), "")) {
66 }
static jubatus::util::lang::shared_ptr< recommender_base > create_recommender(const std::string &name, const common::jsonconfig::config &param, const std::string &id)
static const uint32_t DEFAULT_NEIGHBOR_NUM
Definition: lof_storage.hpp:54
static const uint32_t DEFAULT_REVERSE_NN_NUM
Definition: lof_storage.hpp:55
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_
jubatus::core::anomaly::lof_storage::lof_storage ( jubatus::util::lang::shared_ptr< core::recommender::recommender_base nn_engine)
explicit
jubatus::core::anomaly::lof_storage::lof_storage ( const config config,
jubatus::util::lang::shared_ptr< core::recommender::recommender_base nn_engine 
)
explicit
jubatus::core::anomaly::lof_storage::~lof_storage ( )
virtual

Definition at line 83 of file lof_storage.cpp.

83  {
84 }

Member Function Documentation

void jubatus::core::anomaly::lof_storage::clear ( )

Definition at line 125 of file lof_storage.cpp.

References lof_table_, lof_table_diff_, and nn_engine_.

125  {
126  lof_table_t().swap(lof_table_);
128  nn_engine_->clear();
129 }
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_
jubatus::util::data::unordered_map< std::string, lof_entry > lof_table_t
Definition: lof_storage.hpp:50
float jubatus::core::anomaly::lof_storage::collect_lrds ( const common::sfv_t query,
jubatus::util::data::unordered_map< std::string, float > &  neighbor_lrd 
) const
float jubatus::core::anomaly::lof_storage::collect_lrds ( const std::string &  id,
jubatus::util::data::unordered_map< std::string, float > &  neighbor_lrd 
) const
float jubatus::core::anomaly::lof_storage::collect_lrds_from_neighbors ( const std::vector< std::pair< std::string, float > > &  neighbors,
jubatus::util::data::unordered_map< std::string, float > &  neighbor_lrd 
) const
private

Based on the given neighbors (list of ID and the distance to it), get the LRDs for each neighbor points and return the calculated LRD for the point. neighbors donesn't have to be sorted.

Definition at line 260 of file lof_storage.cpp.

References get_kdist(), and get_lrd().

262  {
263  if (neighbors.empty()) {
264  return numeric_limits<float>::infinity();
265  }
266 
267  // collect lrd values of the nearest neighbors
268  neighbor_lrd.clear();
269  for (size_t i = 0; i < neighbors.size(); ++i) {
270  neighbor_lrd[neighbors[i].first] = get_lrd(neighbors[i].first);
271  }
272 
273  // return lrd of the query
274  float sum_reachability = 0;
275  for (size_t i = 0; i < neighbors.size(); ++i) {
276  sum_reachability += max(neighbors[i].second, get_kdist(neighbors[i].first));
277  }
278 
279  // All the neighbors seem to have a same feature vector.
280  if (sum_reachability == 0) {
281  return numeric_limits<float>::infinity();
282  }
283 
284  return neighbors.size() / sum_reachability;
285 }
float get_kdist(const std::string &row) const
float get_lrd(const std::string &row) const

Here is the call graph for this function:

void jubatus::core::anomaly::lof_storage::collect_neighbors ( const std::string &  row,
jubatus::util::data::unordered_set< std::string > &  nn 
) const
private

Collect neighbors for the given ID.

Definition at line 290 of file lof_storage.cpp.

References nn_engine_, and reverse_nn_num_.

Referenced by update_row().

292  {
293  vector<pair<string, float> > neighbors;
294  nn_engine_->neighbor_row(row, neighbors, reverse_nn_num_);
295 
296  for (size_t i = 0; i < neighbors.size(); ++i) {
297  nn.insert(neighbors[i].first);
298  }
299 }
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_

Here is the caller graph for this function:

void jubatus::core::anomaly::lof_storage::get_all_row_ids ( std::vector< std::string > &  ids) const

Definition at line 131 of file lof_storage.cpp.

References nn_engine_.

Referenced by update_all().

131  {
132  nn_engine_->get_all_row_ids(ids);
133 }
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_

Here is the caller graph for this function:

void jubatus::core::anomaly::lof_storage::get_diff ( lof_table_t diff) const

Definition at line 215 of file lof_storage.cpp.

References lof_table_diff_.

215  {
216  diff = lof_table_diff_;
217 }
float jubatus::core::anomaly::lof_storage::get_kdist ( const std::string &  row) const

Definition at line 158 of file lof_storage.cpp.

References is_removed(), JUBATUS_EXCEPTION, lof_table_, and lof_table_diff_.

Referenced by collect_lrds_from_neighbors(), and update_lrd_with_neighbors().

158  {
159  lof_table_t::const_iterator it = lof_table_diff_.find(row);
160  if (it == lof_table_diff_.end()) {
161  it = lof_table_.find(row);
162  if (it == lof_table_.end()) {
163  throw JUBATUS_EXCEPTION(
164  common::exception::runtime_error("specified row does not exist")
165  << common::exception::error_message("row id: " + row));
166  }
167  } else if (is_removed(it->second)) {
168  throw JUBATUS_EXCEPTION(
169  common::exception::runtime_error("specified row is recently removed")
170  << common::exception::error_message("row id: " + row));
171  }
172  return it->second.kdist;
173 }
static bool is_removed(const lof_entry &entry)
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
error_info< struct error_message_, std::string > error_message
Definition: exception.hpp:59

Here is the call graph for this function:

Here is the caller graph for this function:

float jubatus::core::anomaly::lof_storage::get_lrd ( const std::string &  row) const

Definition at line 175 of file lof_storage.cpp.

References is_removed(), JUBATUS_EXCEPTION, lof_table_, and lof_table_diff_.

Referenced by collect_lrds_from_neighbors().

175  {
176  lof_table_t::const_iterator it = lof_table_diff_.find(row);
177  if (it == lof_table_diff_.end()) {
178  it = lof_table_.find(row);
179  if (it == lof_table_.end()) {
180  throw JUBATUS_EXCEPTION(
181  common::exception::runtime_error("specified row does not exist")
182  << common::exception::error_message("row id: " + row));
183  }
184  } else if (is_removed(it->second)) {
185  throw JUBATUS_EXCEPTION(
186  common::exception::runtime_error("specified row is recently removed")
187  << common::exception::error_message("row id: " + row));
188  }
189  return it->second.lrd;
190 }
static bool is_removed(const lof_entry &entry)
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
error_info< struct error_message_, std::string > error_message
Definition: exception.hpp:59

Here is the call graph for this function:

Here is the caller graph for this function:

storage::version jubatus::core::anomaly::lof_storage::get_version ( ) const
inline

Definition at line 120 of file lof_storage.hpp.

120  {
121  return storage::version();
122  }
bool jubatus::core::anomaly::lof_storage::has_row ( const std::string &  row) const

Definition at line 192 of file lof_storage.cpp.

References lof_table_, and lof_table_diff_.

192  {
193  return lof_table_diff_.count(row) > 0 || lof_table_.count(row) > 0;
194 }
bool jubatus::core::anomaly::lof_storage::is_removed ( const lof_entry entry)
staticprivate

Definition at line 250 of file lof_storage.cpp.

References jubatus::core::anomaly::lof_entry::kdist.

Referenced by get_kdist(), get_lrd(), mix(), and put_diff().

250  {
251  return entry.kdist < 0;
252 }

Here is the caller graph for this function:

void jubatus::core::anomaly::lof_storage::mark_removed ( lof_entry entry)
staticprivate

Definition at line 245 of file lof_storage.cpp.

References jubatus::core::anomaly::lof_entry::kdist.

Referenced by mix(), and remove_row().

245  {
246  entry.kdist = -1;
247 }

Here is the caller graph for this function:

void jubatus::core::anomaly::lof_storage::mix ( const lof_table_t lhs,
lof_table_t rhs 
) const

Definition at line 232 of file lof_storage.cpp.

References is_removed(), and mark_removed().

232  {
233  for (lof_table_t::const_iterator it = lhs.begin(); it != lhs.end(); ++it) {
234  if (is_removed(it->second)) {
235  mark_removed(rhs[it->first]);
236  } else {
237  rhs.insert(*it);
238  }
239  }
240 }
static bool is_removed(const lof_entry &entry)
static void mark_removed(lof_entry &entry)

Here is the call graph for this function:

jubatus::core::anomaly::lof_storage::MSGPACK_DEFINE ( lof_table_  ,
lof_table_diff_  ,
neighbor_num_  ,
reverse_nn_num_   
)
string jubatus::core::anomaly::lof_storage::name ( ) const

Definition at line 154 of file lof_storage.cpp.

154  {
155  return "lof_storage";
156 }
void jubatus::core::anomaly::lof_storage::pack ( framework::packer packer) const
inline

Definition at line 124 of file lof_storage.hpp.

124  {
125  packer.pack(*this);
126  }
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
bool jubatus::core::anomaly::lof_storage::put_diff ( const lof_table_t mixed_diff)

Definition at line 219 of file lof_storage.cpp.

References is_removed(), lof_table_, and lof_table_diff_.

219  {
220  for (lof_table_t::const_iterator it = mixed_diff.begin();
221  it != mixed_diff.end(); ++it) {
222  if (is_removed(it->second)) {
223  lof_table_.erase(it->first);
224  } else {
225  lof_table_[it->first] = it->second;
226  }
227  }
228  lof_table_diff_.clear();
229  return true;
230 }
static bool is_removed(const lof_entry &entry)

Here is the call graph for this function:

void jubatus::core::anomaly::lof_storage::remove_row ( const std::string &  row)

Definition at line 120 of file lof_storage.cpp.

References lof_table_diff_, mark_removed(), and nn_engine_.

120  {
122  nn_engine_->clear_row(row);
123 }
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_
static void mark_removed(lof_entry &entry)

Here is the call graph for this function:

void jubatus::core::anomaly::lof_storage::set_nn_engine ( jubatus::util::lang::shared_ptr< core::recommender::recommender_base nn_engine)

Definition at line 210 of file lof_storage.cpp.

References nn_engine_.

211  {
212  nn_engine_ = nn_engine;
213 }
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_
void jubatus::core::anomaly::lof_storage::unpack ( msgpack::object  o)
inline

Definition at line 127 of file lof_storage.hpp.

127  {
128  o.convert(this);
129  }
void jubatus::core::anomaly::lof_storage::update_all ( )

Definition at line 196 of file lof_storage.cpp.

References get_all_row_ids(), update_kdist(), and update_lrd().

196  {
197  vector<string> ids;
198  get_all_row_ids(ids);
199 
200  // NOTE: These two loops are separated, since update_lrd requires new kdist
201  // values of k-NN.
202  for (size_t i = 0; i < ids.size(); ++i) {
203  update_kdist(ids[i]);
204  }
205  for (size_t i = 0; i < ids.size(); ++i) {
206  update_lrd(ids[i]);
207  }
208 }
void update_kdist(const std::string &row)
void get_all_row_ids(std::vector< std::string > &ids) const
void update_lrd(const std::string &row)

Here is the call graph for this function:

void jubatus::core::anomaly::lof_storage::update_entries ( const jubatus::util::data::unordered_set< std::string > &  rows)
private

Update kdist and LRD for given points and its neighbors.

Definition at line 304 of file lof_storage.cpp.

References neighbor_num_, nn_engine_, update_kdist_with_neighbors(), and update_lrd_with_neighbors().

Referenced by update_row().

304  {
305  // NOTE: These two loops are separated, since update_lrd requires new kdist
306  // values of k-NN.
307  typedef unordered_map<string, vector<pair<string, float> > >
308  rows_to_neighbors_type;
309 
310  rows_to_neighbors_type rows_to_neighbors;
311  for (unordered_set<string>::const_iterator it = rows.begin();
312  it != rows.end(); ++it) {
313  nn_engine_->neighbor_row(*it, rows_to_neighbors[*it], neighbor_num_);
314  }
315 
316  for (rows_to_neighbors_type::const_iterator it = rows_to_neighbors.begin();
317  it != rows_to_neighbors.end(); ++it) {
318  update_kdist_with_neighbors(it->first, it->second);
319  }
320  for (rows_to_neighbors_type::const_iterator it = rows_to_neighbors.begin();
321  it != rows_to_neighbors.end(); ++it) {
322  update_lrd_with_neighbors(it->first, it->second);
323  }
324 }
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_
void update_lrd_with_neighbors(const std::string &row, const std::vector< std::pair< std::string, float > > &neighbors)
void update_kdist_with_neighbors(const std::string &row, const std::vector< std::pair< std::string, float > > &neighbors)

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::anomaly::lof_storage::update_kdist ( const std::string &  row)
private

Update kdist for the row.

Definition at line 329 of file lof_storage.cpp.

References neighbor_num_, nn_engine_, and update_kdist_with_neighbors().

Referenced by update_all().

329  {
330  vector<pair<string, float> > neighbors;
331  nn_engine_->neighbor_row(row, neighbors, neighbor_num_);
332  update_kdist_with_neighbors(row, neighbors);
333 }
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_
void update_kdist_with_neighbors(const std::string &row, const std::vector< std::pair< std::string, float > > &neighbors)

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::anomaly::lof_storage::update_kdist_with_neighbors ( const std::string &  row,
const std::vector< std::pair< std::string, float > > &  neighbors 
)
private

Update kdist for the row using given NN search result (neighbors). Note that this method expects neighbors to be sorted by score.

Definition at line 339 of file lof_storage.cpp.

References lof_table_diff_.

Referenced by update_entries(), and update_kdist().

341  {
342  if (!neighbors.empty()) {
343  lof_table_diff_[row].kdist = neighbors.back().second;
344  }
345 }

Here is the caller graph for this function:

void jubatus::core::anomaly::lof_storage::update_lrd ( const std::string &  row)
private

Update LRD for the row.

Definition at line 350 of file lof_storage.cpp.

References neighbor_num_, nn_engine_, and update_lrd_with_neighbors().

Referenced by update_all().

350  {
351  vector<pair<string, float> > neighbors;
352  nn_engine_->neighbor_row(row, neighbors, neighbor_num_);
353  update_lrd_with_neighbors(row, neighbors);
354 }
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_
void update_lrd_with_neighbors(const std::string &row, const std::vector< std::pair< std::string, float > > &neighbors)

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::anomaly::lof_storage::update_lrd_with_neighbors ( const std::string &  row,
const std::vector< std::pair< std::string, float > > &  neighbors 
)
private

Update LRD for the row using given NN search result (neighbors).

Definition at line 359 of file lof_storage.cpp.

References get_kdist(), lof_table_diff_, and neighbor_num_.

Referenced by update_entries(), and update_lrd().

360  {
361  if (neighbors.empty()) {
362  lof_table_diff_[row].lrd = 1;
363  return;
364  }
365 
366  const size_t length = min(neighbors.size(), size_t(neighbor_num_));
367  float sum_reachability = 0;
368  for (size_t i = 0; i < length; ++i) {
369  sum_reachability += max(neighbors[i].second, get_kdist(neighbors[i].first));
370  }
371 
372  if (sum_reachability == 0) {
373  lof_table_diff_[row].lrd = numeric_limits<float>::infinity();
374  return;
375  }
376 
377  lof_table_diff_[row].lrd = length / sum_reachability;
378 }
float get_kdist(const std::string &row) const

Here is the call graph for this function:

Here is the caller graph for this function:

void jubatus::core::anomaly::lof_storage::update_row ( const std::string &  row,
const common::sfv_t diff 
)

Definition at line 135 of file lof_storage.cpp.

References collect_neighbors(), nn_engine_, and update_entries().

135  {
136  unordered_set<string> update_set;
137 
138  {
139  common::sfv_t query;
140  nn_engine_->decode_row(row, query);
141  if (!query.empty()) {
142  collect_neighbors(row, update_set);
143  }
144  }
145 
146  nn_engine_->update_row(row, diff);
147  collect_neighbors(row, update_set);
148 
149  update_set.insert(row);
150 
151  update_entries(update_set);
152 }
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_
void update_entries(const jubatus::util::data::unordered_set< std::string > &rows)
void collect_neighbors(const std::string &row, jubatus::util::data::unordered_set< std::string > &nn) const
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29

Here is the call graph for this function:

Member Data Documentation

const uint32_t jubatus::core::anomaly::lof_storage::DEFAULT_NEIGHBOR_NUM = 10
static

Definition at line 54 of file lof_storage.hpp.

const uint32_t jubatus::core::anomaly::lof_storage::DEFAULT_REVERSE_NN_NUM = 30
static

Definition at line 55 of file lof_storage.hpp.

lof_table_t jubatus::core::anomaly::lof_storage::lof_table_
private

Definition at line 158 of file lof_storage.hpp.

Referenced by clear(), get_kdist(), get_lrd(), has_row(), and put_diff().

lof_table_t jubatus::core::anomaly::lof_storage::lof_table_diff_
private
uint32_t jubatus::core::anomaly::lof_storage::neighbor_num_
private
jubatus::util::lang::shared_ptr<core::recommender::recommender_base> jubatus::core::anomaly::lof_storage::nn_engine_
private
uint32_t jubatus::core::anomaly::lof_storage::reverse_nn_num_
private

Definition at line 162 of file lof_storage.hpp.

Referenced by collect_neighbors().


The documentation for this class was generated from the following files: