IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
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>
17
18IMPCORE_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.*/
23template <class UF>
24class GenericAttributeSingletonScore : public SingletonScore {
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)
46template <class UF>
48 UF *f, FloatKey k)
49 : SingletonScore("AttributeSingletonScore%1%"), f_(f), k_(k) {}
50template <class UF>
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).*/
72template <class UF>
74 UF *uf, FloatKey k) {
75 return new GenericAttributeSingletonScore<UF>(uf, k);
76}
77IMPCORE_END_NAMESPACE
78
79#endif /* IMPCORE_GENERIC_ATTRIBUTE_SINGLETON_SCORE_H */
A nullptr-initialized pointer to an IMP Object.
Define SingletonScore.
Single variable function.
Class for adding derivatives from restraints to the model.
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Abstract class for scoring object(s) of type ParticleIndex.
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
UF * get_unary_function()
get access to the unary function object used for scoring the attribute
virtual double evaluate_index(Model *m, ParticleIndex p, DerivativeAccumulator *da) const override
Compute the score and the derivative if needed.
GenericAttributeSingletonScore(UF *f, FloatKey k)
Apply function f to attribute k.
FloatKey get_key() const
Get the attribute key used.
Compile-time generic restraint and constraint support.
GenericAttributeSingletonScore< UF > * create_generic_attribute_singleton_score(UF *uf, FloatKey k)
GenericAttributeSingletonScore< UnaryFunction > AttributeSingletonScore
Index< ParticleIndexTag > ParticleIndex
Definition base_types.h:196
ParticlesTemp get_particles(Model *m, const ParticleIndexes &ps)
Get the particles from a list of indexes.
double Float
Basic floating-point value (could be float, double...).
Definition types.h:19
Vector< ParticleIndex, std::allocator< ParticleIndex > > ParticleIndexes
A list of particles, each identified by its ParticleIndex.
Definition base_types.h:199
Key< 0 > FloatKey
The type used to identify float attributes in the Particles.
Definition base_types.h:32
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition base_types.h:122
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Macros for various classes.
#define IMP_SINGLETON_SCORE_METHODS(Name)
A smart pointer to a ref-counted Object that is a class member.
Definition Pointer.h:144