| [8aa597] | 1 | /*
 | 
|---|
 | 2 |  * Extractors.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: 15.10.2012
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef TRAININGDATA_EXTRACTORS_HPP_
 | 
|---|
 | 9 | #define TRAININGDATA_EXTRACTORS_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | // include config.h
 | 
|---|
 | 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 13 | #include <config.h>
 | 
|---|
 | 14 | #endif
 | 
|---|
 | 15 | 
 | 
|---|
| [f5ea10] | 16 | #include <boost/bimap.hpp>
 | 
|---|
 | 17 | #include <boost/bimap/set_of.hpp>
 | 
|---|
 | 18 | #include <boost/bimap/multiset_of.hpp>
 | 
|---|
 | 19 | #include <boost/graph/adjacency_list.hpp>
 | 
|---|
 | 20 | #include <boost/graph/breadth_first_search.hpp>
 | 
|---|
 | 21 | #include <boost/graph/subgraph.hpp>
 | 
|---|
| [caa00e9] | 22 | #include <boost/function.hpp>
 | 
|---|
 | 23 | 
 | 
|---|
| [f5ea10] | 24 | #include <map>
 | 
|---|
 | 25 | #include <set>
 | 
|---|
 | 26 | 
 | 
|---|
| [228340] | 27 | #include "Fragmentation/EdgesPerFragment.hpp"
 | 
|---|
| [fbf143] | 28 | #include "Fragmentation/Summation/SetValues/Fragment.hpp"
 | 
|---|
| [8aa597] | 29 | #include "FunctionApproximation/FunctionModel.hpp"
 | 
|---|
 | 30 | 
 | 
|---|
| [9c793c] | 31 | class BindingModel;
 | 
|---|
| [8aa597] | 32 | class Fragment;
 | 
|---|
| [67044a] | 33 | class HomologyGraph;
 | 
|---|
| [8aa597] | 34 | 
 | 
|---|
 | 35 | /** Namespace containing all simple extractor functions.
 | 
|---|
| [1ba8a1] | 36 |  *
 | 
|---|
 | 37 |  * Extractor functions extract distances from a given fragment matching with
 | 
|---|
 | 38 |  * a given set of particle types (i.e. elements, e.h. H2O).
 | 
|---|
 | 39 |  * Filter functions extract a subset of distances from a given set of distances
 | 
|---|
 | 40 |  * to be used with a specific model.
 | 
|---|
 | 41 |  *
 | 
|---|
 | 42 |  * To this end, each FunctionModel has both a filter and an extractor function.
 | 
|---|
 | 43 |  *
 | 
|---|
 | 44 |  * The functions in this namespace act as helpers or basic building blocks in
 | 
|---|
 | 45 |  * constructing such filters and extractors.
 | 
|---|
| [8aa597] | 46 |  *
 | 
|---|
 | 47 |  */
 | 
