Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

(WIP) throw a exception on cluster methods if it comes before k-means runs #675

Open
wants to merge 4 commits into from

4 participants

@kumagi
Owner

This PR is correspond to #667

Some clustering RPC cause segfault before it runs clustering calculation.
We should check kcenters_ before access it.

@suma
Owner

Test fails.

@kumagi
Owner

@suma Sorry, I'll fix.

@suma
Owner

Could you add unit test for this exception? (in driver or core?)

jubatus/core/clustering/kmeans_clustering_method.hpp
@@ -25,6 +25,18 @@
namespace clustering {
class clustering_method_serializer;
+class no_cluster_exception : public std::exception {
@suma Owner
suma added a note

Should we use jubatus_exception?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@kumagi
Owner

I add test for exception.
But I don't know the behavior of gmm so I can't decide whether it should throw the exception.
I need someone's help.

@unnonouno unnonouno was assigned by kmaehashi
@kmaehashi kmaehashi added this to the Near Future milestone
@unnonouno unnonouno was unassigned by kmaehashi
@kmaehashi kmaehashi self-assigned this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 25, 2014
  1. gmm::get_cluster: range check is needed

    KUMAZAKI Hiroki authored
  2. test: clustering throws exception

    KUMAZAKI Hiroki authored
This page is out of date. Refresh to see the latest.
View
5 jubatus/core/clustering/gmm_clustering_method.cpp
@@ -60,10 +60,13 @@ common::sfv_t gmm_clustering_method::get_nearest_center(
wplist gmm_clustering_method::get_cluster(
size_t cluster_id,
const wplist& points) const {
+ if (k_ <= cluster_id) {
+ return wplist();
+ }
return get_clusters(points)[cluster_id];
}
-std::vector<wplist> gmm_clustering_method::get_clusters(
+vector<wplist> gmm_clustering_method::get_clusters(
const wplist& points) const {
std::vector<wplist> ret(k_);
for (wplist::const_iterator it = points.begin(); it != points.end(); ++it) {
View
13 jubatus/core/clustering/kmeans_clustering_method.cpp
@@ -21,6 +21,7 @@
#include <vector>
#include "../common/exception.hpp"
#include "util.hpp"
+#include "types.hpp"
using std::pair;
using std::vector;
@@ -98,17 +99,26 @@ vector<common::sfv_t> kmeans_clustering_method::get_k_center() const {
int64_t kmeans_clustering_method::get_nearest_center_index(
const common::sfv_t& point) const {
+ if (kcenters_.empty()) {
+ throw JUBATUS_EXCEPTION(no_cluster_exception("get_nearest_center_index"));
+ }
return min_dist(point, kcenters_).first;
}
common::sfv_t kmeans_clustering_method::get_nearest_center(
const common::sfv_t& point) const {
+ if (kcenters_.empty()) {
+ throw JUBATUS_EXCEPTION(no_cluster_exception("get_nearest_center"));
+ }
return kcenters_[get_nearest_center_index(point)];
}
wplist kmeans_clustering_method::get_cluster(
size_t cluster_id,
const wplist& points) const {
+ if (kcenters_.empty()) {
+ throw JUBATUS_EXCEPTION(no_cluster_exception("get_cluster"));
+ }
if (cluster_id >= k_) {
return wplist();
}
@@ -117,6 +127,9 @@ wplist kmeans_clustering_method::get_cluster(
vector<wplist> kmeans_clustering_method::get_clusters(
const wplist& points) const {
+ if (kcenters_.empty()) {
+ throw JUBATUS_EXCEPTION(no_cluster_exception("get_clusters"));
+ }
vector<wplist> ret(k_);
for (wplist::const_iterator it = points.begin(); it != points.end(); ++it) {
pair<int64_t, double> m = min_dist(it->data, kcenters_);
View
15 jubatus/core/clustering/types.hpp
@@ -26,6 +26,7 @@
#include "jubatus/util/data/serialization.h"
#include "../common/type.hpp"
+#include "../common/exception.hpp"
#include "../fv_converter/datum.hpp"
namespace jubatus {
@@ -37,6 +38,20 @@ typedef std::vector<std::pair<cluster_weight,
jubatus::core::fv_converter::datum> > cluster_unit;
typedef std::vector<cluster_unit> cluster_set;
+class no_cluster_exception
+ : public common::exception::jubaexception<no_cluster_exception> {
+ public:
+ explicit no_cluster_exception(const std::string& msg)
+ : what_(msg) {}
+ const char *what() const throw() {
+ return what_.c_str();
+ }
+ ~no_cluster_exception() throw() {}
+
+ private:
+ std::string what_;
+};
+
struct weighted_point {
public:
MSGPACK_DEFINE(weight, data, original, free_double, free_long);
View
13 jubatus/core/driver/clustering_test.cpp
@@ -29,6 +29,7 @@
#include "../clustering/gmm_clustering_method.hpp"
#include "test_util.hpp"
#include "../fv_converter/datum.hpp"
+#include "../clustering/types.hpp"
using std::vector;
using std::string;
@@ -37,6 +38,7 @@ using std::set;
using std::make_pair;
using jubatus::util::lang::shared_ptr;
using jubatus::core::fv_converter::datum;
+using jubatus::core::clustering::no_cluster_exception;
using jubatus::core::clustering::clustering_method;
using jubatus::core::clustering::clustering_config;
@@ -116,6 +118,17 @@ TEST_P(clustering_test, save_load) {
clustering_->get_mixable_holder()->unpack(unpacked.get());
}
+TEST_P(clustering_test, no_cluster) {
+ core::fv_converter::datum d;
+ vector<datum> datums;
+ datums.push_back(single_datum("a", 1));
+ clustering_->push(datums);
+
+ ASSERT_THROW(clustering_->get_nearest_members(d), no_cluster_exception);
+ ASSERT_THROW(clustering_->get_nearest_center(d), no_cluster_exception);
+ ASSERT_THROW(clustering_->get_core_members(), no_cluster_exception);
+}
+
TEST_P(clustering_test, get_k_center) {
jubatus::util::math::random::mtrand r;
vector<datum> one;
Something went wrong with that request. Please try again.