Clipper
clipper_stats.h
1
4//C Copyright (C) 2000-2006 Kevin Cowtan and University of York
5//L
6//L This library is free software and is distributed under the terms
7//L and conditions of version 2.1 of the GNU Lesser General Public
8//L Licence (LGPL) with the following additional clause:
9//L
10//L `You may also combine or link a "work that uses the Library" to
11//L produce a work containing portions of the Library, and distribute
12//L that work under terms of your choice, provided that you give
13//L prominent notice with each copy of the work that the specified
14//L version of the Library is used in it, and that you include or
15//L provide public access to the complete corresponding
16//L machine-readable source code for the Library including whatever
17//L changes were used in the work. (i.e. If you make changes to the
18//L Library you must distribute those, but you do not need to
19//L distribute source or object code to those portions of the work
20//L not covered by this licence.)'
21//L
22//L Note that this clause grants an additional right and does not impose
23//L any additional restriction, and so does not affect compatibility
24//L with the GNU General Public Licence (GPL). If you wish to negotiate
25//L other terms, please contact the maintainer.
26//L
27//L You can redistribute it and/or modify the library under the terms of
28//L the GNU Lesser General Public License as published by the Free Software
29//L Foundation; either version 2.1 of the License, or (at your option) any
30//L later version.
31//L
32//L This library is distributed in the hope that it will be useful, but
33//L WITHOUT ANY WARRANTY; without even the implied warranty of
34//L MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35//L Lesser General Public License for more details.
36//L
37//L You should have received a copy of the CCP4 licence and/or GNU
38//L Lesser General Public License along with this library; if not, write
39//L to the CCP4 Secretary, Daresbury Laboratory, Warrington WA4 4AD, UK.
40//L The GNU Lesser General Public can also be obtained by writing to the
41//L Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
42//L MA 02111-1307 USA
43
44
45#ifndef CLIPPER_STATS
46#define CLIPPER_STATS
47
48
49#include "clipper_types.h"
50
51
52namespace clipper
53{
54
56 template<class T = ftype> class Range
57 {
58 public:
60 inline Range() { lmin = 999999999; lmax = -999999999; }
62 inline Range( const T& min, const T& max ) { lmin = min; lmax = max; }
63 inline const T& min() const { return lmin; }
64 inline const T& max() const { return lmax; }
65 inline T range() const { return lmax-lmin; }
67 inline void include( const T& datum )
68 { lmin = (datum<lmin)?datum:lmin; lmax = (datum>lmax)?datum:lmax; }
70 inline bool contains( const T& datum ) const
71 { return ( datum >= lmin && datum <= lmax ); }
73 inline T truncate( const T& datum ) const
74 { return Util::bound( lmin, datum, lmax ); }
75 private:
76 T lmin, lmax;
77 };
78
80 class Range_sampling : public Range<ftype>
81 {
82 public:
84 inline Range_sampling() : n_(0) {}
86 inline Range_sampling( const int& n ) : n_(n) {}
88 inline Range_sampling( const Range<ftype>& range, const int& n ) :
89 Range<ftype>( range ), n_(n) {}
91 inline ftype indexf( const ftype& x ) const
92 { return ftype(size())*(x-min())/range(); }
94 inline ftype x( const ftype& i ) const
95 { return range()*i/ftype(size())+min(); }
97 inline int index( const ftype& x ) const { return Util::intf( indexf(x) ); }
99 inline int index_bounded( const ftype& x ) const
100 { return Util::bound( 0, Util::intf( indexf(x) ), size()-1 ); }
102 inline ftype x( const int& i ) const { return x( ftype(i)+0.5 ); }
104 inline ftype x_min( const int& i ) const { return x( ftype(i) ); }
106 inline ftype x_max( const int& i ) const { return x( ftype(i)+1.0 ); }
108 inline int size() const { return n_; }
109 private:
110 int n_;
111 };
112
114
118 {
119 public:
123 Histogram( const Range<ftype>& range, const int& n ) :
124 Range_sampling( range, n ), data( n, 0.0 ) {}
126 void accumulate( const ftype& x )
127 { if ( contains(x) ) data[ index_bounded(x) ] += 1.0; }
129 void accumulate( const ftype& x, const ftype& w )
130 { if ( contains(x) ) data[ index_bounded(x) ] += w; }
132 ftype sum() const;
134 inline const ftype& y( const int& i ) const { return data[i]; }
136 ftype y( const ftype& x ) const;
138 const Histogram& operator += ( const Histogram& h );
139 // inherited functions listed for documentation purposes
140 //-- inline ftype x( const int& i ) const;
141 //-- inline ftype x_min( const int& i ) const;
142 //-- inline ftype x_max( const int& i ) const;
143 //-- inline int size() const;
144 private:
145 std::vector<ftype> data;
146 };
147
148
150
164 {
165 public:
169 Generic_ordinal( const Range<ftype>& range, const int& n )
170 { init( range, n ); }
172 void init( const Range<ftype>& range, const int num_ranges = 1000 );
174 void init( const std::vector<ftype>& values, const int num_ranges = 1000 );
176 ftype ordinal( const ftype& value ) const;
177
179 void accumulate( const ftype& value );
181 void accumulate( const ftype& value, const ftype& weight );
183 void prep_ordinal();
185 void invert();
186
188 void init( const int num_ranges = 1000 );
190 void add_pass_1( const ftype& value );
192 void add_pass_2( const ftype& value );
193 protected:
196 std::vector<ftype> hist;
197 };
198
199
200
201} // namespace clipper
202
203#endif
Generic ordinal gernerator.
Definition clipper_stats.h:164
void accumulate(const ftype &value)
accumulate values to build the distribution
Definition clipper_stats.cpp:102
void add_pass_1(const ftype &value)
DEPRECATED: add a value to the distribution (pass 1 of 2)
Definition clipper_stats.cpp:156
Range< ftype > range_
resolution range of data
Definition clipper_stats.h:195
ftype nranges
number of ranges
Definition clipper_stats.h:194
void prep_ordinal()
generate the ordinal histogram
Definition clipper_stats.cpp:118
void invert()
invert distribution to get value from ordinal
Definition clipper_stats.cpp:126
std::vector< ftype > hist
histogram of reflections vs resolution
Definition clipper_stats.h:196
Generic_ordinal(const Range< ftype > &range, const int &n)
constructor: from range and sampling
Definition clipper_stats.h:169
void init(const Range< ftype > &range, const int num_ranges=1000)
initialiser: takes the source range and sampling
Definition clipper_stats.cpp:76
ftype ordinal(const ftype &value) const
return reflection ordinal
Definition clipper_stats.cpp:93
Generic_ordinal()
null constructor
Definition clipper_stats.h:167
void add_pass_2(const ftype &value)
DEPRECATED: add a value to the distribution (pass 2 of 2)
Definition clipper_stats.cpp:161
General histogram class.
Definition clipper_stats.h:118
const ftype & y(const int &i) const
return value at index in histogram (Note: no bound check on i)
Definition clipper_stats.h:134
void accumulate(const ftype &x, const ftype &w)
add specified value to histogram (if it is in range)
Definition clipper_stats.h:129
const Histogram & operator+=(const Histogram &h)
add the contents of two histograms (size must match)
Definition clipper_stats.cpp:66
ftype sum() const
return sum of whole histogram
Definition clipper_stats.cpp:49
Histogram()
null constructor
Definition clipper_stats.h:121
void accumulate(const ftype &x)
add value to histogram (if it is in range)
Definition clipper_stats.h:126
Histogram(const Range< ftype > &range, const int &n)
constructor: from range and sampling
Definition clipper_stats.h:123
Range sampling: discrete sampling of a real range.
Definition clipper_stats.h:81
ftype x(const int &i) const
return x-value corresponding to centre of i'th range
Definition clipper_stats.h:102
Range_sampling()
null constructor
Definition clipper_stats.h:84
int index_bounded(const ftype &x) const
return nearest index to particular x-value (bounded 0...n-1)
Definition clipper_stats.h:99
ftype x_max(const int &i) const
return x-value corresponding to top of i'th range
Definition clipper_stats.h:106
int index(const ftype &x) const
return nearest index to particular x-value
Definition clipper_stats.h:97
ftype x(const ftype &i) const
return x-value (0..n) from fractional posn in counting range
Definition clipper_stats.h:94
int size() const
return number of samplings in range
Definition clipper_stats.h:108
ftype x_min(const int &i) const
return x-value corresponding to bottom of i'th range
Definition clipper_stats.h:104
Range_sampling(const int &n)
constructor: from number of samplings
Definition clipper_stats.h:86
Range_sampling(const Range< ftype > &range, const int &n)
constructor: from range and number of samplings
Definition clipper_stats.h:88
ftype indexf(const ftype &x) const
return fractional posn in counting range from x-value (0..n)
Definition clipper_stats.h:91
Range - upper and lower bounds of some type.
Definition clipper_stats.h:57
Range(const T &min, const T &max)
constructor
Definition clipper_stats.h:62
const T & max() const
maximum value
Definition clipper_stats.h:64
Range()
null constructor
Definition clipper_stats.h:60
T range() const
range = max - min
Definition clipper_stats.h:65
void include(const T &datum)
update limits to include a new datum
Definition clipper_stats.h:67
bool contains(const T &datum) const
test if data is within limits ( min <= datum <= max )
Definition clipper_stats.h:70
T truncate(const T &datum) const
truncate data to be within range
Definition clipper_stats.h:73
const T & min() const
minimum value
Definition clipper_stats.h:63
static int intf(const ftype &a)
Truncate-to-integer: int(floor(a))
Definition clipper_util.h:129
static T bound(const T &min, const T &val, const T &max)
bound a value by limits
Definition clipper_util.h:148
ftype64 ftype
ftype definition for floating point representation
Definition clipper_precision.h:58