| 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/SetValues/Fragment.hpp"
 | 
|---|
| 19 | #include "FunctionApproximation/FunctionModel.hpp"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | class Fragment;
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /** Namespace containing all simple extractor functions.
 | 
|---|
| 24 |  *
 | 
|---|
| 25 |  */
 | 
|---|
| 26 | namespace Extractors {
 | 
|---|
| 27 |   typedef Fragment::charges_t::const_iterator chargeiter_t;
 | 
|---|
| 28 |   typedef std::vector<chargeiter_t> chargeiters_t;
 | 
|---|
| 29 | 
 | 
|---|
| 30 |   typedef size_t count_t;
 | 
|---|
| 31 |   typedef Fragment::charge_t element_t;
 | 
|---|
| 32 |   typedef std::map< element_t, count_t> elementcounts_t;
 | 
|---|
| 33 |   typedef std::map< element_t, chargeiters_t > elementtargets_t;
 | 
|---|
| 34 |   typedef std::vector< chargeiters_t > targets_per_combination_t;
 | 
|---|
| 35 | 
 | 
|---|
| 36 |   /** Namespace for some internal helper functions.
 | 
|---|
| 37 |    *
 | 
|---|
| 38 |    */
 | 
|---|
| 39 |   namespace _detail {
 | 
|---|
| 40 |     /** Gather all distance arguments from the same aligned vector of charges.
 | 
|---|
| 41 |      *
 | 
|---|
| 42 |      * Basically, we filter the positions indicated by the targets but
 | 
|---|
| 43 |      * from a different vector that has the same layout.
 | 
|---|
| 44 |      *
 | 
|---|
| 45 |      * \param positions all nuclei positions
 | 
|---|
| 46 |      * \param charges all nuclei charges
 | 
|---|
| 47 |      * \param targets iterators on charges
 | 
|---|
| 48 |      * \return filtered distance arguments
 | 
|---|
| 49 |      */
 | 
|---|
| 50 |     FunctionModel::arguments_t gatherDistancesFromTargets(
 | 
|---|
| 51 |         const Fragment::positions_t& positions,
 | 
|---|
| 52 |         const Fragment::charges_t& charges,
 | 
|---|
| 53 |         const chargeiters_t &targets,
 | 
|---|
| 54 |         const size_t globalid
 | 
|---|
| 55 |         );
 | 
|---|
| 56 | 
 | 
|---|
| 57 |     /** Gather all positions from the same aligned vector of charges.
 | 
|---|
| 58 |      *
 | 
|---|
| 59 |      * Basically, we filter the positions indicated by the targets but
 | 
|---|
| 60 |      * from a different vector that has the same layout.
 | 
|---|
| 61 |      *
 | 
|---|
| 62 |      * \param positions all nuclei positions
 | 
|---|
| 63 |      * \param charges all nuclei charges
 | 
|---|
| 64 |      * \param targets iterators on charges
 | 
|---|
| 65 |      * \return filtered positions
 | 
|---|
| 66 |      */
 | 
|---|
| 67 |     Fragment::positions_t gatherPositionsFromTargets(
 | 
|---|
| 68 |         const Fragment::positions_t& positions,
 | 
|---|
| 69 |         const Fragment::charges_t& charges,
 | 
|---|
| 70 |         const chargeiters_t& targets
 | 
|---|
| 71 |         );
 | 
|---|
| 72 | 
 | 
|---|
| 73 |     /** Counts all same elements in the vector and places into map of elements.
 | 
|---|
| 74 |      *
 | 
|---|
| 75 |      * \param elements vector of elements
 | 
|---|
| 76 |      * \return count of same element in vector
 | 
|---|
| 77 |      */
 | 
|---|
| 78 |     elementcounts_t getElementCounts(
 | 
|---|
| 79 |         const Fragment::charges_t elements
 | 
|---|
| 80 |         );
 | 
|---|
| 81 | 
 | 
|---|
| 82 |     /** Gather iterators to the elements related to the desired elementcounts.
 | 
|---|
| 83 |      *
 | 
|---|
| 84 |      * \param charges charges wherein to search for the elements
 | 
|---|
| 85 |      * \param elementcounts number of desired hits per element
 | 
|---|
| 86 |      * \return iterators equal to the initial vector of elements
 | 
|---|
| 87 |      */
 | 
|---|
| 88 |     elementtargets_t convertElementcountsToTargets(
 | 
|---|
| 89 |         const Fragment::charges_t &charges,
 | 
|---|
| 90 |         const elementcounts_t &elementcounts
 | 
|---|
| 91 |         );
 | 
|---|
| 92 | 
 | 
|---|
| 93 |     /** Convert the alignment back to as it was in the original vector.
 | 
|---|
| 94 |      *
 | 
|---|
| 95 |      * We lost the information by storing it in a map. Hence, we need this
 | 
|---|
| 96 |      * final step.
 | 
|---|
| 97 |      *
 | 
|---|
| 98 |      * \param elementtargets targets as they are in the map \a elementcounts
 | 
|---|
| 99 |      * \param elements the original order of the elements
 | 
|---|
| 100 |      * \param elementcounts the count per element for debugging checks
 | 
|---|
| 101 |      * \return vector of targets in the order as they are in \a element
 | 
|---|
| 102 |      */
 | 
|---|
| 103 |     chargeiters_t realignElementtargets(
 | 
|---|
| 104 |         const elementtargets_t &elementtargets,
 | 
|---|
| 105 |         const Fragment::charges_t elements,
 | 
|---|
| 106 |         const elementcounts_t &elementcounts
 | 
|---|
| 107 |         );
 | 
|---|
| 108 | 
 | 
|---|
| 109 |     /** Searches for desired elements in charges in a unique manner.
 | 
|---|
| 110 |      *
 | 
|---|
| 111 |      * The idea is to have e.g. a fragment with charges 8,1,1,2 and
 | 
|---|
| 112 |      * elements as 1,8,1 (e.g. for an angle HOH) and we get the
 | 
|---|
| 113 |      * chargeiters in the desired manner on indices: 1,0,3.
 | 
|---|
| 114 |      *
 | 
|---|
| 115 |      * \param charges charges to look through
 | 
|---|
| 116 |      * \param elements vector of elements to find
 | 
|---|
| 117 |      */
 | 
|---|
| 118 |     chargeiters_t
 | 
|---|
| 119 |     gatherTargetsFromFragment(
 | 
|---|
| 120 |         const Fragment::charges_t& charges,
 | 
|---|
| 121 |         const Fragment::charges_t elements
 | 
|---|
| 122 |         );
 | 
|---|
| 123 | 
 | 
|---|
| 124 |     /** Brings all charges together in a map.
 | 
|---|
| 125 |      *
 | 
|---|
| 126 |      * @param charges charges as possible keys and their iterators as values in the map
 | 
|---|
| 127 |      * @param elements list of charges to pick as keys
 | 
|---|
| 128 |      * @return map of key and a vector of charge iterators
 | 
|---|
| 129 |      */
 | 
|---|
| 130 |     elementtargets_t convertChargesToTargetMap(
 | 
|---|
| 131 |         const Fragment::charges_t& charges,
 | 
|---|
| 132 |         Fragment::charges_t elements
 | 
|---|
| 133 |         );
 | 
|---|
| 134 | 
 | 
|---|
| 135 |     /** Brings combinatorially together desired list of \a charges and \a targets.
 | 
|---|
| 136 |      *
 | 
|---|
| 137 |      * @param charges list of desired charges
 | 
|---|
| 138 |      * @param elementtargets map of available targets per element
 | 
|---|
| 139 |      * @return vector of chargeiters with all unique combinations
 | 
|---|
| 140 |      */
 | 
|---|
| 141 |     targets_per_combination_t
 | 
|---|
| 142 |     CombineChargesAndTargets(
 | 
|---|
| 143 |         const Fragment::charges_t& charges,
 | 
|---|
| 144 |         const elementtargets_t& elementtargets
 | 
|---|
| 145 |         );
 | 
|---|
| 146 | 
 | 
|---|
| 147 |     /** Recursive function to pick the next target.
 | 
|---|
| 148 |      *
 | 
|---|
| 149 |      * This is used by \sa CombineChargesAndTargets()
 | 
|---|
| 150 |      *
 | 
|---|
| 151 |      * @param charges set of charges, reduced by one per recursion
 | 
|---|
| 152 |      * @param elementtargets targets, map of targets to pick from
 | 
|---|
| 153 |      * @param currenttargets current set of targets, "global" through recursion
 | 
|---|
| 154 |      * @param addFunction bound function to add a set when complete
 | 
|---|
| 155 |      */
 | 
|---|
| 156 |     void pickLastElementAsTarget(
 | 
|---|
| 157 |         Fragment::charges_t elements,
 | 
|---|
| 158 |         elementtargets_t elementtargets,
 | 
|---|
| 159 |         chargeiters_t& currenttargets,
 | 
|---|
| 160 |         boost::function<void (const chargeiters_t ¤ttargets)> &addFunction
 | 
|---|
| 161 |         );
 | 
|---|
| 162 | 
 | 
|---|
| 163 |     /** Converts a list of chargeiters to a list of respective arguments.
 | 
|---|
| 164 |      *
 | 
|---|
| 165 |      * @param positions positions from fragment
 | 
|---|
| 166 |      * @param charges charges associated to each element in \a positions
 | 
|---|
| 167 |      * @param combinations vector of chargeiters
 | 
|---|
| 168 |      * \param globalid refers to the index within the global set of configurations
 | 
|---|
| 169 |      * @return list of arguments
 | 
|---|
| 170 |      */
 | 
|---|
| 171 |     FunctionModel::arguments_t
 | 
|---|
| 172 |     convertTargetsToArguments(
 | 
|---|
| 173 |         const Fragment::positions_t& positions,
 | 
|---|
| 174 |         const Fragment::charges_t& charges,
 | 
|---|
| 175 |         const targets_per_combination_t combinations,
 | 
|---|
| 176 |         const size_t globalid
 | 
|---|
| 177 |         );
 | 
|---|
| 178 |   }
 | 
|---|
| 179 | 
 | 
|---|
| 180 |   /** Gather all distances from a given set of positions.
 | 
|---|
| 181 |    *
 | 
|---|
| 182 |    * \param positions all nuclei positions
 | 
|---|
| 183 |    * \param charges all nuclei charges
 | 
|---|
| 184 |    * \param globalid index to associated in argument_t with
 | 
|---|
| 185 |    * \return vector of argument_ , each with a distance
 | 
|---|
| 186 |    */
 | 
|---|
| 187 |   FunctionModel::arguments_t
 | 
|---|
| 188 |   gatherAllDistanceArguments(
 | 
|---|
| 189 |       const Fragment::positions_t& positions,
 | 
|---|
| 190 |       const Fragment::charges_t& charges,
 | 
|---|
| 191 |       const size_t globalid);
 | 
|---|
| 192 | 
 | 
|---|
| 193 |   /** Gather all distances from a given set of positions.
 | 
|---|
| 194 |    *
 | 
|---|
| 195 |    *  Here, we only return one of the two equal distances.
 | 
|---|
| 196 |    *
 | 
|---|
| 197 |    * \param positions all nuclei positions
 | 
|---|
| 198 |    * \param charges all nuclei charges
 | 
|---|
| 199 |    * \param globalid index to associated in argument_t with
 | 
|---|
| 200 |    * \return vector of argument_ , each with a distance
 | 
|---|
| 201 |    */
 | 
|---|
| 202 |   FunctionModel::arguments_t
 | 
|---|
| 203 |   gatherAllSymmetricDistanceArguments(
 | 
|---|
| 204 |       const Fragment::positions_t& positions,
 | 
|---|
| 205 |       const Fragment::charges_t& charges,
 | 
|---|
| 206 |       const size_t globalid);
 | 
|---|
| 207 | 
 | 
|---|
| 208 |   /** Simple extractor of all unique pair distances of a given \a fragment.
 | 
|---|
| 209 |    *
 | 
|---|
| 210 |    * \param positions all nuclei positions
 | 
|---|
| 211 |    * \param charges all nuclei charges
 | 
|---|
| 212 |    * \param index index refers to the index within the global set of configurations
 | 
|---|
| 213 |    * \return vector of of argument_t containing all found distances
 | 
|---|
| 214 |    */
 | 
|---|
| 215 |   inline FunctionModel::arguments_t gatherAllDistances(
 | 
|---|
| 216 |       const Fragment::positions_t& positions,
 | 
|---|
| 217 |       const Fragment::charges_t& charges,
 | 
|---|
| 218 |       const size_t index
 | 
|---|
| 219 |       ) {
 | 
|---|
| 220 |     // get distance out of Fragment
 | 
|---|
| 221 |     return gatherAllDistanceArguments(positions, charges, index);
 | 
|---|
| 222 |   }
 | 
|---|
| 223 | 
 | 
|---|
| 224 |   /** Simple extractor of all unique pair distances of a given \a fragment, where
 | 
|---|
| 225 |    * the first index is less than the second one.
 | 
|---|
| 226 |    *
 | 
|---|
| 227 |    * \param positions all nuclei positions
 | 
|---|
| 228 |    * \param charges all nuclei charges
 | 
|---|
| 229 |    * \param index index refers to the index within the global set of configurations
 | 
|---|
| 230 |    * \return vector of of argument_t containing all found distances
 | 
|---|
| 231 |    */
 | 
|---|
| 232 |   inline FunctionModel::arguments_t gatherAllSymmetricDistances(
 | 
|---|
| 233 |       const Fragment::positions_t& positions,
 | 
|---|
| 234 |       const Fragment::charges_t& charges,
 | 
|---|
| 235 |       const size_t index
 | 
|---|
| 236 |       ) {
 | 
|---|
| 237 |     // get distance out of Fragment
 | 
|---|
| 238 |     return gatherAllSymmetricDistanceArguments(positions, charges, index);
 | 
|---|
| 239 |   }
 | 
|---|
| 240 | 
 | 
|---|
| 241 |   /** Filters only those positions out of given \a fragment that match \a elements.
 | 
|---|
| 242 |    *
 | 
|---|
| 243 |    * \param positions all nuclei positions
 | 
|---|
| 244 |    * \param charges all nuclei charges
 | 
|---|
| 245 |    * \param elements tuple of desired elements
 | 
|---|
| 246 |    * \return vector of positions_t containing
 | 
|---|
| 247 |    */
 | 
|---|
| 248 |   Fragment::positions_t gatherPositionsFromFragment(
 | 
|---|
| 249 |       const Fragment::positions_t positions,
 | 
|---|
| 250 |       const Fragment::charges_t charges,
 | 
|---|
| 251 |       const Fragment::charges_t& elements
 | 
|---|
| 252 |       );
 | 
|---|
| 253 | 
 | 
|---|
| 254 |   /** Filters only those distances out of given \a fragment that match \a elements.
 | 
|---|
| 255 |    *
 | 
|---|
| 256 |    * \param positions all nuclei positions
 | 
|---|
| 257 |    * \param charges all nuclei charges
 | 
|---|
| 258 |    * \param elements tuple of desired elements
 | 
|---|
| 259 |    * \param globalid refers to the index within the global set of configurations
 | 
|---|
| 260 |    * \return vector of arguments_t containing those matched with elements
 | 
|---|
| 261 |    */
 | 
|---|
| 262 |   FunctionModel::arguments_t gatherDistancesFromFragment(
 | 
|---|
| 263 |       const Fragment::positions_t positions,
 | 
|---|
| 264 |       const Fragment::charges_t charges,
 | 
|---|
| 265 |       const Fragment::charges_t& elements,
 | 
|---|
| 266 |       const size_t globalid
 | 
|---|
| 267 |       );
 | 
|---|
| 268 | 
 | 
|---|
| 269 |   /** Gather all combinations of charges as distance arguments from the fragment.
 | 
|---|
| 270 |    *
 | 
|---|
| 271 |    * E.g. we have a water fragment, i.e. (8,1,1) and we we want elements (8,1),
 | 
|---|
| 272 |    * then two arguments are returned, first to second and first to third.
 | 
|---|
| 273 |    *
 | 
|---|
| 274 |    * With \sa gatherDistancesFromFragment() only the first distance would be
 | 
|---|
| 275 |    * returned.
 | 
|---|
| 276 |    *
 | 
|---|
| 277 |    * @param positions positions in fragment
 | 
|---|
| 278 |    * @param charges charges in fragment
 | 
|---|
| 279 |    * @param elements list of desired elements
 | 
|---|
| 280 |    * @param globalid some global id to discern training data tuples
 | 
|---|
| 281 |    * @return list of arguments with distances
 | 
|---|
| 282 |    */
 | 
|---|
| 283 |   FunctionModel::arguments_t gatherAllDistancesFromFragment(
 | 
|---|
| 284 |       const Fragment::positions_t& positions,
 | 
|---|
| 285 |       const Fragment::charges_t& charges,
 | 
|---|
| 286 |       const Fragment::charges_t elements,
 | 
|---|
| 287 |       const size_t globalid
 | 
|---|
| 288 |       );
 | 
|---|
| 289 | 
 | 
|---|
| 290 |   /** Reorder arguments by increasing distance.
 | 
|---|
| 291 |    *
 | 
|---|
| 292 |    * \param args arguments to reorder
 | 
|---|
| 293 |    * \return reordered args
 | 
|---|
| 294 |    */
 | 
|---|
| 295 |   FunctionModel::arguments_t reorderArgumentsByIncreasingDistance(
 | 
|---|
| 296 |       const FunctionModel::arguments_t &args
 | 
|---|
| 297 |       );
 | 
|---|
| 298 | 
 | 
|---|
| 299 | }; /* namespace Extractors */
 | 
|---|
| 300 | 
 | 
|---|
| 301 | 
 | 
|---|
| 302 | #endif /* TRAININGDATA_EXTRACTORS_HPP_ */
 | 
|---|