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