IMP logo
IMP Reference Guide  2.9.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-2018 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  @param sd the sd that owns and uses this statistics object
137  @param statistics_interval_frames the interval of simulation frames for gathering
138  statistics
139  @param output_file_name name of output file to which to dump statistics (or update
140  if it already exists) when calling update()
141  */
143  unsigned int statistics_interval_frames,
144  std::string output_file_name);
145 
146  //! add statistics about an FG chain
147  /** add statistics about an FG chains
148 
149  @param fg_chain the chain
150  */
151  void add_fg_chain_stats(FGChain* fg_chain);
152 
153  //! add statistics about a floater particle
154  /** add statistics about a floater particle
155 
156  @param p the particle
157  */
158  void add_floater_stats(IMP::Particle* p);
159 
160  //! add statistics about interactions between particles of type 0 and 1
161  /** add statistics about interactions between particles of type 0 and 1
162  (order does not matter)
163 
164  @param type0 type of first interacting particles
165  @param type1 type of other interacting particles
166  */
167  void add_interaction_stats
168  ( core::ParticleType type0, core::ParticleType type1);
169 
170  //! add all statistics-related optimizer states to o
171  /**
172  @param o optimizer to which optimizer states are added,
173  use get_sd()->get_bd() if nullptr
174  @return the list of optimizer states that were added
175 
176  @note If called for more than one optimizer, only the last
177  optimizer will be guaranteed to work well.
178  */
179  OptimizerStates add_optimizer_states(Optimizer* o = nullptr);
180 
181  /**
182  updates the map of z-r distributions of particle coordinates
183  with p's binned position counts (if z-symmetry flag is on,
184  z is absolute vertical location; r is distance from pore axis)
185 
186  Comment: assume a pore geometry, no checks made that it is so
187  */
188  void update_particle_type_zr_distribution_map(Particle* p);
189 
190  /**
191  updates the map of x-y-z distributions of particle coordinates
192  with p's binned position counts (if z-symmetry flag is on,
193  z is absolute vertical location)
194 
195  Comment: assume a pore geometry, no checks made that it is so
196  */
197  void update_particle_type_xyz_distribution_map(Particle* p);
198 
199 
200  /**
201  opens / creates statistics protobuf file, and update it
202  with appropriate statistics, using statistics file
203  originally specified in the constructor, and based on statistics
204  gathered from the optimizer that was specified by
205  add_optimizer_states().
206 
207  @throw UsageException If add_optimizer_states() was not called yet
208  (= get_is_activated() is false)
209 
210  @param timer the timer that was used to measure the time
211  that has elapsed for statistics
212  @param nf_new the number of frames by which the statistics file
213  should be advanced. This is used to weight the
214  contribution of average statistics over time.
215 
216  @note this method is not const cause it may invoke e.g., energy evaluation
217  though it does not substantially change anything in the state of the object
218  */
219  void update(const boost::timer &timer,
220  unsigned int nf_new = 1);
221 
222  /** resets all the counters of any statistics counters,
223  and the simulation time to zero */
224  void reset_statistics_optimizer_states();
225 
226  //! Loads the stats file and set the interrupted flag to true
227  void set_interrupted(bool tf);
228 
229  /************************************************************/
230  /************* various simple getters and setters *******************/
231  /************************************************************/
232 
233  /** returns the model associated with the owned SimulationData */
234  Model* get_model();
235 
236 #ifndef SWIG
237  /** returns the model associated with the owned SimulationData */
238  Model* get_model() const;
239 #endif
240 
241  /** return the SimulationData object that owns this ScoringFunction */
243  return owner_sd_;
244  }
245 
246  //! if true, statistics have been activated, so add_optimizer_states()
247  //! was called such that statistics are being tracked
249  return is_activated_;
250  }
251 
252 #ifndef SWIG
253  /** return the SimulationData object that owns this ScoringFunction */
254  SimulationData const* get_sd() const{
255  return owner_sd_;
256  }
257 #endif
258 
259  std::string get_output_file_name() const{
260  return output_file_name_;
261  }
262 
263  private:
264 
265  //! update the xyz distribution of type p_type to a dataset in
266  //! hdf5_group, with name p_type.get_string()
267  bool update_xyz_distribution_to_hdf5
268  (RMF::HDF5::Group hdf5_group,
269  core::ParticleType p_type);
270 
271 
272  //! updates pStats with all statistics related to fgs, averaged over
273  //! nf_new additional frames
274  void update_fg_stats( ::npctransport_proto::Statistics* pStats,
275  unsigned int nf_new,
276  unsigned int zr_hist[4][3],
277  RMF::HDF5::File hdf5_file);
278 
279 
280 
281 
282  // TODO: move to util.h, possibly internal
283  // @param floaters a list of floater particles
284  // @param fg_roots a list of hierarchy particles that are the root of an
285  // fg chain (= all their children are fg typed particles)
286  // @return a 4-tuple (1,2,3,4) with:
287  // 1 - total # of individual site-site interactions between the specified
288  // floaters and chains
289  // 2 - total # of floaters that site-interact with any specified chain
290  // 3 - total # of fg bead-floater pairs that site-interact
291  // 4 - sum of # of chain-floater that site-intercat
292  boost::tuple<double, double, double, double> get_interactions_and_interacting
293  ( const ParticlesTemp &floaters, const atom::Hierarchies &chain_roots) const;
294 
295  //! @return the top of a z-axis bin for various stats
296  double get_z_distribution_top() const;
297 
298  //! @return the radius of a outermost radial (x,y) bin for various stats
299  double get_r_distribution_max() const;
300 
301  /**
302  for particles ps, returns the distribution along the z axis, in regions
303  z0 = [top...) ; z1 = [0..top) ; z2 = [-top..top) ; z3 = (...top)
304  with top being the return value of get_z_distribution_top()
305 
306  @param ps the particles
307 
308  @return a tuple <z0,z1,z2,z3> with counts of particles from ps
309  in z0, z1, z2 and z3 regions
310  */
311  boost::tuple<int, int, int, int>
312  get_z_distribution(const ParticlesTemp& ps) const;
313 
314  /** add the z-axis / (x,y)-radial distribution of particles ps
315  to zr_hist, a grid with z-axis bins on the first dimension and
316  radial bins on the second dimension.
317 
318  @param zr_hist a grid on z / (x,y)-radial axis
319  @param ps the particles
320 
321  */
322  void fill_in_zr_hist(unsigned int zr_hist[4][3],
323  ParticlesTemp ps) const;
324 
325 
326  public:
328 
329 
330 };
331 
332 inline IMPNPCTRANSPORTEXPORT boost::timer create_boost_timer() {
333  return boost::timer();
334 }
335 
336 IMPNPCTRANSPORT_END_NAMESPACE
337 
338 #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 molecular 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:39
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:72
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.