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