IMP logo
IMP Reference Guide  develop.08ecd6469d,2026/04/02
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-2026 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,
32  DerivativeAccumulator *da) const override;
34  Model *m, const ParticleIndexes &pis) const 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_;};
39  //! Get the attribute key used
40  FloatKey get_key() const {return k_;};
43  ;
44 };
45 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
46 template <class UF>
48  UF *f, FloatKey k)
49  : SingletonScore("AttributeSingletonScore%1%"), f_(f), k_(k) {}
50 template <class UF>
51 inline Float GenericAttributeSingletonScore<UF>::evaluate_index(
52  Model *m, ParticleIndex pi,
53  DerivativeAccumulator *da) const {
54  if (da) {
55  Float v, d;
56  boost::tie(v, d) =
57  f_->UF::evaluate_with_derivative(m->get_attribute(k_, pi));
58  m->add_to_derivative(k_, pi, d, *da);
59  return v;
60  } else {
61  return f_->UF::evaluate(m->get_attribute(k_, pi));
62  }
63 }
64 #endif
65 
66 /** For Python users. C++ users should use create_attribute_singleton_score().*/
68 
69 /** Provide a compile time binding version of AttributeSingletonScore,
70  which runs faster than the runtime bound version. UF should be a subclass of
71  UnaryFunction (and not pointer to a generic UnaryFunction).*/
72 template <class UF>
74  UF *uf, FloatKey k) {
75  return new GenericAttributeSingletonScore<UF>(uf, k);
76 }
77 IMPCORE_END_NAMESPACE
78 
79 #endif /* IMPCORE_GENERIC_ATTRIBUTE_SINGLETON_SCORE_H */
UF * get_unary_function()
get access to the unary function object used for scoring the attribute
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
#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)
Get the particles from a list of indexes.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
FloatKey get_key() const
Get the attribute key used.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
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:19
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
Class for adding derivatives from restraints to the model.
Compile-time generic restraint and constraint support.