IMP logo
IMP Reference Guide  2.11.1
The Integrative Modeling Platform
GenericAttributeSingletonScore.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/GenericAttributeSingletonScore.h
3  * \brief A score based on the unmodified value of an attribute.
4  *
5  * Copyright 2007-2019 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPCORE_GENERIC_ATTRIBUTE_SINGLETON_SCORE_H
9 #define IMPCORE_GENERIC_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  }
37  //! get access to the unary function object used for scoring the attribute
38  UF* get_unary_function() {return f_;};
41  ;
42 };
43 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
44 template <class UF>
46  UF *f, FloatKey k)
47  : f_(f), k_(k) {}
48 template <class UF>
49 inline Float GenericAttributeSingletonScore<UF>::evaluate_index(
50  Model *m, ParticleIndex pi,
51  DerivativeAccumulator *da) const {
52  if (da) {
53  Float v, d;
54  boost::tie(v, d) =
55  f_->UF::evaluate_with_derivative(m->get_attribute(k_, pi));
56  m->add_to_derivative(k_, pi, d, *da);
57  return v;
58  } else {
59  return f_->UF::evaluate(m->get_attribute(k_, pi));
60  }
61 }
62 #endif
63 
64 /** For Python users. C++ users should use create_attribute_singleton_score().*/
66 
67 /** Provide a compile time binding version of AttributeSingletonScore,
68  which runs faster than the runtime bound version. UF should be a subclass of
69  UnaryFunction (and not pointer to a generic UnaryFunction).*/
70 template <class UF>
72  UF *uf, FloatKey k) {
73  return new GenericAttributeSingletonScore<UF>(uf, k);
74 }
75 IMPCORE_END_NAMESPACE
76 
77 #endif /* IMPCORE_GENERIC_ATTRIBUTE_SINGLETON_SCORE_H */
UF * get_unary_function()
get access to the unary function object used for scoring the attribute
#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)
A more IMP-like version of the std::vector.
Definition: Vector.h:39
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
GenericAttributeSingletonScore< UF > * create_generic_attribute_singleton_score(UF *uf, FloatKey k)
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
get the value of the particle attribute with the specified key
#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
Overload this method to specify the inputs.
Various important functionality for implementing decorators.