cvs: pear /Math_Histogram AbstractHistogram.php Histogram.php
| From: | Jesus M. Castagnetto | Date: | Sat, 18 May 2002 01:57:31 +0000 |
| Subject: | cvs: pear /Math_Histogram AbstractHistogram.php Histogram.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-3918@lists.php.net to get a copy of this message | ||
jmcastagnetto Fri May 17 21:57:31 2002 EDT
Modified files:
/pear/Math_Histogram AbstractHistogram.php Histogram.php
Log:
refactoring of methods
Index: pear/Math_Histogram/AbstractHistogram.php diff -u pear/Math_Histogram/AbstractHistogram.php:1.1 pear/Math_Histogram/AbstractHistogram.php:1.2 --- pear/Math_Histogram/AbstractHistogram.php:1.1 Fri May 17 17:56:47 2002 +++ pear/Math_Histogram/AbstractHistogram.php Fri May 17 21:57:31 2002 @@ -16,7 +16,7 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: AbstractHistogram.php,v 1.1 2002/05/17 21:56:47 jmcastagnetto Exp $ +// $Id: AbstractHistogram.php,v 1.2 2002/05/18 01:57:31 jmcastagnetto Exp $ // include_once "Math/Stats.php"; @@ -70,29 +70,28 @@ */ var $_bins = array(); /** - * Number of bins to use in calculation + * Number(s) of bins to use in calculation * * @access private - * @var int + * @var mixed */ var $_nbins; /** - * The lowest value to be used when generating the bins + * The lowest value(s) to be used when generating the bins * * @access private - * @var float + * @var mixed */ var $_rangeLow; /** - * The highest value to be used when generating the bins + * The highest value(s) to be used when generating the bins * * @access private - * @var float + * @var mixed */ var $_rangeHigh; /** - * The data set after filtering using $this->_rangeHigh and - * $this->_rangeLow + * The data set * * @access private * @var array @@ -100,14 +99,6 @@ * @see $_rangeHigh */ var $_data = null; - /** - * The original data, count, etc. - * - * @access private - * @var array - * @see $_data - */ - var $_orig = array(); /*}}}*/ @@ -120,7 +111,7 @@ * @see setType() * @see setBinOptions() */ - function Math_AbstractHistogram($type=HISTOGRAM_SIMPLE) { + function Math_AbstractHistogram($type=HISTOGRAM_SIMPLE) {/*{{{*/ $this->setType($type); }/*}}}*/ @@ -140,8 +131,89 @@ } }/*}}}*/ + /** + * Sets the binning options + * + * @access public + * @param array $binOptions associative array of bin options + * @return mixed true on succcess, a PEAR_Error object otherwise + */ + function setBinOptions($binOptions) {/*{{{*/ + if (!is_array($binOptions)) + return PEAR::raiseError("incorrect options array"); + $this->_rangeLow = $binOptions["low"]; + $this->_rangeHigh = $binOptions["high"]; + $this->_nbins = $binOptions["nbins"]; + return true; + }/*}}}*/ + + /** + * Abstract method to set data. Needs to be implemented in each subclass + * + * @access public + * @param array $data + */ + function setData($data) {/*{{{*/ + }/*}}}*/ + + /** + * Returns the array of data set using setData() + * + * @access public + * @return mixed a numerical array on success, a PEAR_Error object otherwise + * + * @see setData() + */ + function getData() {/*{{{*/ + if (is_null($this->_data)) + return PEAR::raiseError("data has not been set"); + else + return $this->_data; + }/*}}}*/ + + + /** + * Utility function to check that a value is in the given range + * + * @access _private + * @param numeric $val the value + * @param numeric $lo the lower range limit + * @param numeric $hi the upper range limit + * @param optional boolean $loOpen whether the lower range is open, i.e. [$lo + * @param optional boolean $hiOpen whether the upper range is open, i.e. $hi] + * @return boolean + */ + function _inRange($val, $lo, $hi, $loOpen=true, $hiOpen=false) {/*{{{*/ + if ($hiOpen) + $hbool = ($val <= $hi); + else + $hbool = ($val < $hi); + + if ($loOpen) + $lbool = ($val <= $lo); + else + $lbool = ($val < $lo); + return $hbool && $lbool; + }/*}}}*/ + + /** + * Resets the values of several private properties + * + * @access private + * @return void + */ + function _clear() {/*{{{*/ + $this->_stats = null; + $this->_statsMode = null; + $this->_data = null; + $this->_orig = array(); + $this->_bins = array(); + }/*}}}*/ + + + }/*}}}*/ // vim: ts=4:sw=4:et: -// vim6: fdl=0: +// vim6: fdl=1: ?> Index: pear/Math_Histogram/Histogram.php diff -u pear/Math_Histogram/Histogram.php:1.3 pear/Math_Histogram/Histogram.php:1.4 --- pear/Math_Histogram/Histogram.php:1.3 Fri May 17 18:36:41 2002 +++ pear/Math_Histogram/Histogram.php Fri May 17 21:57:31 2002 @@ -16,7 +16,7 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Histogram.php,v 1.3 2002/05/17 22:36:41 jmcastagnetto Exp $ +// $Id: Histogram.php,v 1.4 2002/05/18 01:57:31 jmcastagnetto Exp $ // //include_once "Math/AbstractHistogram.php"; @@ -54,11 +54,11 @@ * * // let us read a bigger data set: * $data = array(); - * foreach(file("sqldata") as $item) + * foreach(file("bigdatafile") as $item) * $data[] = floatval(trim($item)); * * // and set new bin options - * $h->setBinOptions(6,null,null); + * $h->setBinOptions(20,1.7,2.7); * // then set a the big data set * $h->setData($data); * // let's do a regular histogram @@ -98,7 +98,7 @@ }/*}}}*/ /** - * Sets the binning options + * Sets the binning options. Overrides parent's method. * * @access public * @param int $nbins the number of bins to use for computing the histogram @@ -113,16 +113,17 @@ }/*}}}*/ /** - * Sets the data to be processed. The data will be filtered using - * the low and high values for the range if set. So in the general - * case the original data and the one used for the histogram will - * not have the same number of elements. + * Sets the data to be processed. The data will be validated to + * be a simple uni-dimensional numerical array * * @access public * @param array $data the numeric array * @return mixed boolean true on success, a PEAR_Error object otherwise * * @see _clear() + * @see Math_AbstractHistogram::getData() + * @see Math_AbstractHistogram + * @see getHistogramData() */ function setData($data) {/*{{{*/ $this->_clear(); @@ -131,36 +132,31 @@ foreach ($data as $item) if (!is_numeric($item)) return PEAR::raiseError("non-numeric item in array"); - $this->_orig["data"] = array_values($data); - if (!is_null($this->_rangeLow) && !is_null($this->_rangeHigh)) { - foreach ($data as $item) - if ($item >= $this->_rangeLow || $item <= $this->_rangeHigh) - $this->_data[] = $item; - } else { - $this->_data =& $this->_orig["data"]; + $this->_data = $data; + if (is_null($this->_rangeLow)) $this->_rangeLow = min($this->_data); + if (is_null($this->_rangeHigh)) $this->_rangeHigh = max($this->_data); - } - $this->_orig["count"] = count($data); - $this->_orig["min"] = min($data); - $this->_orig["max"] = max($data); sort($this->_data); return true; }/*}}}*/ - function getData() { - if (is_null($this->_data)) - return PEAR::raiseError("data has not been set"); - else - return $this->_data; - } - - function getHistogramData() { + /** + * Returns the array of data used to calculate the histogram, + * i.e. the data that was inside the range specified for the + * histogram bins + * + * @access public + * @return mixed a numerical array on success, a PEAR_Error object otherwise + * + * @see setData() + */ + function getHistogramData() {/*{{{*/ if (is_null($this->_data)) return PEAR::raiseError("data has not been set"); else return $this->_histogramData(); - } + }/*}}}*/ /** * Calculates the histogram bins and frequencies @@ -190,17 +186,15 @@ else $this->_bins[$i]["count"] = 0; for ($j=$lastpos; $j < $ndata; $j++) { - if ($data[$j] <= $hiBin) { - if (($i == 0 && $data[$j] >= $loBin) - || ($i > 0 && $data[$j] > $loBin)) { - $this->_bins[$i]["count"]++; - if ($this->_type == HISTOGRAM_CUMMULATIVE) - $cumm++; - continue; - } + if ( ($i == 0 && $this->_inRange($data[$j], $loBin, $hiBin, true, true)) + || ($i > 0 && $this->_inRange($data[$j], $loBin, $hiBin))) { + $this->_bins[$i]["count"]++; + if ($this->_type == HISTOGRAM_CUMMULATIVE) + $cumm++; + continue; } else { - $lastpos = $j; - break; + $lastpos = $j; + break; } } } @@ -235,21 +229,7 @@ }/*}}}*/ /** - * Returns the original data set, before it was filtered using the range - * limits - * - * @access public - * @return mixed an associative array on success, a PEAR_Error object otherwise - */ - function getOriginal() {/*{{{*/ - if (empty($this->_orig)) - return PEAR::raiseError("data has not been set"); - else - return $this->_orig; - }/*}}}*/ - - /** - * Returns just the statistics for the data set + * Returns the statistics for the data set * * @access public * @return mixed an associative array on success, a PEAR_Error object otherwise @@ -264,8 +244,7 @@ }/*}}}*/ /** - * Returns just the statistics for the data set, filtered using the bin - * range + * Returns the statistics for the data set, filtered using the bin range * * @access public * @return mixed an associative array on success, a PEAR_Error object otherwise @@ -281,7 +260,7 @@ /** - * Returns just bins and frequencies for the data set + * Returns bins and frequencies for the histogram data set * * @access public * @param optional int $mode one of HISTOGRAM_ALL_BINS, HISTOGRAM_LO_BINS, HISTOGRAM_MID_BINS, or HISTOGRAM_HI_BINS @@ -330,6 +309,15 @@ return $out; }/*}}}*/ + /** + * Prints a simple ASCII bar + * + * @access private + * @param int $freq the frequency + * @param int $maxfreq the maximum frequency + * @param int $total the total count + * @return string + */ function _bar($freq, $maxfreq, $total) {/*{{{*/ $fact = floatval(($maxfreq > 40) ? 40/$maxfreq: 1); $niter = round($freq * $fact); @@ -339,15 +327,13 @@ return $out.sprintf(" (%.1f%%)", $freq/$total * 100); }/*}}}*/ - - function _clear() {/*{{{*/ - $this->_stats = null; - $this->_statsMode = null; - $this->_data = null; - $this->_orig = array(); - $this->_bins = array(); - }/*}}}*/ - + /** + * Returns a subset of the bins array by bin value type + * + * @access private + * @param int $mode one of HISTOGRAM_MID_BINS, HISTOGRAM_LO_BINS or HISTOGRAM_HI_BINS + * @return array + */ function _filterBins($mode) {/*{{{*/ $map = array ( HISTOGRAM_MID_BINS => "mid", @@ -360,7 +346,14 @@ return $filtered; }/*}}}*/ - function _histogramData() { + /** + * Returns an array of data contained within the range for + * the histogram calculation + * + * @access private + * @return array + */ + function _histogramData() {/*{{{*/ $data = array(); foreach ($this->_data as $val) if ($val < $this->_rangeLow || $val > $this->_rangeHigh) @@ -368,11 +361,11 @@ else $data[] = $val; return $data; - } + }/*}}}*/ }/*}}}*/ // vim: ts=4:sw=4:et: -// vim6: fdl=0: +// vim6: fdl=1: ?>
Index: pear/Math_Histogram/AbstractHistogram.php diff -u pear/Math_Histogram/AbstractHistogram.php:1.1 pear/Math_Histogram/AbstractHistogram.php:1.2 --- pear/Math_Histogram/AbstractHistogram.php:1.1 Fri May 17 17:56:47 2002 +++ pear/Math_Histogram/AbstractHistogram.php Fri May 17 21:57:31 2002 @@ -16,7 +16,7 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: AbstractHistogram.php,v 1.1 2002/05/17 21:56:47 jmcastagnetto Exp $ +// $Id: AbstractHistogram.php,v 1.2 2002/05/18 01:57:31 jmcastagnetto Exp $ // include_once "Math/Stats.php"; @@ -70,29 +70,28 @@ */ var $_bins = array(); /** - * Number of bins to use in calculation + * Number(s) of bins to use in calculation * * @access private - * @var int + * @var mixed */ var $_nbins; /** - * The lowest value to be used when generating the bins + * The lowest value(s) to be used when generating the bins * * @access private - * @var float + * @var mixed */ var $_rangeLow; /** - * The highest value to be used when generating the bins + * The highest value(s) to be used when generating the bins * * @access private - * @var float + * @var mixed */ var $_rangeHigh; /** - * The data set after filtering using $this->_rangeHigh and - * $this->_rangeLow + * The data set * * @access private * @var array @@ -100,14 +99,6 @@ * @see $_rangeHigh */ var $_data = null; - /** - * The original data, count, etc. - * - * @access private - * @var array - * @see $_data - */ - var $_orig = array(); /*}}}*/ @@ -120,7 +111,7 @@ * @see setType() * @see setBinOptions() */ - function Math_AbstractHistogram($type=HISTOGRAM_SIMPLE) { + function Math_AbstractHistogram($type=HISTOGRAM_SIMPLE) {/*{{{*/ $this->setType($type); }/*}}}*/ @@ -140,8 +131,89 @@ } }/*}}}*/ + /** + * Sets the binning options + * + * @access public + * @param array $binOptions associative array of bin options + * @return mixed true on succcess, a PEAR_Error object otherwise + */ + function setBinOptions($binOptions) {/*{{{*/ + if (!is_array($binOptions)) + return PEAR::raiseError("incorrect options array"); + $this->_rangeLow = $binOptions["low"]; + $this->_rangeHigh = $binOptions["high"]; + $this->_nbins = $binOptions["nbins"]; + return true; + }/*}}}*/ + + /** + * Abstract method to set data. Needs to be implemented in each subclass + * + * @access public + * @param array $data + */ + function setData($data) {/*{{{*/ + }/*}}}*/ + + /** + * Returns the array of data set using setData() + * + * @access public + * @return mixed a numerical array on success, a PEAR_Error object otherwise + * + * @see setData() + */ + function getData() {/*{{{*/ + if (is_null($this->_data)) + return PEAR::raiseError("data has not been set"); + else + return $this->_data; + }/*}}}*/ + + + /** + * Utility function to check that a value is in the given range + * + * @access _private + * @param numeric $val the value + * @param numeric $lo the lower range limit + * @param numeric $hi the upper range limit + * @param optional boolean $loOpen whether the lower range is open, i.e. [$lo + * @param optional boolean $hiOpen whether the upper range is open, i.e. $hi] + * @return boolean + */ + function _inRange($val, $lo, $hi, $loOpen=true, $hiOpen=false) {/*{{{*/ + if ($hiOpen) + $hbool = ($val <= $hi); + else + $hbool = ($val < $hi); + + if ($loOpen) + $lbool = ($val <= $lo); + else + $lbool = ($val < $lo); + return $hbool && $lbool; + }/*}}}*/ + + /** + * Resets the values of several private properties + * + * @access private + * @return void + */ + function _clear() {/*{{{*/ + $this->_stats = null; + $this->_statsMode = null; + $this->_data = null; + $this->_orig = array(); + $this->_bins = array(); + }/*}}}*/ + + + }/*}}}*/ // vim: ts=4:sw=4:et: -// vim6: fdl=0: +// vim6: fdl=1: ?> Index: pear/Math_Histogram/Histogram.php diff -u pear/Math_Histogram/Histogram.php:1.3 pear/Math_Histogram/Histogram.php:1.4 --- pear/Math_Histogram/Histogram.php:1.3 Fri May 17 18:36:41 2002 +++ pear/Math_Histogram/Histogram.php Fri May 17 21:57:31 2002 @@ -16,7 +16,7 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Histogram.php,v 1.3 2002/05/17 22:36:41 jmcastagnetto Exp $ +// $Id: Histogram.php,v 1.4 2002/05/18 01:57:31 jmcastagnetto Exp $ // //include_once "Math/AbstractHistogram.php"; @@ -54,11 +54,11 @@ * * // let us read a bigger data set: * $data = array(); - * foreach(file("sqldata") as $item) + * foreach(file("bigdatafile") as $item) * $data[] = floatval(trim($item)); * * // and set new bin options - * $h->setBinOptions(6,null,null); + * $h->setBinOptions(20,1.7,2.7); * // then set a the big data set * $h->setData($data); * // let's do a regular histogram @@ -98,7 +98,7 @@ }/*}}}*/ /** - * Sets the binning options + * Sets the binning options. Overrides parent's method. * * @access public * @param int $nbins the number of bins to use for computing the histogram @@ -113,16 +113,17 @@ }/*}}}*/ /** - * Sets the data to be processed. The data will be filtered using - * the low and high values for the range if set. So in the general - * case the original data and the one used for the histogram will - * not have the same number of elements. + * Sets the data to be processed. The data will be validated to + * be a simple uni-dimensional numerical array * * @access public * @param array $data the numeric array * @return mixed boolean true on success, a PEAR_Error object otherwise * * @see _clear() + * @see Math_AbstractHistogram::getData() + * @see Math_AbstractHistogram + * @see getHistogramData() */ function setData($data) {/*{{{*/ $this->_clear(); @@ -131,36 +132,31 @@ foreach ($data as $item) if (!is_numeric($item)) return PEAR::raiseError("non-numeric item in array"); - $this->_orig["data"] = array_values($data); - if (!is_null($this->_rangeLow) && !is_null($this->_rangeHigh)) { - foreach ($data as $item) - if ($item >= $this->_rangeLow || $item <= $this->_rangeHigh) - $this->_data[] = $item; - } else { - $this->_data =& $this->_orig["data"]; + $this->_data = $data; + if (is_null($this->_rangeLow)) $this->_rangeLow = min($this->_data); + if (is_null($this->_rangeHigh)) $this->_rangeHigh = max($this->_data); - } - $this->_orig["count"] = count($data); - $this->_orig["min"] = min($data); - $this->_orig["max"] = max($data); sort($this->_data); return true; }/*}}}*/ - function getData() { - if (is_null($this->_data)) - return PEAR::raiseError("data has not been set"); - else - return $this->_data; - } - - function getHistogramData() { + /** + * Returns the array of data used to calculate the histogram, + * i.e. the data that was inside the range specified for the + * histogram bins + * + * @access public + * @return mixed a numerical array on success, a PEAR_Error object otherwise + * + * @see setData() + */ + function getHistogramData() {/*{{{*/ if (is_null($this->_data)) return PEAR::raiseError("data has not been set"); else return $this->_histogramData(); - } + }/*}}}*/ /** * Calculates the histogram bins and frequencies @@ -190,17 +186,15 @@ else $this->_bins[$i]["count"] = 0; for ($j=$lastpos; $j < $ndata; $j++) { - if ($data[$j] <= $hiBin) { - if (($i == 0 && $data[$j] >= $loBin) - || ($i > 0 && $data[$j] > $loBin)) { - $this->_bins[$i]["count"]++; - if ($this->_type == HISTOGRAM_CUMMULATIVE) - $cumm++; - continue; - } + if ( ($i == 0 && $this->_inRange($data[$j], $loBin, $hiBin, true, true)) + || ($i > 0 && $this->_inRange($data[$j], $loBin, $hiBin))) { + $this->_bins[$i]["count"]++; + if ($this->_type == HISTOGRAM_CUMMULATIVE) + $cumm++; + continue; } else { - $lastpos = $j; - break; + $lastpos = $j; + break; } } } @@ -235,21 +229,7 @@ }/*}}}*/ /** - * Returns the original data set, before it was filtered using the range - * limits - * - * @access public - * @return mixed an associative array on success, a PEAR_Error object otherwise - */ - function getOriginal() {/*{{{*/ - if (empty($this->_orig)) - return PEAR::raiseError("data has not been set"); - else - return $this->_orig; - }/*}}}*/ - - /** - * Returns just the statistics for the data set + * Returns the statistics for the data set * * @access public * @return mixed an associative array on success, a PEAR_Error object otherwise @@ -264,8 +244,7 @@ }/*}}}*/ /** - * Returns just the statistics for the data set, filtered using the bin - * range + * Returns the statistics for the data set, filtered using the bin range * * @access public * @return mixed an associative array on success, a PEAR_Error object otherwise @@ -281,7 +260,7 @@ /** - * Returns just bins and frequencies for the data set + * Returns bins and frequencies for the histogram data set * * @access public * @param optional int $mode one of HISTOGRAM_ALL_BINS, HISTOGRAM_LO_BINS, HISTOGRAM_MID_BINS, or HISTOGRAM_HI_BINS @@ -330,6 +309,15 @@ return $out; }/*}}}*/ + /** + * Prints a simple ASCII bar + * + * @access private + * @param int $freq the frequency + * @param int $maxfreq the maximum frequency + * @param int $total the total count + * @return string + */ function _bar($freq, $maxfreq, $total) {/*{{{*/ $fact = floatval(($maxfreq > 40) ? 40/$maxfreq: 1); $niter = round($freq * $fact); @@ -339,15 +327,13 @@ return $out.sprintf(" (%.1f%%)", $freq/$total * 100); }/*}}}*/ - - function _clear() {/*{{{*/ - $this->_stats = null; - $this->_statsMode = null; - $this->_data = null; - $this->_orig = array(); - $this->_bins = array(); - }/*}}}*/ - + /** + * Returns a subset of the bins array by bin value type + * + * @access private + * @param int $mode one of HISTOGRAM_MID_BINS, HISTOGRAM_LO_BINS or HISTOGRAM_HI_BINS + * @return array + */ function _filterBins($mode) {/*{{{*/ $map = array ( HISTOGRAM_MID_BINS => "mid", @@ -360,7 +346,14 @@ return $filtered; }/*}}}*/ - function _histogramData() { + /** + * Returns an array of data contained within the range for + * the histogram calculation + * + * @access private + * @return array + */ + function _histogramData() {/*{{{*/ $data = array(); foreach ($this->_data as $val) if ($val < $this->_rangeLow || $val > $this->_rangeHigh) @@ -368,11 +361,11 @@ else $data[] = $val; return $data; - } + }/*}}}*/ }/*}}}*/ // vim: ts=4:sw=4:et: -// vim6: fdl=0: +// vim6: fdl=1: ?>