IMP logo
IMP Reference Guide  develop.cb6747d2d1,2024/03/28
The Integrative Modeling Platform
PredicateSingletonsRestraint.h
Go to the documentation of this file.
1 // Autogenerated by ../../../../tmp/nightly-build-61059/imp-20240328.develop.cb6747d2d1/tools/build/make_containers.py
2 // from ../../../../tmp/nightly-build-61059/imp-20240328.develop.cb6747d2d1/tools/build/container_templates/container/PredicateClassnamesRestraint.h
3 // Do not edit - any changes will be lost!
4 
5 /**
6  * \file IMP/container/PredicateSingletonsRestraint.h
7  * \brief Apply a SingletonScore to each Singleton in a list.
8  *
9  * Copyright 2007-2023 IMP Inventors. All rights reserved.
10  *
11  */
12 
13 #ifndef IMPCONTAINER_PREDICATE_SINGLETONS_RESTRAINT_H
14 #define IMPCONTAINER_PREDICATE_SINGLETONS_RESTRAINT_H
15 
16 #include <IMP/container/container_config.h>
17 #include <IMP/container/internal/robin_map_cereal.h>
18 #include <IMP/SingletonPredicate.h>
19 #include "generic.h"
20 #include <cereal/access.hpp>
21 #include <cereal/types/base_class.hpp>
22 #include <cereal/types/map.hpp>
23 
24 
25 #if IMP_CONTAINER_HAS_ROBIN_MAP==1
26 #define IMP_CONTAINER_PREDICATE_USE_ROBIN_MAP
27 #include <tsl/robin_map.h>
28 #elif IMP_CONTAINER_HAS_GOOGLE_DENSE_HASH_MAP==1
29 #define IMP_CONTAINER_PREDICATE_USE_GOOGLE_DENSE_HASH_MAP
30 #include <google/dense_hash_map>
31 #else
32 #define IMP_CONTAINER_PREDICATE_USE_KERNEL_MAP
33 #include <IMP/set_map_macros.h>
34 //#include <boost/unordered_map.hpp>
35 #endif
36 
37 #include <iostream>
38 
39 
40 IMPCONTAINER_BEGIN_NAMESPACE
41 
42 //! Applies a SingletonScore to each Singleton in a list based on a predicate
43 /** This restraint uses a passed predicate to choose which score to apply
44  to each tuple in the input container. The selections are cached, making it
45  substantially faster than using a core::TypedPairScore.
46 
47  \note The ordering of particles within a tuple may vary depending on the
48  input container used. You may need to call set_score() with several
49  different predicate values for different orderings.
50 */
51 class IMPCONTAINEREXPORT PredicateSingletonsRestraint : public Restraint {
54 #if IMP_CONTAINER_HAS_ROBIN_MAP==1
55  typedef tsl::robin_map<int, ParticleIndexes> t_lists_map;
56  typedef tsl::robin_map<int, PointerMember<SingletonScore> > t_score_map;
57 #elif IMP_CONTAINER_HAS_GOOGLE_DENSE_HASH_MAP==1
58  typedef google::dense_hash_map<int, ParticleIndexes> t_lists_map;
59  typedef google::dense_hash_map<int, PointerMember<SingletonScore> > t_score_map;
60 #else
61  typedef IMP_KERNEL_LARGE_UNORDERED_MAP<int, ParticleIndexes> t_lists_map;
62  typedef IMP_KERNEL_LARGE_UNORDERED_MAP<int, PointerMember<SingletonScore> > t_score_map;
63 #endif
64  mutable t_lists_map lists_;
65  t_score_map scores_;
66 
67  bool is_get_inputs_ignores_individual_scores_;
68  mutable std::size_t input_version_;
69  // bool is_unknown_score_set_;
70  bool error_on_unknown_;
71  PointerMember<SingletonScore> unknown_score_;
72 
73  friend class cereal::access;
74  template<class Archive> void serialize(Archive &ar) {
75  ar(cereal::base_class<Restraint>(this),
76  predicate_, input_, scores_,
77  is_get_inputs_ignores_individual_scores_,
78  error_on_unknown_, unknown_score_);
79  if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
80  // Force lists_ to be rebuilt (from scores_)
81  lists_.clear();
82  input_version_ = -1;
83  }
84  }
86 
87  // void update_lists_with_item(ParticleIndex const &it) const;
88  void update_lists_if_necessary() const;
89 
90  public:
93  std::string name =
94  "PredicateSingletonsRestraint %1%");
95 
97 
98  /** Apply the passed score to all pairs whose predicate values match
99  the passed value.*/
100  void set_score(int predicate_value, SingletonScore *score);
101 
102  /** Apply this score to any pair whose predicate value does not match
103  one passed to set_score().*/
104  void set_unknown_score(SingletonScore *score);
105 
106  /** By default, it is an error if the predicate returns a value that is
107  not known. If this is false, then they are silently skipped even
108  if set_unknown_score() was not called.
109  */
110  void set_is_complete(bool tf) { error_on_unknown_ = tf; }
111 
112  /** return the indexes of all particles for a given predicate value.*/
113  ParticleIndexes get_indexes(int predicate_value) const {
114  return lists_.find(predicate_value)->second;
115  }
116 
117  public:
118  void do_add_score_and_derivatives(IMP::ScoreAccumulator sa) const
119  override;
120  IMP::ModelObjectsTemp do_get_inputs() const override;
121 
122 #ifndef SWIG
123  //! use at own risk - for efficiency in case e.g. all scores use same inputs
124  //! If true, do_get_inputs() includes all inputs in input container
125  //! but not individual inputs from individual scores
127  is_get_inputs_ignores_individual_scores_= is_ignore;
128  }
129 #endif
131 
132  private:
133  /**
134  Returns the score for specified predicate based on set_score()
135 
136  Unknown scores:
137  If predicate was not added with set_score(), return the default unknown score
138  set by set_unknown_score() if applicable. If not, then if set_is_complete(false)
139  was called, return nullptr. Otherwise, undefined behavior (will fail when checks
140  are on)
141  */
142  inline SingletonScore*
143  get_score_for_predicate(int predicate) const;
144 
145 
147 };
148 
149 
150 #ifndef SWIG
152 PredicateSingletonsRestraint::get_score_for_predicate
153 (int predicate) const
154 {
155  SingletonScore* score;
156  t_score_map::const_iterator it= scores_.find(predicate);
157  if(it!=scores_.end()){
158  score= it->second;
159  } else {
160  IMP_USAGE_CHECK(!error_on_unknown_, // is_unknown_score_set_,
161  "unknown interaction type cannot be scored when error_on_unknown is true");
162  score = unknown_score_;
163  }
164  return score;
165 }
166 #endif
167 
168 
169 IMPCONTAINER_END_NAMESPACE
170 
171 #endif /* IMPCONTAINER_PREDICATE_SINGLETONS_RESTRAINT_H */
Various important functionality for implementing decorators.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Macros to choose the best set or map for different purposes.
Define SingletonPredicate.
virtual Restraints do_create_current_decomposition() const
Definition: Restraint.h:315
Abstract class for scoring object(s) of type ParticleIndex.
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
A smart pointer to a ref-counted Object that is a class member.
Definition: Pointer.h:143
Class for adding up scores during ScoringFunction evaluation.
Applies a SingletonScore to each Singleton in a list based on a predicate.
Abstract predicate function.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
ParticleIndexes get_indexes(int predicate_value) const
virtual ModelObjectsTemp do_get_inputs() const =0
A restraint is a term in an IMP ScoringFunction.
Definition: Restraint.h:56