More value heuristics
More value heuristics

--- a/displayContract.php
+++ b/displayContract.php
@@ -28,9 +28,17 @@
 echo "<br>";

 }

 }

-echo '<a href="https://www.tenders.gov.au/?event=public.advancedsearch.keyword&keyword=CN'.$_REQUEST['CNID'].'"> View original record @ tenders.gov.au</a>';

+echo '<br><a href="https://www.tenders.gov.au/?event=public.advancedsearch.keyword&keyword=CN'.$_REQUEST['CNID'].'"> View original record @ tenders.gov.au</a><br>';

 

 mysql_free_result($result);

+

+$query = "SELECT * FROM `heuristic_results` where CNID = ".$_REQUEST['CNID'];

+$result = mysql_query($query);

+if (!$result) echo mysql_error().$query;

+while ($r = mysql_fetch_array($result, MYSQL_ASSOC)) {

+	echo "<b>{$r['heuristic_name']}</b>: {$r['heuristic_value']} (raw value: {$r['raw_value']}, mean: {$r['mean']}, stddev: {$r['stddev']})<br>";

+}

+

 include_footer();

 ?>

 

--- a/exportData.csv.php
+++ b/exportData.csv.php
@@ -4,10 +4,11 @@
 setlocale(LC_CTYPE, 'C');
 // source: http://stackoverflow.com/questions/81934/easy-way-to-export-a-sql-table-without-access-to-the-server-or-phpmyadmin#81951
 $result = mysql_query('
-SELECT CNID,contractnotice.agencyName,agency.abn as agencyABN,publishDate,contractStart,contractEnd,value,description,procurementMethod,category,categoryUNSPSC
+SELECT CNID,contractnotice.agencyName,agency.abn as agencyABN,DATE(publishDate),contractStart,contractEnd,value,description,procurementMethod,category,categoryUNSPSC
 ,supplierABN,supplierName,supplierCity,supplierPostcode,supplierCountry,contactPostcode,
 IF(supplierABN != "",supplierABN,supplierName) as supplierID,
-concat("https://www.tenders.gov.au/?event=public.advancedsearch.keyword&keyword=CN",CNID) as sourceURL FROM `contractnotice` join agency on contractnotice.agencyName=agency.agencyName where childCN = 0');
+concat("https://www.tenders.gov.au/?event=public.advancedsearch.keyword&keyword=CN",CNID) as sourceURL 
+FROM `contractnotice` join agency on contractnotice.agencyName=agency.agencyName where childCN = 0');
 if (!$result) die('Couldn\'t fetch records');
 $num_fields = mysql_num_fields($result);
 $headers = array();

--- a/heuristics/dateHeuristics.php
+++ b/heuristics/dateHeuristics.php
@@ -1,4 +1,102 @@
-        - long contract period (number of weeks/days?)
-            - Reported late
-        - 45 days? A late contract is a dodgy contract except maybe for variations?
+<?php
+//long contract period (number of weeks/days?)
+$heuristics["DATE_LONG_CONTRACT_PERIOD"] = Array(
+	"description" => "long contract period (number of weeks/days?)"
+);
+function DATE_LONG_CONTRACT_PERIOD($cn)
+{
+	$averageContractPeriod = getAverageContractPeriod();
+	$stddevContractPeriod = getstddevContractPeriod();
+	$diff = strtotime($cn['contractEnd']) - strtotime($cn['contractStart']);
+	$days = intval($diff / (60 * 60 * 24));
+	$value = abs($days - $averageContractPeriod) / $stddevContractPeriod;
+	return Array(
+		"heuristic_value" => $value,
+		"raw_value" => $days,
+		"mean" => $averageContractPeriod,
+		"stddev" => $stddevContractPeriod
+	);
+}
+$averageContractPeriod;
+function getAverageContractPeriod()
+{
+	global $averageContractPeriod, $stddevContractPeriod;
+	if (!$averageContractPeriod) {
+		getStddevAverageContractPeriod();
+	}
+	return $averageContractPeriod;
+}
+$stddevContractPeriod;
+function getstddevContractPeriod()
+{
+	global $averageContractPeriod, $stddevContractPeriod;
+	if (!$stddevContractPeriod) {
+		getStddevAverageContractPeriod();
+	}
+	return $stddevContractPeriod;
+}
+function getStddevAverageContractPeriod()
+{
+	global $averageContractPeriod, $stddevContractPeriod;
+	$query = "select AVG(dateDiff(contractEnd,contractStart)),stddev(dateDiff(contractEnd,contractStart)) from contractnotice";
+	$result = mysql_query($query);
+	$r = mysql_fetch_array($result, MYSQL_BOTH);
+	$averageContractPeriod = $r[0];
+	$stddevContractPeriod = $r[1];
+}
 
+
+//Reported late, 45 days? A late contract is a dodgy contract except maybe for variations?
+$heuristics["DATE_REPORTED_LATE"] = Array(
+	"description" => "Reported late, 45 days?"
+);
+function DATE_REPORTED_LATE($cn)
+{
+	$averageDaysLate = getAverageDaysLate();
+	$stddevDaysLate = getStddevDaysLate();
+	$diff = strtotime($cn['publishDate']) - strtotime($cn['contractStart']);
+	$days = intval($diff / (60 * 60 * 24));
+	if ($days <= 0) {
+		$value = 0;
+	}
+	else {
+		// +1 demerit for exceeding 45 day requirement
+		$value = (abs($days - $averageDaysLate) / $stddevDaysLate) + ($days < 45 ? 0 : 1);
+	}
+	return Array(
+		"heuristic_value" => $value,
+		"raw_value" => $days,
+		"mean" => $averageDaysLate,
+		"stddev" => $stddevDaysLate
+	);
+}
+$averageDaysLate;
+function getAverageDaysLate()
+{
+	global $averageDaysLate;
+	if (!$averageDaysLate) {
+            getDaysLate();
+	}
+	return $averageDaysLate;
+}
+$stddevDaysLate;
+function getStddevDaysLate()
+{
+	global $stddevDaysLate;
+	if (!$stddevDaysLate) {
+	getDaysLate();
+	}
+	return $stddevDaysLate;
+}
+function getDaysLate() {
+    
+	global $averageDaysLate,$stddevDaysLate;
+    
+    		$query = "select AVG(dateDiff(publishDate,contractStart)), STDDEV(dateDiff(publishDate,contractStart)) from contractnotice";
+		$result = mysql_query($query);
+		$r = mysql_fetch_array($result, MYSQL_BOTH);
+		$averageDaysLate = $r[0];
+                	$stddevDaysLate = $r[1];
+
+}
+?>

--- /dev/null
+++ b/heuristics/heuristics.inc.php
@@ -1,1 +1,39 @@
-
+<?php
+include_once ("../lib/common.inc.php");
+$heuristics = Array();
+//each heuristic adds self to description array
+include ("dateHeuristics.php");
+//include ("historyHeuristics.php");
+//include ("metadataHeuristics.php");
+//include ("valueHeuristics.php");
+function runHeuristic($heuristicName, $cn)
+{
+	// check  if already ran
+	$query = "select count(*) from heuristic_results where heuristic_name = '$heuristicName' and CNID = '{$CN['CNID']}";
+	$result = mysql_query($query);
+	$r = mysql_fetch_array($result);
+	if ($r[0] == 0) {
+		// if not, run now
+		$hresults = call_user_func($heuristicName, $cn);
+		if (!isset($hresults["heuristic_value"]) || !isset($hresults["raw_value"]) || !isset($hresults["mean"]) || !isset($hresults["stddev"])) {
+			print_r($hresults);
+			die("Missing field in heurtistic $heuristicName result");
+		}
+		$query = "insert into heuristic_results values('$heuristicName',
+    '{$hresults["heuristic_value"]}',
+     '{$hresults["raw_value"]}',
+      '{$hresults["mean"]}',
+       '{$hresults["stddev"]}',
+           '{$cn["CNID"]}',
+           NOW(),
+           '{$cn["publishDate"]}',
+           '{$cn["agencyABN"]}',
+           '{$cn["supplierID"]}'
+    )";
+		// save value and cn data via sql
+		$result = mysql_query($query);
+		if ($result) echo "Saved $heuristicName for {$cn["CNID"]} <br>\n";
+		elseif (strpos(mysql_error() , "Duplicate entry") === false) echo $hresults . " failed insert.<br>" . mysql_error() . " <br>  $query <br><br>\n";
+	}
+}
+?>

