|
1
|
|
|
<?php |
|
2
|
|
|
namespace HuasoFoundries\Histogram; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Abstract class defining common properties and methods for |
|
6
|
|
|
* the other histogram classes |
|
7
|
|
|
* |
|
8
|
|
|
* Originally this class was part of NumPHP (Numeric PHP package) |
|
9
|
|
|
* |
|
10
|
|
|
* @author Jesus M. Castagnetto <[email protected]> |
|
11
|
|
|
* @version 0.9.1beta |
|
12
|
|
|
* @access public |
|
13
|
|
|
* @package Math_Histogram |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
class AbstractHistogram |
|
17
|
|
|
{ |
|
18
|
|
|
const HISTOGRAM_ALL_BINS = 1; |
|
19
|
|
|
const HISTOGRAM_MID_BINS = 2; |
|
20
|
|
|
const HISTOGRAM_LO_BINS = 3; |
|
21
|
|
|
const HISTOGRAM_HI_BINS = 4; |
|
22
|
|
|
|
|
23
|
|
|
const HISTOGRAM_SIMPLE = 1; |
|
24
|
|
|
const HISTOGRAM_CUMMULATIVE = 2; |
|
25
|
|
|
|
|
26
|
|
|
// properties |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The Math_Stats object |
|
30
|
|
|
* |
|
31
|
|
|
* @access private |
|
32
|
|
|
* @var object Math_Stats |
|
33
|
|
|
* @see Math_Stats |
|
34
|
|
|
*/ |
|
35
|
|
|
public $_stats = null; |
|
36
|
|
|
/** |
|
37
|
|
|
* Mode for the calculation of statistics |
|
38
|
|
|
* |
|
39
|
|
|
* @access private |
|
40
|
|
|
* @var int one of STATS_BASIC or STATS_FULL |
|
41
|
|
|
* @see Math_Stats |
|
42
|
|
|
*/ |
|
43
|
|
|
public $_statsMode; |
|
44
|
|
|
/** |
|
45
|
|
|
* Array of bins |
|
46
|
|
|
* |
|
47
|
|
|
* @access private |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
public $_bins = array(); |
|
51
|
|
|
/** |
|
52
|
|
|
* Number(s) of bins to use in calculation |
|
53
|
|
|
* |
|
54
|
|
|
* @access private |
|
55
|
|
|
* @var mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
public $_nbins; |
|
58
|
|
|
/** |
|
59
|
|
|
* The lowest value(s) to be used when generating the bins |
|
60
|
|
|
* |
|
61
|
|
|
* @access private |
|
62
|
|
|
* @var mixed |
|
63
|
|
|
*/ |
|
64
|
|
|
public $_rangeLow; |
|
65
|
|
|
/** |
|
66
|
|
|
* The highest value(s) to be used when generating the bins |
|
67
|
|
|
* |
|
68
|
|
|
* @access private |
|
69
|
|
|
* @var mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
public $_rangeHigh; |
|
72
|
|
|
/** |
|
73
|
|
|
* The data set |
|
74
|
|
|
* |
|
75
|
|
|
* @access private |
|
76
|
|
|
* @var array |
|
77
|
|
|
* @see $_rangeLow |
|
78
|
|
|
* @see $_rangeHigh |
|
79
|
|
|
*/ |
|
80
|
|
|
public $_data = null; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Constructor |
|
84
|
|
|
* @param optional float $rangeHigh highest value to use for bin frequency calculation |
|
85
|
|
|
* @return object Math_Histogram |
|
86
|
|
|
* |
|
87
|
|
|
* @see setType() |
|
88
|
|
|
* @see setBinOptions() |
|
89
|
|
|
*/ |
|
90
|
|
|
public function __construct($type = self::HISTOGRAM_SIMPLE) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->setType($type); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Sets the type of histogram to compute |
|
97
|
|
|
* |
|
98
|
|
|
* @access public |
|
99
|
|
|
* @param int $type one of HISTOGRAM_SIMPLE or HISTOGRAM_CUMMULATIVE |
|
100
|
|
|
* @return mixed boolean true on success, a PEAR_Error object otherwise |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setType($type) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($type == self::HISTOGRAM_SIMPLE || $type == self::HISTOGRAM_CUMMULATIVE) { |
|
105
|
|
|
$this->_type = $type; |
|
|
|
|
|
|
106
|
|
|
return true; |
|
107
|
|
|
} else { |
|
108
|
|
|
throw new \PEAR_Exception("wrong histogram type requested"); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Sets the binning options |
|
114
|
|
|
* |
|
115
|
|
|
* @access public |
|
116
|
|
|
* @param array $binOptions associative array of bin options |
|
117
|
|
|
* @return mixed true on succcess, a PEAR_Error object otherwise |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setBinOptions($binOptions) |
|
120
|
|
|
{ |
|
121
|
|
|
if (!is_array($binOptions)) { |
|
122
|
|
|
throw new \PEAR_Exception("incorrect options array"); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->_rangeLow = $binOptions["low"]; |
|
126
|
|
|
$this->_rangeHigh = $binOptions["high"]; |
|
127
|
|
|
$this->_nbins = $binOptions["nbins"]; |
|
128
|
|
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Abstract method to set data. Needs to be implemented in each subclass |
|
133
|
|
|
* |
|
134
|
|
|
* @access public |
|
135
|
|
|
* @param array $data |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setData($data) |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns the array of data set using setData() |
|
143
|
|
|
* |
|
144
|
|
|
* @access public |
|
145
|
|
|
* @return mixed a numerical array on success, a PEAR_Error object otherwise |
|
146
|
|
|
* |
|
147
|
|
|
* @see setData() |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getData() |
|
150
|
|
|
{ |
|
151
|
|
|
if (is_null($this->_data)) { |
|
152
|
|
|
throw new \PEAR_Exception("data has not been set"); |
|
153
|
|
|
} else { |
|
154
|
|
|
return $this->_data; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns the array of data used to calculate the histogram, |
|
160
|
|
|
* i.e. the data that was inside the range specified for the |
|
161
|
|
|
* histogram bins |
|
162
|
|
|
* |
|
163
|
|
|
* @access public |
|
164
|
|
|
* @return mixed a numerical array on success, a PEAR_Error object otherwise |
|
165
|
|
|
* |
|
166
|
|
|
* @see setData() |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getHistogramData() |
|
169
|
|
|
{ |
|
170
|
|
|
if (is_null($this->_data)) { |
|
171
|
|
|
throw new \PEAR_Exception("data has not been set"); |
|
172
|
|
|
} else { |
|
173
|
|
|
return $this->_histogramData(); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Returns bins and frequencies for the histogram data set |
|
179
|
|
|
* |
|
180
|
|
|
* @access public |
|
181
|
|
|
* @param optional int $mode one of HISTOGRAM_ALL_BINS, HISTOGRAM_LO_BINS, HISTOGRAM_MID_BINS, or HISTOGRAM_HI_BINS |
|
182
|
|
|
* @return mixed an associative array on success, a PEAR_Error object otherwise |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getBins($mode = self::HISTOGRAM_ALL_BINS) |
|
185
|
|
|
{ |
|
186
|
|
|
if (empty($this->_bins)) { |
|
187
|
|
|
throw new \PEAR_Exception("histogram has not been calculated"); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
switch ($mode) { |
|
191
|
|
|
case self::HISTOGRAM_ALL_BINS: |
|
192
|
|
|
return $this->_bins; |
|
193
|
|
|
break; |
|
|
|
|
|
|
194
|
|
|
case self::HISTOGRAM_MID_BINS: |
|
195
|
|
|
case self::HISTOGRAM_LO_BINS: |
|
196
|
|
|
case self::HISTOGRAM_HI_BINS: |
|
197
|
|
|
return $this->_filterBins($mode); |
|
|
|
|
|
|
198
|
|
|
break; |
|
199
|
|
|
default: |
|
200
|
|
|
throw new \PEAR_Exception("incorrect mode for bins"); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Returns the statistics for the data set and the histogram bins and |
|
206
|
|
|
* frequencies |
|
207
|
|
|
* |
|
208
|
|
|
* @access public |
|
209
|
|
|
* @return mixed an associative array on success, a PEAR_Error object otherwise |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getHistogramInfo() |
|
212
|
|
|
{ |
|
213
|
|
|
if (!empty($this->_nbins)) { |
|
214
|
|
|
$data_stats = $this->getDataStats(); |
|
|
|
|
|
|
215
|
|
|
$getHistogramDataStats = $this->getHistogramDataStats(); |
|
|
|
|
|
|
216
|
|
|
//\Util\Helpers::prdie('getHistogramInfo after $getHistogramDataStats'); |
|
217
|
|
|
|
|
218
|
|
|
$info = array( |
|
219
|
|
|
"type" => ($this->_type == self::HISTOGRAM_CUMMULATIVE) ? |
|
220
|
|
|
"cummulative frequency" : "histogram", |
|
221
|
|
|
"data_stats" => $data_stats, |
|
222
|
|
|
"hist_data_stats" => $getHistogramDataStats, |
|
223
|
|
|
"bins" => $this->_bins, |
|
224
|
|
|
"nbins" => $this->_nbins, |
|
225
|
|
|
"range" => array( |
|
226
|
|
|
"low" => $this->_rangeLow, |
|
227
|
|
|
"high" => $this->_rangeHigh, |
|
228
|
|
|
), |
|
229
|
|
|
); |
|
230
|
|
|
return $info; |
|
231
|
|
|
} else { |
|
232
|
|
|
throw new \PEAR_Exception("histogram has not been calculated"); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Resets the values of several private properties |
|
238
|
|
|
* |
|
239
|
|
|
* @access private |
|
240
|
|
|
* @return void |
|
241
|
|
|
*/ |
|
242
|
|
|
public function _clear() |
|
243
|
|
|
{ |
|
244
|
|
|
$this->_stats = null; |
|
245
|
|
|
$this->_statsMode = null; |
|
246
|
|
|
$this->_data = null; |
|
247
|
|
|
$this->_orig = array(); |
|
|
|
|
|
|
248
|
|
|
$this->_bins = array(); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Abstract method that returns an array of data contained within the |
|
253
|
|
|
* range for the histogram calculation |
|
254
|
|
|
* Each subclass must implement this method |
|
255
|
|
|
* |
|
256
|
|
|
* @access private |
|
257
|
|
|
* @return array |
|
258
|
|
|
*/ |
|
259
|
|
|
public function _histogramData() |
|
260
|
|
|
{ |
|
261
|
|
|
return array(); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
// vim: ts=4:sw=4:et: |
|
265
|
|
|
// vim6: fdl=1: |
|
266
|
|
|
|