/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/histogramview.php

  • Committer: Suren A. Chilingaryan
  • Date: 2012-10-31 20:11:03 UTC
  • Revision ID: csa@dside.dyndns.org-20121031201103-gwzrqhwta2i3i6tz
Histogram view (based on the code by Hovhannes)

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
class histogramview extends VIEW {
 
6
    var $title = "Histogram";
 
7
 
 
8
    function __construct(REQUEST $req  = NULL, $opts) {
 
9
        //$this->shelldisplay = false;
 
10
        parent::__construct($req, $opts);
 
11
        $this->object = $this->req->GetProp("view_object", false);
 
12
 
 
13
        if ($this->object)
 
14
            $this->max_points = $this->GetOption('max_points', 500);
 
15
        else
 
16
            $this->max_points = $this->GetOption('max_points', 5000);
 
17
 
 
18
        $this->min_width = $this->GetOption('min_width', 300);
 
19
        $this->min_height = $this->GetOption('min_height', 300);
 
20
 
 
21
        $this->num_bins = $this->GetOption('bins', array(0));
 
22
    }
 
23
 
 
24
    function GetOption($opt, $default = false) {
 
25
        if (isset($this->options[$opt])) return $this->options[$opt];
 
26
        return $default;
 
27
    }
 
28
 
 
29
    function GetOptions() {
 
30
        $req = $this->req->CreateGroupRequest();
 
31
        $x = $req->GetProp("view_x", false);
 
32
        if ($x) list($x_gid, $x_id) = explode(":", $x);
 
33
        else list($x_gid, $x_id) = array(0, 0);
 
34
 
 
35
        $rdr = $req->CreateReader();
 
36
        $group = $rdr->CreateGroup();
 
37
        $caches = $rdr->CreateCacheSet($group, $mask);
 
38
 
 
39
        $gid = 0;
 
40
        $result = array();
 
41
        foreach ($caches as $key => $cachewrap) {
 
42
            array_push($result, array("label" => $cachewrap->GetGroupTitle(), "disabled" => 1));
 
43
 
 
44
            $id = 0;
 
45
            $list = $cachewrap->GetItemList();
 
46
            foreach ($list as $id => $info) {
 
47
                $title = $info['name'];
 
48
                array_push($result, array("value" => "$gid:$id", "label" => "  $title"));
 
49
                $id++;
 
50
            }
 
51
            $gid++;
 
52
        }
 
53
        
 
54
        $bins = array();
 
55
        foreach ($this->num_bins as $bin) {
 
56
            array_push($bins, array("value" => $bin, "label" => ($bin?$bin:"Auto")));
 
57
        }
 
58
 
 
59
        $checkboxNorm = array("label" => _("Normalize"), "id" => "hist_norm");
 
60
        if ($req->GetProp("view_hist_norm", 0)) $checkboxNorm["checked"] = "checked";
 
61
 
 
62
        $checkboxGFit = array("label" => _("Gaussian Fit"), "id" => "hist_fit");
 
63
        if ($req->GetProp("view_hist_fit", 0)) $checkboxGFit["checked"] = "checked";
 
64
 
 
65
        return array(
 
66
                   array("select" => array("id" => "x", "label" => _("x"), "options" => $result)),
 
67
                   array("select" => array("id" => "bins" , "label" => _("Bins"),  "options" => $bins)),
 
68
                   array("checkbox" => $checkboxNorm),
 
69
                   array("checkbox" => $checkboxGFit)
 
70
        );
 
71
    }
 
72
 
 
73
    function GetView() {
 
74
        global $TMP_PATH;
 
75
 
 
76
        $req = $this->req->CreateDataRequest();
 
77
        $x = $req->GetProp("view_x", false);
 
78
 
 
79
        if (!$x) throw new ADEIException(translate("Parameter view_x is not set"));
 
80
 
 
81
        list($x_gid, $x_id) = explode(":", $x);
 
82
 
 
83
        if ($this->object) {
 
84
            $width = $req->GetProp($this->object . "_width", $this->min_width + 20) - 20;
 
85
            if ($width < $this->min_width) $width = $this->min_width;
 
86
            $height = $width - 40;//$req->GetProp($this->object . "_height", $this->min_height);
 
87
        } else {
 
88
            $width = $req->GetProp("page_width", $this->min_width + 5) - 5;
 
89
            $height = $req->GetProp("page_height", $this->min_height);
 
90
            if ($width < $this->min_width) $width = $this->min_width;
 
91
            if ($height < $this->min_height) $height = $this->min_height;
 
92
        }
 
93
 
 
94
 
 
95
        $rdr = $req->CreateReader();
 
96
        $group = $rdr->CreateGroup();
 
97
        $caches = $rdr->CreateCacheSet($group, $mask);
 
98
 
 
99
 
 
100
        $myreq = $this->req->CreateDataRequest();
 
101
        $iv = $caches->CreateInterval($req, true);
 
102
        $window_size = $iv->GetWindowSize();
 
103
        $window_start = $iv->GetWindowStart();
 
104
        $window_end = $iv->GetWindowEnd();
 
105
 
 
106
        $rescfg = array(
 
107
            'limit' => $this->max_points,
 
108
            'resolution' => $res
 
109
        );
 
110
 
 
111
 
 
112
        $gid = 0;
 
113
        $res = array();
 
114
        foreach ($caches as $key => $cachewrap) {
 
115
            if (($gid != $x_gid)) {
 
116
                $gid++;
 
117
                continue;
 
118
            }
 
119
            
 
120
            $resolution = $cachewrap->GetResolution();
 
121
            $r = $resolution->Get($iv, $width);
 
122
 
 
123
            $size = $resolution->GetWindowSize($r);
 
124
            if (($size > 0)&&(($window_size / $size) > $this->max_points)) {
 
125
                $new_r = $resolution->Larger($r);
 
126
                if ($new_r !== false) $r = $new_r;
 
127
                $size = $resolution->GetWindowSize($r);
 
128
            }
 
129
 
 
130
            $rescfg['resolution'] = $r;
 
131
 
 
132
            $points = $cachewrap->GetIntervals($iv, $rescfg, CACHE::TRUNCATE_INTERVALS);
 
133
            $operation_info = $points->GetOperationInfo();
 
134
 
 
135
            if ($gid == $x_gid) $res_x = $size;
 
136
 
 
137
 
 
138
            foreach($points as $t => $v) {
 
139
                /*          if (($t < $window_start)||(($t + $size) > $window_end)) {
 
140
                                continue;
 
141
                            }*/
 
142
                if (($gid == $x_gid)&&(is_numeric($v['mean'.$x_id]))) {
 
143
                    if (!is_array($res[$t])) $res[$t] = array();
 
144
                    $res[$t]['x'] = $v['mean'.$x_id];
 
145
                    $res[$t]['t'] = $t;
 
146
                }
 
147
            }
 
148
            $gid++;
 
149
        }
 
150
 
 
151
        $x = array();
 
152
        $t = array();
 
153
 
 
154
        foreach ($res as $val) {
 
155
            if (isset($val['x'])) array_push($x, $val['x']);
 
156
        }
 
157
 
 
158
        if (!$x) {
 
159
            throw new ADEIException(translate("No data found"));
 
160
        }
 
161
        
 
162
        $bins = $req->GetProp("view_bins", 0);
 
163
        if (!$bins) $bins = ceil(sqrt(sizeof($x)));
 
164
        
 
165
        $norm = $req->GetProp("view_hist_norm", 0);
 
166
        $fit = $req->GetProp("view_hist_fit", 0);
 
167
 
 
168
 
 
169
        $min = min($x);
 
170
        $max = max($x);
 
171
        $step = ($max - $min) / $bins;
 
172
        
 
173
        $coef = $norm?(1/($step * sizeof($x))):1;
 
174
 
 
175
        $h = array_fill(0, $bins, 0); 
 
176
        foreach ($x as $val) {
 
177
            $idx = ($val - $min)/$step;
 
178
            if ($idx == $bins) $idx--;
 
179
            $h[$idx] += $coef;
 
180
        }
 
181
 
 
182
        for($i = 0 ; $i < $bins ; $i++)
 
183
            array_push($t, sprintf("%3.1e", $min + $i*$step));
 
184
 
 
185
        $tmp_file1 = ADEI::GetTmpFile();
 
186
 
 
187
        $graph = new Graph($width,$height);
 
188
/*        $title = "Resolution: $res_x";
 
189
 
 
190
        $graph->title->SetFont(FF_ARIAL,FS_BOLD,10);
 
191
        $graph->title->Set($title);*/
 
192
        $graph->SetTickDensity(TICKD_SPARSE, TICKD_SPARSE);
 
193
        $graph->img->SetMargin(55,5,10,20);
 
194
 
 
195
        $graph->SetScale("textlin");
 
196
        $graph->xaxis->SetPos("min");
 
197
        $graph->yaxis->SetPos("min");
 
198
//        $graph->xaxis->SetLabelFormat('%3.1e');
 
199
//        if (abs(max($h))<9999 && (abs(min($h))>0.01)) $graph->yaxis->SetLabelFormat('%01.2f');
 
200
//        else $graph->yaxis->SetLabelFormat('%3.1e');
 
201
        $graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
 
202
        $graph->yaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
 
203
//        $graph->yaxis->HideFirstTickLabel();
 
204
 
 
205
        $graph->xaxis->title->SetFont(FF_ARIAL,FS_BOLD);
 
206
        $graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD);
 
207
 
 
208
        //$graph->xaxis->title->Set($arr[0]['select']['options'][$x_idg]['label']);
 
209
        if($bins > 8) $graph->xaxis->SetTextLabelInterval(ceil(($bins / 6)));
 
210
        $graph->xaxis->SetTickLabels($t);
 
211
 
 
212
        $bplot = new BarPlot($h);
 
213
 
 
214
        $bplot->SetWidth(1);
 
215
        $graph->Add($bplot);
 
216
 
 
217
        $graph->yaxis->scale->SetGrace(14);
 
218
 
 
219
        $mean = array_sum($x)/sizeof($x);
 
220
        $stddev = stats_standard_deviation($x);
 
221
        $var = stats_variance($x);
 
222
        $sigma = sqrt($var);
 
223
        $re = 100 * $sigma/$mean;
 
224
        
 
225
        sort($x);
 
226
        if (sizeof($x) % 2) 
 
227
            $median = $x[(sizeof($x) - 1)/2];
 
228
        else
 
229
            $median = ($x[sizeof($x)/2 - 1] + $x[sizeof($x)/2])/2;
 
230
 
 
231
 
 
232
            // Gaussian fitting
 
233
        if ($fit) {
 
234
            $ydata = array();
 
235
            $xdata = array();
 
236
            if ($norm) $coef = (1/(sqrt(2 * pi() * $var)));
 
237
            else $coef = ((sizeof($x)*$step)/(sqrt(2 * pi() * $var)));
 
238
            
 
239
            $xi2 = 0;
 
240
            for($i = 0; $i <= $bins; $i++) {
 
241
                $offset = $i * $step;
 
242
                $y = $coef * exp(-pow($min + $offset - $mean, 2)/(2*$var));
 
243
                array_push($xdata, $i);
 
244
                array_push($ydata, $y);
 
245
                $xi2 += (pow($y - $h[$i], 2)) / $y;
 
246
            }
 
247
            $xi2 /= $bins;
 
248
 
 
249
            $lineplot = new LinePlot($ydata , $xdata);
 
250
            $graph->Add($lineplot);
 
251
        }
 
252
 
 
253
        $char_sigma = SymChar::Get('sigma', false);
 
254
 
 
255
/*
 
256
        $txt = new Text();
 
257
        $txt->SetFont(FF_ARIAL,FS_BOLD,10);
 
258
        if( $req->GetProp("view_GFit", false) == "true")
 
259
            $txt->Set("m=$mean\n$char_sigma=$sigma\nRE=$RE%\nx^2=$xi2");//\ns=$stdDev
 
260
        else
 
261
            $txt->Set("m=$mean\n$char_sigma=$sigma\nRE=$RE%");//\ns=$stdDev
 
262
        $txt->ParagraphAlign('right');
 
263
        $txt->SetPos(0.96,0.1,'right');
 
264
        //$txt->SetBox('white');
 
265
        $graph->Add($txt);
 
266
*/
 
267
 
 
268
        $graph->Stroke("$TMP_PATH/$tmp_file1");
 
269
 
 
270
        if ($this->object) {
 
271
            $res = array(
 
272
                array("img" => array("id" => $tmp_file1)),
 
273
                array("info" => array(
 
274
                    array("title"=>_("From"), "value" => date('c', $iv->GetWindowStart())),
 
275
                    array("title"=>_("To"), "value" => date('c', $iv->GetWindowEnd())),
 
276
                    array("title"=>_("Resolution"), "value" => $res_x),
 
277
                    array("title"=>_("Bins"), "value" => $bins),
 
278
                    array("title"=>_("First Bin"), "value" =>  $min),
 
279
                    array("title"=>_("Last Bin"), "value" =>  $min + $bins * $step),
 
280
                    array("title"=>_("Mean"), "value" => $mean),
 
281
                    array("title"=>_("Median"), "value" => $median),
 
282
                    array("title"=>_("StdDev"), "value" => $stddev),
 
283
                    array("title"=>_("Sigma"), "value" => $sigma),
 
284
                    array("title"=>_("RE"), "value" => ($re . "%")),
 
285
                ))
 
286
            );
 
287
            if ($fit) {
 
288
                array_push($res[1]["info"], array("title"=>_("xi2"), "value" => $xi2));
 
289
            }
 
290
            return $res;
 
291
        } else {
 
292
            return array(
 
293
                "img" => array("id" => $tmp_file)
 
294
            );
 
295
        }
 
296
 
 
297
    }
 
298
 
 
299
};
 
300
 
 
301
?>