Total Complexity | 88 |
Total Lines | 383 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Histogram4D often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Histogram4D, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Histogram4D extends AbstractHistogram |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * Constructor for Math_Histogram4D |
||
20 | * |
||
21 | * @access public |
||
22 | * @param optional int $type one of HISTOGRAM_SIMPLE or HISTOGRAM_CUMMULATIVE |
||
23 | * @param optional array $binOptions an array of options for binning the data |
||
24 | * @return object Math_Histogram3D |
||
25 | * |
||
26 | * @see setBinOptions() |
||
27 | * @see Math_AbstractHistogram::setType() |
||
28 | * @see Math_AbstractHistogram |
||
29 | */ |
||
30 | public function __construct($type = self::HISTOGRAM_SIMPLE, $binOptions = "") |
||
36 | // Falling back to default options |
||
37 | } |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Sets the binning options. Overrides parent's method. |
||
42 | * |
||
43 | * @access public |
||
44 | * @param array $binOptions an array of options for binning the data |
||
45 | * @return void |
||
46 | */ |
||
47 | public function setBinOptions($binOptions) |
||
48 | { |
||
49 | if ($this->_validBinOptions($binOptions)) { |
||
50 | return parent::setBinOptions($binOptions); |
||
|
|||
51 | } else { |
||
52 | throw new \PEAR_Exception("incorrect options array"); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Sets the data to be processed. The data will be validated to |
||
58 | * be a simple tri-dimensional numerical array |
||
59 | * |
||
60 | * @access public |
||
61 | * @param array $data the numeric array |
||
62 | * @return mixed boolean true on success, a \PEAR_Error object otherwise |
||
63 | * |
||
64 | * @see _clear() |
||
65 | * @see Math_AbstractHistogram::getData() |
||
66 | * @see Math_AbstractHistogram |
||
67 | * @see getHistogramData() |
||
68 | */ |
||
69 | public function setData($data) |
||
70 | { |
||
71 | $this->_clear(); |
||
72 | if (!$this->_validData($data)) { |
||
73 | throw new \PEAR_Exception("array of numeric coordinates expected"); |
||
74 | } |
||
75 | |||
76 | $this->_data = $data; |
||
77 | list($xMin, $xMax) = $this->_getMinMax('x'); |
||
78 | list($yMin, $yMax) = $this->_getMinMax('y'); |
||
79 | list($zMin, $zMax) = $this->_getMinMax('z'); |
||
80 | if (is_null($this->_rangeLow)) { |
||
81 | $this->_rangeLow = array('x' => $xMin, 'y' => $yMin, 'z' => $zMin); |
||
82 | } |
||
83 | |||
84 | if (is_null($this->_rangeHigh)) { |
||
85 | $this->_rangeHigh = array('x' => $xMax, 'y' => $yMax, 'z' => $zMax); |
||
86 | } |
||
87 | |||
88 | if (is_null($this->_nbins)) { |
||
89 | $this->_nbins = array('x' => 10, 'y' => 10, 'z' => 10); |
||
90 | } |
||
91 | |||
92 | return true; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Calculates the histogram bins and frequencies |
||
97 | * |
||
98 | * @access public |
||
99 | * @param optional $statsMode calculate basic statistics (STATS_BASIC) or full (STATS_FULL) |
||
100 | * @return mixed boolean true on success, a \PEAR_Error object otherwise |
||
101 | * |
||
102 | * @see Math_Stats |
||
103 | */ |
||
104 | public function calculate($statsMode = \HuasoFoundries\Math\Stats::STATS_BASIC) |
||
105 | { |
||
106 | $this->_bins = array(); |
||
107 | $this->_stats = array('x' => new \HuasoFoundries\Math\Stats(), 'y' => new \HuasoFoundries\Math\Stats(), 'z' => new \HuasoFoundries\Math\Stats()); |
||
108 | $this->_statsMode = $statsMode; |
||
109 | $deltaX = ($this->_rangeHigh['x'] - $this->_rangeLow['x']) / $this->_nbins['x']; |
||
110 | $deltaY = ($this->_rangeHigh['y'] - $this->_rangeLow['y']) / $this->_nbins['y']; |
||
111 | $deltaZ = ($this->_rangeHigh['z'] - $this->_rangeLow['z']) / $this->_nbins['z']; |
||
112 | $data = $this->_histogramData(); |
||
113 | $dataX = $data['x']; |
||
114 | $dataY = $data['y']; |
||
115 | $dataZ = $data['z']; |
||
116 | $ignoreList = array(); |
||
117 | $cumm = 0; |
||
118 | $nData = count($dataX); |
||
119 | for ($i = 0; $i < $this->_nbins['x']; $i++) { |
||
120 | $loXBin = $this->_rangeLow['x'] + $i * $deltaX; |
||
121 | $hiXBin = $loXBin + $deltaX; |
||
122 | $xBin = array('low' => $loXBin, 'high' => $hiXBin, |
||
123 | 'mid' => ($hiXBin + $loXBin) / 2); |
||
124 | for ($j = 0; $j < $this->_nbins['y']; $j++) { |
||
125 | $loYBin = $this->_rangeLow['y'] + $j * $deltaY; |
||
126 | $hiYBin = $loYBin + $deltaY; |
||
127 | $yBin = array('low' => $loYBin, 'high' => $hiYBin, |
||
128 | 'mid' => ($hiYBin + $loYBin) / 2); |
||
129 | for ($m = 0; $m < $this->_nbins['z']; $m++) { |
||
130 | $loZBin = $this->_rangeLow['z'] + $m * $deltaZ; |
||
131 | $hiZBin = $loZBin + $deltaZ; |
||
132 | $zBin = array('low' => $loZBin, 'high' => $hiZBin, |
||
133 | 'mid' => ($hiZBin + $loZBin) / 2); |
||
134 | $bin = array('x' => $xBin, 'y' => $yBin, 'z' => $zBin); |
||
135 | $freq = 0; |
||
136 | for ($k = 0; $k < $nData; $k++) { |
||
137 | if (!empty($ignoreList) && in_array($k, $ignoreList)) { |
||
138 | continue; |
||
139 | } |
||
140 | |||
141 | $valueX = $dataX[$k]; |
||
142 | $valueY = $dataY[$k]; |
||
143 | $valueZ = $dataZ[$k]; |
||
144 | $inRangeX = $inRangeY = $inRangeZ = false; |
||
145 | if ($i == 0) { |
||
146 | $inRangeX = ($loXBin <= $valueX && $hiXBin >= $valueX); |
||
147 | } else { |
||
148 | $inRangeX = ($loXBin < $valueX && $hiXBin >= $valueX); |
||
149 | } |
||
150 | |||
151 | if ($j == 0) { |
||
152 | $inRangeY = ($loYBin <= $valueY && $hiYBin >= $valueY); |
||
153 | } else { |
||
154 | $inRangeY = ($loYBin < $valueY && $hiYBin >= $valueY); |
||
155 | } |
||
156 | |||
157 | if ($m == 0) { |
||
158 | $inRangeZ = ($loZBin <= $valueZ && $hiZBin >= $valueZ); |
||
159 | } else { |
||
160 | $inRangeZ = ($loZBin < $valueZ && $hiZBin >= $valueZ); |
||
161 | } |
||
162 | |||
163 | if ($inRangeX && $inRangeY && $inRangeZ) { |
||
164 | $freq++; |
||
165 | $cumm++; |
||
166 | $ignoreList[] = $k; |
||
167 | } |
||
168 | } |
||
169 | if ($this->_type == self::HISTOGRAM_CUMMULATIVE) { |
||
170 | if ($freq > 0) { |
||
171 | $bin['count'] = $freq + $cumm - 1; |
||
172 | } else { |
||
173 | $bin['count'] = 0; |
||
174 | } |
||
175 | } else { |
||
176 | $bin['count'] = $freq; |
||
177 | } |
||
178 | $bin['xbin'] = $i; |
||
179 | $bin['ybin'] = $j; |
||
180 | $bin['zbin'] = $m; |
||
181 | $this->_bins[] = $bin; |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Returns the statistics for the data set |
||
189 | * |
||
190 | * @access public |
||
191 | * @return mixed an associative array on success, a \PEAR_Error object otherwise |
||
192 | */ |
||
193 | public function getDataStats() |
||
194 | { |
||
195 | if (empty($this->_bins)) { |
||
196 | throw new \PEAR_Exception("histogram has not been calculated"); |
||
197 | } |
||
198 | |||
199 | $this->_stats['x']->setData($this->_data['x']); |
||
200 | $this->_stats['y']->setData($this->_data['y']); |
||
201 | $this->_stats['z']->setData($this->_data['z']); |
||
202 | return array('x' => $this->_stats['x']->calc($this->_statsMode), |
||
203 | 'y' => $this->_stats['y']->calc($this->_statsMode), |
||
204 | 'z' => $this->_stats['z']->calc($this->_statsMode)); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Returns the statistics for the data set, filtered using the bin ranges |
||
209 | * |
||
210 | * @access public |
||
211 | * @return mixed an associative array on success, a \PEAR_Error object otherwise |
||
212 | */ |
||
213 | public function getHistogramDataStats() |
||
214 | { |
||
215 | if (empty($this->_bins)) { |
||
216 | throw new \PEAR_Exception("histogram has not been calculated"); |
||
217 | } |
||
218 | |||
219 | $data = $this->_histogramData(); |
||
220 | $this->_stats['x']->setData($data['x']); |
||
221 | $this->_stats['y']->setData($data['y']); |
||
222 | $this->_stats['z']->setData($data['z']); |
||
223 | return array('x' => $this->_stats['x']->calc($this->_statsMode), |
||
224 | 'y' => $this->_stats['y']->calc($this->_statsMode), |
||
225 | 'z' => $this->_stats['z']->calc($this->_statsMode)); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Returns the bins and frequencies calculated using the given |
||
230 | * bin mode and separator |
||
231 | * |
||
232 | * @access public |
||
233 | * @param int $mode one of HISTOGRAM_LO_BINS, HISTOGRAM_MID_BINS (default), or HISTOGRAM_HI_BINS |
||
234 | * @param string $separator the separator, default ", " |
||
235 | * @return mixed a string on success, a \PEAR_Error object otherwise |
||
236 | */ |
||
237 | public function toSeparated($mode = self::HISTOGRAM_MID_BINS, $separator = ", ") |
||
238 | { |
||
239 | try { |
||
240 | $bins = $this->getBins($mode); |
||
241 | } catch (\PEAR_Exception $e) { |
||
242 | return $bins; |
||
243 | } |
||
244 | |||
245 | |||
246 | $nbins = count($bins); |
||
247 | $out = array("# x_bin{$separator}y_bin{$separator}z_bin{$separator}frequency"); |
||
248 | for ($i = 0; $i < $nbins; $i++) { |
||
249 | $out[] = implode($separator, $bins[$i]); |
||
250 | } |
||
251 | |||
252 | return implode("\n", $out) . "\n"; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Returns the minimum and maximum of the given unidimensional numeric |
||
257 | * array |
||
258 | * |
||
259 | * @access private |
||
260 | * @param array $elem |
||
261 | * @return array of values: array(min, max) |
||
262 | */ |
||
263 | public function _getMinMax($elem) |
||
264 | { |
||
265 | return array(min($this->_data[$elem]), max($this->_data[$elem])); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Returns a subset of the bins array by bin value type |
||
270 | * |
||
271 | * @access private |
||
272 | * @param int $mode one of HISTOGRAM_MID_BINS, HISTOGRAM_LO_BINS or HISTOGRAM_HI_BINS |
||
273 | * @return array |
||
274 | */ |
||
275 | public function _filterBins($mode) |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Checks that the array of options passed is valid |
||
295 | * Options array should have the form: |
||
296 | * |
||
297 | * $opt = array ('low'=>array('x'=>10, 'y'=>10, 'z'=>10), |
||
298 | * 'high'=>array(...), |
||
299 | * 'nbins'=>array(...)); |
||
300 | * |
||
301 | * @access private |
||
302 | * @return boolean |
||
303 | */ |
||
304 | public function _validBinOptions($binOptions) |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Checks that the data passed is tri-dimensional numeric array |
||
328 | * of the form: |
||
329 | * |
||
330 | * $data = array ('x'=>array(...), 'y'=>array(...), 'z'=>array(...)); |
||
331 | * |
||
332 | * It also checks that: count($data['x']) == count($data['y'] and |
||
333 | * count($data['x']) == count($data['z'] |
||
334 | * |
||
335 | * @access private |
||
336 | * @return boolean |
||
337 | */ |
||
338 | public function _validData($data) |
||
358 | } |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Returns an array of data contained within the ranges for the |
||
363 | * histogram calculation. Overrides the empty implementation in |
||
364 | * Math_AbstractHistogram::_histogramData() |
||
365 | * |
||
366 | * @access private |
||
367 | * @return array |
||
368 | */ |
||
369 | public function _histogramData() |
||
398 | } |
||
399 | } |
||
400 | |||
401 | // vim: ts=4:sw=4:et: |
||
402 | // vim6: fdl=1: |
||
403 |