| [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 |  | 
|---|
|  | 119 | HomologyGraph createHomologyGraphFromNodes( | 
|---|
|  | 120 | const nodes_t &nodes, | 
|---|
|  | 121 | const type_index_lookup_t &type_index_lookup, | 
|---|
|  | 122 | const UndirectedGraph &graph, | 
|---|
|  | 123 | const index_map_t &index_map | 
|---|
|  | 124 | ); | 
|---|
|  | 125 |  | 
|---|
|  | 126 | void generateAllInducedConnectedSubgraphs( | 
|---|
|  | 127 | const size_t N, | 
|---|
|  | 128 | const level_t level, | 
|---|
|  | 129 | const nodes_t &nodes, | 
|---|
|  | 130 | set_of_nodes_t &set_of_nodes, | 
|---|
|  | 131 | const nodes_per_level_t &nodes_per_level, | 
|---|
|  | 132 | const UndirectedGraph &graph, | 
|---|
|  | 133 | const std::vector<size_t> &_distance, | 
|---|
|  | 134 | const index_map_t &index_map); | 
|---|
|  | 135 |  | 
|---|
| [8aa597] | 136 | /** Namespace for some internal helper functions. | 
|---|
|  | 137 | * | 
|---|
|  | 138 | */ | 
|---|
|  | 139 | namespace _detail { | 
|---|
| [bc6705] | 140 |  | 
|---|
|  | 141 | /** Counts all same elements in the vector and places into map of elements. | 
|---|
|  | 142 | * | 
|---|
|  | 143 | * \param elements vector of elements | 
|---|
|  | 144 | * \return count of same element in vector | 
|---|
|  | 145 | */ | 
|---|
|  | 146 | elementcounts_t getElementCounts( | 
|---|
| [c5e75f3] | 147 | const Fragment::atomicnumbers_t elements | 
|---|
| [bc6705] | 148 | ); | 
|---|
|  | 149 |  | 
|---|
| [8aa597] | 150 | } | 
|---|
|  | 151 |  | 
|---|
| [49f163] | 152 | /** Gather all distances from a given set of positions. | 
|---|
|  | 153 | * | 
|---|
|  | 154 | *  Here, we only return one of the two equal distances. | 
|---|
|  | 155 | * | 
|---|
| [691be4] | 156 | * \param positions all nuclei positions | 
|---|
| [c7aac9] | 157 | * \param atomicNumber all nuclei atomic numbers | 
|---|
| [228340] | 158 | * \param edges edges of the fragment's bond graph | 
|---|
| [49f163] | 159 | * \param globalid index to associated in argument_t with | 
|---|
|  | 160 | * \return vector of argument_ , each with a distance | 
|---|
|  | 161 | */ | 
|---|
|  | 162 | FunctionModel::arguments_t | 
|---|
|  | 163 | gatherAllSymmetricDistanceArguments( | 
|---|
| [691be4] | 164 | const Fragment::positions_t& positions, | 
|---|
| [c7aac9] | 165 | const Fragment::atomicnumbers_t& atomicnumbers, | 
|---|
| [228340] | 166 | const FragmentationEdges::edges_t &edges, | 
|---|
| [49f163] | 167 | const size_t globalid); | 
|---|
|  | 168 |  | 
|---|
| [691be4] | 169 | /** Simple extractor of all unique pair distances of a given \a fragment, where | 
|---|
|  | 170 | * the first index is less than the second one. | 
|---|
| [8aa597] | 171 | * | 
|---|
| [691be4] | 172 | * \param positions all nuclei positions | 
|---|
| [c7aac9] | 173 | * \param atomicNumber all nuclei atomic numbers | 
|---|
| [228340] | 174 | * \param edges edges of the fragment's bond graph | 
|---|
| [691be4] | 175 | * \param index index refers to the index within the global set of configurations | 
|---|
|  | 176 | * \return vector of of argument_t containing all found distances | 
|---|
|  | 177 | */ | 
|---|
|  | 178 | inline FunctionModel::arguments_t gatherAllSymmetricDistances( | 
|---|
|  | 179 | const Fragment::positions_t& positions, | 
|---|
| [c7aac9] | 180 | const Fragment::atomicnumbers_t& atomicnumbers, | 
|---|
| [228340] | 181 | const FragmentationEdges::edges_t &edges, | 
|---|
| [691be4] | 182 | const size_t index | 
|---|
|  | 183 | ) { | 
|---|
|  | 184 | // get distance out of Fragment | 
|---|
| [228340] | 185 | return gatherAllSymmetricDistanceArguments(positions, atomicnumbers, edges, index); | 
|---|
| [691be4] | 186 | } | 
|---|
|  | 187 |  | 
|---|
| [1155ba] | 188 | /** Filter the arguments to select only these required by the model. | 
|---|
| [df350c] | 189 | * | 
|---|
| [1155ba] | 190 | * \warning this is meant as a faster way of getting the arguments for simple | 
|---|
|  | 191 | * pair potentials. In any other case, one should use filterArgumentsByBindingModel() | 
|---|
| [df350c] | 192 | * | 
|---|
| [e1fe7e] | 193 | * \param listargs list of arguments to reorder each | 
|---|
| [e60558] | 194 | * \param _graph contains binding model of graph | 
|---|
| [df350c] | 195 | * \param _types particle type vector | 
|---|
|  | 196 | * \return reordered args | 
|---|
|  | 197 | */ | 
|---|
| [1155ba] | 198 | FunctionModel::list_of_arguments_t filterArgumentsByParticleTypes( | 
|---|
|  | 199 | const FunctionModel::arguments_t &args, | 
|---|
| [e60558] | 200 | const HomologyGraph &_graph, | 
|---|
| [67044a] | 201 | const ParticleTypes_t &_types, | 
|---|
| [9c793c] | 202 | const BindingModel &_bindingmodel | 
|---|
| [df350c] | 203 | ); | 
|---|
| [9897ee9] | 204 |  | 
|---|
| [1155ba] | 205 | /** Filter and reorder the arguments to bring adjacent ones together. | 
|---|
| [51e0e3] | 206 | * | 
|---|
| [1155ba] | 207 | * We need to find all matching subgraphs (given by \a _bindingmodel) in the | 
|---|
|  | 208 | * given homology graph (given by \a _graph) of the fragment molecule. | 
|---|
|  | 209 | * This means filtering down to the desired particle types and then find | 
|---|
|  | 210 | * all possible matching subgraphs in each of argument lists, \a eachargs. | 
|---|
| [51e0e3] | 211 | * | 
|---|
| [1155ba] | 212 | * \param listargs list of arguments to filter and order appropriately | 
|---|
| [e60558] | 213 | * \param _graph contains binding model of graph | 
|---|
| [51e0e3] | 214 | * \param _types particle type vector | 
|---|
| [1155ba] | 215 | * \return reordered args | 
|---|
| [51e0e3] | 216 | */ | 
|---|
| [1155ba] | 217 | FunctionModel::list_of_arguments_t filterArgumentsByBindingModel( | 
|---|
| [51e0e3] | 218 | const FunctionModel::arguments_t &args, | 
|---|
| [e60558] | 219 | const HomologyGraph &_graph, | 
|---|
| [67044a] | 220 | const ParticleTypes_t &_types, | 
|---|
| [9c793c] | 221 | const BindingModel &_bindingmodel | 
|---|
| [51e0e3] | 222 | ); | 
|---|
|  | 223 |  | 
|---|
| [cf4905] | 224 | /** Combines two argument lists by sorting and making unique. | 
|---|
| [9897ee9] | 225 | * | 
|---|
|  | 226 | * @param firstargs first list of arguments | 
|---|
|  | 227 | * @param secondargs second list of arguments | 
|---|
|  | 228 | * @return concatenated lists | 
|---|
|  | 229 | */ | 
|---|
|  | 230 | FunctionModel::arguments_t combineArguments( | 
|---|
|  | 231 | const FunctionModel::arguments_t &firstargs, | 
|---|
|  | 232 | const FunctionModel::arguments_t &secondargs); | 
|---|
|  | 233 |  | 
|---|
| [cf4905] | 234 | /** Combines two argument lists by concatenation. | 
|---|
|  | 235 | * | 
|---|
|  | 236 | * @param firstargs first list of arguments | 
|---|
|  | 237 | * @param secondargs second list of arguments | 
|---|
|  | 238 | * @return concatenated lists | 
|---|
|  | 239 | */ | 
|---|
|  | 240 | FunctionModel::arguments_t concatenateArguments( | 
|---|
|  | 241 | const FunctionModel::arguments_t &firstargs, | 
|---|
|  | 242 | const FunctionModel::arguments_t &secondargs); | 
|---|
|  | 243 |  | 
|---|
| [e1fe7e] | 244 | /** Combines two argument lists by concatenation. | 
|---|
|  | 245 | * | 
|---|
|  | 246 | * @param firstlistargs first list of argument tuples | 
|---|
|  | 247 | * @param secondlistargs second list of argument tuples | 
|---|
|  | 248 | * @return concatenated lists | 
|---|
|  | 249 | */ | 
|---|
|  | 250 | FunctionModel::list_of_arguments_t concatenateListOfArguments( | 
|---|
|  | 251 | const FunctionModel::list_of_arguments_t &firstlistargs, | 
|---|
|  | 252 | const FunctionModel::list_of_arguments_t &secondlistargs); | 
|---|
|  | 253 |  | 
|---|
| [8aa597] | 254 | }; /* namespace Extractors */ | 
|---|
|  | 255 |  | 
|---|
|  | 256 |  | 
|---|
|  | 257 | #endif /* TRAININGDATA_EXTRACTORS_HPP_ */ | 
|---|