IMP logo
IMP Reference Guide  2.6.1
The Integrative Modeling Platform
AttributeSingletonScore.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/AttributeSingletonScore.h
3  * \brief A score based on the unmodified value of an attribute.
4  *
5  * Copyright 2007-2016 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPCORE_ATTRIBUTE_SINGLETON_SCORE_H
9 #define IMPCORE_ATTRIBUTE_SINGLETON_SCORE_H
10 
11 #include <IMP/core/core_config.h>
12 #include <IMP/generic.h>
13 #include <IMP/SingletonScore.h>
14 #include <IMP/Pointer.h>
15 #include <IMP/UnaryFunction.h>
16 #include <IMP/singleton_macros.h>
17 
18 IMPCORE_BEGIN_NAMESPACE
19 
20 //! Apply a function to an attribute.
21 /** This Score scores a particle by passing an attribute value directly
22  to a unary function.*/
23 template <class UF>
26  FloatKey k_;
27 
28  public:
29  //! Apply function f to attribute k
31  virtual double evaluate_index(Model *m, ParticleIndex p,
34  Model *m, const ParticleIndexes &pis) const IMP_OVERRIDE {
35  return IMP::get_particles(m, pis);
36  }
39  ;
40 };
41 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
42 template <class UF>
44  UnaryFunction *f, FloatKey k)
45  : f_(f), k_(k) {}
46 template <class UF>
48  Model *m, ParticleIndex pi,
49  DerivativeAccumulator *da) const {
50  if (da) {
51  Float v, d;
52  boost::tie(v, d) =
53  f_->UF::evaluate_with_derivative(m->get_attribute(k_, pi));
54  m->add_to_derivative(k_, pi, d, *da);
55  return v;
56  } else {
57  return f_->UF::evaluate(m->get_attribute(k_, pi));
58  }
59 }
60 #endif
61 
62 /** For Python users. C++ users should use create_attribute_singleton_score().*/
64 
65 /** Provide a compile time binding version of AttributeSingletonScore,
66  which runs faster than the runtime bound version. UF should be a subclass of
67  UnaryFunction (and not pointer to a generic UnaryFunction).*/
68 template <class UF>
70  UF *uf, FloatKey k) {
71  return new GenericAttributeSingletonScore<UF>(uf, k);
72 }
73 IMPCORE_END_NAMESPACE
74 
75 #endif /* IMPCORE_ATTRIBUTE_SINGLETON_SCORE_H */
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Single variable function.
Macros for various classes.
ParticlesTemp get_particles(Model *m, const ParticleIndexes &ps)
virtual double evaluate_index(Model *m, ParticleIndex p, DerivativeAccumulator *da) const
Compute the score and the derivative if needed.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:72
Abstract class for scoring object(s) of type ParticleIndex.
#define IMP_SINGLETON_SCORE_METHODS(Name)
GenericAttributeSingletonScore< UnaryFunction > AttributeSingletonScore
Define SingletonScore.
A nullptr-initialized pointer to an IMP Object.
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:20
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
Apply a function to an attribute.
Abstract single variable functor class for score functions.
Definition: UnaryFunction.h:25
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.
Class for adding derivatives from restraints to the model.
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const
GenericAttributeSingletonScore< UF > * create_attribute_singleton_score(UF *uf, FloatKey k)
Various important functionality for implementing decorators.