--- a/heuristics/historyHeuristics.php
+++ b/heuristics/historyHeuristics.php
@@ -1,6 +1,126 @@
-      - unusual for agency/supplier
-        - previous low number of transactions 
-          - zero ie. new agency/supplier is huge score
-    - unusual value for time of year
-        - compare to all other records in last 2 weeks
-        - ie. many large contracts in june so takes more to standout
+<?php
+$heuristics["HISTORY_LOW_TRANSACTIONS_AGENCY"] = Array(
+	"description" => "unusual for agency due to previous low number of transactions "
+);
+function HISTORY_LOW_TRANSACTIONS_AGENCY($cn)
+{
+	$thisAgencyTransactions = getAgencyTransactions($cn['agencyName']);
+	$averageAgencyTransactions = getAverageAgencyTransactions();
+	$stddevAgencyTransactions = getstddevAgencyTransactions();
+	$diff = strtotime($cn['contractEnd']) - strtotime($cn['contractStart']);
+	$days = intval($diff / (60 * 60 * 24));
+	$value = abs($days - $averageAgencyTransactions) / $stddevAgencyTransactions;
+	return Array(
+		"heuristic_value" => $value,
+		"raw_value" => $days,
+		"mean" => $averageAgencyTransactions,
+		"stddev" => $stddevAgencyTransactions
+	);
+}
+$agencyTransactions = Array();
+function getAgencyTransactions($agencyName)
+{
+	global $agencyTransactions;
+	if (!$agencyTransactions[$agencyName]) {
+		$query = 'select count(*) from contractnotice where agencyName = "' . $agencyName . '"';
+		$result = mysql_query($query);
+		$r = mysql_fetch_array($result, MYSQL_BOTH);
+		$agencyTransactions[$agencyName] = $r[0];
+	}
+	return $agencyTransactions[$agencyName];
+}
+$averageAgencyTransactions;
+function getAverageAgencyTransactions()
+{
+	global $averageAgencyTransactions;
+	if (!$averageAgencyTransactions) {
+		getStatsAgencyTransactions();
+	}
+	return $averageAgencyTransactions;
+}
+$stddevAgencyTransactions;
+function getstddevAgencyTransactions()
+{
+	global $stddevAgencyTransactions;
+	if (!$stddevAgencyTransactions) {
+		getStatsAgencyTransactions();
+	}
+	return $stddevAgencyTransactions;
+}
+function getStatsAgencyTransactions()
+{
+	global $averageAgencyTransactions, $stddevAgencyTransactions;
+	$query = "select avg(count), STDDEV(count) from (select count(*) as count
+                from contractnotice group by agencyName) as a;";
+	$result = mysql_query($query);
+	$r = mysql_fetch_array($result, MYSQL_BOTH);
+	$averageAgencyTransactions = $r[0];
+	$stddevAgencyTransactions = $r[1];
+}
+$heuristics["HISTORY_LOW_TRANSACTIONS_SUPPLIER"] = Array(
+	"description" => "unusual for supplier due to previous low number of transactions "
+);
+function HISTORY_LOW_TRANSACTIONS_SUPPLIER($cn)
+{
+	$thisSupplierTransactions = getSupplierTransactions($cn['supplierName'], $cn['supplierABN']);
+	$averageSupplierTransactions = getAverageSupplierTransactions();
+	$stddevSupplierTransactions = getstddevSupplierTransactions();
+	$diff = strtotime($cn['contractEnd']) - strtotime($cn['contractStart']);
+	$days = intval($diff / (60 * 60 * 24));
+	$value = abs($days - $averageSupplierTransactions) / $stddevSupplierTransactions;
+	return Array(
+		"heuristic_value" => $value,
+		"raw_value" => $days,
+		"mean" => $averageSupplierTransactions,
+		"stddev" => $stddevSupplierTransactions
+	);
+}
+$supplierTransactions = Array();
+function getSupplierTransactions($supplierName, $supplierABN)
+{
+	global $supplierTransactions;
+	if ($supplierABN != 0 && $supplierABN != "") {
+		if (!$supplierTransactions[$supplierABN]) {
+			$query = 'select count(*) from contractnotice where supplierABN = "' . $supplierABN . '"';
+			$result = mysql_query($query);
+			$r = mysql_fetch_array($result, MYSQL_BOTH);
+			$supplierTransactions[$supplierABN] = $r[0];
+		}
+		return $supplierTransactions[$supplierABN];
+	}
+	if (!$supplierTransactions[$supplierName]) {
+		$query = 'select count(*) from contractnotice where supplierName = "' . $supplierName . '"';
+		$result = mysql_query($query);
+		$r = mysql_fetch_array($result, MYSQL_BOTH);
+		$supplierTransactions[$supplierName] = $r[0];
+	}
+	return $supplierTransactions[$supplierName];
+}
+$averageSupplierTransactions;
+function getAverageSupplierTransactions()
+{
+	global $averageSupplierTransactions;
+	if (!$averageSupplierTransactions) {
+		getStatsSupplierTransactions();
+	}
+	return $averageSupplierTransactions;
+}
+$stddevSupplierTransactions;
+function getstddevSupplierTransactions()
+{
+	global $stddevSupplierTransactions;
+	if (!$stddevSupplierTransactions) {
+		getStatsSupplierTransactions();
+	}
+	return $stddevSupplierTransactions;
+}
+function getStatsSupplierTransactions()
+{
+	global $averageSupplierTransactions, $stddevSupplierTransactions;
+	$query = 'select avg(count), stddev(count) from (select IF(supplierABN != "",supplierABN,supplierName) as supplierID, count(*) as count from contractnotice group by supplierID) as a;';
+	$result = mysql_query($query);
+	$r = mysql_fetch_array($result, MYSQL_BOTH);
+	$averageSupplierTransactions = $r[0];
+	$stddevSupplierTransactions = $r[1];
+}
+

