Collects vectors, but only stores a subsample of them. More...
#include <mbl_stochastic_data_collector.h>
Public Member Functions | |
mbl_stochastic_data_collector () | |
Dflt ctor. | |
mbl_stochastic_data_collector (unsigned n) | |
Set number of samples to be stored. | |
virtual | ~mbl_stochastic_data_collector () |
Destructor. | |
virtual void | clear () |
Clear any stored data. | |
virtual void | set_n_samples (int n) |
Set number of samples to be stored. | |
virtual void | record (const T &v) |
Record given value. | |
void | force_record (const T &v) |
Force recording of this given value. | |
bool | store_next () |
Will decide whether to store the next value. | |
virtual mbl_data_wrapper< T > & | data_wrapper () |
Return object describing stored data. | |
void | reseed (unsigned long seed) |
Reseed the internal random number generator. | |
unsigned long | n_presented () const |
The number of vectors that have been presented so far. | |
short | version_no () const |
Version number for I/O. | |
virtual vcl_string | is_a () const |
Name of the class. | |
virtual bool | is_class (vcl_string const &s) const |
Does the name of the class match the argument?. | |
virtual mbl_data_collector_base * | clone () const |
Create a copy on the heap and return base class pointer. | |
virtual void | print_summary (vcl_ostream &os) const |
Print class to os. | |
virtual void | b_write (vsl_b_ostream &bfs) const |
Save class to binary file stream. | |
virtual void | b_read (vsl_b_istream &bfs) |
Load class from binary file stream. | |
Private Attributes | |
vcl_vector< T > | samples_ |
Recorded samples are stored here. | |
mbl_data_array_wrapper< T > | v_data_ |
Provides iterator access to the data via data_wrapper(). | |
unsigned long | nPresented_ |
The number of samples presented to record() so far. | |
vnl_random | rand |
Random number generator used to decide whether to store a particular vector. |
Collects vectors, but only stores a subsample of them.
This is useful if you are unsure how many vectors examples a routine might store, but you only want a randomly selected fixed number of them. The stored vectors are (in the limit) unbiased w.r.t. the order in which they were presented,
If calculating the values to be stored is expensive, this class can be used as follows.
mbl_stochastic_data_collector<double> c(100); while (..) { if (c.store_next()) c.force_record(f()); }
Definition at line 32 of file mbl_stochastic_data_collector.h.
mbl_stochastic_data_collector< T >::mbl_stochastic_data_collector | ( | ) |
Dflt ctor.
Definition at line 16 of file mbl_stochastic_data_collector.txx.
mbl_stochastic_data_collector< T >::mbl_stochastic_data_collector | ( | unsigned | n | ) | [explicit] |
Set number of samples to be stored.
This is the number of vectors that can be actually retrieved.
Definition at line 25 of file mbl_stochastic_data_collector.txx.
mbl_stochastic_data_collector< T >::~mbl_stochastic_data_collector | ( | ) | [virtual] |
Destructor.
Definition at line 34 of file mbl_stochastic_data_collector.txx.
void mbl_stochastic_data_collector< T >::b_read | ( | vsl_b_istream & | bfs | ) | [virtual] |
Load class from binary file stream.
Implements mbl_data_collector_base.
Definition at line 191 of file mbl_stochastic_data_collector.txx.
void mbl_stochastic_data_collector< T >::b_write | ( | vsl_b_ostream & | bfs | ) | const [virtual] |
Save class to binary file stream.
Implements mbl_data_collector_base.
Definition at line 180 of file mbl_stochastic_data_collector.txx.
void mbl_stochastic_data_collector< T >::clear | ( | ) | [virtual] |
Clear any stored data.
Implements mbl_data_collector< T >.
Definition at line 41 of file mbl_stochastic_data_collector.txx.
mbl_data_collector_base * mbl_stochastic_data_collector< T >::clone | ( | ) | const [virtual] |
Create a copy on the heap and return base class pointer.
Implements mbl_data_collector_base.
Definition at line 163 of file mbl_stochastic_data_collector.txx.
mbl_data_wrapper< T > & mbl_stochastic_data_collector< T >::data_wrapper | ( | ) | [virtual] |
Return object describing stored data.
Implements mbl_data_collector< T >.
Definition at line 129 of file mbl_stochastic_data_collector.txx.
void mbl_stochastic_data_collector< T >::force_record | ( | const T & | v | ) |
Force recording of this given value.
This does not increment n_presented() Used with next(), to avoid calculating values that will not be stored.
Definition at line 89 of file mbl_stochastic_data_collector.txx.
virtual vcl_string mbl_stochastic_data_collector< T >::is_a | ( | ) | const [virtual] |
Name of the class.
Reimplemented from mbl_data_collector_base.
bool mbl_stochastic_data_collector< T >::is_class | ( | vcl_string const & | s | ) | const [virtual] |
Does the name of the class match the argument?.
Reimplemented from mbl_data_collector_base.
Definition at line 147 of file mbl_stochastic_data_collector.txx.
unsigned long mbl_stochastic_data_collector< T >::n_presented | ( | ) | const [inline] |
The number of vectors that have been presented so far.
Definition at line 87 of file mbl_stochastic_data_collector.h.
void mbl_stochastic_data_collector< T >::print_summary | ( | vcl_ostream & | os | ) | const [virtual] |
Print class to os.
Implements mbl_data_collector_base.
Definition at line 171 of file mbl_stochastic_data_collector.txx.
void mbl_stochastic_data_collector< T >::record | ( | const T & | v | ) | [virtual] |
Record given value.
Stochastically record given vector.
The vector will be recorded, and saved with a probability equal to that of every other vector presented to this function. returns true if it actually stored the value.
Implements mbl_data_collector< T >.
Definition at line 65 of file mbl_stochastic_data_collector.txx.
void mbl_stochastic_data_collector< T >::reseed | ( | unsigned long | seed | ) |
Reseed the internal random number generator.
Definition at line 139 of file mbl_stochastic_data_collector.txx.
void mbl_stochastic_data_collector< T >::set_n_samples | ( | int | n | ) | [virtual] |
Set number of samples to be stored.
If not set, the value defaults to 1000. Calling this function implicitly calls clean().
Implements mbl_data_collector< T >.
Definition at line 52 of file mbl_stochastic_data_collector.txx.
bool mbl_stochastic_data_collector< T >::store_next | ( | ) |
Will decide whether to store the next value.
This will increment n_presented()
This will increment n_presented()
Definition at line 103 of file mbl_stochastic_data_collector.txx.
short mbl_stochastic_data_collector< T >::version_no | ( | ) | const |
Version number for I/O.
Definition at line 155 of file mbl_stochastic_data_collector.txx.
unsigned long mbl_stochastic_data_collector< T >::nPresented_ [private] |
The number of samples presented to record() so far.
Definition at line 42 of file mbl_stochastic_data_collector.h.
vnl_random mbl_stochastic_data_collector< T >::rand [private] |
Random number generator used to decide whether to store a particular vector.
Definition at line 45 of file mbl_stochastic_data_collector.h.
vcl_vector<T > mbl_stochastic_data_collector< T >::samples_ [private] |
Recorded samples are stored here.
Definition at line 36 of file mbl_stochastic_data_collector.h.
mbl_data_array_wrapper<T > mbl_stochastic_data_collector< T >::v_data_ [private] |
Provides iterator access to the data via data_wrapper().
Definition at line 39 of file mbl_stochastic_data_collector.h.