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