/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk

« back to all changes in this revision

Viewing changes to classes/views/basehistogramview.php

  • Committer: Suren A. Chilingaryan
  • Date: 2018-07-15 01:53:01 UTC
  • Revision ID: csa@suren.me-20180715015301-s17qbq19snb3wlr5
Adding generalized data functions to VIEW

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
require_once($ADEI_ROOTDIR . "/classes/jpgraph.php");
 
4
 
 
5
abstract class BASEHistogramView extends VIEW {
 
6
 function __construct(REQUEST $req  = NULL, $options) {
 
7
    parent::__construct($req, $options);
 
8
 
 
9
    $this->num_bins = $this->GetOption('bins', array(0));
 
10
    $this->min_width = $this->GetOption('min_width', 300);
 
11
    $this->min_height = $this->GetOption('min_height', 300);
 
12
 }
 
13
 
 
14
 function GetHistOptions() {
 
15
    $bins = array();
 
16
    foreach ($this->num_bins as $bin) {
 
17
        array_push($bins, array("value" => $bin, "label" => ($bin?$bin:"Auto")));
 
18
    }
 
19
 
 
20
    $checkboxNorm = array("label" => _("Normalize"), "id" => "hist_norm");
 
21
    if ($this->req->GetProp("view_hist_norm", 0)) $checkboxNorm["checked"] = "checked";
 
22
 
 
23
    $checkboxGFit = array("label" => _("Gaussian Fit"), "id" => "hist_fit");
 
24
    if ($this->req->GetProp("view_hist_fit", 0)) $checkboxGFit["checked"] = "checked";
 
25
 
 
26
    return array(
 
27
        array("select" => array("id" => "bins" , "label" => _("Bins"),  "options" => $bins)),
 
28
        array("xml"=>"<br/>"),
 
29
        array("checkbox" => $checkboxNorm),
 
30
        array("checkbox" => $checkboxGFit),
 
31
    );
 
32
 }
 
33
 
 
34
 function GetHistView(&$x) {
 
35
    global $TMP_PATH;
 
36
 
 
37
    if (!$x)
 
38
        throw new ADEIException(translate("No data found"));
 
39
 
 
40
    $tmp_file = ADEI::GetTmpFile();
 
41
 
 
42
    $width = $this->width;
 
43
    $height = $this->height;
 
44
 
 
45
    $bins = $this->req->GetProp("view_bins", 0);
 
46
    if (!$bins) $bins = ceil(sqrt(sizeof($x)));
 
47
        
 
48
    $norm = $this->req->GetProp("view_hist_norm", 0);
 
49
    $fit = $this->req->GetProp("view_hist_fit", 0);
 
50
 
 
51
    $min = min($x);
 
52
    $max = max($x);
 
53
    $step = ($max - $min) / $bins;
 
54
    
 
55
    $coef = $norm?(1/($step * sizeof($x))):1;
 
56
 
 
57
    $h = array_fill(0, $bins, 0); 
 
58
    foreach ($x as $val) {
 
59
            $idx = ($val - $min)/$step;
 
60
            if ($idx == $bins) $idx--;
 
61
            $h[$idx] += $coef;
 
62
    }
 
63
 
 
64
    $t = array();
 
65
    for($i = 0 ; $i < $bins ; $i++)
 
66
        array_push($t, sprintf("%3.1e", $min + $i*$step));
 
67
 
 
68
    $graph = new Graph($width,$height);
 
69
    $graph->SetTickDensity(TICKD_SPARSE, TICKD_SPARSE);
 
70
    $graph->img->SetMargin(55,5,10,20);
 
71
 
 
72
    $graph->SetScale("textlin");
 
73
    $graph->xaxis->SetPos("min");
 
74
    $graph->yaxis->SetPos("min");
 
75
    $graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
 
76
    $graph->yaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
 
77
    $graph->xaxis->title->SetFont(FF_ARIAL,FS_BOLD);
 
78
    $graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD);
 
79
 
 
80
    if($bins > 8) $graph->xaxis->SetTextLabelInterval(ceil(($bins / 6)));
 
81
    $graph->xaxis->SetTickLabels($t);
 
82
 
 
83
    $bplot = new BarPlot($h);
 
84
 
 
85
    $bplot->SetWidth(1);
 
86
    $graph->Add($bplot);
 
87
 
 
88
    $graph->yaxis->scale->SetGrace(14);
 
89
 
 
90
    $mean = array_sum($x)/sizeof($x);
 
91
    $stddev = stats_standard_deviation($x);
 
92
    $var = stats_variance($x);
 
93
    $sigma = sqrt($var);
 
94
    $re = 100 * $sigma/$mean;
 
95
    
 
96
    sort($x);
 
97
    if (sizeof($x) % 2) 
 
98
        $median = $x[(sizeof($x) - 1)/2];
 
99
    else
 
100
        $median = ($x[sizeof($x)/2 - 1] + $x[sizeof($x)/2])/2;
 
101
 
 
102
            // Gaussian fitting
 
103
    if ($fit) {
 
104
        $ydata = array();
 
105
        $xdata = array();
 
106
        if ($norm) $coef = (1/(sqrt(2 * pi() * $var)));
 
107
        else $coef = ((sizeof($x)*$step)/(sqrt(2 * pi() * $var)));
 
108
        
 
109
        $xi2 = 0;
 
110
        for($i = 0; $i <= $bins; $i++) {
 
111
            $offset = $i * $step;
 
112
            $y = $coef * exp(-pow($min + $offset - $mean, 2)/(2*$var));
 
113
            array_push($xdata, $i);
 
114
            array_push($ydata, $y);
 
115
            $xi2 += (pow($y - $h[$i], 2)) / $y;
 
116
        }
 
117
        $xi2 /= $bins;
 
118
 
 
119
        $lineplot = new LinePlot($ydata , $xdata);
 
120
        $graph->Add($lineplot);
 
121
    }
 
122
 
 
123
    $char_sigma = SymChar::Get('sigma', false);
 
124
 
 
125
    $graph->Stroke("$TMP_PATH/$tmp_file");
 
126
 
 
127
    if ($this->object) {
 
128
        $res = array(
 
129
            array("img" => array("id" => $tmp_file)),
 
130
            array("info" => array(
 
131
                    array("title"=>_("Bins"), "value" => $bins),
 
132
                    array("title"=>_("First Bin"), "value" =>  $min),
 
133
                    array("title"=>_("Last Bin"), "value" =>  $min + $bins * $step),
 
134
                    array("title"=>_("Mean"), "value" => $mean),
 
135
                    array("title"=>_("Median"), "value" => $median),
 
136
                    array("title"=>_("StdDev"), "value" => $stddev),
 
137
                    array("title"=>_("Sigma"), "value" => $sigma),
 
138
                    array("title"=>_("RE"), "value" => ($re . "%")),
 
139
                ))
 
140
            );
 
141
        if ($fit) {
 
142
                array_push($res[1]["info"], array("title"=>_("xi2"), "value" => $xi2));
 
143
        }
 
144
        return $res;
 
145
    } else {
 
146
        return array(
 
147
                "img" => array("id" => $tmp_file)
 
148
        );
 
149
    }
 
150
 
 
151
    return $res;
 
152
 }
 
153
}
 
154
 
 
155
?>
 
 
b'\\ No newline at end of file'