/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php

require_once($ADEI_ROOTDIR . "/classes/jpgraph.php");

abstract class BASEHistogramView extends VIEW {
 function __construct(REQUEST $req  = NULL, $options) {
    parent::__construct($req, $options);

    $this->num_bins = $this->GetOption('bins', array(0));
    $this->min_width = $this->GetOption('min_width', 300);
    $this->min_height = $this->GetOption('min_height', 300);
 }

 function GetHistOptions() {
    $bins = array();
    foreach ($this->num_bins as $bin) {
	array_push($bins, array("value" => $bin, "label" => ($bin?$bin:"Auto")));
    }

    $checkboxNorm = array("label" => _("Normalize"), "id" => "hist_norm");
    if ($this->req->GetProp("view_hist_norm", 0)) $checkboxNorm["checked"] = "checked";

    $checkboxGFit = array("label" => _("Gaussian Fit"), "id" => "hist_fit");
    if ($this->req->GetProp("view_hist_fit", 0)) $checkboxGFit["checked"] = "checked";

    return array(
        array("select" => array("id" => "bins" , "label" => _("Bins"),  "options" => $bins)),
        array("xml"=>"<br/>"),
        array("checkbox" => $checkboxNorm),
        array("checkbox" => $checkboxGFit),
    );
 }

 function GetHistView(&$x) {
    global $TMP_PATH;

    if (!$x)
        throw new ADEIException(translate("No data found"));

    $tmp_file = ADEI::GetTmpFile();

    $width = $this->width;
    $height = $this->height;

    $bins = $this->req->GetProp("view_bins", 0);
    if (!$bins) $bins = ceil(sqrt(sizeof($x)));
	
    $norm = $this->req->GetProp("view_hist_norm", 0);
    $fit = $this->req->GetProp("view_hist_fit", 0);

    $min = min($x);
    $max = max($x);
    $step = ($max - $min) / $bins;
    
    $coef = $norm?(1/($step * sizeof($x))):1;

    $h = array_fill(0, $bins, 0); 
    foreach ($x as $val) {
	    $idx = ($val - $min)/$step;
	    if ($idx == $bins) $idx--;
	    $h[$idx] += $coef;
    }

    $t = array();
    for($i = 0 ; $i < $bins ; $i++)
        array_push($t, sprintf("%3.1e", $min + $i*$step));

    $graph = new Graph($width,$height);
    $graph->SetTickDensity(TICKD_SPARSE, TICKD_SPARSE);
    $graph->img->SetMargin(55,5,10,20);

    $graph->SetScale("textlin");
    $graph->xaxis->SetPos("min");
    $graph->yaxis->SetPos("min");
    $graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
    $graph->yaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
    $graph->xaxis->title->SetFont(FF_ARIAL,FS_BOLD);
    $graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD);

    if($bins > 8) $graph->xaxis->SetTextLabelInterval(ceil(($bins / 6)));
    $graph->xaxis->SetTickLabels($t);

    $bplot = new BarPlot($h);

    $bplot->SetWidth(1);
    $graph->Add($bplot);

    $graph->yaxis->scale->SetGrace(14);

    $mean = array_sum($x)/sizeof($x);
    $stddev = stats_standard_deviation($x);
    $var = stats_variance($x);
    $sigma = sqrt($var);
    $re = 100 * $sigma/$mean;
    
    sort($x);
    if (sizeof($x) % 2) 
	$median = $x[(sizeof($x) - 1)/2];
    else
	$median = ($x[sizeof($x)/2 - 1] + $x[sizeof($x)/2])/2;

	    // Gaussian fitting
    if ($fit) {
        $ydata = array();
        $xdata = array();
        if ($norm) $coef = (1/(sqrt(2 * pi() * $var)));
        else $coef = ((sizeof($x)*$step)/(sqrt(2 * pi() * $var)));
        
        $xi2 = 0;
        for($i = 0; $i <= $bins; $i++) {
    	    $offset = $i * $step;
    	    $y = $coef * exp(-pow($min + $offset - $mean, 2)/(2*$var));
            array_push($xdata, $i);
    	    array_push($ydata, $y);
            $xi2 += (pow($y - $h[$i], 2)) / $y;
        }
	$xi2 /= $bins;

        $lineplot = new LinePlot($ydata , $xdata);
        $graph->Add($lineplot);
    }

    $char_sigma = SymChar::Get('sigma', false);

    $graph->Stroke("$TMP_PATH/$tmp_file");

    if ($this->object) {
        $res = array(
            array("img" => array("id" => $tmp_file)),
            array("info" => array(
        	    array("title"=>_("Bins"), "value" => $bins),
        	    array("title"=>_("First Bin"), "value" =>  $min),
		    array("title"=>_("Last Bin"), "value" =>  $min + $bins * $step),
        	    array("title"=>_("Mean"), "value" => $mean),
        	    array("title"=>_("Median"), "value" => $median),
        	    array("title"=>_("StdDev"), "value" => $stddev),
        	    array("title"=>_("Sigma"), "value" => $sigma),
        	    array("title"=>_("RE"), "value" => ($re . "%")),
        	))
	    );
        if ($fit) {
		array_push($res[1]["info"], array("title"=>_("xi2"), "value" => $xi2));
        }
        return $res;
    } else {
        return array(
		"img" => array("id" => $tmp_file)
        );
    }

    return $res;
 }
}

?>