--- a/heuristics/metadataHeuristics.php
+++ b/heuristics/metadataHeuristics.php
@@ -1,3 +1,59 @@
-      - duplicated description
-        - most duplicated overall, most duplicated per agency/category/supplier etc.
-  
+ <?php
+/* all
+   SELECT description, count(*) as count
+FROM `contractnotice` 
+group by description having count > 1 order by count
+*/
+/*- duplicated description
+ - most duplicated overall, most duplicated per agency/category/supplier etc. */
+$heuristics["METADATA_DUPLICATED_DESCRIPTION"] = Array(
+	"description" => ""
+);
+function METADATA_DUPLICATED_DESCRIPTION($cn)
+{
+	$averageDuplicatedDescriptions = getAverageDuplicatedDescriptions();
+	$stddevDuplicatedDescriptions = getstddevDuplicatedDescriptions();
+	$query = 'select count(*) from contractnotice where description = "' . $agencyName . '"';
+	$result = mysql_query($query);
+	$r = mysql_fetch_array($result, MYSQL_BOTH);
+	$dupeDesc = $r[0];
+        if ($dupeDesc == 1) $value = 0;
+	else $value = abs($dupeDesc - $averageDuplicatedDescriptions) / $stddevDuplicatedDescriptions;
+	return Array(
+		"heuristic_value" => $value,
+		"raw_value" => $dupeDesc,
+		"mean" => $averageDuplicatedDescriptions,
+		"stddev" => $stddevDuplicatedDescriptions
+	);
+}
+$averageDuplicatedDescriptions;
+function getAverageDuplicatedDescriptions()
+{
+	global $averageDuplicatedDescriptions;
+	if (!$averageDuplicatedDescriptions) {
+		getStatsDuplicatedDescriptions();
+	}
+	return $averageDuplicatedDescriptions;
+}
+$stddevDuplicatedDescriptions;
+function getstddevDuplicatedDescriptions()
+{
+	global $stddevDuplicatedDescriptions;
+	if (!$stddevDuplicatedDescriptions) {
+		getStatsDuplicatedDescriptions();
+	}
+	return $stddevDuplicatedDescriptions;
+}
+function getStatsDuplicatedDescriptions()
+{
+	$query = "select avg(count),STDDEV(count) from (
+        SELECT description, count(*) as count
+FROM `contractnotice` 
+group by description having count > 1 
+        ) as a;";
+	$result = mysql_query($query);
+	$r = mysql_fetch_array($result, MYSQL_BOTH);
+	$averageDuplicatedDescriptions = $r[0];
+	$stddevDuplicatedDescriptions = $r[1];
+}
+

