clustering  0.12
Clustering suite for molecular dynamics trajectories.
 All Classes Namespaces Files Functions Variables Typedefs
density_clustering.hpp
1 /*
2 Copyright (c) 2015, Florian Sittel (www.lettis.net)
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 
11 2. Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
18 SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
22 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25 
26 #pragma once
27 
28 #include <vector>
29 #include <map>
30 #include <set>
31 #include <array>
32 #include <utility>
33 #include <string>
34 
35 #include <boost/program_options.hpp>
36 
37 #include "tools.hpp"
38 
40 namespace Clustering {
42  namespace Density {
44  using FreeEnergy = std::pair<std::size_t, float>;
50  using Box = std::array<int, 2>;
54  constexpr int BOX_DIFF[9][2] = {{-1, 1}, { 0, 1}, { 1, 1}
55  , {-1, 0}, { 0, 0}, { 1, 0}
56  , {-1,-1}, { 0,-1}, { 1,-1}};
58  const int N_NEIGHBOR_BOXES = 9;
59 // //! encodes box differences in 3D, i.e. if you are at
60 // //! the center box, the 27 different tuples hold the steppings
61 // //! to the 27 spacial neighbors (including the center box itself).
62 // constexpr int BOX_DIFF[27][3] = {{-1, 1,-1}, { 0, 1,-1}, { 1, 1,-1}
63 // , {-1, 0,-1}, { 0, 0,-1}, { 1, 0,-1}
64 // , {-1,-1,-1}, { 0,-1,-1}, { 1,-1,-1}
65 // , {-1, 1, 0}, { 0, 1, 0}, { 1, 1, 0}
66 // , {-1, 0, 0}, { 0, 0, 0}, { 1, 0, 0}
67 // , {-1,-1, 0}, { 0,-1, 0}, { 1,-1, 0}
68 // , {-1, 1, 1}, { 0, 1, 1}, { 1, 1, 1}
69 // , {-1, 0, 1}, { 0, 0, 1}, { 1, 0, 1}
70 // , {-1,-1, 1}, { 0,-1, 1}, { 1,-1, 1}};
71 // //! number of neigbor boxes in cubic 3D grid (including center box).
72 // const int N_NEIGHBOR_BOXES = 27;
75  struct BoxGrid {
77  std::vector<int> n_boxes;
79  std::vector<Box> assigned_box;
81  std::map<Box, std::vector<int>> boxes;
82  };
85  constexpr Box
86  neighbor_box(const Box center, const int i_neighbor);
89  BoxGrid
90  compute_box_grid(const float* coords,
91  const std::size_t n_rows,
92  const std::size_t n_cols,
93  const float radius);
96  bool
97  is_valid_box(const Box box,
98  const BoxGrid& grid);
100  std::vector<std::size_t>
101  calculate_populations(const float* coords,
102  const std::size_t n_rows,
103  const std::size_t n_cols,
104  const float radius);
108  std::map<float, std::vector<std::size_t>>
109  calculate_populations(const float* coords,
110  const std::size_t n_rows,
111  const std::size_t n_cols,
112  const std::vector<float> radii);
115  std::vector<float>
116  calculate_free_energies(const std::vector<std::size_t>& pops);
119  std::vector<FreeEnergy>
120  sorted_free_energies(const std::vector<float>& fe);
123  std::tuple<Neighborhood, Neighborhood>
124  nearest_neighbors(const float* coords,
125  const std::size_t n_rows,
126  const std::size_t n_cols,
127  const std::vector<float>& free_energy);
131  std::set<std::size_t>
132  high_density_neighborhood(const float* coords,
133  const std::size_t n_cols,
134  const std::vector<FreeEnergy>& sorted_fe,
135  const std::size_t i_frame,
136  const std::size_t limit,
137  const float max_dist);
141  double
142  compute_sigma2(const Neighborhood& nh);
149  std::vector<std::size_t>
150  assign_low_density_frames(const std::vector<std::size_t>& initial_clustering,
151  const Neighborhood& nh_high_dens,
152  const std::vector<float>& free_energy);
168  void
169  main(boost::program_options::variables_map args);
170  } // end namespace Density
171 } // end namespace Clustering
172 
std::vector< std::size_t > assign_low_density_frames(const std::vector< std::size_t > &initial_clustering, const Neighborhood &nh_high_dens, const std::vector< float > &free_energy)
double compute_sigma2(const Neighborhood &nh)
const int N_NEIGHBOR_BOXES
number of neigbor boxes in 2D grid (including center box).
bool is_valid_box(const Box box, const BoxGrid &grid)
std::set< std::size_t > high_density_neighborhood(const float *coords, const std::size_t n_cols, const std::vector< FreeEnergy > &sorted_fe, const std::size_t i_frame, const std::size_t limit, const float max_dist)
void main(boost::program_options::variables_map args)
Clustering::Tools::Neighborhood Neighborhood
map frame id to neighbors
std::vector< int > n_boxes
total number of boxes
std::map< Box, std::vector< int > > boxes
the boxes with a list of assigned frame ids
std::tuple< Neighborhood, Neighborhood > nearest_neighbors(const float *coords, const std::size_t n_rows, const std::size_t n_cols, const std::vector< float > &free_energy)
constexpr int BOX_DIFF[9][2]
std::pair< std::size_t, float > Neighbor
matches neighbor's frame id to distance
Definition: tools.hpp:53
std::map< std::size_t, Clustering::Tools::Neighbor > Neighborhood
map frame id to neighbors
Definition: tools.hpp:55
BoxGrid compute_box_grid(const float *coords, const std::size_t n_rows, const std::size_t n_cols, const float radius)
std::vector< std::size_t > calculate_populations(const float *coords, const std::size_t n_rows, const std::size_t n_cols, const float radius)
calculate population of n-dimensional hypersphere per frame for one fixed radius. ...
constexpr Box neighbor_box(const Box center, const int i_neighbor)
std::vector< float > calculate_free_energies(const std::vector< std::size_t > &pops)
std::vector< FreeEnergy > sorted_free_energies(const std::vector< float > &fe)
std::pair< std::size_t, float > FreeEnergy
matches frame id to free energy
std::array< int, 2 > Box
encodes 2D box for box-assisted search algorithm
Clustering::Tools::Neighbor Neighbor
matches neighbor's frame id to distance
std::vector< Box > assigned_box
matching frame id to the frame's assigned box