Debian Codegraph @

dstream/base.go

raw symbols SCIP
1 package dstream
2
3 import (
4 "fmt"
5 "io"
6 "os"
7 )
8
9 // Dstream streams chunks of data to a consumer.
10 type Dstream interface {
11
12 // Next attempts to advance to the next chunk and returns true
13 // if successful.
14 Next() bool
15
16 // Names returns the variable names.
17 Names() []string
18
19 // Get returns the values for one variable in the current
20 // chunk, referring to the variable by name
21 Get(string) interface{}
22
23 // Get returns the values for one variable for the current
24 // chunk, referring to the variable by position.
25 GetPos(int) interface{}
26
27 // NumVar returns the number of variables in the data set.
28 NumVar() int
29
30 // NumObs returns the number of rows in the data set, it may
31 // return -1 if not known.
32 NumObs() int
33
34 // Reset sets the provider so that the data are read from the
35 // beginning of the dataset.
36 Reset()
37
38 // Close frees any resources suh as file handles used by the dstream.
39 Close()
40 }
41
42 // Dtype represents a data type
43 type Dtype uint8
44
45 // Uint8, etc. are constants defining data types
46 const (
47 Uint8 Dtype = iota
48 Uint16
49 Uint32
50 Uint64
51 Int8
52 Int16
53 Int32
54 Int64
55 Float32
56 Float64
57 Time
58 String
59 )
60
61 // DataFrame is an implementation of Dstream based on sharded arrays.
62 type DataFrame struct {
63 xform // bdata is not used
64
65 // data is the underlying data to be passed to the
66 // consumer.
67 data [][]interface{}
68
69 chunk int // 1-based
70 done bool
71 nobs int
72 }
73
74 // Next advances to the next chunk and returns true if successful. If there are
75 // no more chunks, it returns false.
76 func (da *DataFrame) Next() bool {
77
78 da.chunk++
79 if da.chunk <= len(da.data[0]) {
80 return true
81 }
82 da.done = true
83 return false
84 }
85
86 // NumObs returns the number of observations in the DataFrame, if known. If the number
87 // of observations is not known, it returns -1.
88 func (da *DataFrame) NumObs() int {
89
90 if da.nobs > 0 {
91 return da.nobs
92 }
93
94 if da.data == nil || len(da.data) == 0 {
95 // Not yet known
96 return -1
97 }
98
99 var nobs int
100 for _, v := range da.data[0] {
101 nobs += ilen(v)
102 }
103 da.nobs = nobs
104
105 return nobs
106 }
107
108 func (da *DataFrame) init() {
109 da.setNamePos() // TODO should get rid of this
110 }
111
112 // Names returns the variable (column) names of the dstream.
113 func (da *DataFrame) Names() []string {
114 return da.names
115 }
116
117 // Reset resets the dstream so that after the next call to Next, the
118 // dstream is at chunk zero.
119 func (da *DataFrame) Reset() {
120 da.chunk = 0
121 da.nobs = 0
122 da.done = false
123 }
124
125 // GetPos returns the data slice for the variable at the given position.
126 func (da *DataFrame) GetPos(j int) interface{} {
127 if da.done {
128 return nil
129 }
130
131 return da.data[j][da.chunk-1]
132 }
133
134 // Get returns the data slice for the variable with the given name.
135 func (da *DataFrame) Get(na string) interface{} {
136
137 pos := -1
138 for j, nm := range da.Names() {
139 if nm == na {
140 pos = j
141 break
142 }
143 }
144
145 if pos == -1 {
146 msg := fmt.Sprintf("Get: variable '%s' not found", na)
147 panic(msg)
148 }
149
150 return da.GetPos(pos)
151 }
152
153 // NumVar returns the number of variables in the dstream.
154 func (da *DataFrame) NumVar() int {
155 return len(da.data)
156 }
157
158 // NewFromArrays creates a Dstream from raw data stored as slices;
159 // data[i][j] is the data for the i^th variable in the j^th chunk.
160 func NewFromArrays(data [][]interface{}, names []string) Dstream {
161
162 if len(data) != len(names) {
163 msg := fmt.Sprintf("NewFromArrays: len(data) = %d != len(names) = %d",
164 len(data), len(names))
165 panic(msg)
166 }
167
168 da := &DataFrame{
169 data: data,
170 xform: xform{
171 names: names,
172 },
173 }
174
175 da.init()
176
177 return da
178 }
179
180 // CheckValid runs through the chunks and confirms that the lengths of the slices within
181 // the chunks are the same. If CheckValid returns false, the dstream is in a corrupted
182 // state. On completion, the dstream is in its initial state.
183 func CheckValid(data Dstream) bool {
184
185 data.Reset()
186 names := data.Names()
187
188 for c := 0; data.Next(); c++ {
189 n0 := ilen(data.GetPos(0))
190 for j := 1; j < len(names); j++ {
191 n1 := ilen(data.GetPos(j))
192 if n1 != n0 {
193 msg := fmt.Sprintf("Length mismatch in chunk %d: len(%s) = %d, len(%s) = %d\n",
194 c, names[0], n0, names[j], n1)
195 _, _ = io.WriteString(os.Stderr, msg)
196 return false
197 }
198 }
199 }
200
201 data.Reset()
202
203 return true
204 }
205
206 // NewFromFlat creates a Dstream from raw data stored as contiguous
207 // (flat) arrays. data[i] is the data for the i^th variable, it is
208 // a slice of fixed-width values, e.g. a []float64.
209 func NewFromFlat(data []interface{}, names []string) Dstream {
210
211 if len(data) != len(names) {
212 msg := fmt.Sprintf("NewFromFlat: len(data) = %d != len(names) = %d",
213 len(data), len(names))
214 panic(msg)
215 }
216
217 var ar [][]interface{}
218 for _, v := range data {
219 ar = append(ar, []interface{}{v})
220 }
221
222 da := &DataFrame{
223 data: ar,
224 xform: xform{
225 names: names,
226 },
227 }
228
229 da.init()
230
231 return da
232 }
233
234 // Shallow attempts to make a shallow copy of the data stream.
235 // Currently, only memory-backed data streams can be shallow copied,
236 // otherwise a deep copy is returned. Shallow copies of the same data
237 // can be read in parallel.
238 func Shallow(data Dstream) Dstream {
239
240 data.Reset()
241 switch data := data.(type) {
242 case *DataFrame:
243 var dy DataFrame = *data
244 return &dy
245 default:
246 return MemCopy(data, true)
247 }
248 }