float KernelKmeansClusterer::distanceToCluster(int sampleId, const boost::unordered_set<int>& clusterIdSet) { if (clusterIdSet.empty()) { return FLT_MAX; } // the sum of weight of the cluster float weightSum = 0.0f; // the sum of kernels from the sample to all samples in the cluster, multiplied by the weight float kernelSum = 0.0f; // the sum of kernels from each pair of samples in the cluster, multiplied by weights float kernelPairSum = 0.0f; for (boost::unordered_set<int>::const_iterator iter = clusterIdSet.begin(); iter != clusterIdSet.end(); ++iter) { float weight = mWeightArray[*iter]; weightSum += weight; float kernelResult = (*mpKernel)(sampleId, *iter); kernelSum += weight * kernelResult; for (boost::unordered_set<int>::const_iterator _iter = clusterIdSet.begin(); _iter != clusterIdSet.end(); ++_iter) { float _kernelResult = (*mpKernel)(*iter, *_iter); kernelPairSum += weight * mWeightArray[*_iter] * _kernelResult; } } float kernelSelf = (*mpKernel)(sampleId, sampleId); if (weightSum == 0.0f) { return FLT_MAX; } return kernelSelf - 2 * kernelSum / weightSum + kernelPairSum / (weightSum * weightSum); }
inline Observer& Observer::operator=(const Observer& o) { iterator i; for (i=observables_.begin(); i!=observables_.end(); ++i) (*i)->unregisterObserver(this); observables_ = o.observables_; for (i=observables_.begin(); i!=observables_.end(); ++i) (*i)->registerObserver(this); return *this; }
inline void ObservableSettings::registerDeferredObservers(boost::unordered_set<Observer*>& observers) { if (updatesDeferred()) { QL_TRACE("adding " << observers.size() << " observers to the deferred list"); deferredObservers_.insert(observers.begin(), observers.end()); } }
//calculates average of vectors boost::unordered_map<string, double> Cluster::_centroid(boost::unordered_set<int>& cluster_list) { unordered_string_map centroid; int cluster_size = cluster_list.size(); for (boost::unordered_set<int>::iterator it = cluster_list.begin(); it != cluster_list.end(); ++it) { //add each element of vector to centroid for (unordered_string_map::iterator ivec = doc_term_index[ *it ].begin(); ivec != doc_term_index[ *it ].end(); ++ivec) { //element (term) doesn't exist, we add it to the map if ( centroid.count ( ivec->first ) == 0) { //cout << "ivec second: " << ivec->second <<endl; //cout << "cluster_size: " << cluster_size <<endl; centroid.insert(unordered_string_map::value_type( ivec->first, double( (double)ivec->second / cluster_size ) )); } else { centroid[ivec->first] += double( ((double)ivec->second / cluster_size) ); } } } return centroid; }
inline void ObservableSettings::enableUpdates() { updatesEnabled_ = true; updatesDeferred_ = false; // if there are outstanding deferred updates, do the notification if (deferredObservers_.size() > 0) { bool successful = true; std::string errMsg; QL_TRACE("deferred notification of " << deferredObservers_.size() << " observers"); for (iterator i=deferredObservers_.begin(); i!=deferredObservers_.end(); ++i) { try { (*i)->update(); } catch (std::exception& e) { successful = false; errMsg = e.what(); } catch (...) { successful = false; } } deferredObservers_.clear(); QL_ENSURE(successful, "could not notify one or more observers: " << errMsg); } }
static void serialize(storage_type& s, const boost::unordered_set<T>& o) { uint32_t sz = o.size(); s.serialize(sz); for (auto it = o.begin(), it_end = o.end(); it != it_end; ++it) s.serialize(*it); }
Strings get_live_object_names() { IMP::Vector<std::string> ret; for (boost::unordered_set<Object*>::const_iterator it = live_.begin(); it != live_.end(); ++it) { ret.push_back((*it)->get_name()); } return ret; }
IMPKERNEL_END_NAMESPACE IMPKERNEL_BEGIN_INTERNAL_NAMESPACE void check_live_objects() { for (boost::unordered_set<Object*>::const_iterator it = live_.begin(); it != live_.end(); ++it) { IMP_USAGE_CHECK((*it)->get_ref_count() > 0, "Object " << (*it)->get_name() << " is not ref counted."); } }
IMPKERNEL_END_NAMESPACE IMPKERNEL_BEGIN_INTERNAL_NAMESPACE void check_live_objects() { for (boost::unordered_set<Object*>::const_iterator it = live_.begin(); it != live_.end(); ++it) { IMP_USAGE_CHECK((*it)->get_ref_count() > 0, "Object " << (*it)->get_name() << " is alive but not ref counted - memory leakage possible." << " This usually happens if an owning pointer is released" << " without being either deleted manually or assigned to" << " another owning pointer."); } }
void save(Archive & ar, const boost::unordered_set<T> & t, unsigned int version) { // write the size // TODO: should we handle bucket size as well? typedef typename boost::unordered_set<T>::size_type size_type; typedef typename boost::unordered_set<T>::const_iterator const_iterator; size_type size = t.size(); ar & BOOST_SERIALIZATION_NVP(size); unsigned int count = 0; for (const_iterator it = t.begin(); it != t.end(); it++) { ar & boost::serialization::make_nvp("item" + count, *it); count++; } }
inline void Observable::notifyObservers() { // check whether the notifications should be triggered if (!settings_.updatesEnabled()) { // if updates are only deferred, flag this for later notification // these are held centrally by the settings singleton if (settings_.updatesDeferred()) settings_.registerDeferredObservers(observers_); return; } if (observers_.size() > 0) { bool successful = true; std::string errMsg; QL_TRACE("direct notification of " << observers_.size() << " observers"); for (iterator i=observers_.begin(); i!=observers_.end(); ++i) { try { (*i)->update(); } catch (std::exception& e) { // quite a dilemma. If we don't catch the exception, // other observers will not receive the notification // and might be left in an incorrect state. If we do // catch it and continue the loop (as we do here) we // lose the exception. The least evil might be to try // and notify all observers, while raising an // exception if something bad happened. successful = false; errMsg = e.what(); } catch (...) { successful = false; } } QL_ENSURE(successful, "could not notify one or more observers: " << errMsg); } }
Objects get_live_objects() { Objects ret(live_.begin(), live_.end()); return ret; }
static void exec(OutArcType& oarc, const boost::unordered_set<T>& vec){ serialize_iterator(oarc, vec.begin(), vec.end(), vec.size()); }
void insert(const boost::unordered_set<key_type>& inKeys, const boost::unordered_set<value_type>& inValues) { for (auto it = inValues.begin(); it != inValues.end();++it) insert(inKeys, *it); }
/** * Return the mirror information of the vertex in the i'th position. */ inline const std::vector<graph_shard_id_t> mirrors(size_t i) const { const boost::unordered_set<graph_shard_id_t> mirrorset = shard_impl.vertex_mirrors[i]; std::vector<graph_shard_id_t> ret(mirrorset.begin(), mirrorset.end()); return ret; }
inline void Observer::unregisterWithAll() { for (iterator i=observables_.begin(); i!=observables_.end(); ++i) (*i)->unregisterObserver(this); observables_.clear(); }
inline Observer::~Observer() { for (iterator i=observables_.begin(); i!=observables_.end(); ++i) (*i)->unregisterObserver(this); }
void drop(const boost::unordered_set<key_type>& inKey, const value_type& inValue) { for (auto it = inKey.begin(); it != inKey.end();++it) drop(*it, inValue); }
inline Observer::Observer(const Observer& o) : observables_(o.observables_) { for (iterator i=observables_.begin(); i!=observables_.end(); ++i) (*i)->registerObserver(this); }
inline void ObservableSettings::registerDeferredObservers( const boost::unordered_set<Observer*>& observers) { if (updatesDeferred()) { deferredObservers_.insert(observers.begin(), observers.end()); } }
void drop(const key_type& inKey, const boost::unordered_set<value_type>& inValue) { for (auto it = inValue.begin(); it != inValue.end();++it) drop(inKey, *it); }