IMP logo
IMP Reference Guide  develop.3beb078e5e,2026/08/23
The Integrative Modeling Platform
Nuisance.h
Go to the documentation of this file.
1 /**
2  * \file IMP/isd/Nuisance.h
3  * \brief A decorator for nuisance parameters particles
4  *
5  * Copyright 2007-2026 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPISD_NUISANCE_H
9 #define IMPISD_NUISANCE_H
10 
11 #include <IMP/isd/isd_config.h>
12 
13 #include <IMP/Decorator.h>
14 #include <IMP/decorator_macros.h>
15 #include <cereal/access.hpp>
16 #include <cereal/types/base_class.hpp>
17 
18 IMPISD_BEGIN_NAMESPACE
19 
20 //! Add nuisance parameter to particle
21 /** The value of the nuisance parameter may express data
22  or theory uncertainty. It can be initialized with or without
23  specifying its value. Default is 1. On construction, the Nuisance is
24  unbounded. It can be bounded with set_upper and set_lower. Setting it
25  to values outside of bounds results in setting it to the bound value.
26  */
27 class IMPISDEXPORT Nuisance : public Decorator {
28  static void do_setup_particle(Model *m, ParticleIndex pi,
29  double nuisance = 1.0);
30 
31  public:
34  IMP_DECORATOR_SETUP_1(Nuisance, double, nuisance);
35 
36  static bool get_is_setup(Model *m, ParticleIndex pi) {
37  return m->get_has_attribute(get_nuisance_key(), pi);
38  }
39 
40  static FloatKey get_nuisance_key();
41  Float get_nuisance() const {
42  return get_model()->get_attribute(get_nuisance_key(), get_particle_index());
43  }
44  void set_nuisance(Float d);
45 
46  /** set upper and lower bound of nuisance by specifying
47  * either a float or another nuisance. Both can be set at the same
48  * time in which case the upper bound is the minimum of the two values.
49  * This constraint is enforced with the help of a ScoreState that will be
50  * created on-the-fly.
51  */
52  bool get_has_lower() const;
53  bool get_has_lower_float() const;
54  bool get_has_lower_particle() const;
55  Float get_lower() const;
56  static FloatKey get_lower_key();
57  static ParticleIndexKey get_lower_particle_key();
58  void set_lower(Float d);
59  void set_lower(Particle *d);
60  void remove_lower();
61 
62  bool get_has_upper() const;
63  bool get_has_upper_float() const;
64  bool get_has_upper_particle() const;
65  Float get_upper() const;
66  static FloatKey get_upper_key();
67  static ParticleIndexKey get_upper_particle_key();
68  void set_upper(Float d);
69  void set_upper(Particle *d);
70  void remove_upper();
71 
72  Float get_nuisance_derivative() const {
73  return get_particle()->get_derivative(get_nuisance_key());
74  }
75 
76  void add_to_nuisance_derivative(Float d, DerivativeAccumulator &accum) {
77  get_particle()->add_to_derivative(get_nuisance_key(), d, accum);
78  }
79 
80  bool get_nuisance_is_optimized() const {
81  return get_particle()->get_is_optimized(get_nuisance_key());
82  }
83 
84  void set_nuisance_is_optimized(bool val) {
85  get_particle()->set_is_optimized(get_nuisance_key(), val);
86  }
87 
88  friend class NuisanceScoreState;
89 
90  private:
91  // scorestate-related bookkeeping
92  static ObjectKey get_ss_key();
93  void enforce_bounds();
94  void remove_bounds();
95 };
96 
97 IMP_DECORATORS(Nuisance, Nuisances, ParticlesTemp);
98 
99 //! Constrain a Nuisance to lie within its bounds
100 class IMPISDEXPORT NuisanceScoreState : public ScoreState {
101  ParticleIndex pi_;
102 
103  friend class cereal::access;
104  template<class Archive> void serialize(Archive &ar) {
105  ar(cereal::base_class<ScoreState>(this), pi_);
106  }
108 
109  public:
111  : ScoreState(p->get_model(), "NuisanceScoreState%1%"),
112  pi_(p->get_index()) {}
113 
114  NuisanceScoreState() {}
115 
116  ParticleIndex get_index() const { return pi_; }
117 
118  friend class Nuisance;
119  virtual void do_before_evaluate() override;
120  virtual void do_after_evaluate(DerivativeAccumulator *da) override;
121  virtual ModelObjectsTemp do_get_inputs() const override;
122  virtual ModelObjectsTemp do_get_outputs() const override;
124 };
125 
126 IMPISD_END_NAMESPACE
127 
128 #endif /* IMPISD_NUISANCE_H */
The base class for decorators.
ParticleIndex get_particle_index() const
Returns the particle index decorated by this decorator.
Definition: Decorator.h:211
virtual void do_before_evaluate()=0
Update the state given the current state of the model.
#define IMP_DECORATOR_SETUP_1(Name, FirstArgumentType, first_argument_name)
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Model * get_model() const
Returns the Model containing the particle.
Definition: Decorator.h:214
Constrain a Nuisance to lie within its bounds.
Definition: Nuisance.h:100
virtual void do_after_evaluate(DerivativeAccumulator *accpt)=0
Do any necessary updates after the model score is calculated.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Ints get_index(const ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
ScoreStates maintain invariants in the Model.
Definition: ScoreState.h:56
Add nuisance parameter to particle.
Definition: Nuisance.h:27
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
Helper macros for implementing Decorators.
#define IMP_DECORATOR_SETUP_0(Name)
Particle * get_particle() const
Returns the particle decorated by this decorator.
Definition: Decorator.h:194
Key< 4 > ObjectKey
The type used to identify an Object attribute.
Definition: base_types.h:48
Interface to specialized Particle types (e.g. atoms)
Definition: Decorator.h:119
virtual ModelObjectsTemp do_get_outputs() const =0
#define IMP_DECORATOR_METHODS(Name, Parent)
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
Class to handle individual particles of a Model object.
Definition: Particle.h:45
#define IMP_DECORATORS(Name, PluralName, Parent)
Define the types for storing sets of decorators.
ParticleIndex get_index() const
returns the particle index of this particle in its model
bool get_has_attribute(TypeKey attribute_key, ParticleIndex particle) const
return true if particle has attribute with the specified key
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
get the value of the particle attribute with the specified key
virtual ModelObjectsTemp do_get_inputs() const =0
Class for adding derivatives from restraints to the model.