file:a/heuristics/readme.txt (deleted)
--- a/heuristics/readme.txt
+++ /dev/null
@@ -1,11 +1,1 @@
-heuristicResults
-heuristic = BY_DATE...
-value = 0.0
-cnPubDate = 
-lastUpdated = 1/1/1970
-contractNotice =
-agency = 
-supplier =
 
-work out total value by summing value
-

--- a/heuristics/runHeuristics.php
+++ b/heuristics/runHeuristics.php
@@ -1,6 +1,32 @@
-<?
-if agency
-if supplier
-if CN
+<?php
+include_once("heuristics.inc.php");
+$query = "SELECT *, agency.abn as agencyABN, IF(supplierABN != '',supplierABN,supplierName) as supplierID
+FROM contractnotice JOIN agency ON contractnotice.agencyName=agency.agencyName";
+
+$query = "SELECT *, agency.abn as agencyABN, IF(supplierABN != '',supplierABN,supplierName) as supplierID
+FROM contractnotice JOIN agency ON contractnotice.agencyName=agency.agencyName
+WHERE  DATE(importDate) = (select * from (SELECT DATE(importDate) 
+FROM contractnotice ORDER BY importDate DESC limit 1) alias)";
+$result = mysql_query($query);
+if (!$result) echo mysql_error().$query;
+while ($cn = mysql_fetch_array($result, MYSQL_BOTH)) {
+	//get each new CN from latest update
+	foreach ($heuristics as $heuristic => $description) {
+		// run all heuristics
+		runHeuristic($heuristic, $cn);
+	}
+        flush();
+}
+/*foreach agency
+
+aggregate agency metrics
+
+foreach supplier
+
+aggreate supplier metrics
+
+foreach CN
+
+aggregate CN metrics */
 ?>
 

