26 #include <unordered_map>
41 MinimumDistance(
const int& cluster_index,
const int& nearest_neighbour_index,
const double& distance);
98 template <
typename Metric>
110 typedef std::unordered_multimap<int, MultisetIterator>::const_iterator
NNIterator;
124 const std::vector<double>& data_y,
const std::vector<int>& properties_A,
125 const std::vector<int>& properties_B, std::vector<double> grid_spacing_x,
126 std::vector<double> grid_spacing_y) :
128 grid_(grid_spacing_x, grid_spacing_y)
130 init_(data_x, data_y, properties_A, properties_B);
143 const std::vector<double>& data_y, std::vector<double> grid_spacing_x,
144 std::vector<double> grid_spacing_y) :
146 grid_(grid_spacing_x, grid_spacing_y)
149 std::vector<int> properties_A(data_x.size(), -1);
150 std::vector<int> properties_B(data_x.size(), -1);
151 init_(data_x, data_y, properties_A, properties_B);
175 int cluster_index1 = smallest_distance_it->getClusterIndex();
176 int cluster_index2 = smallest_distance_it->getNearestNeighbourIndex();
181 std::map<int, GridBasedCluster>::iterator cluster1_it =
clusters_.find(cluster_index1);
182 std::map<int, GridBasedCluster>::iterator cluster2_it =
clusters_.find(cluster_index2);
185 const std::vector<int>& points1 = cluster1.
getPoints();
186 const std::vector<int>& points2 = cluster2.
getPoints();
187 std::vector<int> new_points;
188 new_points.reserve(points1.size() + points2.size());
189 new_points.insert(new_points.end(), points1.begin(), points1.end());
190 new_points.insert(new_points.end(), points2.begin(), points2.end());
192 double new_x = (cluster1.
getCentre().
getX() * points1.size() + cluster2.
getCentre().
getX() * points2.size()) / (points1.size() + points2.size());
193 double new_y = (cluster1.
getCentre().
getY() * points1.size() + cluster2.
getCentre().
getY() * points2.size()) / (points1.size() + points2.size());
214 throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
"Property A of both clusters not the same. ",
"A");
220 std::vector<int> new_B;
221 new_B.reserve(B1.size() + B2.size());
222 new_B.insert(new_B.end(), B1.begin(), B1.end());
223 new_B.insert(new_B.end(), B2.begin(), B2.end());
229 clusters_.insert(std::make_pair(cluster_index1, new_cluster));
231 std::set<int> clusters_to_be_updated;
232 clusters_to_be_updated.insert(cluster_index1);
239 std::pair<NNIterator, NNIterator> nn_range =
reverse_nns_.equal_range(cluster_index1);
240 for (
NNIterator nn_it = nn_range.first; nn_it != nn_range.second;)
242 clusters_to_be_updated.insert(nn_it->second->getClusterIndex());
246 for (
NNIterator nn_it = nn_range.first; nn_it != nn_range.second;)
248 clusters_to_be_updated.insert(nn_it->second->getClusterIndex());
253 for (std::set<int>::const_iterator cluster_index = clusters_to_be_updated.begin(); cluster_index != clusters_to_be_updated.end(); ++cluster_index)
255 std::map<int, GridBasedCluster>::iterator c_it =
clusters_.find(*cluster_index);
278 std::vector<double> grid_spacing_y_new;
279 grid_spacing_y_new.push_back(grid_spacing_y.front());
280 grid_spacing_y_new.push_back(grid_spacing_y.back());
286 int cluster_index = it->first;
293 for (
unsigned cell = 0; cell < grid_spacing_x.size(); ++cell)
298 std::list<int> cluster_indices = grid_x_only.
getClusters(grid_index);
299 if (cluster_indices.size() > 1)
302 std::list<GridBasedCluster> cluster_list;
303 std::map<GridBasedCluster, int> index_list;
304 for (std::list<int>::const_iterator it = cluster_indices.begin(); it != cluster_indices.end(); ++it)
307 index_list.insert(std::make_pair(
clusters_final_.find(*it)->second, *it));
312 std::list<GridBasedCluster>::const_iterator c1 = cluster_list.begin();
313 std::list<GridBasedCluster>::const_iterator c2 = cluster_list.begin();
315 while (c1 != cluster_list.end())
317 double centre1x = (*c1).getCentre().getX();
318 double centre1y = (*c1).getCentre().getY();
319 double centre2x = (*c2).getCentre().getX();
320 double centre2y = (*c2).getCentre().getY();
322 double box1x_min = (*c1).getBoundingBox().minX();
323 double box1x_max = (*c1).getBoundingBox().maxX();
324 double box1y_min = (*c1).getBoundingBox().minY();
325 double box1y_max = (*c1).getBoundingBox().maxY();
326 double box2x_min = (*c2).getBoundingBox().minX();
327 double box2x_max = (*c2).getBoundingBox().maxX();
328 double box2y_min = (*c2).getBoundingBox().minY();
329 double box2y_max = (*c2).getBoundingBox().maxY();
336 bool overlap = (box1x_min <= box2x_max && box1x_min >= box2x_min) || (box1x_max >= box2x_min && box1x_max <= box2x_max);
350 std::vector<int> points1 = (*c1).getPoints();
351 std::vector<int> points2 = (*c2).getPoints();
352 std::vector<int> new_points;
353 new_points.reserve(points1.size() + points2.size());
354 new_points.insert(new_points.end(), points1.begin(), points1.end());
355 new_points.insert(new_points.end(), points2.begin(), points2.end());
357 double new_x = (centre1x * points1.size() + centre2x * points2.size()) / (points1.size() + points2.size());
358 double new_y = (centre1y * points1.size() + centre2y * points2.size()) / (points1.size() + points2.size());
360 double min_x = std::min(box1x_min, box2x_min);
361 double min_y = std::min(box1y_min, box2y_min);
362 double max_x = std::max(box1x_max, box2x_max);
363 double max_y = std::max(box1y_max, box2y_max);
365 Point new_centre(new_x, new_y);
366 Point position_min(min_x, min_y);
367 Point position_max(max_x, max_y);
368 Rectangle new_bounding_box(position_min, position_max);
375 clusters_final_.insert(std::make_pair(index_list.find(*c1)->second, new_cluster));
382 grid_x_only.
removeCluster(cell_for_cluster1, index_list.find(*c1)->second);
383 grid_x_only.
removeCluster(cell_for_cluster2, index_list.find(*c2)->second);
384 grid_x_only.
addCluster(cell_for_new_cluster, index_list.find(*c1)->second);
401 std::map<int, GridBasedCluster>::iterator it =
clusters_final_.begin();
404 Rectangle box = it->second.getBoundingBox();
405 if (box.
maxY() - box.
minY() < threshold_y)
459 std::unordered_multimap<int, std::multiset<MinimumDistance>::const_iterator>
reverse_nns_;
475 void init_(
const std::vector<double>& data_x,
const std::vector<double>& data_y,
476 const std::vector<int>& properties_A,
const std::vector<int>& properties_B)
479 for (
unsigned i = 0; i < data_x.size(); ++i)
481 Point position(data_x[i], data_y[i]);
487 pb.push_back(properties_B[i]);
498 std::map<int, GridBasedCluster>::iterator iterator =
clusters_.begin();
501 int cluster_index = iterator->first;
537 if (A1 == -1 || A2 == -1)
543 if (A1 != A2)
return true;
549 if (std::find(B1.begin(), B1.end(), -1) != B1.end() || std::find(B2.begin(), B2.end(), -1) != B2.end())
556 std::vector<int> B_intersection;
557 sort(B1.begin(), B1.end());
558 sort(B2.begin(), B2.end());
559 set_intersection(B1.begin(), B1.end(), B2.begin(), B2.end(), back_inserter(B_intersection));
561 return !B_intersection.empty();
582 int nearest_neighbour = -1;
585 for (
int i = -1; i <= 1; ++i)
587 for (
int j = -1; j <= 1; ++j)
590 cell_index2.first += i;
591 cell_index2.second += j;
595 for (std::list<int>::const_iterator cluster_index2 = cluster_indices.begin(); cluster_index2 != cluster_indices.end(); ++cluster_index2)
597 if (*cluster_index2 != cluster_index)
601 double distance =
metric_(centre, centre2);
603 if (distance < min_dist || nearest_neighbour == -1)
609 nearest_neighbour = *cluster_index2;
618 if (nearest_neighbour == -1)
626 std::multiset<MinimumDistance>::const_iterator it =
distances_.insert(
MinimumDistance(cluster_index, nearest_neighbour, min_dist));
628 reverse_nns_.insert(std::make_pair(nearest_neighbour, it));
647 std::pair<NNIterator, NNIterator> nn_range =
reverse_nns_.equal_range(it->getNearestNeighbourIndex());
648 for (
NNIterator nn_it = nn_range.first; nn_it != nn_range.second; ++nn_it)
650 if (nn_it->second == it)
data structure to store 2D data to be clustered e.g. (m/z, retention time) coordinates from multiplex...
Definition: ClusteringGrid.h:30
std::vector< double > getGridSpacingX() const
returns grid spacing in x direction
CellIndex getIndex(const Point &position) const
returns grid cell index (i,j) for the positions (x,y)
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
std::list< int > getClusters(const CellIndex &cell_index) const
returns clusters in this grid cell
std::pair< int, int > CellIndex
Definition: ClusteringGrid.h:35
std::vector< double > getGridSpacingY() const
returns grid spacing in y direction
bool isNonEmptyCell(const CellIndex &cell_index) const
checks if there are clusters at this cell index
void addCluster(const CellIndex &cell_index, const int &cluster_index)
adds a cluster to this grid cell
void enlarge(const PositionType &p)
Enlarges the bounding box such that it contains a position.
Definition: DBoundingBox.h:95
CoordinateType getY() const
Name accessor for the second dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:149
CoordinateType getX() const
Name accessor for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:142
Invalid value exception.
Definition: Exception.h:305
basic data structure for clustering
Definition: GridBasedCluster.h:24
const Point & getCentre() const
returns cluster centre
const std::vector< int > & getPoints() const
returns indices of points in cluster
const std::vector< int > & getPropertiesB() const
returns properties B of all points
const Rectangle & getBoundingBox() const
returns bounding box
int getPropertyA() const
returns property A
2D hierarchical clustering implementation optimized for large data sets containing many small cluster...
Definition: GridBasedClustering.h:101
void removeSmallClustersY(double threshold_y)
removes clusters with bounding box dimension in y-direction below certain threshold
Definition: GridBasedClustering.h:399
bool findNearestNeighbour_(const GridBasedCluster &cluster, int cluster_index)
determines the nearest neighbour for each cluster
Definition: GridBasedClustering.h:577
std::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:465
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:531
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:475
ClusteringGrid grid_
grid on which the position of the clusters are registered used in cluster method
Definition: GridBasedClustering.h:435
void extendClustersY()
extends clusters in y-direction if possible (merges clusters further in y-direction,...
Definition: GridBasedClustering.h:272
Metric metric_
metric for measuring the distance between points in the 2D plane
Definition: GridBasedClustering.h:429
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:142
GridBasedCluster::Rectangle Rectangle
Definition: GridBasedClustering.h:107
std::multiset< MinimumDistance > distances_
list of minimum distances stores the smallest of the distances in the head
Definition: GridBasedClustering.h:453
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:123
void cluster()
performs the hierarchical clustering (merges clusters until their dimension exceeds that of cell)
Definition: GridBasedClustering.h:158
GridBasedCluster::Point Point
cluster centre, cluster bounding box, grid index
Definition: GridBasedClustering.h:106
std::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:459
std::multiset< MinimumDistance >::const_iterator MultisetIterator
Definition: GridBasedClustering.h:109
std::map< int, GridBasedCluster > clusters_
list of clusters maps cluster indices to clusters
Definition: GridBasedClustering.h:441
std::unordered_multimap< int, MultisetIterator >::const_iterator NNIterator
Definition: GridBasedClustering.h:110
std::map< int, GridBasedCluster > clusters_final_
list of final clusters i.e. clusters that are no longer merged
Definition: GridBasedClustering.h:447
void eraseMinDistance_(const std::multiset< MinimumDistance >::const_iterator it)
remove minimum distance object and its related data
Definition: GridBasedClustering.h:644
ClusteringGrid::CellIndex CellIndex
Definition: GridBasedClustering.h:108
std::map< int, GridBasedCluster > getResults() const
returns final results (mapping of cluster indices to clusters)
Definition: GridBasedClustering.h:419
PositionType const & maxPosition() const
Accessor to maximum position.
Definition: DIntervalBase.h:104
CoordinateType maxY() const
Accessor for max_ coordinate maximum.
Definition: DIntervalBase.h:286
CoordinateType minY() const
Accessor for max_ coordinate minimum.
Definition: DIntervalBase.h:274
PositionType const & minPosition() const
Accessor to minimum position.
Definition: DIntervalBase.h:98
basic data structure for distances between clusters
Definition: GridBasedClustering.h:35
MinimumDistance()
hide default constructor
bool operator>(const MinimumDistance &other) const
int getClusterIndex() const
returns cluster index
MinimumDistance(const int &cluster_index, const int &nearest_neighbour_index, const double &distance)
constructor
int nearest_neighbour_index_
index of the nearest neighbour of the above cluster
Definition: GridBasedClustering.h:74
int getNearestNeighbourIndex() const
returns index of nearest cluster
double distance_
distance between cluster and its nearest neighbour
Definition: GridBasedClustering.h:79
bool operator==(const MinimumDistance &other) const
int cluster_index_
index in the cluster list
Definition: GridBasedClustering.h:69
bool operator<(const MinimumDistance &other) const
operators for comparisons (for multiset)
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:27
void setProgress(SignedSize value) const
Sets the current progress.
void startProgress(SignedSize begin, SignedSize end, const String &label) const
Initializes the progress display.
void endProgress(UInt64 bytes_processed=0) const
A more convenient string class.
Definition: String.h:34
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
const double c
Definition: Constants.h:188
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19