Dr. Arne Jachens

Arnes Skriptbibliothek

sortIntoBins

Wird verwendet von:

Keine Erläuterungen gefunden.

<?php
  /*
To handle large amounts of data it is possible, to read a data file
by some external program line by line, select some subset ($x,$y) of 
this data and sort it into bins step by step using this function.
After all data is sorted into bins call this function again and set 
$final=true to gain the final result - and to save and plot the data
if desired.
Dr. A. Jachens
2011-03-10
   */
$BinCount=array();
$mean=array();
$square=array();
$distribution['x']=array();
$distribution['y']=array();
#$distribution['x'][-1]=0;
#$distribution['y'][-1]=0;

function sortIntoBins($x,$y,$xMin,$xMax,$dimX,$final){
  global $BinCount,$mean,$square,$distribution;
  $save=true;
  $plot=true;
  $showData=true;
  $binWidth=($xMax-$xMin)/$dimX;
  if ($final){
    /* calculate and save result */
    if ($BinCount[$dimX]>0){
      echo $BinCount[$dimX]." Werte waren groesser als das Sortierintervall!\n";
    }
    for($i=0;$i<$dimX;$i++){
      if ($BinCount[$i]>1){
	$variance[$i]=( $square[$i] - pow($mean[$i],2)/$BinCount[$i] )/($BinCount[$i]-1);
      }else{
	$variance[$i]=0;
      }
      if ($BinCount[$i]>0){
	$std[$i]= sqrt($variance[$i]);
	$mean[$i]=$mean[$i]/$BinCount[$i];
      }
     #i
      
    $NoOfSamples=0;
    for($i=0;$i<$dimX;$i++){
      $NoOfSamples=$NoOfSamples+$BinCount[$i];
    }
    echo "No of samples= ".$NoOfSamples."\n";
    if ($save){$fid = fopen("MeanStd.dat","w");}
    for($i=0;$i<$dimX;$i++){
      $x=$xMin+($i+0.5)*$binWidth;
      $probDens=$BinCount[$i]/$NoOfSamples;
      #$probDens=$probDens/$binWidth;
      if ($save){
	$string  = sprintf("%6.5e",$x)."\t";
	$string .= sprintf("%6.5e",$mean[$i])."\t";
	$string .= sprintf("%6.5e",$std[$i])."\t";
	$string .= sprintf("%6.5e",$probDens)."\n";
	if ($probDens>0){fputs($fid,$string);}
      } #fi
     #i
    if ($save){fclose($fid);}
    if ($showData){
      $fid=fopen("distribution.dat","w");
      for($t=0;$t<count($distribution['x']);$t++){
	$string=sprintf("%4.3e",$distribution['x'][$t]);
	$string=sprintf("%s\t%4.3e\n",$string,$distribution['y'][$t]);
	fputs($fid,$string);
      } #t
      fclose($fid);
  }
    if ($plot){
      $plot =" #set term png\n";
      $plot.=" #set output 'MeanStd.png'\n";
      $plot.="set grid\n";
      $plot.="set y2range [0:1]\n";
      $plot.="set y2label 'probability'\n";
      $plot.="set y2tics border\n";
      if ($showData){
	$plot.="plot 'distribution.dat' lc rgb '#008000' notitle, \\\n";
	$plot.="'MeanStd.dat' u 1:2:3 lc rgb 'red' with errorbars ti 'mean', \\\n";
      }else{
	$plot.="plot 'MeanStd.dat' u 1:2:3 with errorbars ti 'mean', \\\n";
      }
      $plot.="'MeanStd.dat' u 1:4 lc rgb 'gray' axes x1y2 with boxes ti 'probability'\n";
      $plot.="pause -1";
      $fid = fopen("MeanStd.plot","w");
      fputs($fid,$plot);
      fclose($fid);
    } #fi
    return array($BinCount,$mean,$std);
  }else{
    /* sort into bins */
    if ($showData){
      #array_push($distribution['x'],$x);
      #array_push($distribution['y'],$y);
      $tmp=count($distribution['x']);
      $distribution['x'][$tmp]=$x;
      $distribution['y'][$tmp]=$y;
    }
    for($i=0;$i<$dimX;$i++){
      $xLow =$xMin+ $i   *$binWidth;
      $xHigh=$xMin+($i+1)*$binWidth;
      if ($xLow<=$x AND $x<$xHigh){
	$BinCount[$i]=$BinCount[$i]+1;
	$mean[$i]  =$mean[$i]  +$y;
	$square[$i]=$square[$i]+pow($y,2);
	break;
      }
      if ($i==$dimX-1){
	$BinCount[$dimX]=$BinCount[$dimX]+1;
      }
     #i
    return true;
  } #fi
}
?>

1fraktal.php
2VereinsverwaltungKartei.php
3VereinsverwaltungKarteikarte.php
4VereinsverwaltungKonto.php
5VereinsverwaltungLastschriften.php
6VereinsverwaltungMenue.php
7whichLengthDoWeHaveInXfigFile.php
8XML_extractAttribute.php
9XML_extractElement.php
10XML_extractTag.php
11XML_findAllElements.php
12XML_readFile.php
13writeNamedData.php
14menu.php
15content.php
16cms.php
17indexCms.php
18areaOfPolygon.php
19areaOfTriangle.php
20calculateAreasOfPolygonsInXfig.php
21calculateMeanStd.php
22checkChanges.php
23closeFigures.php
24db.php
25distanceOfPoints.php
26dtaus.php
27enumerateFiles.php
28fileGrep.php
29fitData.php
30fotoalbum.php
31genAlbum.php
32genDTAUS.php
33genFotoalbum.php
34genThumbnails.php
35globals.php
36hex2dec.php
37LaTeXbrief.php
38norm.php
39pdf.php
40plotResult.php
41pointsOfPolygon.php
42polynominalFit.php
43readData.php
44readDymolaOuput.php
45readFilenames.php
46readFromStdIn.php
47readNamedData.php
48readXmlFile.php
49searchMedian.php
50sortIEindex.php
51sortIntoBins.php
52sortPDF.php
53synchronise.php
54synchroniseDirectories.php
55Vereinsverwaltung.php
56writeData.php

 

gnuplot

gnuplot is used to visualise the results - via script you can do infinite diagrams in 1 second.
Run 'test' in gnuplot to get an owerview of the available line- and symbolstyles.