--- a/heuristics/valueHeuristics.php
+++ b/heuristics/valueHeuristics.php
@@ -1,10 +1,58 @@
+<?php 
+      /*  - large contract value
+          - standard dev from mean/median
+          - percent of total contracts for supplier/agency*/
+   $heuristics["VALUE_LARGE_CONTRACT_OVERALL"] = Array(
+	"description" => "unusual value for time of year");
+function METADATA_DUPLICATED_DESCRIPTION($cn)
+{
+    	$averageContractPeriod = getAverageContractPeriod();
+	$diff = strtotime($cn['contractStart']) - strtotime($cn['publishDate']);
+	$days = intval($diff / 24);
+	return ($days > 45 ? 1 : 0);
+}
 
-        - large contract value
-          - chi-square test for outliers / standard dev from mean/median
-          - percent of total contracts for supplier/agency
+      /*  - peculiar value
+        - Just under 80k, amplified if other contracts with same supplier are just under
+      */
+         $heuristics["VALUE_NEAR_THRESHOLD"] = Array(
+	"description" => "unusual value for time of year");
+      /*
+    - unusual variation amount - absolute value; large reductions as well as large increases
+      */
+           $heuristics["VALUE_LARGE_VARIATION"] = Array(
+	"description" => "unusual value for time of year");
+function METADATA_DUPLICATED_DESCRIPTION($cn)
+{
+    	$averageContractPeriod = getAverageContractPeriod();
+	$diff = strtotime($cn['contractStart']) - strtotime($cn['publishDate']);
+	$days = intval($diff / 24);
+	return ($days > 45 ? 1 : 0);
+}
 
