| [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 |  | 
|---|
| [caa00e9] | 16 | #include <boost/function.hpp> | 
|---|
|  | 17 |  | 
|---|
| [fbf143] | 18 | #include "Fragmentation/Summation/SetValues/Fragment.hpp" | 
|---|
| [8aa597] | 19 | #include "FunctionApproximation/FunctionModel.hpp" | 
|---|
|  | 20 |  | 
|---|
|  | 21 | class Fragment; | 
|---|
|  | 22 |  | 
|---|
|  | 23 | /** Namespace containing all simple extractor functions. | 
|---|
| [1ba8a1] | 24 | * | 
|---|
|  | 25 | * Extractor functions extract distances from a given fragment matching with | 
|---|
|  | 26 | * a given set of particle types (i.e. elements, e.h. H2O). | 
|---|
|  | 27 | * Filter functions extract a subset of distances from a given set of distances | 
|---|
|  | 28 | * to be used with a specific model. | 
|---|
|  | 29 | * | 
|---|
|  | 30 | * To this end, each FunctionModel has both a filter and an extractor function. | 
|---|
|  | 31 | * | 
|---|
|  | 32 | * The functions in this namespace act as helpers or basic building blocks in | 
|---|
|  | 33 | * constructing such filters and extractors. | 
|---|
| [8aa597] | 34 | * | 
|---|
|  | 35 | */ | 
|---|
|  | 36 | namespace Extractors { | 
|---|
| [301dbf] | 37 | typedef Fragment::charges_t::const_iterator chargeiter_t; | 
|---|
|  | 38 | typedef std::vector<chargeiter_t> chargeiters_t; | 
|---|
|  | 39 |  | 
|---|
|  | 40 | typedef size_t count_t; | 
|---|
|  | 41 | typedef Fragment::charge_t element_t; | 
|---|
|  | 42 | typedef std::map< element_t, count_t> elementcounts_t; | 
|---|
|  | 43 | typedef std::map< element_t, chargeiters_t > elementtargets_t; | 
|---|
| [caa00e9] | 44 | typedef std::vector< chargeiters_t > targets_per_combination_t; | 
|---|
| [df350c] | 45 | //!> typedef for particle designation | 
|---|
|  | 46 | typedef int ParticleType_t; | 
|---|
|  | 47 | //!> typedef for a vector of particle designations | 
|---|
|  | 48 | typedef std::vector<ParticleType_t> ParticleTypes_t; | 
|---|
| [301dbf] | 49 |  | 
|---|
| [8aa597] | 50 | /** Namespace for some internal helper functions. | 
|---|
|  | 51 | * | 
|---|
|  | 52 | */ | 
|---|
|  | 53 | namespace _detail { | 
|---|
| [691be4] | 54 | /** Gather all distance arguments from the same aligned vector of charges. | 
|---|
|  | 55 | * | 
|---|
|  | 56 | * Basically, we filter the positions indicated by the targets but | 
|---|
|  | 57 | * from a different vector that has the same layout. | 
|---|
|  | 58 | * | 
|---|
|  | 59 | * \param positions all nuclei positions | 
|---|
|  | 60 | * \param charges all nuclei charges | 
|---|
|  | 61 | * \param targets iterators on charges | 
|---|
|  | 62 | * \return filtered distance arguments | 
|---|
|  | 63 | */ | 
|---|
|  | 64 | FunctionModel::arguments_t gatherDistancesFromTargets( | 
|---|
|  | 65 | const Fragment::positions_t& positions, | 
|---|
|  | 66 | const Fragment::charges_t& charges, | 
|---|
|  | 67 | const chargeiters_t &targets, | 
|---|
|  | 68 | const size_t globalid | 
|---|
|  | 69 | ); | 
|---|
|  | 70 |  | 
|---|
| [301dbf] | 71 | /** Gather all positions from the same aligned vector of charges. | 
|---|
|  | 72 | * | 
|---|
|  | 73 | * Basically, we filter the positions indicated by the targets but | 
|---|
|  | 74 | * from a different vector that has the same layout. | 
|---|
|  | 75 | * | 
|---|
| [691be4] | 76 | * \param positions all nuclei positions | 
|---|
|  | 77 | * \param charges all nuclei charges | 
|---|
| [301dbf] | 78 | * \param targets iterators on charges | 
|---|
|  | 79 | * \return filtered positions | 
|---|
|  | 80 | */ | 
|---|
| [691be4] | 81 | Fragment::positions_t gatherPositionsFromTargets( | 
|---|
|  | 82 | const Fragment::positions_t& positions, | 
|---|
|  | 83 | const Fragment::charges_t& charges, | 
|---|
|  | 84 | const chargeiters_t& targets | 
|---|
| [301dbf] | 85 | ); | 
|---|
| [bc6705] | 86 |  | 
|---|
|  | 87 | /** Counts all same elements in the vector and places into map of elements. | 
|---|
|  | 88 | * | 
|---|
|  | 89 | * \param elements vector of elements | 
|---|
|  | 90 | * \return count of same element in vector | 
|---|
|  | 91 | */ | 
|---|
|  | 92 | elementcounts_t getElementCounts( | 
|---|
|  | 93 | const Fragment::charges_t elements | 
|---|
|  | 94 | ); | 
|---|
|  | 95 |  | 
|---|
| [c211f7] | 96 | /** Gather iterators to the elements related to the desired elementcounts. | 
|---|
|  | 97 | * | 
|---|
|  | 98 | * \param charges charges wherein to search for the elements | 
|---|
|  | 99 | * \param elementcounts number of desired hits per element | 
|---|
|  | 100 | * \return iterators equal to the initial vector of elements | 
|---|
|  | 101 | */ | 
|---|
|  | 102 | elementtargets_t convertElementcountsToTargets( | 
|---|
|  | 103 | const Fragment::charges_t &charges, | 
|---|
|  | 104 | const elementcounts_t &elementcounts | 
|---|
|  | 105 | ); | 
|---|
| [63e786] | 106 |  | 
|---|
|  | 107 | /** Convert the alignment back to as it was in the original vector. | 
|---|
|  | 108 | * | 
|---|
|  | 109 | * We lost the information by storing it in a map. Hence, we need this | 
|---|
|  | 110 | * final step. | 
|---|
|  | 111 | * | 
|---|
|  | 112 | * \param elementtargets targets as they are in the map \a elementcounts | 
|---|
|  | 113 | * \param elements the original order of the elements | 
|---|
|  | 114 | * \param elementcounts the count per element for debugging checks | 
|---|
|  | 115 | * \return vector of targets in the order as they are in \a element | 
|---|
|  | 116 | */ | 
|---|
|  | 117 | chargeiters_t realignElementtargets( | 
|---|
|  | 118 | const elementtargets_t &elementtargets, | 
|---|
|  | 119 | const Fragment::charges_t elements, | 
|---|
|  | 120 | const elementcounts_t &elementcounts | 
|---|
|  | 121 | ); | 
|---|
| [691be4] | 122 |  | 
|---|
|  | 123 | /** Searches for desired elements in charges in a unique manner. | 
|---|
|  | 124 | * | 
|---|
|  | 125 | * The idea is to have e.g. a fragment with charges 8,1,1,2 and | 
|---|
|  | 126 | * elements as 1,8,1 (e.g. for an angle HOH) and we get the | 
|---|
|  | 127 | * chargeiters in the desired manner on indices: 1,0,3. | 
|---|
|  | 128 | * | 
|---|
|  | 129 | * \param charges charges to look through | 
|---|
|  | 130 | * \param elements vector of elements to find | 
|---|
|  | 131 | */ | 
|---|
|  | 132 | chargeiters_t | 
|---|
|  | 133 | gatherTargetsFromFragment( | 
|---|
|  | 134 | const Fragment::charges_t& charges, | 
|---|
|  | 135 | const Fragment::charges_t elements | 
|---|
|  | 136 | ); | 
|---|
| [caa00e9] | 137 |  | 
|---|
|  | 138 | /** Brings all charges together in a map. | 
|---|
|  | 139 | * | 
|---|
|  | 140 | * @param charges charges as possible keys and their iterators as values in the map | 
|---|
|  | 141 | * @param elements list of charges to pick as keys | 
|---|
|  | 142 | * @return map of key and a vector of charge iterators | 
|---|
|  | 143 | */ | 
|---|
|  | 144 | elementtargets_t convertChargesToTargetMap( | 
|---|
|  | 145 | const Fragment::charges_t& charges, | 
|---|
|  | 146 | Fragment::charges_t elements | 
|---|
|  | 147 | ); | 
|---|
|  | 148 |  | 
|---|
|  | 149 | /** Brings combinatorially together desired list of \a charges and \a targets. | 
|---|
|  | 150 | * | 
|---|
|  | 151 | * @param charges list of desired charges | 
|---|
|  | 152 | * @param elementtargets map of available targets per element | 
|---|
|  | 153 | * @return vector of chargeiters with all unique combinations | 
|---|
|  | 154 | */ | 
|---|
|  | 155 | targets_per_combination_t | 
|---|
|  | 156 | CombineChargesAndTargets( | 
|---|
|  | 157 | const Fragment::charges_t& charges, | 
|---|
|  | 158 | const elementtargets_t& elementtargets | 
|---|
|  | 159 | ); | 
|---|
|  | 160 |  | 
|---|
|  | 161 | /** Recursive function to pick the next target. | 
|---|
|  | 162 | * | 
|---|
|  | 163 | * This is used by \sa CombineChargesAndTargets() | 
|---|
|  | 164 | * | 
|---|
|  | 165 | * @param charges set of charges, reduced by one per recursion | 
|---|
|  | 166 | * @param elementtargets targets, map of targets to pick from | 
|---|
|  | 167 | * @param currenttargets current set of targets, "global" through recursion | 
|---|
|  | 168 | * @param addFunction bound function to add a set when complete | 
|---|
|  | 169 | */ | 
|---|
|  | 170 | void pickLastElementAsTarget( | 
|---|
|  | 171 | Fragment::charges_t elements, | 
|---|
|  | 172 | elementtargets_t elementtargets, | 
|---|
|  | 173 | chargeiters_t& currenttargets, | 
|---|
|  | 174 | boost::function<void (const chargeiters_t ¤ttargets)> &addFunction | 
|---|
|  | 175 | ); | 
|---|
|  | 176 |  | 
|---|
|  | 177 | /** Converts a list of chargeiters to a list of respective arguments. | 
|---|
|  | 178 | * | 
|---|
|  | 179 | * @param positions positions from fragment | 
|---|
|  | 180 | * @param charges charges associated to each element in \a positions | 
|---|
|  | 181 | * @param combinations vector of chargeiters | 
|---|
|  | 182 | * \param globalid refers to the index within the global set of configurations | 
|---|
|  | 183 | * @return list of arguments | 
|---|
|  | 184 | */ | 
|---|
|  | 185 | FunctionModel::arguments_t | 
|---|
|  | 186 | convertTargetsToArguments( | 
|---|
|  | 187 | const Fragment::positions_t& positions, | 
|---|
|  | 188 | const Fragment::charges_t& charges, | 
|---|
|  | 189 | const targets_per_combination_t combinations, | 
|---|
|  | 190 | const size_t globalid | 
|---|
|  | 191 | ); | 
|---|
| [8aa597] | 192 | } | 
|---|
|  | 193 |  | 
|---|
| [49f163] | 194 | /** Gather all distances from a given set of positions. | 
|---|
|  | 195 | * | 
|---|
| [691be4] | 196 | * \param positions all nuclei positions | 
|---|
|  | 197 | * \param charges all nuclei charges | 
|---|
| [49f163] | 198 | * \param globalid index to associated in argument_t with | 
|---|
|  | 199 | * \return vector of argument_ , each with a distance | 
|---|
|  | 200 | */ | 
|---|
|  | 201 | FunctionModel::arguments_t | 
|---|
|  | 202 | gatherAllDistanceArguments( | 
|---|
| [691be4] | 203 | const Fragment::positions_t& positions, | 
|---|
|  | 204 | const Fragment::charges_t& charges, | 
|---|
| [49f163] | 205 | const size_t globalid); | 
|---|
|  | 206 |  | 
|---|
|  | 207 | /** Gather all distances from a given set of positions. | 
|---|
|  | 208 | * | 
|---|
|  | 209 | *  Here, we only return one of the two equal distances. | 
|---|
|  | 210 | * | 
|---|
| [691be4] | 211 | * \param positions all nuclei positions | 
|---|
|  | 212 | * \param charges all nuclei charges | 
|---|
| [49f163] | 213 | * \param globalid index to associated in argument_t with | 
|---|
|  | 214 | * \return vector of argument_ , each with a distance | 
|---|
|  | 215 | */ | 
|---|
|  | 216 | FunctionModel::arguments_t | 
|---|
|  | 217 | gatherAllSymmetricDistanceArguments( | 
|---|
| [691be4] | 218 | const Fragment::positions_t& positions, | 
|---|
|  | 219 | const Fragment::charges_t& charges, | 
|---|
| [49f163] | 220 | const size_t globalid); | 
|---|
|  | 221 |  | 
|---|
| [8aa597] | 222 | /** Simple extractor of all unique pair distances of a given \a fragment. | 
|---|
|  | 223 | * | 
|---|
| [691be4] | 224 | * \param positions all nuclei positions | 
|---|
|  | 225 | * \param charges all nuclei charges | 
|---|
| [8aa597] | 226 | * \param index index refers to the index within the global set of configurations | 
|---|
|  | 227 | * \return vector of of argument_t containing all found distances | 
|---|
|  | 228 | */ | 
|---|
|  | 229 | inline FunctionModel::arguments_t gatherAllDistances( | 
|---|
| [691be4] | 230 | const Fragment::positions_t& positions, | 
|---|
|  | 231 | const Fragment::charges_t& charges, | 
|---|
| [8aa597] | 232 | const size_t index | 
|---|
|  | 233 | ) { | 
|---|
|  | 234 | // get distance out of Fragment | 
|---|
| [691be4] | 235 | return gatherAllDistanceArguments(positions, charges, index); | 
|---|
| [8aa597] | 236 | } | 
|---|
|  | 237 |  | 
|---|
| [691be4] | 238 | /** Simple extractor of all unique pair distances of a given \a fragment, where | 
|---|
|  | 239 | * the first index is less than the second one. | 
|---|
| [8aa597] | 240 | * | 
|---|
| [691be4] | 241 | * \param positions all nuclei positions | 
|---|
|  | 242 | * \param charges all nuclei charges | 
|---|
|  | 243 | * \param index index refers to the index within the global set of configurations | 
|---|
|  | 244 | * \return vector of of argument_t containing all found distances | 
|---|
|  | 245 | */ | 
|---|
|  | 246 | inline FunctionModel::arguments_t gatherAllSymmetricDistances( | 
|---|
|  | 247 | const Fragment::positions_t& positions, | 
|---|
|  | 248 | const Fragment::charges_t& charges, | 
|---|
|  | 249 | const size_t index | 
|---|
|  | 250 | ) { | 
|---|
|  | 251 | // get distance out of Fragment | 
|---|
|  | 252 | return gatherAllSymmetricDistanceArguments(positions, charges, index); | 
|---|
|  | 253 | } | 
|---|
|  | 254 |  | 
|---|
|  | 255 | /** Filters only those positions out of given \a fragment that match \a elements. | 
|---|
|  | 256 | * | 
|---|
|  | 257 | * \param positions all nuclei positions | 
|---|
|  | 258 | * \param charges all nuclei charges | 
|---|
| [301dbf] | 259 | * \param elements tuple of desired elements | 
|---|
|  | 260 | * \return vector of positions_t containing | 
|---|
| [8aa597] | 261 | */ | 
|---|
| [691be4] | 262 | Fragment::positions_t gatherPositionsFromFragment( | 
|---|
|  | 263 | const Fragment::positions_t positions, | 
|---|
|  | 264 | const Fragment::charges_t charges, | 
|---|
|  | 265 | const Fragment::charges_t& elements | 
|---|
|  | 266 | ); | 
|---|
|  | 267 |  | 
|---|
|  | 268 | /** Filters only those distances out of given \a fragment that match \a elements. | 
|---|
|  | 269 | * | 
|---|
|  | 270 | * \param positions all nuclei positions | 
|---|
|  | 271 | * \param charges all nuclei charges | 
|---|
|  | 272 | * \param elements tuple of desired elements | 
|---|
| [caa00e9] | 273 | * \param globalid refers to the index within the global set of configurations | 
|---|
| [691be4] | 274 | * \return vector of arguments_t containing those matched with elements | 
|---|
|  | 275 | */ | 
|---|
|  | 276 | FunctionModel::arguments_t gatherDistancesFromFragment( | 
|---|
|  | 277 | const Fragment::positions_t positions, | 
|---|
|  | 278 | const Fragment::charges_t charges, | 
|---|
|  | 279 | const Fragment::charges_t& elements, | 
|---|
|  | 280 | const size_t globalid | 
|---|
| [301dbf] | 281 | ); | 
|---|
|  | 282 |  | 
|---|
| [caa00e9] | 283 | /** Gather all combinations of charges as distance arguments from the fragment. | 
|---|
|  | 284 | * | 
|---|
|  | 285 | * E.g. we have a water fragment, i.e. (8,1,1) and we we want elements (8,1), | 
|---|
|  | 286 | * then two arguments are returned, first to second and first to third. | 
|---|
|  | 287 | * | 
|---|
|  | 288 | * With \sa gatherDistancesFromFragment() only the first distance would be | 
|---|
|  | 289 | * returned. | 
|---|
|  | 290 | * | 
|---|
|  | 291 | * @param positions positions in fragment | 
|---|
|  | 292 | * @param charges charges in fragment | 
|---|
|  | 293 | * @param elements list of desired elements | 
|---|
|  | 294 | * @param globalid some global id to discern training data tuples | 
|---|
|  | 295 | * @return list of arguments with distances | 
|---|
|  | 296 | */ | 
|---|
|  | 297 | FunctionModel::arguments_t gatherAllDistancesFromFragment( | 
|---|
|  | 298 | const Fragment::positions_t& positions, | 
|---|
|  | 299 | const Fragment::charges_t& charges, | 
|---|
|  | 300 | const Fragment::charges_t elements, | 
|---|
|  | 301 | const size_t globalid | 
|---|
|  | 302 | ); | 
|---|
|  | 303 |  | 
|---|
| [301dbf] | 304 | /** Reorder arguments by increasing distance. | 
|---|
|  | 305 | * | 
|---|
| [e1fe7e] | 306 | * \param listargs list of arguments to reorder each | 
|---|
| [301dbf] | 307 | * \return reordered args | 
|---|
|  | 308 | */ | 
|---|
| [e1fe7e] | 309 | FunctionModel::list_of_arguments_t reorderArgumentsByIncreasingDistance( | 
|---|
|  | 310 | const FunctionModel::list_of_arguments_t &listargs | 
|---|
| [8aa597] | 311 | ); | 
|---|
|  | 312 |  | 
|---|
| [51e0e3] | 313 | /** Reorder the arguments to bring adjacent ones together. | 
|---|
| [df350c] | 314 | * | 
|---|
| [51e0e3] | 315 | * After filtering via particle types arguments related via same indices | 
|---|
|  | 316 | * must not necessarily be contained in the same bunch. This reordering | 
|---|
|  | 317 | * is done here, preserving the alignment given in | 
|---|
|  | 318 | * \sa filterArgumentsByParticleTypes() | 
|---|
| [df350c] | 319 | * | 
|---|
| [e1fe7e] | 320 | * \param listargs list of arguments to reorder each | 
|---|
| [df350c] | 321 | * \param _types particle type vector | 
|---|
|  | 322 | * \return reordered args | 
|---|
|  | 323 | */ | 
|---|
| [e1fe7e] | 324 | FunctionModel::list_of_arguments_t reorderArgumentsByParticleTypes( | 
|---|
|  | 325 | const FunctionModel::list_of_arguments_t &eachargs, | 
|---|
| [df350c] | 326 | const ParticleTypes_t &_types | 
|---|
|  | 327 | ); | 
|---|
| [9897ee9] | 328 |  | 
|---|
| [e1fe7e] | 329 | /** Filter arguments according to types, allowing multiples. | 
|---|
| [51e0e3] | 330 | * | 
|---|
|  | 331 | * If particle types is (0,1,2) and three arguments, each with a pair of types, | 
|---|
|  | 332 | * are given, then the alignment will be: (0,1), (0,2), and (1,2). | 
|---|
|  | 333 | * | 
|---|
|  | 334 | * \param args arguments to reorder | 
|---|
|  | 335 | * \param _types particle type vector | 
|---|
| [e1fe7e] | 336 | * \return filtered list of args | 
|---|
| [51e0e3] | 337 | */ | 
|---|
| [e1fe7e] | 338 | FunctionModel::list_of_arguments_t filterArgumentsByParticleTypes( | 
|---|
| [51e0e3] | 339 | const FunctionModel::arguments_t &args, | 
|---|
|  | 340 | const ParticleTypes_t &_types | 
|---|
|  | 341 | ); | 
|---|
|  | 342 |  | 
|---|
| [cf4905] | 343 | /** Combines two argument lists by sorting and making unique. | 
|---|
| [9897ee9] | 344 | * | 
|---|
|  | 345 | * @param firstargs first list of arguments | 
|---|
|  | 346 | * @param secondargs second list of arguments | 
|---|
|  | 347 | * @return concatenated lists | 
|---|
|  | 348 | */ | 
|---|
|  | 349 | FunctionModel::arguments_t combineArguments( | 
|---|
|  | 350 | const FunctionModel::arguments_t &firstargs, | 
|---|
|  | 351 | const FunctionModel::arguments_t &secondargs); | 
|---|
|  | 352 |  | 
|---|
| [cf4905] | 353 | /** Combines two argument lists by concatenation. | 
|---|
|  | 354 | * | 
|---|
|  | 355 | * @param firstargs first list of arguments | 
|---|
|  | 356 | * @param secondargs second list of arguments | 
|---|
|  | 357 | * @return concatenated lists | 
|---|
|  | 358 | */ | 
|---|
|  | 359 | FunctionModel::arguments_t concatenateArguments( | 
|---|
|  | 360 | const FunctionModel::arguments_t &firstargs, | 
|---|
|  | 361 | const FunctionModel::arguments_t &secondargs); | 
|---|
|  | 362 |  | 
|---|
| [e1fe7e] | 363 | /** Combines two argument lists by concatenation. | 
|---|
|  | 364 | * | 
|---|
|  | 365 | * @param firstlistargs first list of argument tuples | 
|---|
|  | 366 | * @param secondlistargs second list of argument tuples | 
|---|
|  | 367 | * @return concatenated lists | 
|---|
|  | 368 | */ | 
|---|
|  | 369 | FunctionModel::list_of_arguments_t concatenateListOfArguments( | 
|---|
|  | 370 | const FunctionModel::list_of_arguments_t &firstlistargs, | 
|---|
|  | 371 | const FunctionModel::list_of_arguments_t &secondlistargs); | 
|---|
|  | 372 |  | 
|---|
| [8aa597] | 373 | }; /* namespace Extractors */ | 
|---|
|  | 374 |  | 
|---|
|  | 375 |  | 
|---|
|  | 376 | #endif /* TRAININGDATA_EXTRACTORS_HPP_ */ | 
|---|