-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDescribe.php
More file actions
133 lines (123 loc) · 3.69 KB
/
Describe.php
File metadata and controls
133 lines (123 loc) · 3.69 KB
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
<?php
/**
* This file contains Describe process providing the mean and median for data.
*
* @author KoolPHP Inc (support@koolphp.net)
* @link https://www.koolphp.net
* @copyright KoolPHP Inc
* @license https://www.koolreport.com/license
*/
namespace koolreport\cleandata;
use \koolreport\core\Process;
use \koolreport\core\Utility;
class Describe extends Process
{
protected $data = array();
protected $targetColumns;
protected $excludedColumns;
protected $targetColumnType;
protected $columns;
protected function onInit()
{
$this->newValue = Utility::get($this->params,"newValue");
$this->targetValue = Utility::get($this->params,"targetValue");
$this->targetColumnType = Utility::get($this->params,"targetColumnType");
$this->targetColumns = Utility::get($this->params,"targetColumns");
$this->excludedColumns = Utility::get($this->params,"excludedColumns");
}
protected function onInput($row)
{
array_push($this->data,$row);
}
public function receiveMeta($meta,$source)
{
$this->streamingSource = $source;
$this->metaData = $meta;
$columns = array();
foreach($meta["columns"] as $cName=>$cSetting)
{
$columnName = $cName;
if($this->targetColumns!==null && !in_array($cName,$this->targetColumns))
{
$columnName = null;
}
if($columnName!==null)
{
if($this->excludedColumns!==null && in_array($cName,$this->excludedColumns))
{
$columnName = null;
}
}
if($columnName!==null)
{
if($this->targetColumnType!==null && $this->targetColumnType!==Utility::get($cSetting,"type"))
{
$columnName = null;
}
}
if($columnName!==null)
{
array_push($columns,$columnName);
}
}
$this->columns = $columns;
}
protected function median($arr)
{
sort($arr);
$count = count($arr);
if($count%2==0)
{
return ($arr[$count/2-1]+$arr[$count/2])/2;
}
else
{
return $arr[floor($count/2)];
}
}
protected function onInputEnd()
{
$dMeta = array();
if($this->newValue==FillNull::MEAN)
{
foreach($this->columns as $cName)
{
$dMeta[$cName] = array(
"count"=>0,
"sum"=>0,
);
foreach($this->data as $row)
{
if($row[$cName]!=$this->targetValue)
{
$dMeta[$cName]["count"]++;
$dMeta[$cName]["sum"]+=$row[$cName];
}
}
$dMeta[$cName]["mean"] = $dMeta[$cName]["sum"]/$dMeta[$cName]["count"];
}
}
else if($this->newValue==FillNull::MEDIAN)
{
foreach($this->columns as $cName)
{
$list = array();
foreach($this->data as $row)
{
if($row[$cName]!=$this->targetValue)
{
array_push($list,$row[$cName]);
}
}
$dMeta[$cName] = array("median"=>$this->median($list));
}
}
$meta = $this->metaData;
$meta["describe"] = $dMeta;
$this->sendMeta($meta);
while($item = array_shift($this->data))
{
$this->next($item);
}
}
}