-
-        - peculiar value
-        - Just under 80k, amplified if other contracts with same supplier are just under
-    - unusual variation amount
-        - absolute value; large reductions as well as large increases
+/*   - unusual value for time of year
+        - compare to all other records in last 2 weeks
+        - ie. many large contracts in june so takes more to standout*/
+$heuristics["VALUE_HIGH_FOR_MONTH"] = Array(
+	"description" => "unusual value for time of year"
+);
+function VALUE_HIGH_FOR_MONTH($cn, $monthAsInt)
+{
+	$averageContractPeriod = getAverageContractPeriod();
+	$diff = strtotime($cn['contractStart']) - strtotime($cn['publishDate']);
+	$days = intval($diff / 24);
+	return ($days > 45 ? 1 : 0);
+}
+$monthlyValueAverage = Array();
+function getAgencyTransactions($agencyName)
+{
+	global $agencyTransactions;
+	if (!$agencyTransactions[$agencyName]) {
+		$query = 'select count(*) from contractnotice where agencyName = "' . $agencyName . '"';
+		$result = mysql_query($query);
+		$r = mysql_fetch_array($result, MYSQL_BOTH);
+		$agencyTransactions[$agencyName] = $r[0];
+	}
+	return $agencyTransactions[$agencyName];
+}
+?>

--- /dev/null
+++ b/heuristics/viewHeuristicsColormap.php
@@ -1,1 +1,60 @@
+<?php    
+  include_once("../lib/common.inc.php");
+  echo '<style>
+  div {
+  padding: 5px;
+  display: inline-block;
+  }
+  </style>';
+// http://www.herethere.net/~samson/php/color_gradient/color_gradient_generator.php.txt
+// return the interpolated value between pBegin and pEnd
+function interpolate($pBegin, $pEnd, $pStep, $pMax)
+{
+	if ($pBegin < $pEnd) {
+		return (($pEnd - $pBegin) * ($pStep / $pMax)) + $pBegin;
+	}
+	else {
+		return (($pBegin - $pEnd) * (1 - ($pStep / $pMax))) + $pEnd;
+	}
+}
+function Gradient($HexFrom, $HexTo, $ColorSteps)
+{
+	$theColorBegin = hexdec($HexFrom);
+	$theColorEnd = hexdec($HexTo);
+	$theNumSteps = intval($ColorSteps);
+	$theR0 = ($theColorBegin & 0xff0000) >> 16;
+	$theG0 = ($theColorBegin & 0x00ff00) >> 8;
+	$theB0 = ($theColorBegin & 0x0000ff) >> 0;
+	$theR1 = ($theColorEnd & 0xff0000) >> 16;
+	$theG1 = ($theColorEnd & 0x00ff00) >> 8;
+	$theB1 = ($theColorEnd & 0x0000ff) >> 0;
+	$GradientColors = array();
+	// generate gradient swathe now
+	for ($i = 0; $i <= $theNumSteps; $i++) {
+		$theR = interpolate($theR0, $theR1, $i, $theNumSteps);
+		$theG = interpolate($theG0, $theG1, $i, $theNumSteps);
+		$theB = interpolate($theB0, $theB1, $i, $theNumSteps);
+		$theVal = ((($theR << 8) | $theG) << 8) | $theB;
+		$GradientColors[] = sprintf("%06X", $theVal);
+	}
+	return $GradientColors;
+}
+$Gradients = Gradient("66FF00" , "FF0000" , 10); 
+  
+$query = "select max(sum) from (SELECT sum(heuristic_value) 
+as sum FROM heuristic_results group by CNID) as a";
+$result = mysql_query($query);
+$r = mysql_fetch_array($result, MYSQL_BOTH);
+$maxVal = $r[0];
+  
+$query = "SELECT sum(heuristic_value) as sum, CNID
+FROM `heuristic_results` group by CNID order by sum DESC LIMIT 300";
+$result = mysql_query($query);
+if (!$result) echo mysql_error().$query;
+while ($r = mysql_fetch_array($result, MYSQL_BOTH)) {
+    echo '<div style="background: #'.$Gradients[floor(($r['sum']/$maxVal) * 10)].';">';
+    echo '<a title="'.$r['sum'].'" href="../displayContract.php?CNID='.$r['CNID'].'">X</a>';
+    echo "</div>";
+}
 
