cvs: pear /Math_Histogram AbstractHistogram.php Histogram.php Histogram3D.php /Math_Histogram/examples ex_histogram.php sqldata

From: Date: Wed, 22 May 2002 03:01:16 +0000
Subject: cvs: pear /Math_Histogram AbstractHistogram.php Histogram.php Histogram3D.php /Math_Histogram/examples ex_histogram.php sqldata
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-4028@lists.php.net to get a copy of this message
jmcastagnetto Tue May 21 23:01:16 2002 EDT Removed files: /pear/Math_Histogram/examples sqldata Modified files: /pear/Math_Histogram AbstractHistogram.php Histogram.php Histogram3D.php /pear/Math_Histogram/examples ex_histogram.php Log: More factorization between Histogram and AbstractHistogram Finished Histogram3D Minor modifications to example for 2D histogram

Index: pear/Math_Histogram/AbstractHistogram.php diff -u pear/Math_Histogram/AbstractHistogram.php:1.3 pear/Math_Histogram/AbstractHistogram.php:1.4 --- pear/Math_Histogram/AbstractHistogram.php:1.3 Sun May 19 15:21:38 2002 +++ pear/Math_Histogram/AbstractHistogram.php Tue May 21 23:01:16 2002 @@ -16,7 +16,9 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: AbstractHistogram.php,v 1.3 2002/05/19 19:21:38 jmcastagnetto Exp $ +// $Id: AbstractHistogram.php,v 1.4 2002/05/22 03:01:16 jmcastagnetto Exp $ +// +// Last change: Tuesday 2002-05-21 18:52:58 PDT. // include_once "Math/Stats.php"; @@ -189,28 +191,57 @@ }/*}}}*/ /** - * Utility function to check that a value is in the given range + * Returns bins and frequencies for the histogram data set * - * @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; - }/*}}}*/ + * @access public + * @param optional int $mode one of HISTOGRAM_ALL_BINS, HISTOGRAM_LO_BINS, HISTOGRAM_MID_BINS, or HISTOGRAM_HI_BINS + * @return mixed an associative array on success, a PEAR_Error object otherwise + */ + function getBins($mode = HISTOGRAM_ALL_BINS) {/*{{{*/ + if (empty($this->_bins)) + return PEAR::raiseError("histogram has not been calculated"); + switch ($mode) { + case HISTOGRAM_ALL_BINS : + return $this->_bins; + break; + case HISTOGRAM_MID_BINS : + case HISTOGRAM_LO_BINS : + case HISTOGRAM_HI_BINS : + return $this->_filterBins($mode); + break; + default : + return PEAR::raiseError("incorrect mode for bins"); + } + }/*}}}*/ + + /** + * Returns the statistics for the data set and the histogram bins and + * frequencies + * + * @access public + * @return mixed an associative array on success, a PEAR_Error object otherwise + */ + function getHistogramInfo() {/*{{{*/ + if (!empty($this->_nbins)) { + $info = array ( + "type" => ($this->_type == HISTOGRAM_CUMMULATIVE) ? + "cummulative frequency" : "histogram", + "data_stats" => $this->getDataStats(), + "hist_data_stats" => $this->getHistogramDataStats(), + "bins" => $this->_bins, + "nbins" => $this->_nbins, + "range" => array( + "low" => $this->_rangeLow, + "high" => $this->_rangeHigh + ) + ); + return $info; + } else { + return PEAR::raiseError("histogram has not been calculated"); + } + }/*}}}*/ + + /** * Resets the values of several private properties Index: pear/Math_Histogram/Histogram.php diff -u pear/Math_Histogram/Histogram.php:1.5 pear/Math_Histogram/Histogram.php:1.6 --- pear/Math_Histogram/Histogram.php:1.5 Sun May 19 15:21:38 2002 +++ pear/Math_Histogram/Histogram.php Tue May 21 23:01:16 2002 @@ -16,7 +16,9 @@ // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Histogram.php,v 1.5 2002/05/19 19:21:38 jmcastagnetto Exp $ +// $Id: Histogram.php,v 1.6 2002/05/22 03:01:16 jmcastagnetto Exp $ +// +// Last change: Tuesday 2002-05-21 19:58:48 PDT. // //include_once "Math/AbstractHistogram.php"; @@ -158,6 +160,7 @@ $cumm = 0; $data = $this->_histogramData(); $ndata = count($data); + $ignoreList = array(); for ($i=0; $i < $this->_nbins; $i++) { $loBin = $this->_rangeLow + $i * $delta; $hiBin = $loBin + $delta; @@ -168,16 +171,18 @@ $this->_bins[$i]["count"] = $cumm; else $this->_bins[$i]["count"] = 0; - for ($j=$lastpos; $j < $ndata; $j++) { - if ( ($i == 0 && $this->_inRange($data[$j], $loBin, $hiBin, true, true)) - || ($i > 0 && $this->_inRange($data[$j], $loBin, $hiBin))) { + for ($j=0; $j < $ndata; $j++) { + if (!empty($ignoreList) && in_array($j, $ignoreList)) + continue; + if ( $j == 0 ) + $inRange = ($loBin <= $data[$j] && $hiBin >= $data[$j]); + else + $inRange = ($loBin < $data[$j] && $hiBin >= $data[$j]); + if ($inRange) { $this->_bins[$i]["count"]++; if ($this->_type == HISTOGRAM_CUMMULATIVE) $cumm++; - continue; - } else { - $lastpos = $j; - break; + $ignoreList[] = $j; } } } @@ -185,40 +190,13 @@ }/*}}}*/ /** - * Returns the statistics for the data set and the histogram bins and - * frequencies - * - * @access public - * @return mixed an associative array on success, a PEAR_Error object otherwise - */ - function getHistogramInfo() {/*{{{*/ - if (!empty($this->_nbins)) { - $info = array ( - "type" => ($this->_type == HISTOGRAM_CUMMULATIVE) ? - "cummulative frequency" : "histogram", - "data_stats" => $this->getDataStats(), - "hist_data_stats" => $this->getHistogramDataStats(), - "bins" => $this->_bins, - "nbins" => $this->_nbins, - "range" => array( - "low" => $this->_rangeLow, - "high" => $this->_rangeHigh - ) - ); - return $info; - } else { - return PEAR::raiseError("histogram has not been calculated"); - } - }/*}}}*/ - - /** * Returns the statistics for the data set * * @access public * @return mixed an associative array on success, a PEAR_Error object otherwise */ function getDataStats() {/*{{{*/ - if (!empty($this->_nbins)) { + if (!empty($this->_bins)) { $this->_stats->setData($this->_data); return $this->_stats->calc($this->_statsMode); } else { @@ -241,29 +219,24 @@ } }/*}}}*/ - /** - * Returns bins and frequencies for the histogram data set + * Returns the bins and frequencies calculated using the given + * bin mode and separator * * @access public - * @param optional int $mode one of HISTOGRAM_ALL_BINS, HISTOGRAM_LO_BINS, HISTOGRAM_MID_BINS, or HISTOGRAM_HI_BINS - * @return mixed an associative array on success, a PEAR_Error object otherwise - */ - function getBins($mode = HISTOGRAM_ALL_BINS) {/*{{{*/ - if (empty($this->_bins)) - return PEAR::raiseError("histogram has not been calculated"); - switch ($mode) { - case HISTOGRAM_ALL_BINS : - return $this->_bins; - break; - case HISTOGRAM_MID_BINS : - case HISTOGRAM_LO_BINS : - case HISTOGRAM_HI_BINS : - return $this->_filterBins($mode); - break; - default : - return PEAR::raiseError("incorrect mode for bins"); - } + * @param int $mode one of HISTOGRAM_LO_BINS, HISTOGRAM_MID_BINS (default), or HISTOGRAM_HI_BINS + * @param string $separator the separator, default ", " + * @return mixed a string on success, a PEAR_Error object otherwise + */ + function toSeparated($mode=HISTOGRAM_MID_BINS, $separator=", ") {/*{{{*/ + $bins = $this->getBins($mode); + if (PEAR::isError($bins)) + return $bins; + $nbins = count($bins); + $out = array("# bin{$separator}frequency"); + foreach ($bins as $bin=>$freq) + $out[] = "{$bin}{$separator}{$freq}"; + return implode("\n", $out)."\n"; }/*}}}*/ /** @@ -282,8 +255,8 @@ $hdata = $this->_histogramData(); $out .= "\tData range: [".min($hdata).", ".max($hdata)."]\n"; $out .= "\tOriginal data range: [".min($this->_data).", ".max($this->_data)."]\n"; - $out .= "BIN (COUNT) BAR (%)\n"; - $fmt = "%-4.2f (%-4d) |%s\n"; + $out .= "BIN (FREQUENCY) ASCII_BAR (%)\n"; + $fmt = "%-4.3f (%-4d) |%s\n"; $bins = $this->_filterBins($mode); $maxfreq = max(array_values($bins)); $total = count($this->_data); Index: pear/Math_Histogram/Histogram3D.php diff -u pear/Math_Histogram/Histogram3D.php:1.1 pear/Math_Histogram/Histogram3D.php:1.2 --- pear/Math_Histogram/Histogram3D.php:1.1 Sun May 19 15:21:38 2002 +++ pear/Math_Histogram/Histogram3D.php Tue May 21 23:01:16 2002 @@ -1,4 +1,27 @@ <?php +// +// +----------------------------------------------------------------------+ +// | PHP Version 4 | +// +----------------------------------------------------------------------+ +// | Copyright (c) 1997-2002 The PHP Group | +// +----------------------------------------------------------------------+ +// | This source file is subject to version 2.0 of the PHP license, | +// | that is bundled with this package in the file LICENSE, and is | +// | available at through the world-wide-web at | +// | http://www.php.net/license/2_02.txt. | +// | If you did not receive a copy of the PHP license and are unable to | +// | obtain it through the world-wide-web, please send a note to | +// | license@php.net so we can mail you a copy immediately. | +// +----------------------------------------------------------------------+ +// | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> | +// +----------------------------------------------------------------------+ +// +// $Id: Histogram3D.php,v 1.2 2002/05/22 03:01:16 jmcastagnetto Exp $ +// +// Last change: Tuesday 2002-05-21 19:57:18 PDT. +// + +require_once 'AbstractHistogram.php'; class Math_Histogram3D extends Math_AbstractHistogram {/*{{{*/ @@ -30,15 +53,127 @@ return true; }/*}}}*/ - function calculate() { - } + function calculate($statsMode=STATS_BASIC) {/*{{{*/ + $this->_bins = array(); + $this->_stats = array('x' => new Math_Stats(), 'y' => new Math_Stats()); + $this->_statsMode = $statsMode; + $deltaX = ($this->_rangeHigh['x'] - $this->_rangeLow['x']) / $this->_nbins['x']; + $deltaY = ($this->_rangeHigh['y'] - $this->_rangeLow['y']) / $this->_nbins['y']; + $data = $this->_histogramData(); + //$dataX = $this->_data['x']; + //$dataY = $this->_data['y']; + $dataX = $data['x']; + $dataY = $data['y']; + $ignoreList = array(); + $cumm = 0; + $nData = count($dataX); + for ($i=0; $i < $this->_nbins['x']; $i++) { + $loXBin = $this->_rangeLow['x'] + $i * $deltaX; + $hiXBin = $loXBin + $deltaX; + $xBin = array('low' => $loXBin, 'high' => $hiXBin, + 'mid' => ($hiXBin + $loXBin) / 2); + for ($j=0; $j < $this->_nbins['y']; $j++) { + $loYBin = $this->_rangeLow['y'] + $j * $deltaY; + $hiYBin = $loYBin + $deltaY; + $yBin = array('low' => $loYBin, 'high' => $hiYBin, + 'mid' => ($hiYBin + $loYBin) / 2); + $bin = array('x'=>$xBin, 'y'=>$yBin); + $freq = 0; + for ($k=0; $k < $nData; $k++) { + if (!empty($ignoreList) && in_array($k, $ignoreList)) + continue; + $valueX = $dataX[$k]; + $valueY = $dataY[$k]; + $inRangeX = $inRangeY = false; + if ($i == 0) + $inRangeX = ($loXBin <= $valueX && $hiXBin >= $valueX); + else + $inRangeX = ($loXBin < $valueX && $hiXBin >= $valueX); + if ($j == 0) + $inRangeY = ($loYBin <= $valueY && $hiYBin >= $valueY); + else + $inRangeY = ($loYBin < $valueY && $hiYBin >= $valueY); + if ($inRangeX && $inRangeY) { + $freq++; + $cumm++; + $ignoreList[] = $k; + } + } + if ($this->_type == HISTOGRAM_CUMMULATIVE) { + if ($freq > 0) + $bin['count'] = $freq + $cumm - 1; + else + $bin['count'] = 0; + } else { + $bin['count'] = $freq; + } + $bin['xbin'] = $i; + $bin['ybin'] = $j; + $this->_bins[] = $bin; + } + } + }/*}}}*/ + function getDataStats() {/*{{{*/ + if (empty($this->_bins)) + return PEAR::raiseError("histogram has not been calculated"); + $this->_stats['x']->setData($this->_data['x']); + $this->_stats['y']->setData($this->_data['y']); + return array('x' => $this->_stats['x']->calc($this->_statsMode), + 'y' => $this->_stats['y']->calc($this->_statsMode)); + }/*}}}*/ - function _getMinMax($elem) { + function getHistogramDataStats() {/*{{{*/ + if (empty($this->_bins)) + return PEAR::raiseError("histogram has not been calculated"); + $data = $this->_histogramData(); + $this->_stats['x']->setData($data['x']); + $this->_stats['y']->setData($data['y']); + return array('x' => $this->_stats['x']->calc($this->_statsMode), + 'y' => $this->_stats['y']->calc($this->_statsMode)); + }/*}}}*/ + + /** + * Returns the bins and frequencies calculated using the given + * bin mode and separator + * + * @access public + * @param int $mode one of HISTOGRAM_LO_BINS, HISTOGRAM_MID_BINS (default), or HISTOGRAM_HI_BINS + * @param string $separator the separator, default ", " + * @return mixed a string on success, a PEAR_Error object otherwise + */ + function toSeparated($mode=HISTOGRAM_MID_BINS, $separator=", ") {/*{{{*/ + $bins = $this->getBins($mode); + if (PEAR::isError($bins)) + return $bins; + $nbins = count($bins); + $out = array("# x_bin{$separator}y_bin{$separator}frequency"); + for ($i=0; $i < $nbins; $i++) + $out[] = implode($separator, $bins[$i]); + return implode("\n", $out)."\n"; + }/*}}}*/ + + function _getMinMax($elem) {/*{{{*/ return array(min($this->_data[$elem]), max($this->_data[$elem])); - } + }/*}}}*/ - function _validBinOptions($binOptions) { + function _filterBins($mode) {/*{{{*/ + $map = array ( + HISTOGRAM_MID_BINS => "mid", + HISTOGRAM_LO_BINS => "low", + HISTOGRAM_HI_BINS => "high" + ); + $filtered = array(); + foreach ($this->_bins as $bin) { + $tmp['x'] = $bin['x'][$map[$mode]]; + $tmp['y'] = $bin['y'][$map[$mode]]; + $tmp['count'] = $bin['count']; + $filtered[] = $tmp; + } + return $filtered; + }/*}}}*/ + + function _validBinOptions($binOptions) {/*{{{*/ $barray = ( is_array($binOptions) && is_array($binOptions['low']) && is_array($binOptions['high']) @@ -53,23 +188,47 @@ $bnbins = ( isset($nbins['x']) && isset($nbins['y']) && is_numeric($nbins['x']) && is_numeric($nbins['y']) ); return ($barray && $blow && $bhigh && $bnbins); - } + }/*}}}*/ - function _validData($data) { + function _validData($data) {/*{{{*/ if (is_array($data) && is_array($data['x']) && is_array($data['y'])) { - $n = count($data['x']) - if (count($data) == 2 && $n == count($data['y'])) + $n = count($data['x']); + if (count($data) == 2 && $n == count($data['y'])) { for ($i=0; $i < $n; $i++) if (!is_numeric($data['x'][$i]) || !is_numeric($data['y'][$i])) return false; // if everything checks out return true; - else + } else { return false; + } } else { return false; } - } + }/*}}}*/ + + function _histogramData() {/*{{{*/ + if ( $this->_rangeLow['x'] == min($this->_data['x']) + && $this->_rangeHigh['x'] == max($this->_data['x']) + && $this->_rangeLow['y'] == min($this->_data['y']) + && $this->_rangeHigh['y'] == max($this->_data['y']) ) + return $this->_data; + $data = array(); + $ndata = count($this->_data['x']); + for ($i=0; $i < $ndata; $i++) { + $x = $this->_data['x'][$i]; + $y = $this->_data['y'][$i]; + $inRangeX = ($this->_rangeLow['x'] <= $x && $this->_rangeHigh['x'] >= $x); + $inRangeY = ($this->_rangeLow['y'] <= $y && $this->_rangeHigh['y'] >= $y); + if ($inRangeX && $inRangeY) { + $data['x'][] = $x; + $data['y'][] = $y; + } else { + continue; + } + } + return $data; + }/*}}}*/ }/*}}}*/ Index: pear/Math_Histogram/examples/ex_histogram.php diff -u pear/Math_Histogram/examples/ex_histogram.php:1.1 pear/Math_Histogram/examples/ex_histogram.php:1.2 --- pear/Math_Histogram/examples/ex_histogram.php:1.1 Thu May 16 23:17:11 2002 +++ pear/Math_Histogram/examples/ex_histogram.php Tue May 21 23:01:16 2002 @@ -2,41 +2,40 @@ require_once "Math/Histogram.php"; -/* create a boring array */ +// create a boring array $vals = array( 1.5,2,3,4,0,3.2,0.1,0,0,5,3,2,3,4,1,2,4,5,1,3,2,4,5,2,3,4,1,2, 1.5,2,3,4,0,3.2,0.1,0,0,5,3,2,3,4,1,2,4,5,1,3,2,4,5,2,3,4,1,2, 1.5,2,3,4,0,3.2,0.1,0,0,5,3,2,3,4,1,2,4,5,1,3,2,4,5,2,3,4,1,2 ); -/* create an instance */ +// create an instance $h = new Math_Histogram(); // let's do a cummulative histogram $h->setType(HISTOGRAM_CUMMULATIVE); $h->setData($vals); $h->calculate(); -//print_r($h->getHistogramInfo()); +print_r($h->getHistogramInfo()); print_r($h->getBins(HISTOGRAM_HI_BINS)); -print_r($h->getStats()); echo $h->printHistogram(); -echo "\n\n"; +echo "\n=====\n"; // let us read a bigger data set: $data = array(); -foreach(file("sqldata") as $item) +foreach(file("ex_histogram.data") as $item) $data[] = floatval(trim($item)); // let's do a simple histogram $h->setType(HISTOGRAM_SIMPLE); // and set new bin options -$h->setBinOptions(10,null,null); +$h->setBinOptions(20,1.7,2.7); // then set a the big data set $h->setData($data); // and calculate using full stats $h->calculate(STATS_FULL); +print_r($h->getHistogramInfo()); print_r($h->getBins(HISTOGRAM_MID_BINS)); -print_r($h->getStats()); -echo $h->printHistogram(); +echo $h->printHistogram(HISTOGRAM_MID_BINS); ?>
« previous php.pear.cvs (#4028) next »