-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdense_mask_filling.go
122 lines (114 loc) · 2.36 KB
/
dense_mask_filling.go
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
package tensor
import (
"unsafe"
)
// FillValue returns the value used to fill the invalid entries of a masked array
func (t *Dense) FillValue() interface{} {
switch t.Dtype() {
case Bool:
return true
case Int:
return int(999999)
case Int8:
return int8(99)
case Int16:
return int16(9999)
case Int32:
return int32(999999)
case Int64:
return int64(999999)
case Uint:
return uint(999999)
case Byte:
return byte(99)
case Uint8:
return uint8(99)
case Uint16:
return uint16(9999)
case Uint32:
return uint32(999999)
case Uint64:
return uint64(999999)
case Float32:
return float32(1.0e20)
case Float64:
return float64(1.0e20)
case Complex64:
return complex64(1.0e20 + 0i)
case Complex128:
return complex128(1.0e20 + 0i)
case String:
return `N/A`
case Uintptr:
return uintptr(0x999999)
case UnsafePointer:
return unsafe.Pointer(nil)
default:
return nil
}
}
// Filled returns a tensor with masked data replaced by default fill value,
// or by optional passed value
func (t *Dense) Filled(val ...interface{}) (interface{}, error) {
tc := t.Clone().(*Dense)
if !t.IsMasked() {
return tc, nil
}
fillval := t.FillValue()
if len(val) > 0 {
fillval = val[0]
}
switch {
case tc.IsScalar():
if tc.mask[0] {
tc.Set(0, fillval)
}
case tc.IsRowVec() || tc.IsColVec():
sliceList := t.FlatMaskedContiguous()
for i := range sliceList {
tt, err := tc.Slice(nil, sliceList[i])
if err != nil {
ts := tt.(*Dense)
ts.Memset(fillval)
}
}
default:
it := IteratorFromDense(tc)
for i, _, err := it.NextInvalid(); err == nil; i, _, err = it.NextInvalid() {
tc.Set(i, fillval)
}
}
return tc, nil
}
// FilledInplace replaces masked data with default fill value,
// or by optional passed value
func (t *Dense) FilledInplace(val ...interface{}) (interface{}, error) {
if !t.IsMasked() {
return t, nil
}
fillval := t.FillValue()
if len(val) > 0 {
fillval = val[0]
}
switch {
case t.IsScalar():
if t.mask[0] {
t.Set(0, fillval)
}
case t.IsRowVec() || t.IsColVec():
sliceList := t.FlatMaskedContiguous()
for i := range sliceList {
tt, err := t.Slice(nil, sliceList[i])
if err != nil {
ts := tt.(*Dense)
ts.Memset(fillval)
}
}
default:
it := IteratorFromDense(t)
for i, _, err := it.NextInvalid(); err == nil; i, _, err = it.NextInvalid() {
t.Set(i, fillval)
}
}
return t, nil
}