IMP logo
IMP Reference Guide  2.20.0
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-2023 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  Float get_lower() const;
54  static FloatKey get_lower_key();
55  static ParticleIndexKey get_lower_particle_key();
56  void set_lower(Float d);
57  void set_lower(Particle *d);
58  void remove_lower();
59 
60  bool get_has_upper() const;
61  Float get_upper() const;
62  static FloatKey get_upper_key();
63  static ParticleIndexKey get_upper_particle_key();
64  void set_upper(Float d);
65  void set_upper(Particle *d);
66  void remove_upper();
67 
68  Float get_nuisance_derivative() const {
69  return get_particle()->get_derivative(get_nuisance_key());
70  }
71 
72  void add_to_nuisance_derivative(Float d, DerivativeAccumulator &accum) {
73  get_particle()->add_to_derivative(get_nuisance_key(), d, accum);
74  }
75 
76  bool get_nuisance_is_optimized() const {
77  return get_particle()->get_is_optimized(get_nuisance_key());
78  }
79 
80  void set_nuisance_is_optimized(bool val) {
81  get_particle()->set_is_optimized(get_nuisance_key(), val);
82  }
83 
84  friend class NuisanceScoreState;
85 
86  private:
87  // scorestate-related bookkeeping
88  static ObjectKey get_ss_key();
89  void enforce_bounds();
90  void remove_bounds();
91 };
92 
93 IMP_DECORATORS(Nuisance, Nuisances, ParticlesTemp);
94 
95 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
96 class IMPISDEXPORT NuisanceScoreState : public ScoreState {
97  ParticleIndex pi_;
98 
99  friend class cereal::access;
100  template<class Archive> void serialize(Archive &ar) {
101  ar(cereal::base_class<ScoreState>(this), pi_);
102  }
103  IMP_OBJECT_SERIALIZE_DECL(NuisanceScoreState);
104 
105  private:
106  NuisanceScoreState(Particle *p)
107  : ScoreState(p->get_model(), "NuisanceScoreState%1%"),
108  pi_(p->get_index()) {}
109 
110  NuisanceScoreState() {}
111 
112  public:
113  friend class Nuisance;
114  virtual void do_before_evaluate() override;
115  virtual void do_after_evaluate(DerivativeAccumulator *da) override;
116  virtual ModelObjectsTemp do_get_inputs() const override;
117  virtual ModelObjectsTemp do_get_outputs() const override;
118  IMP_OBJECT_METHODS(NuisanceScoreState);
119 };
120 #endif
121 
122 IMPISD_END_NAMESPACE
123 
124 #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
#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
Index< ParticleIndexTag > ParticleIndex
Definition: base_types.h:178
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition: base_types.h:106
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)
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
#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:43
#define IMP_DECORATORS(Name, PluralName, Parent)
Define the types for storing sets of decorators.
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