IMP logo
IMP Reference Guide  2.19.0
The Integrative Modeling Platform
npctransport/Statistics.h
Go to the documentation of this file.
1 /**
2  * \file npctransport/Statistics.h
3  * \brief statistics and order parameters about the simulations
4  * that is associated with a SimulationData object
5  *
6  * \author Barak Raveh, Daniel Russell
7  * Copyright 2007-2022 IMP Inventors. All rights reserved.
8  */
9 
10 #ifndef IMPNPCTRANSPORT_STATISTICS_H
11 #define IMPNPCTRANSPORT_STATISTICS_H
12 
13 
14 #include "npctransport_config.h"
15 #include <IMP/Model.h>
16 #include <IMP/PairContainer.h>
18 #include <IMP/atom/Hierarchy.h>
25 #include <IMP/core/Typed.h>
28 #include <IMP/Pointer.h>
29 #include <IMP/set_map_macros.h>
30 #include <RMF/HDF5/File.h>
31 #include "io.h"
37 #include "Parameter.h"
38 #include "Scoring.h"
39 #include "typedefs.h"
40 
41 #include <IMP/internal/SimpleTimer.h>
42 #include "boost/tuple/tuple.hpp"
43 #include <boost/utility/value_init.hpp>
44 #include <boost/unordered_map.hpp>
45 #include <boost/unordered_set.hpp>
46 #include <stdint.h>
47 #include <string>
48 
49 #ifdef SWIG
50 namespace IMP {
51 namespace internal {
52 struct SimpleTimer {};
53 }
54 }
55 #endif
56 
57 
58 IMPNPCTRANSPORT_BEGIN_NAMESPACE
59 
60 class SimulationData; // fwd incomplete declaration
61 
62 //! Statistics and order parameters about the simulations
63 class IMPNPCTRANSPORTEXPORT Statistics : public Object {
64  private:
66 
67  // Whether statistics gathering has been activated (= optimizer states added to optimizer)
68  bool is_activated_;
69 
70  // interval of simulation frames for gathering stats
71  Parameter<int> statistics_interval_frames_;
72 
73  // the file to which simulation statistics are dumped:
74  std::string output_file_name_;
75 
77 
78  // statistics about all fgs, per particle, per chain, per particle type
79  typedef std::vector< BodyStatisticsOptimizerStates >
80  FGsBodyStatisticsOSs;
81  typedef IMP_KERNEL_LARGE_UNORDERED_MAP<core::ParticleType, FGsBodyStatisticsOSs>
82  FGsBodyStatisticsOSsMap;
83  FGsBodyStatisticsOSsMap fgs_bodies_stats_map_;
84 
85  // statistics about all floaters (kaps etc.), per particle type
86  typedef IMP_KERNEL_LARGE_UNORDERED_MAP<core::ParticleType, BodyStatisticsOptimizerStates>
87  BodyStatisticsOSsMap;
88  BodyStatisticsOSsMap floaters_stats_map_;
89 
90  // transport statistics about all floaters (kaps etc.) per particle type
93  ParticleTransportStatisticsOSsMap;
94  ParticleTransportStatisticsOSsMap floaters_transport_stats_map_;
95 
96 #ifndef SWIG
97  // distributions
99  std::vector< std::vector<int> > >
100  ParticleTypeZRDistributionMap;
101  ParticleTypeZRDistributionMap particle_type_zr_distribution_map_;
102  typedef IMP_KERNEL_LARGE_UNORDERED_MAP< uint_fast8_t,
103  IMP_KERNEL_LARGE_UNORDERED_MAP< uint_fast8_t,
104  IMP_KERNEL_LARGE_UNORDERED_MAP< uint_fast8_t,
105  boost::value_initialized<unsigned int> > > >
106  t_sparse_3d_matrix;
108  t_sparse_3d_matrix >
109  // std::vector< std::vector< std::vector<int> > > >
110  ParticleTypeXYZDistributionMap;
111  ParticleTypeXYZDistributionMap particle_type_xyz_distribution_map_;
112  struct t_size_3d_matrix{
113  uint_fast8_t d0;
114  uint_fast8_t d1;
115  uint_fast8_t d2;
116  };
117  t_size_3d_matrix xyz_distribution_sizes_;
118 #endif
119 
120  // statistics about entire FG chains, for each FG type
121  typedef IMP_KERNEL_LARGE_UNORDERED_MAP<core::ParticleType, ChainStatisticsOptimizerStates>
122  ChainStatisticsOSsMap;
123  ChainStatisticsOSsMap chains_stats_map_;
124 
125  // statistics of pairs of interactions for each interaction type
128  BipartitePairsStatisticsOSMap;
129  BipartitePairsStatisticsOSMap interaction_stats_map_;
130 
131  // true if statistics were recently reset, so that update()
132  // should restart averaging from 0 frames
133  mutable bool is_stats_reset_;
134 
135 
136  public:
137 
138  //! Remove all statistics related to particle type pt
139  /**
140  Removes statistics of particle type pt from all internal maps
141  and from the optimizer states of get_sd()->get_bd()
142 
143  @param pt the particle type to be removed
144  */
145  void remove_particle_type(core::ParticleType pt);
146 
147  /**
148  @param sd the sd that owns and uses this statistics object
149  @param statistics_interval_frames the interval of simulation frames for gathering
150  statistics
151  @param output_file_name name of output file to which to dump statistics (or update
152  if it already exists) when calling update()
153  */
155  unsigned int statistics_interval_frames,
156  std::string output_file_name);
157 
158  //! add statistics about an FG chain
159  /** add statistics about an FG chains
160 
161  @param fg_chain the chain
162  */
163  void add_fg_chain_stats(FGChain* fg_chain);
164 
165  //! add statistics about a floater particle
166  /** add statistics about a floater particle
167 
168  @param p the particle
169  */
170  void add_floater_stats(IMP::Particle* p);
171 
172  //! add statistics about interactions between particles of type 0 and 1
173  /** add statistics about interactions between particles of type 0 and 1
174  (order does not matter)
175 
176  @param type0 type of first interacting particles
177  @param type1 type of other interacting particles
178  */
179  void add_interaction_stats
180  ( core::ParticleType type0, core::ParticleType type1);
181 
182  //! add all statistics-related optimizer states to o
183  /**
184  @param o optimizer to which optimizer states are added,
185  use get_sd()->get_bd() if nullptr
186  @return the list of optimizer states that were added
187 
188  @note If called for more than one optimizer, only the last
189  optimizer will be guaranteed to work well.
190  */
191  OptimizerStates add_optimizer_states(Optimizer* o = nullptr);
192 
193  /**
194  updates the map of z-r distributions of particle coordinates
195  with p's binned position counts (if z-symmetry flag is on,
196  z is absolute vertical location; r is distance from pore axis)
197 
198  Comment: assume a pore geometry, no checks made that it is so
199  */
200  void update_particle_type_zr_distribution_map(Particle* p);
201 
202  /**
203  updates the map of x-y-z distributions of particle coordinates
204  with p's binned position counts (if z-symmetry flag is on,
205  z is absolute vertical location)
206 
207  Comment: assume a pore geometry, no checks made that it is so
208  */
209  void update_particle_type_xyz_distribution_map(Particle* p);
210 
211 
212  /**
213  opens / creates statistics protobuf file, and update it
214  with appropriate statistics, using statistics file
215  originally specified in the constructor, and based on statistics
216  gathered from the optimizer that was specified by
217  add_optimizer_states().
218 
219  @throw UsageException If add_optimizer_states() was not called yet
220  (= get_is_activated() is false)
221 
222  @param timer the timer that was used to measure the time
223  that has elapsed for statistics
224  @param nf_new the number of frames by which the statistics file
225  should be advanced. This is used to weight the
226  contribution of average statistics over time.
227 
228  @note this method is not const cause it may invoke e.g., energy evaluation
229  though it does not substantially change anything in the state of the object
230  */
231  void update(const IMP::internal::SimpleTimer &timer,
232  unsigned int nf_new = 1);
233 
234  /** resets all the counters of any statistics counters,
235  and the simulation time to zero */
236  void reset_statistics_optimizer_states();
237 
238  //! Loads the stats file and set the interrupted flag to true
239  void set_interrupted(bool tf);
240 
241  /************************************************************/
242  /************* various simple getters and setters *******************/
243  /************************************************************/
244 
245  /** returns the model associated with the owned SimulationData */
246  Model* get_model();
247 
248 #ifndef SWIG
249  /** returns the model associated with the owned SimulationData */
250  Model* get_model() const;
251 #endif
252 
253  /** return the SimulationData object that owns this ScoringFunction */
255  return owner_sd_;
256  }
257 
258  //! if true, statistics have been activated, so add_optimizer_states()
259  //! was called such that statistics are being tracked
261  return is_activated_;
262  }
263 
264 #ifndef SWIG
265  /** return the SimulationData object that owns this ScoringFunction */
266  SimulationData const* get_sd() const{
267  return owner_sd_;
268  }
269 #endif
270 
271  std::string get_output_file_name() const{
272  return output_file_name_;
273  }
274 
275  private:
276 
277  //! update the xyz distribution of type p_type to a dataset in
278  //! hdf5_group, with name p_type.get_string()
279  bool update_xyz_distribution_to_hdf5
280  (RMF::HDF5::Group hdf5_group,
281  core::ParticleType p_type);
282 
283 
284  //! updates pStats with all statistics related to fgs, averaged over
285  //! nf_new additional frames
286  void update_fg_stats( ::npctransport_proto::Statistics* pStats,
287  unsigned int nf_new,
288  unsigned int zr_hist[4][3],
289  RMF::HDF5::File hdf5_file);
290 
291 
292 
293 
294  // TODO: move to util.h, possibly internal
295  // @param floaters a list of floater particles
296  // @param fg_roots a list of hierarchy particles that are the root of an
297  // fg chain (= all their children are fg typed particles)
298  // @return a 4-tuple (1,2,3,4) with:
299  // 1 - total # of individual site-site interactions between the specified
300  // floaters and chains
301  // 2 - total # of floaters that site-interact with any specified chain
302  // 3 - total # of fg bead-floater pairs that site-interact
303  // 4 - sum of # of chain-floater that site-intercat
304  boost::tuple<double, double, double, double> get_interactions_and_interacting
305  ( const ParticlesTemp &floaters, const atom::Hierarchies &chain_roots) const;
306 
307  //! @return the top of a z-axis bin for various stats
308  double get_z_distribution_top() const;
309 
310  //! @return the radius of a outermost radial (x,y) bin for various stats
311  double get_r_distribution_max() const;
312 
313  /**
314  for particles ps, returns the distribution along the z axis, in regions
315  z0 = [top...) ; z1 = [0..top) ; z2 = [-top..top) ; z3 = (...top)
316  with top being the return value of get_z_distribution_top()
317 
318  @param ps the particles
319 
320  @return a tuple <z0,z1,z2,z3> with counts of particles from ps
321  in z0, z1, z2 and z3 regions
322  */
323  boost::tuple<int, int, int, int>
324  get_z_distribution(const ParticlesTemp& ps) const;
325 
326  /** add the z-axis / (x,y)-radial distribution of particles ps
327  to zr_hist, a grid with z-axis bins on the first dimension and
328  radial bins on the second dimension.
329 
330  @param zr_hist a grid on z / (x,y)-radial axis
331  @param ps the particles
332 
333  */
334  void fill_in_zr_hist(unsigned int zr_hist[4][3],
335  ParticlesTemp ps) const;
336 
337 
338  public:
340 
341 
342 };
343 
344 inline IMPNPCTRANSPORTEXPORT IMP::internal::SimpleTimer create_boost_timer() {
345  return IMP::internal::SimpleTimer();
346 }
347 
348 IMPNPCTRANSPORT_END_NAMESPACE
349 
350 #endif /* IMPNPCTRANSPORT_STATISTICS_H */
Apply a PairScore to each Pair in a list.
std::pair< IMP::core::ParticleType, IMP::core::ParticleType > InteractionType
an interaction that involves particles of two types
Definition: typedefs.h:21
Simple Brownian dynamics optimizer.
scoring associated with a SimulationData object
Store a list of ParticleIndexes.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Store a set of PairContainers.
Storage of a model, its restraints, constraints and particles.
Score particles based on a bounding box.
Dump the state of all associated objects into the RMF file.
Key< 34897493 > ParticleType
An IMP::Key object for identifying types of particles by strings.
Definition: Typed.h:28
Statistics and order parameters about the simulations.
A weak pointer to an Object or RefCountedObject.
Definition: WeakPointer.h:33
description
A more IMP-like version of the std::vector.
Definition: Vector.h:42
Implement geometry for the basic shapes from IMP.algebra.
A particle with a user-defined type.
Macros to choose the best set or map for different purposes.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Decorator for helping deal with a hierarchy of molecules.
A container for Pairs.
Common base class for heavy weight IMP objects.
Definition: Object.h:111
Return all pairs from a SingletonContainer.
Base class for all optimizers.
Definition: Optimizer.h:48
A smart pointer to a ref-counted Object that is a class member.
Definition: Pointer.h:143
SimulationData const * get_sd() const
Store all parameters for a simulation.
A nullptr-initialized pointer to an IMP Object.
Define some predicates.
description
Class to handle individual particles of a Model object.
Definition: Particle.h:43
description
#define IMP_KERNEL_LARGE_UNORDERED_MAP
Macro to support platform independent declaration of a large unordered map.