IMP  2.3.0
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-2014 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/base/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(kernel::Model *m, kernel::ParticleIndex p,
34  kernel::Model *m, const kernel::ParticleIndexes &pis) const IMP_OVERRIDE {
35  return IMP::kernel::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>
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 */
Class for adding derivatives from restraints to the model.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Import IMP/kernel/UnaryFunction.h in the namespace.
ParticlesTemp get_particles(kernel::Model *m, const ParticleIndexes &ps)
Import IMP/kernel/singleton_macros.h in the namespace.
Abstract single variable functor class for score functions.
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
GenericAttributeSingletonScore< UnaryFunction > AttributeSingletonScore
Import IMP/kernel/SingletonScore.h in the namespace.
virtual double evaluate_index(kernel::Model *m, kernel::ParticleIndex p, DerivativeAccumulator *da) const
Compute the score and the derivative if needed.
A nullptr-initialized pointer to an IMP Object.
Abstract class for scoring object(s) of type Particle.
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:20
virtual kernel::ModelObjectsTemp do_get_inputs(kernel::Model *m, const kernel::ParticleIndexes &pis) const
Apply a function to an attribute.
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.
#define IMP_SINGLETON_SCORE_METHODS(Name)
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73
GenericAttributeSingletonScore< UF > * create_attribute_singleton_score(UF *uf, FloatKey k)
Import IMP/kernel/generic.h in the namespace.