This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
fillmissing.hhs
183 lines (158 loc) · 5.3 KB
/
fillmissing.hhs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @Author Jianan Lin (林家南)
* @param input: a JS array / matrix / tensor
* @param content: use this to fill the missing part, should be a constant / array / matrix / tensor
* @param method: 1. 'constant' means use constant from content, 2. 'previous' means use the previous value in input and if no previous, then use constant
* @returns - input with missing value filled
*
*/
function fillmissing(input, content = 0, method = "constant") {
*import math: ndim
*import math: deep_copy
*import math: flatten
// argument check
if (arguments.length === 0) {
throw new Error('Exception occurred in fillmissing - no argument given');
}
if (arguments.length > 3) {
throw new Error('Exception occurred in fillmissing - wrong argument number');
}
// type check
if (!(Array.isArray(input)) && !(input instanceof Mat) && !(input instanceof Tensor)) {
throw new Error('Exception occurred in fillmissing - argument[0] must be JS array, matrix or tensor');
}
if (!(method === 'constant') && !(method === 'previous')) {
throw new Error('Exception occurred in fillmissing - argument[2] must be constant or previous');
}
// we do not check content because we should allow users to use anything they like, even class / array. However, they should fix the errors when facing such problems.
let in_type = input instanceof Mat || input instanceof Tensor;
let raw_in = in_type ? input.clone().val : deep_copy(input);
let flag = false; // flag means content is array / constant
let index = 0; // the index of content
if (Array.isArray(content) || content instanceof Mat || content instanceof Tensor) {
content = flatten(content);
flag = true;
if (content.length === 0) {
throw new Error('Exception occurred in fillmissing - argument[1] should not be empty');
}
}
let result = [];
if (flag === true && method === 'constant') {
result = fill_constant_true(raw_in);
}
else if (flag === false && method === 'constant') {
result = fill_constant_false(raw_in);
}
else if (flag === true && method === 'previous') {
result = fill_previous_true(raw_in);
}
else {
result = fill_previous_false(raw_in);
}
if (ndim(result) <= 2) {
return mat(result);
}
else {
return new Tensor(result);
}
function fill_constant_true(data) {
if (ndim(data) === 1) {
let result = deep_copy(data);
for (let i = 0; i < data.length; i++) {
if (is_miss(data[i])) {
result[i] = content[index];
if (index < content.length - 1) {
index += 1;
}
}
}
return result;
}
else {
let result = [];
for (let i = 0; i < data.length; i++) {
let temp = fill_constant_true(data[i]);
result.push(temp);
}
return result;
}
}
function fill_constant_false(data) {
if (ndim(data) === 1) {
let result = deep_copy(data);
for (let i = 0; i < data.length; i++) {
if (is_miss(data[i])) {
result[i] = content;
}
}
return result;
}
else {
let result = [];
for (let i = 0; i < data.length; i++) {
let temp = fill_constant_false(data[i]);
result.push(temp);
}
return result;
}
}
function fill_previous_true(data) {
if (ndim(data) === 1) {
let result = deep_copy(data);
for (let i = 0; i < data.length; i++) {
if (is_miss(data[i])) {
if (i > 0) {
result[i] = result[i - 1];
}
else {
result[i] = content[index];
if (index < content.length - 1) {
index += 1;
}
}
}
}
return result;
}
else {
let result = [];
for (let i = 0; i < data.length; i++) {
let temp = fill_previous_true(data[i]);
result.push(temp);
}
return result;
}
}
function fill_previous_false(data) {
if (ndim(data) === 1) {
let result = deep_copy(data);
for (let i = 0; i < data.length; i++) {
if (is_miss(data[i])) {
if (i > 0) {
result[i] = result[i - 1];
}
else {
result[i] = content;
}
}
}
return result;
}
else {
let result = [];
for (let i = 0; i < data.length; i++) {
let temp = fill_previous_false(data[i]);
result.push(temp);
}
return result;
}
}
function is_miss(x) {
if (x || x === 0) {
return false;
}
else {
return true;
}
}
}