Debian Codegraph @

dstream/dropna.go

raw symbols SCIP
1 package dstream
2
3 import "math"
4
5 // TODO: change this to DropNaN, then add a separate dropna that uses
6 // a function to define what is missing.
7
8 // DropNA returns a Dstream in which all rows containing any missing
9 // values are dropped.
10 func DropNA(data Dstream) Dstream {
11 dna := &dropNA{
12 xform: xform{
13 source: data,
14 },
15 }
16
17 return dna
18 }
19
20 type dropNA struct {
21 xform
22
23 mask []bool
24 pos []int
25 nobs int
26 nobsKnown bool
27 }
28
29 func (dna *dropNA) NumObs() int {
30 if dna.nobsKnown {
31 return dna.nobs
32 }
33 return -1
34 }
35
36 func (dna *dropNA) Reset() {
37 dna.source.Reset()
38 dna.nobsKnown = false
39
40 }
41
42 func (dna *dropNA) Next() bool {
43
44 if !dna.source.Next() {
45 dna.nobsKnown = true
46 return false
47 }
48
49 // The size of the source data chunk
50 n := ilen(dna.source.GetPos(0))
51
52 dna.mask = resizeBool(dna.mask, n)
53 for j := range dna.mask {
54 dna.mask[j] = false
55 }
56
57 // Get the missing mask (true = missing)
58 nvar := dna.source.NumVar()
59 for j := 0; j < nvar; j++ {
60 v := dna.source.GetPos(j)
61 switch v := v.(type) {
62 case []float64:
63 for i, y := range v {
64 if math.IsNaN(y) {
65 dna.mask[i] = true
66 }
67 }
68 case []string:
69 // Currently strings cannot be missing
70 // TODO: allow specified labels to denote missing
71 }
72 }
73
74 dna.pos = wherefalse(dna.mask, dna.pos)
75 m := len(dna.pos)
76 dna.nobs += m
77
78 data := dna.bdata
79 if data == nil {
80 data = make([]interface{}, nvar)
81 }
82 for j := 0; j < nvar; j++ {
83 v := dna.source.GetPos(j)
84 switch v := v.(type) {
85 case []float64:
86 var x []float64
87 if data[j] != nil {
88 x = data[j].([]float64)
89 }
90 x = resizeFloat64(x, m)
91 for i, p := range dna.pos {
92 x[i] = v[p]
93 }
94 data[j] = x
95 case []string:
96 var x []string
97 if data[j] != nil {
98 x = data[j].([]string)
99 }
100 x = resizeString(x, m)
101 for i, p := range dna.pos {
102 x[i] = v[p]
103 }
104 data[j] = x
105 default:
106 panic("unknown data type")
107 }
108 }
109
110 dna.bdata = data
111 return true
112 }