+?>

--- /dev/null
+++ b/heuristics/viewHeuristicsDistribution.php
@@ -1,1 +1,82 @@
+<?php    
 
+/*// most interesting
+SELECT sum(heuristic_value) as sum, CNID
+FROM `heuristic_results` group by CNID order by sum DESC limit 30
+
+// spread of values
+select floor(sum) as val,count(*) from (SELECT sum(heuristic_value) 
+as sum FROM heuristic_results group by CNID) as a group by val*/
+
+ /* CAT:Spline chart */ 
+
+ /* pChart library inclusions */ 
+ include("../lib/pChart2.1.0/class/pData.class.php"); 
+ include("../lib/pChart2.1.0/class/pDraw.class.php"); 
+ include("../lib/pChart2.1.0/class/pImage.class.php"); 
+
+$series = Array();
+
+  include_once("../lib/common.inc.php");
+$query = "select heuristic_name, floor(heuristic_value) as val,count(*) from heuristic_results group by heuristic_name, val";
+$result = mysql_query($query);
+if (!$result) echo mysql_error().$query;
+while ($r = mysql_fetch_array($result, MYSQL_BOTH)) {
+    $series[$r["heuristic_name"]][$r["val"]] = $r[2];
+}
+
+ /* Create and populate the pData object */ 
+ $MyData = new pData();
+ $labels = Array();
+ foreach ($series as $value) {
+    $labels = $labels+array_keys($value);
+ }
+ $labels =  Array(0,1,2,3,4,5);
+foreach ($series as $seriesName => $seriesEntry) {
+    $data;
+ foreach ($labels as $label) {
+    $data[$label] = ($seriesEntry[$label] ? $seriesEntry[$label] : 0);
+ }
+  
+$MyData->addPoints($data,$seriesName); 
+ }
+ $MyData->setAxisName(0,"# of records"); 
+ $MyData->addPoints($labels,"Labels"); 
+ $MyData->setSerieDescription("Labels","Bins"); 
+ $MyData->setAbscissa("Labels"); 
+
+ /* Create the pChart object */ 
+ $myPicture = new pImage(700,230,$MyData); 
+
+ /* Turn of Antialiasing */ 
+ $myPicture->Antialias = FALSE; 
+
+ /* Add a border to the picture */ 
+ $myPicture->drawRectangle(0,0,699,229,array("R"=>0,"G"=>0,"B"=>0)); 
+  
+ /* Write the chart title */  
+ $myPicture->setFontProperties(array("FontName"=>"../lib/pChart2.1.0/fonts/Forgotte.ttf","FontSize"=>11)); 
+ $myPicture->drawText(150,35,"Record distribution",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); 
+
+ /* Set the default font */ 
+ $myPicture->setFontProperties(array("FontName"=>"../lib/pChart2.1.0/fonts/pf_arma_five.ttf","FontSize"=>6)); 
+
+ /* Define the chart area */ 
+ $myPicture->setGraphArea(60,40,650,200); 
+
+ /* Draw the scale */ 
+ $scaleSettings = array("XMargin"=>10,"YMargin"=>0,"Floating"=>TRUE,"GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE);
+ $myPicture->drawScale($scaleSettings); 
+
+ /* Turn on Antialiasing */ 
+ $myPicture->Antialias = TRUE; 
+
+ /* Draw the line chart */ 
+ $myPicture->drawSplineChart(); 
+
+ /* Write the chart legend */ 
+ $myPicture->drawLegend(540,20,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_VERTICAL)); 
+
+ /* Render the picture (choose the best way) */ 
+ $myPicture->autoOutput("pictures/example.drawSplineChart.simple.png"); 
+?>

