35 #ifndef OPENMS_COMPARISON_CLUSTERING_GRIDBASEDCLUSTERING_H
36 #define OPENMS_COMPARISON_CLUSTERING_GRIDBASEDCLUSTERING_H
51 #include <boost/unordered_map.hpp>
66 MinimumDistance(
const int& cluster_index,
const int& nearest_neighbour_index,
const double& distance);
71 int getClusterIndex()
const;
76 int getNearestNeighbourIndex()
const;
123 template <
typename Metric>
135 typedef boost::unordered::unordered_multimap<int, MultisetIterator>::const_iterator
NNIterator;
148 const std::vector<double>& data_y,
const std::vector<int>& properties_A,
149 const std::vector<int>& properties_B, std::vector<double> grid_spacing_x,
150 std::vector<double> grid_spacing_y) :
152 grid_(grid_spacing_x, grid_spacing_y)
154 init_(data_x, data_y, properties_A, properties_B);
166 const std::vector<double>& data_y, std::vector<double> grid_spacing_x,
167 std::vector<double> grid_spacing_y) :
169 grid_(grid_spacing_x, grid_spacing_y)
172 std::vector<int> properties_A(data_x.size(), -1);
173 std::vector<int> properties_B(data_x.size(), -1);
174 init_(data_x, data_y, properties_A, properties_B);
194 MultisetIterator smallest_distance_it =
distances_.lower_bound(zero_distance);
196 int cluster_index1 = smallest_distance_it->getClusterIndex();
197 int cluster_index2 = smallest_distance_it->getNearestNeighbourIndex();
202 std::map<int, GridBasedCluster>::iterator cluster1_it =
clusters_.find(cluster_index1);
203 std::map<int, GridBasedCluster>::iterator cluster2_it =
clusters_.find(cluster_index2);
206 const std::vector<int>& points1 = cluster1.
getPoints();
207 const std::vector<int>& points2 = cluster2.
getPoints();
208 std::vector<int> new_points;
209 new_points.reserve(points1.size() + points2.size());
210 new_points.insert(new_points.end(), points1.begin(), points1.end());
211 new_points.insert(new_points.end(), points2.begin(), points2.end());
213 double new_x = (cluster1.
getCentre().
getX() * points1.size() + cluster2.
getCentre().
getX() * points2.size()) / (points1.size() + points2.size());
214 double new_y = (cluster1.
getCentre().
getY() * points1.size() + cluster2.
getCentre().
getY() * points2.size()) / (points1.size() + points2.size());
227 Rectangle new_box(box1);
235 throw Exception::InvalidValue(__FILE__, __LINE__, __PRETTY_FUNCTION__,
"Property A of both clusters not the same. ",
"A");
241 std::vector<int> new_B;
242 new_B.reserve(B1.size() + B2.size());
243 new_B.insert(new_B.end(), B1.begin(), B1.end());
244 new_B.insert(new_B.end(), B2.begin(), B2.end());
250 clusters_.insert(std::make_pair(cluster_index1, new_cluster));
252 std::set<int> clusters_to_be_updated;
253 clusters_to_be_updated.insert(cluster_index1);
260 std::pair<NNIterator, NNIterator> nn_range =
reverse_nns_.equal_range(cluster_index1);
261 for (NNIterator nn_it = nn_range.first; nn_it != nn_range.second;)
263 clusters_to_be_updated.insert(nn_it->second->getClusterIndex());
267 for (NNIterator nn_it = nn_range.first; nn_it != nn_range.second;)
269 clusters_to_be_updated.insert(nn_it->second->getClusterIndex());
274 for (std::set<int>::const_iterator cluster_index = clusters_to_be_updated.begin(); cluster_index != clusters_to_be_updated.end(); ++cluster_index)
276 std::map<int, GridBasedCluster>::iterator c_it =
clusters_.find(*cluster_index);
299 std::vector<double> grid_spacing_y_new;
300 grid_spacing_y_new.push_back(grid_spacing_y.front());
301 grid_spacing_y_new.push_back(grid_spacing_y.back());
307 int cluster_index = it->first;
314 for (
unsigned cell = 0; cell < grid_spacing_x.size(); ++cell)
316 CellIndex grid_index(cell, 1);
319 std::list<int> cluster_indices = grid_x_only.
getClusters(grid_index);
320 if (cluster_indices.size() > 1)
323 std::list<GridBasedCluster> cluster_list;
324 std::map<GridBasedCluster, int> index_list;
325 for (std::list<int>::const_iterator it = cluster_indices.begin(); it != cluster_indices.end(); ++it)
328 index_list.insert(std::make_pair(
clusters_final_.find(*it)->second, *it));
333 std::list<GridBasedCluster>::const_iterator c1 = cluster_list.begin();
334 std::list<GridBasedCluster>::const_iterator c2 = cluster_list.begin();
336 while (c1 != cluster_list.end())
338 double centre1x = (*c1).getCentre().getX();
339 double centre1y = (*c1).getCentre().getY();
340 double centre2x = (*c2).getCentre().getX();
341 double centre2y = (*c2).getCentre().getY();
343 double box1x_min = (*c1).getBoundingBox().minX();
344 double box1x_max = (*c1).getBoundingBox().maxX();
345 double box1y_min = (*c1).getBoundingBox().minY();
346 double box1y_max = (*c1).getBoundingBox().maxY();
347 double box2x_min = (*c2).getBoundingBox().minX();
348 double box2x_max = (*c2).getBoundingBox().maxX();
349 double box2y_min = (*c2).getBoundingBox().minY();
350 double box2y_max = (*c2).getBoundingBox().maxY();
357 bool overlap = (box1x_min <= box2x_max && box1x_min >= box2x_min) || (box1x_max >= box2x_min && box1x_max <= box2x_max);
371 std::vector<int> points1 = (*c1).getPoints();
372 std::vector<int> points2 = (*c2).getPoints();
373 std::vector<int> new_points;
374 new_points.reserve(points1.size() + points2.size());
375 new_points.insert(new_points.end(), points1.begin(), points1.end());
376 new_points.insert(new_points.end(), points2.begin(), points2.end());
378 double new_x = (centre1x * points1.size() + centre2x * points2.size()) / (points1.size() + points2.size());
379 double new_y = (centre1y * points1.size() + centre2y * points2.size()) / (points1.size() + points2.size());
381 double min_x = std::min(box1x_min, box2x_min);
382 double min_y = std::min(box1y_min, box2y_min);
383 double max_x = std::max(box1x_max, box2x_max);
384 double max_y = std::max(box1y_max, box2y_max);
386 Point new_centre(new_x, new_y);
387 Point position_min(min_x, min_y);
388 Point position_max(max_x, max_y);
389 Rectangle new_bounding_box(position_min, position_max);
396 clusters_final_.insert(std::make_pair(index_list.find(*c1)->second, new_cluster));
399 CellIndex cell_for_cluster1 = grid_x_only.
getIndex((*c1).getCentre());
400 CellIndex cell_for_cluster2 = grid_x_only.
getIndex((*c2).getCentre());
401 CellIndex cell_for_new_cluster = grid_x_only.
getIndex(new_centre);
403 grid_x_only.
removeCluster(cell_for_cluster1, index_list.find(*c1)->second);
404 grid_x_only.
removeCluster(cell_for_cluster2, index_list.find(*c2)->second);
405 grid_x_only.
addCluster(cell_for_new_cluster, index_list.find(*c1)->second);
422 std::map<int, GridBasedCluster>::iterator it =
clusters_final_.begin();
425 Rectangle box = it->second.getBoundingBox();
426 if (box.
maxY() - box.
minY() < threshold_y)
480 boost::unordered::unordered_multimap<int, std::multiset<MinimumDistance>::const_iterator>
reverse_nns_;
496 void init_(
const std::vector<double>& data_x,
const std::vector<double>& data_y,
497 const std::vector<int>& properties_A,
const std::vector<int>& properties_B)
500 for (
unsigned i = 0; i < data_x.size(); ++i)
502 Point position(data_x[i], data_y[i]);
503 Rectangle box(position, position);
508 pb.push_back(properties_B[i]);
512 clusters_.insert(std::make_pair(i, cluster));
519 std::map<int, GridBasedCluster>::iterator iterator = clusters_.begin();
520 while (iterator != clusters_.end())
522 int cluster_index = iterator->first;
530 clusters_.erase(iterator++);
558 if (A1 == -1 || A2 == -1)
564 if (A1 != A2)
return true;
570 if (std::find(B1.begin(), B1.end(), -1) != B1.end() || std::find(B2.begin(), B2.end(), -1) != B2.end())
577 std::vector<int> B_intersection;
578 sort(B1.begin(), B1.end());
579 sort(B2.begin(), B2.end());
580 set_intersection(B1.begin(), B1.end(), B2.begin(), B2.end(), back_inserter(B_intersection));
582 return !B_intersection.empty();
600 const Point& centre = cluster.
getCentre();
601 const CellIndex& cell_index = grid_.
getIndex(centre);
603 int nearest_neighbour = -1;
606 for (
int i = -1; i <= 1; ++i)
608 for (
int j = -1; j <= 1; ++j)
610 CellIndex cell_index2(cell_index);
611 cell_index2.first += i;
612 cell_index2.second += j;
615 const std::list<int>& cluster_indices = grid_.
getClusters(cell_index2);
616 for (std::list<int>::const_iterator cluster_index2 = cluster_indices.begin(); cluster_index2 != cluster_indices.end(); ++cluster_index2)
618 if (*cluster_index2 != cluster_index)
621 const Point& centre2 = cluster2.
getCentre();
622 double distance =
metric_(centre, centre2);
624 if (distance < min_dist || nearest_neighbour == -1)
630 nearest_neighbour = *cluster_index2;
639 if (nearest_neighbour == -1)
642 clusters_final_.insert(std::make_pair(cluster_index, clusters_.find(cluster_index)->second));
647 std::multiset<MinimumDistance>::const_iterator it = distances_.insert(
MinimumDistance(cluster_index, nearest_neighbour, min_dist));
649 reverse_nns_.insert(std::make_pair(nearest_neighbour, it));
651 distance_it_for_cluster_idx_[cluster_index] = it;
668 std::pair<NNIterator, NNIterator> nn_range = reverse_nns_.equal_range(it->getNearestNeighbourIndex());
669 for (NNIterator nn_it = nn_range.first; nn_it != nn_range.second; ++nn_it)
671 if (nn_it->second == it)
673 reverse_nns_.erase(nn_it);
679 distance_it_for_cluster_idx_.erase(it->getClusterIndex());
682 distances_.erase(it);
CoordinateType maxY() const
Accessor for max_ coordinate maximum.
Definition: DIntervalBase.h:259
void removeCluster(const CellIndex &cell_index, const int &cluster_index)
removes a cluster from this grid cell and removes the cell if no other cluster left ...
boost::unordered::unordered_multimap< int, MultisetIterator >::const_iterator NNIterator
Definition: GridBasedClustering.h:135
std::pair< int, int > CellIndex
Definition: ClusteringGrid.h:57
double distance_
distance between cluster and its nearest neighbour
Definition: GridBasedClustering.h:104
std::vector< double > getGridSpacingX() const
returns grid spacing in x direction
std::map< int, GridBasedCluster > clusters_final_
list of final clusters i.e. clusters that are no longer merged
Definition: GridBasedClustering.h:468
bool mergeVeto_(const GridBasedCluster &c1, const GridBasedCluster &c2) const
checks if two clusters can be merged Each point in a cluster can (optionally) have two properties A a...
Definition: GridBasedClustering.h:552
void enlarge(const PositionType &p)
Enlarges the bounding box such that it contains a position.
Definition: DBoundingBox.h:122
boost::unordered::unordered_multimap< int, std::multiset< MinimumDistance >::const_iterator > reverse_nns_
reverse nearest neighbor lookup table for finding out which clusters need to be updated faster ...
Definition: GridBasedClustering.h:480
std::multiset< MinimumDistance > distances_
list of minimum distances stores the smallest of the distances in the head
Definition: GridBasedClustering.h:474
GridBasedCluster::Point Point
cluster centre, cluster bounding box, grid index
Definition: GridBasedClustering.h:131
std::list< int > getClusters(const CellIndex &cell_index) const
returns clusters in this grid cell
void removeSmallClustersY(double threshold_y)
removes clusters with bounding box dimension in y-direction below certain threshold ...
Definition: GridBasedClustering.h:420
std::vector< double > getGridSpacingY() const
returns grid spacing in y direction
void cluster()
performs the hierarchical clustering (merges clusters until their dimension exceeds that of cell) ...
Definition: GridBasedClustering.h:181
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
ClusteringGrid grid_
grid on which the position of the clusters are registered used in cluster method
Definition: GridBasedClustering.h:456
GridBasedCluster::Rectangle Rectangle
Definition: GridBasedClustering.h:132
bool operator<(const MultiplexDeltaMasses &dm1, const MultiplexDeltaMasses &dm2)
std::map< int, GridBasedCluster > clusters_
list of clusters maps cluster indices to clusters
Definition: GridBasedClustering.h:462
void extendClustersY()
extends clusters in y-direction if possible (merges clusters further in y-direction, i.e. clusters can now span multiple cells)
Definition: GridBasedClustering.h:293
GridBasedClustering(Metric metric, const std::vector< double > &data_x, const std::vector< double > &data_y, std::vector< double > grid_spacing_x, std::vector< double > grid_spacing_y)
initialises all data structures
Definition: GridBasedClustering.h:165
bool isNonEmptyCell(const CellIndex &cell_index) const
checks if there are clusters at this cell index
2D hierarchical clustering implementation optimized for large data sets containing many small cluster...
Definition: GridBasedClustering.h:124
CoordinateType minY() const
Accessor for max_ coordinate minimum.
Definition: DIntervalBase.h:247
bool findNearestNeighbour_(const GridBasedCluster &cluster, int cluster_index)
determines the nearest neighbour for each cluster
Definition: GridBasedClustering.h:598
void addCluster(const CellIndex &cell_index, const int &cluster_index)
adds a cluster to this grid cell
void endProgress() const
Ends the progress display.
data structure to store 2D data to be clustered e.g. (m/z, retention time) coordinates from multiplex...
Definition: ClusteringGrid.h:51
int cluster_index_
index in the cluster list
Definition: GridBasedClustering.h:94
int getPropertyA() const
returns property A
void init_(const std::vector< double > &data_x, const std::vector< double > &data_y, const std::vector< int > &properties_A, const std::vector< int > &properties_B)
initialises all data structures
Definition: GridBasedClustering.h:496
GridBasedClustering(Metric metric, const std::vector< double > &data_x, const std::vector< double > &data_y, const std::vector< int > &properties_A, const std::vector< int > &properties_B, std::vector< double > grid_spacing_x, std::vector< double > grid_spacing_y)
initialises all data structures
Definition: GridBasedClustering.h:147
Metric metric_
metric for measuring the distance between points in the 2D plane
Definition: GridBasedClustering.h:450
std::map< int, GridBasedCluster > getResults() const
returns final results (mapping of cluster indices to clusters)
Definition: GridBasedClustering.h:440
const std::vector< int > & getPoints() const
returns indices of points in cluster
Invalid value exception.
Definition: Exception.h:336
PositionType const & minPosition() const
Accessor to minimum position.
Definition: DIntervalBase.h:122
CellIndex getIndex(const Point &position) const
returns grid cell index (i,j) for the positions (x,y)
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
void startProgress(SignedSize begin, SignedSize end, const String &label) const
Initializes the progress display.
const Rectangle & getBoundingBox() const
returns bounding box
void eraseMinDistance_(const std::multiset< MinimumDistance >::const_iterator it)
remove minimum distance object and its related data
Definition: GridBasedClustering.h:665
void setProgress(SignedSize value) const
Sets the current progress.
ClusteringGrid::CellIndex CellIndex
Definition: GridBasedClustering.h:133
boost::unordered::unordered_map< int, std::multiset< MinimumDistance >::const_iterator > distance_it_for_cluster_idx_
cluster index to distance iterator lookup table for finding out which clusters need to be updated fas...
Definition: GridBasedClustering.h:486
PositionType const & maxPosition() const
Accessor to maximum position.
Definition: DIntervalBase.h:128
CoordinateType getY() const
Name accessor for the second dimension. Only for DPosition<2>, for visualization. ...
Definition: DPosition.h:158
basic data structure for distances between clusters
Definition: GridBasedClustering.h:59
std::multiset< MinimumDistance >::const_iterator MultisetIterator
Definition: GridBasedClustering.h:134
basic data structure for clustering
Definition: GridBasedCluster.h:55
int nearest_neighbour_index_
index of the nearest neighbour of the above cluster
Definition: GridBasedClustering.h:99
const Point & getCentre() const
returns cluster centre
const std::vector< int > & getPropertiesB() const
returns properties B of all points
CoordinateType getX() const
Name accessor for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:151