| 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 |  | 
|---|
| 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> | 
|---|
| 22 | #include <boost/function.hpp> | 
|---|
| 23 |  | 
|---|
| 24 | #include <map> | 
|---|
| 25 | #include <set> | 
|---|
| 26 |  | 
|---|
| 27 | #include "Fragmentation/EdgesPerFragment.hpp" | 
|---|
| 28 | #include "Fragmentation/Summation/SetValues/Fragment.hpp" | 
|---|
| 29 | #include "FunctionApproximation/FunctionModel.hpp" | 
|---|
| 30 |  | 
|---|
| 31 | class BindingModel; | 
|---|
| 32 | class Fragment; | 
|---|
| 33 | class HomologyGraph; | 
|---|
| 34 |  | 
|---|
| 35 | /** Namespace containing all simple extractor functions. | 
|---|
| 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. | 
|---|
| 46 | * | 
|---|
| 47 | */ | 
|---|
| 48 | namespace Extractors { | 
|---|
| 49 | typedef Fragment::charges_t::const_iterator chargeiter_t; | 
|---|
| 50 | typedef std::vector<chargeiter_t> chargeiters_t; | 
|---|
| 51 |  | 
|---|
| 52 | typedef size_t count_t; | 
|---|
| 53 | typedef Fragment::atomicNumber_t element_t; | 
|---|
| 54 | typedef std::map< element_t, count_t> elementcounts_t; | 
|---|
| 55 | typedef std::map< element_t, chargeiters_t > elementtargets_t; | 
|---|
| 56 | typedef std::vector< chargeiters_t > targets_per_combination_t; | 
|---|
| 57 | //!> typedef for particle designation | 
|---|
| 58 | typedef unsigned int ParticleType_t; | 
|---|
| 59 | //!> typedef for a vector of particle designations | 
|---|
| 60 | typedef std::vector<ParticleType_t> ParticleTypes_t; | 
|---|
| 61 |  | 
|---|
| 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 |  | 
|---|
| 119 | Extractors::ParticleType_t getParticleTypeToNode( | 
|---|
| 120 | const type_index_lookup_t &type_index_lookup, | 
|---|
| 121 | const size_t nodeindex); | 
|---|
| 122 |  | 
|---|
| 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 |  | 
|---|
| 140 | /** Namespace for some internal helper functions. | 
|---|
| 141 | * | 
|---|
| 142 | */ | 
|---|
| 143 | namespace _detail { | 
|---|
| 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( | 
|---|
| 151 | const Fragment::atomicnumbers_t elements | 
|---|
| 152 | ); | 
|---|
| 153 |  | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | /** Gather all distances from a given set of positions. | 
|---|
| 157 | * | 
|---|
| 158 | *  Here, we only return one of the two equal distances. | 
|---|
| 159 | * | 
|---|
| 160 | * \param positions all nuclei positions | 
|---|
| 161 | * \param atomicNumber all nuclei atomic numbers | 
|---|
| 162 | * \param edges edges of the fragment's bond graph | 
|---|
| 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( | 
|---|
| 168 | const Fragment::positions_t& positions, | 
|---|
| 169 | const Fragment::atomicnumbers_t& atomicnumbers, | 
|---|
| 170 | const FragmentationEdges::edges_t &edges, | 
|---|
| 171 | const size_t globalid); | 
|---|
| 172 |  | 
|---|
| 173 | /** Simple extractor of all unique pair distances of a given \a fragment, where | 
|---|
| 174 | * the first index is less than the second one. | 
|---|
| 175 | * | 
|---|
| 176 | * \param positions all nuclei positions | 
|---|
| 177 | * \param atomicNumber all nuclei atomic numbers | 
|---|
| 178 | * \param edges edges of the fragment's bond graph | 
|---|
| 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, | 
|---|
| 184 | const Fragment::atomicnumbers_t& atomicnumbers, | 
|---|
| 185 | const FragmentationEdges::edges_t &edges, | 
|---|
| 186 | const size_t index | 
|---|
| 187 | ) { | 
|---|
| 188 | // get distance out of Fragment | 
|---|
| 189 | return gatherAllSymmetricDistanceArguments(positions, atomicnumbers, edges, index); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | /** Filter the arguments to select only these required by the model. | 
|---|
| 193 | * | 
|---|
| 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() | 
|---|
| 196 | * | 
|---|
| 197 | * \param listargs list of arguments to reorder each | 
|---|
| 198 | * \param _graph contains binding model of graph | 
|---|
| 199 | * \param _types particle type vector | 
|---|
| 200 | * \return reordered args | 
|---|
| 201 | */ | 
|---|
| 202 | FunctionModel::list_of_arguments_t filterArgumentsByParticleTypes( | 
|---|
| 203 | const FunctionModel::arguments_t &args, | 
|---|
| 204 | const HomologyGraph &_graph, | 
|---|
| 205 | const ParticleTypes_t &_types, | 
|---|
| 206 | const BindingModel &_bindingmodel | 
|---|
| 207 | ); | 
|---|
| 208 |  | 
|---|
| 209 | /** Filter and reorder the arguments to bring adjacent ones together. | 
|---|
| 210 | * | 
|---|
| 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. | 
|---|
| 215 | * | 
|---|
| 216 | * \param listargs list of arguments to filter and order appropriately | 
|---|
| 217 | * \param _graph contains binding model of graph | 
|---|
| 218 | * \param _types particle type vector | 
|---|
| 219 | * \return reordered args | 
|---|
| 220 | */ | 
|---|
| 221 | FunctionModel::list_of_arguments_t filterArgumentsByBindingModel( | 
|---|
| 222 | const FunctionModel::arguments_t &args, | 
|---|
| 223 | const HomologyGraph &_graph, | 
|---|
| 224 | const ParticleTypes_t &_types, | 
|---|
| 225 | const BindingModel &_bindingmodel | 
|---|
| 226 | ); | 
|---|
| 227 |  | 
|---|
| 228 | /** Combines two argument lists by sorting and making unique. | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 258 | }; /* namespace Extractors */ | 
|---|
| 259 |  | 
|---|
| 260 |  | 
|---|
| 261 | #endif /* TRAININGDATA_EXTRACTORS_HPP_ */ | 
|---|