|---|
 | 48 | namespace Extractors {
 | 
|---|
| [301dbf] | 49 |   typedef Fragment::charges_t::const_iterator chargeiter_t;
 | 
|---|
 | 50 |   typedef std::vector<chargeiter_t> chargeiters_t;
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 |   typedef size_t count_t;
 | 
|---|
| [c5e75f3] | 53 |   typedef Fragment::atomicNumber_t element_t;
 | 
|---|
| [301dbf] | 54 |   typedef std::map< element_t, count_t> elementcounts_t;
 | 
|---|
 | 55 |   typedef std::map< element_t, chargeiters_t > elementtargets_t;
 | 
|---|
| [caa00e9] | 56 |   typedef std::vector< chargeiters_t > targets_per_combination_t;
 | 
|---|
| [df350c] | 57 |   //!> typedef for particle designation
 | 
|---|
| [c5e75f3] | 58 |   typedef unsigned int ParticleType_t;
 | 
|---|
| [df350c] | 59 |   //!> typedef for a vector of particle designations
 | 
|---|
 | 60 |   typedef std::vector<ParticleType_t> ParticleTypes_t;
 | 
|---|
| [301dbf] | 61 | 
 | 
|---|
| [f5ea10] | 62 |   typedef size_t level_t;
 | 
|---|
 | 63 |   typedef size_t node_t;
 | 
|---|
 | 64 |   typedef std::multimap< level_t, node_t > nodes_per_level_t;
 | 
|---|
 | 65 |   typedef std::set<node_t> nodes_t;
 | 
|---|
 | 66 |   typedef std::set<nodes_t> set_of_nodes_t;
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 |   typedef boost::bimap<
 | 
|---|
 | 69 |       boost::bimaps::set_of< size_t >,
 | 
|---|
 | 70 |       boost::bimaps::multiset_of< Extractors::ParticleType_t >
 | 
|---|
 | 71 |   > type_index_lookup_t;
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 |   typedef std::set<node_t> set_type;
 | 
|---|
 | 74 |   typedef std::set<set_type> powerset_type;
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 |   typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS,
 | 
|---|
 | 77 |       boost::property<boost::vertex_name_t, atomId_t>,
 | 
|---|
 | 78 |       boost::property<boost::vertex_color_t, boost::default_color_type> /* needed for limited-depth DFS,
 | 
|---|
 | 79 |       otherwise the property_map gets full size of graph */
 | 
|---|
 | 80 |       > UndirectedGraph;
 | 
|---|
 | 81 |   typedef boost::subgraph< UndirectedGraph > UndirectedSubgraph;
 | 
|---|
 | 82 | 
 | 
|---|
 | 83 |   typedef boost::property_map < UndirectedGraph, boost::vertex_index_t >::type index_map_t;
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 |   typedef std::map< node_t, std::pair<Extractors::ParticleType_t, size_t> > node_FragmentNode_map_t;
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 |   typedef std::map< argument_t::indices_t, size_t> argument_placement_map_t;
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 |   typedef std::map<size_t, size_t> argindex_to_nodeindex_t;
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |   /**
 | 
|---|
 | 92 |    * I have no idea why this is so complicated with BGL ...
 | 
|---|
 | 93 |    *
 | 
|---|
 | 94 |    * This is taken from the book "The Boost Graph Library: User Guide and Reference Manual, Portable Documents",
 | 
|---|
 | 95 |    * chapter "Basic Graph Algorithms", example on calculating the bacon number.
 | 
|---|
 | 96 |    */
 | 
|---|
 | 97 |   template <typename DistanceMap>
 | 
|---|
 | 98 |   class distance_recorder : public boost::default_bfs_visitor
 | 
|---|
 | 99 |   {
 | 
|---|
 | 100 |   public:
 | 
|---|
 | 101 |     distance_recorder(DistanceMap dist) : d(dist) {}
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 |     template <typename Edge, typename Graph>
 | 
|---|
 | 104 |     void tree_edge(Edge e, const Graph &g) const {
 | 
|---|
 | 105 |       typename boost::graph_traits<Graph>::vertex_descriptor u = source(e,g), v = target(e,g);
 | 
|---|
 | 106 |       d[v] = d[u] + 1;
 | 
|---|
 | 107 |     }
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |   private:
 | 
|---|
 | 110 |     DistanceMap d;
 | 
|---|
 | 111 |   };
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |   template <typename DistanceMap>
 | 
|---|
 | 114 |   distance_recorder<DistanceMap> record_distance(DistanceMap d)
 | 
|---|
 | 115 |   {
 | 
|---|
 | 116 |     return distance_recorder<DistanceMap>(d);
 | 
|---|
 | 117 |   }
 | 
|---|
 | 118 | 
 | 
|---|
| [d951a5] | 119 |   Extractors::ParticleType_t getParticleTypeToNode(
 | 
|---|
 | 120 |       const type_index_lookup_t &type_index_lookup,
 | 
|---|
 | 121 |       const size_t nodeindex);
 | 
|---|
 | 122 | 
 | 
|---|
| [f5ea10] | 123 |   HomologyGraph createHomologyGraphFromNodes(
 | 
|---|
 | 124 |       const nodes_t &nodes,
 | 
|---|
 | 125 |       const type_index_lookup_t &type_index_lookup,
 | 
|---|
 | 126 |       const UndirectedGraph &graph,
 | 
|---|
 | 127 |       const index_map_t &index_map
 | 
|---|
 | 128 |       );
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 |   void generateAllInducedConnectedSubgraphs(
 | 
|---|
 | 131 |       const size_t N,
 | 
|---|
 | 132 |       const level_t level,
 | 
|---|
 | 133 |       const nodes_t &nodes,
 | 
|---|
 | 134 |       set_of_nodes_t &set_of_nodes,
 | 
|---|
 | 135 |       const nodes_per_level_t &nodes_per_level,
 | 
|---|
 | 136 |       const UndirectedGraph &graph,
 | 
|---|
 | 137 |       const std::vector<size_t> &_distance,
 | 
|---|
 | 138 |       const index_map_t &index_map);
 | 
|---|
 | 139 | 
 | 
|---|
| [8aa597] | 140 |   /** Namespace for some internal helper functions.
 | 
|---|
 | 141 |    *
 | 
|---|
 | 142 |    */
 | 
|---|
 | 143 |   namespace _detail {
 | 
|---|
| [bc6705] | 144 | 
 | 
|---|
 | 145 |     /** Counts all same elements in the vector and places into map of elements.
 | 
|---|
 | 146 |      *
 | 
|---|
 | 147 |      * \param elements vector of elements
 | 
|---|
 | 148 |      * \return count of same element in vector
 | 
|---|
 | 149 |      */
 | 
|---|
 | 150 |     elementcounts_t getElementCounts(
 | 
|---|
| [c5e75f3] | 151 |         const Fragment::atomicnumbers_t elements
 | 
|---|
| [bc6705] | 152 |         );
 | 
|---|
 | 153 | 
 | 
|---|
| [8aa597] | 154 |   }
 | 
|---|
 | 155 | 
 | 
|---|
| [49f163] | 156 |   /** Gather all distances from a given set of positions.
 | 
|---|
 | 157 |    *
 | 
|---|
 | 158 |    *  Here, we only return one of the two equal distances.
 | 
|---|
 | 159 |    *
 | 
|---|
| [691be4] | 160 |    * \param positions all nuclei positions
 | 
|---|
| [c7aac9] | 161 |    * \param atomicNumber all nuclei atomic numbers
 | 
|---|
| [228340] | 162 |    * \param edges edges of the fragment's bond graph
 | 
|---|
| [49f163] | 163 |    * \param globalid index to associated in argument_t with
 | 
|---|
 | 164 |    * \return vector of argument_ , each with a distance
 | 
|---|
 | 165 |    */
 | 
|---|
 | 166 |   FunctionModel::arguments_t
 | 
|---|
 | 167 |   gatherAllSymmetricDistanceArguments(
 | 
|---|
| [691be4] | 168 |       const Fragment::positions_t& positions,
 | 
|---|
| [c7aac9] | 169 |       const Fragment::atomicnumbers_t& atomicnumbers,
 | 
|---|
| [228340] | 170 |       const FragmentationEdges::edges_t &edges,
 | 
|---|
| [49f163] | 171 |       const size_t globalid);
 | 
|---|
 | 172 | 
 | 
|---|
| [691be4] | 173 |   /** Simple extractor of all unique pair distances of a given \a fragment, where
 | 
|---|
 | 174 |    * the first index is less than the second one.
 | 
|---|
| [8aa597] | 175 |    *
 | 
|---|
| [691be4] | 176 |    * \param positions all nuclei positions
 | 
|---|
| [c7aac9] | 177 |    * \param atomicNumber all nuclei atomic numbers
 | 
|---|
| [228340] | 178 |    * \param edges edges of the fragment's bond graph
 | 
|---|
| [691be4] | 179 |    * \param index index refers to the index within the global set of configurations
 | 
|---|
 | 180 |    * \return vector of of argument_t containing all found distances
 | 
|---|
 | 181 |    */
 | 
|---|
 | 182 |   inline FunctionModel::arguments_t gatherAllSymmetricDistances(
 | 
|---|
 | 183 |       const Fragment::positions_t& positions,
 | 
|---|
| [c7aac9] | 184 |       const Fragment::atomicnumbers_t& atomicnumbers,
 | 
|---|
| [228340] | 185 |       const FragmentationEdges::edges_t &edges,
 | 
|---|
| [691be4] | 186 |       const size_t index
 | 
|---|
 | 187 |       ) {
 | 
|---|
 | 188 |     // get distance out of Fragment
 | 
|---|
| [228340] | 189 |     return gatherAllSymmetricDistanceArguments(positions, atomicnumbers, edges, index);
 | 
|---|
| [691be4] | 190 |   }
 | 
|---|
 | 191 | 
 | 
|---|
| [1155ba] | 192 |   /** Filter the arguments to select only these required by the model.
 | 
|---|
| [df350c] | 193 |    *
 | 
|---|
| [1155ba] | 194 |    * \warning this is meant as a faster way of getting the arguments for simple
 | 
|---|
 | 195 |    * pair potentials. In any other case, one should use filterArgumentsByBindingModel()
 | 
|---|
| [df350c] | 196 |    *
 | 
|---|
| [e1fe7e] | 197 |    * \param listargs list of arguments to reorder each
 | 
|---|
| [e60558] | 198 |    * \param _graph contains binding model of graph
 | 
|---|
| [df350c] | 199 |    * \param _types particle type vector
 | 
|---|
 | 200 |    * \return reordered args
 | 
|---|
 | 201 |    */
 | 
|---|
| [1155ba] | 202 |   FunctionModel::list_of_arguments_t filterArgumentsByParticleTypes(
 | 
|---|
 | 203 |       const FunctionModel::arguments_t &args,
 | 
|---|
| [e60558] | 204 |       const HomologyGraph &_graph,
 | 
|---|
| [67044a] | 205 |       const ParticleTypes_t &_types,
 | 
|---|
| [9c793c] | 206 |       const BindingModel &_bindingmodel
 | 
|---|
| [df350c] | 207 |       );
 | 
|---|
| [9897ee9] | 208 | 
 | 
|---|
| [1155ba] | 209 |   /** Filter and reorder the arguments to bring adjacent ones together.
 | 
|---|
| [51e0e3] | 210 |    *
 | 
|---|
| [1155ba] | 211 |    * We need to find all matching subgraphs (given by \a _bindingmodel) in the
 | 
|---|
 | 212 |    * given homology graph (given by \a _graph) of the fragment molecule.
 | 
|---|
 | 213 |    * This means filtering down to the desired particle types and then find
 | 
|---|
 | 214 |    * all possible matching subgraphs in each of argument lists, \a eachargs.
 | 
|---|
| [51e0e3] | 215 |    *
 | 
|---|
| [1155ba] | 216 |    * \param listargs list of arguments to filter and order appropriately
 | 
|---|
| [e60558] | 217 |    * \param _graph contains binding model of graph
 | 
|---|
| [51e0e3] | 218 |    * \param _types particle type vector
 | 
|---|
| [1155ba] | 219 |    * \return reordered args
 | 
|---|
| [51e0e3] | 220 |    */
 | 
|---|
| [1155ba] | 221 |   FunctionModel::list_of_arguments_t filterArgumentsByBindingModel(
 | 
|---|
| [51e0e3] | 222 |       const FunctionModel::arguments_t &args,
 | 
|---|
| [e60558] | 223 |       const HomologyGraph &_graph,
 | 
|---|
| [67044a] | 224 |       const ParticleTypes_t &_types,
 | 
|---|
| [9c793c] | 225 |       const BindingModel &_bindingmodel
 | 
|---|
| [51e0e3] | 226 |       );
 | 
|---|
 | 227 | 
 | 
|---|
| [cf4905] | 228 |   /** Combines two argument lists by sorting and making unique.
 | 
|---|
| [9897ee9] | 229 |    *
 | 
|---|
 | 230 |    * @param firstargs first list of arguments
 | 
|---|
 | 231 |    * @param secondargs second list of arguments
 | 
|---|
 | 232 |    * @return concatenated lists
 | 
|---|
 | 233 |    */
 | 
|---|
 | 234 |   FunctionModel::arguments_t combineArguments(
 | 
|---|
 | 235 |       const FunctionModel::arguments_t &firstargs,
 | 
|---|
 | 236 |       const FunctionModel::arguments_t &secondargs);
 | 
|---|
 | 237 | 
 | 
|---|
| [cf4905] | 238 |   /** Combines two argument lists by concatenation.
 | 
|---|
 | 239 |    *
 | 
|---|
 | 240 |    * @param firstargs first list of arguments
 | 
|---|
 | 241 |    * @param secondargs second list of arguments
 | 
|---|
 | 242 |    * @return concatenated lists
 | 
|---|
 | 243 |    */
 | 
|---|
 | 244 |   FunctionModel::arguments_t concatenateArguments(
 | 
|---|
 | 245 |       const FunctionModel::arguments_t &firstargs,
 | 
|---|
 | 246 |       const FunctionModel::arguments_t &secondargs);
 | 
|---|
 | 247 | 
 | 
|---|
| [e1fe7e] | 248 |   /** Combines two argument lists by concatenation.
 | 
|---|
 | 249 |    *
 | 
|---|
 | 250 |    * @param firstlistargs first list of argument tuples
 | 
|---|
 | 251 |    * @param secondlistargs second list of argument tuples
 | 
|---|
 | 252 |    * @return concatenated lists
 | 
|---|
 | 253 |    */
 | 
|---|
 | 254 |   FunctionModel::list_of_arguments_t concatenateListOfArguments(
 | 
|---|
 | 255 |       const FunctionModel::list_of_arguments_t &firstlistargs,
 | 
|---|
 | 256 |       const FunctionModel::list_of_arguments_t &secondlistargs);
 | 
|---|
 | 257 | 
 | 
|---|
| [8aa597] | 258 | }; /* namespace Extractors */
 | 
|---|
 | 259 | 
 | 
|---|
 | 260 | 
 | 
|---|
 | 261 | #endif /* TRAININGDATA_EXTRACTORS_HPP_ */
 | 
|---|