--- /dev/null
+++ b/lib/pChart2.1.0/GPLv3.txt
@@ -1,1 +1,676 @@
+                    GNU GENERAL PUBLIC LICENSE

+                       Version 3, 29 June 2007

+

+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>

+ Everyone is permitted to copy and distribute verbatim copies

+ of this license document, but changing it is not allowed.

+

+                            Preamble

+

+  The GNU General Public License is a free, copyleft license for

+software and other kinds of works.

+

+  The licenses for most software and other practical works are designed

+to take away your freedom to share and change the works.  By contrast,

+the GNU General Public License is intended to guarantee your freedom to

+share and change all versions of a program--to make sure it remains free

+software for all its users.  We, the Free Software Foundation, use the

+GNU General Public License for most of our software; it applies also to

+any other work released this way by its authors.  You can apply it to

+your programs, too.

+

+  When we speak of free software, we are referring to freedom, not

+price.  Our General Public Licenses are designed to make sure that you

+have the freedom to distribute copies of free software (and charge for

+them if you wish), that you receive source code or can get it if you

+want it, that you can change the software or use pieces of it in new

+free programs, and that you know you can do these things.

+

+  To protect your rights, we need to prevent others from denying you

+these rights or asking you to surrender the rights.  Therefore, you have

+certain responsibilities if you distribute copies of the software, or if

+you modify it: responsibilities to respect the freedom of others.

+

+  For example, if you distribute copies of such a program, whether

+gratis or for a fee, you must pass on to the recipients the same

+freedoms that you received.  You must make sure that they, too, receive

+or can get the source code.  And you must show them these terms so they

+know their rights.

+

+  Developers that use the GNU GPL protect your rights with two steps:

+(1) assert copyright on the software, and (2) offer you this License

+giving you legal permission to copy, distribute and/or modify it.

+

+  For the developers' and authors' protection, the GPL clearly explains

+that there is no warranty for this free software.  For both users' and

+authors' sake, the GPL requires that modified versions be marked as

+changed, so that their problems will not be attributed erroneously to

+authors of previous versions.

+

+  Some devices are designed to deny users access to install or run

+modified versions of the software inside them, although the manufacturer

+can do so.  This is fundamentally incompatible with the aim of

+protecting users' freedom to change the software.  The systematic

+pattern of such abuse occurs in the area of products for individuals to

+use, which is precisely where it is most unacceptable.  Therefore, we

+have designed this version of the GPL to prohibit the practice for those

+products.  If such problems arise substantially in other domains, we

+stand ready to extend this provision to those domains in future versions

+of the GPL, as needed to protect the freedom of users.

+

+  Finally, every program is threatened constantly by software patents.

+States should not allow patents to restrict development and use of

+software on general-purpose computers, but in those that do, we wish to

+avoid the special danger that patents applied to a free program could

+make it effectively proprietary.  To prevent this, the GPL assures that

+patents cannot be used to render the program non-free.

+

+  The precise terms and conditions for copying, distribution and

+modification follow.

+

+                       TERMS AND CONDITIONS

+

+  0. Definitions.

+

+  "This License" refers to version 3 of the GNU General Public License.

+

+  "Copyright" also means copyright-like laws that apply to other kinds of

+works, such as semiconductor masks.

+

+  "The Program" refers to any copyrightable work licensed under this

+License.  Each licensee is addressed as "you".  "Licensees" and

+"recipients" may be individuals or organizations.

+

+  To "modify" a work means to copy from or adapt all or part of the work

+in a fashion requiring copyright permission, other than the making of an

+exact copy.  The resulting work is c