forked from suyashkumar/dicom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_test.go
283 lines (255 loc) · 6.81 KB
/
dataset_test.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package dicom
import (
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/suyashkumar/dicom/pkg/tag"
)
func makeSequenceElement(tg tag.Tag, items [][]*Element) *Element {
sequenceItems := make([]*SequenceItemValue, 0, len(items))
for _, item := range items {
sequenceItems = append(sequenceItems, &SequenceItemValue{elements: item})
}
return &Element{
Tag: tg,
ValueRepresentation: tag.VRSequence,
RawValueRepresentation: "SQ",
Value: &sequencesValue{
value: sequenceItems,
},
}
}
func TestDataset_FindElementByTag(t *testing.T) {
data := Dataset{
Elements: []*Element{
{
Tag: tag.Rows,
ValueRepresentation: tag.VRInt32List,
Value: &intsValue{
value: []int{100},
},
},
{
Tag: tag.Columns,
ValueRepresentation: tag.VRInt32List,
Value: &intsValue{
value: []int{200},
},
},
},
}
elem, err := data.FindElementByTag(tag.Rows)
if err != nil {
t.Errorf("FindElementByTag(%v): unexpected err: %v", tag.Rows, err)
}
if rows := MustGetInts(elem.Value)[0]; rows != 100 {
t.Errorf("FindElementByTag(%v): got: %v, want: %v", tag.Rows, rows, 100)
}
}
func TestDataset_FlatStatefulIterator(t *testing.T) {
cases := []struct {
name string
dataset Dataset
expectedFlatElements []*Element
}{
{
name: "flat dataset",
dataset: Dataset{Elements: []*Element{
mustNewElement(tag.PatientName, []string{"Bob", "Smith"}),
mustNewElement(tag.PatientName, []string{"Bob", "Jones"}),
}},
expectedFlatElements: []*Element{
mustNewElement(tag.PatientName, []string{"Bob", "Smith"}),
mustNewElement(tag.PatientName, []string{"Bob", "Jones"}),
},
},
{
name: "nested dataset",
dataset: Dataset{Elements: []*Element{
makeSequenceElement(tag.AddOtherSequence, [][]*Element{
// Item 1
{
mustNewElement(tag.PatientName, []string{"Bob", "Jones"}),
// Nested Sequence.
makeSequenceElement(tag.AnatomicRegionSequence, [][]*Element{
{
mustNewElement(tag.PatientName, []string{"Bob", "Smith"}),
},
}),
},
}),
}},
expectedFlatElements: []*Element{
// First, expect the entire SQ element
makeSequenceElement(tag.AddOtherSequence, [][]*Element{
// Item 1
{
mustNewElement(tag.PatientName, []string{"Bob", "Jones"}),
// Nested Sequence.
makeSequenceElement(tag.AnatomicRegionSequence, [][]*Element{
{
mustNewElement(tag.PatientName, []string{"Bob", "Smith"}),
},
}),
},
}),
// Then expect the inner elements
mustNewElement(tag.PatientName, []string{"Bob", "Jones"}),
// Inner SQ element
makeSequenceElement(tag.AnatomicRegionSequence, [][]*Element{
{
mustNewElement(tag.PatientName, []string{"Bob", "Smith"}),
},
}),
// Inner element of the inner SQ
mustNewElement(tag.PatientName, []string{"Bob", "Smith"}),
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var gotElems []*Element
for iter := tc.dataset.FlatStatefulIterator(); iter.HasNext(); {
gotElems = append(gotElems, iter.Next())
}
if diff := cmp.Diff(tc.expectedFlatElements, gotElems, cmp.AllowUnexported(allValues...)); diff != "" {
t.Errorf("FlatStatefulIterator(%v) returned unexpected set of elements: %v", tc.dataset, diff)
}
})
}
}
func ExampleDataset_FlatIterator() {
nestedData := [][]*Element{
{
mustNewElement(tag.PatientName, []string{"Bob"}),
},
}
data := Dataset{
Elements: []*Element{
mustNewElement(tag.Rows, []int{100}),
mustNewElement(tag.Columns, []int{100}),
makeSequenceElement(tag.AddOtherSequence, nestedData),
},
}
// Use this style if you will always exhaust all of the elements in the
// channel. Otherwise, you must call ExhaustElementChannel. See the
// FlatIteratorWithExhaustAllElements example for that. If you don't need
// a channel API (just want to loop over items), use FlatStatefulIterator
// instead, which is much simpler.
for elem := range data.FlatIterator() {
fmt.Println(elem.Tag)
}
// Note the output below includes all three leaf elements __as well as__ the sequence element's tag
// Unordered output:
// (0028,0010)
// (0028,0011)
// (0010,0010)
// (0046,0102)
}
func ExampleDataset_FlatIteratorWithExhaustAllElements() {
nestedData := [][]*Element{
{
mustNewElement(tag.PatientName, []string{"Bob"}),
},
}
data := Dataset{
Elements: []*Element{
mustNewElement(tag.Rows, []int{100}),
mustNewElement(tag.Columns, []int{100}),
makeSequenceElement(tag.AddOtherSequence, nestedData),
},
}
// Because we read only one element from the channel, we want to make sure
// the channel is exhausted to ensure it is closed properly under the hood.
// This is also needed if you have any situation in which you may not
// read all the elements in the channel (e.g. if you are looping over it,
// but might return early if there's an error).
elemChan := data.FlatIterator()
defer ExhaustElementChannel(elemChan)
fmt.Println((<-elemChan).Tag)
// Note the output below includes all three leaf elements __as well as__ the sequence element's tag
// Unordered output:
// (0028,0010)
}
func ExampleDataset_FlatStatefulIterator() {
nestedData := [][]*Element{
{
{
Tag: tag.PatientName,
ValueRepresentation: tag.VRString,
Value: &stringsValue{
value: []string{"Bob"},
},
},
},
}
data := Dataset{
Elements: []*Element{
{
Tag: tag.Rows,
ValueRepresentation: tag.VRInt32List,
Value: &intsValue{
value: []int{100},
},
},
{
Tag: tag.Columns,
ValueRepresentation: tag.VRInt32List,
Value: &intsValue{
value: []int{200},
},
},
makeSequenceElement(tag.AddOtherSequence, nestedData),
},
}
for iter := data.FlatStatefulIterator(); iter.HasNext(); {
fmt.Println(iter.Next().Tag)
}
// Note the output below includes all three leaf elements __as well as__ the sequence element's tag
// Unordered output:
// (0028,0010)
// (0028,0011)
// (0010,0010)
// (0046,0102)
}
func ExampleDataset_String() {
d := Dataset{
Elements: []*Element{
{
Tag: tag.Rows,
ValueRepresentation: tag.VRInt32List,
RawValueRepresentation: "UL",
Value: &intsValue{
value: []int{100},
},
},
{
Tag: tag.Columns,
ValueRepresentation: tag.VRInt32List,
RawValueRepresentation: "UL",
Value: &intsValue{
value: []int{200},
},
},
},
}
fmt.Println(d.String())
// Output:
// [
// Tag: (0028,0010)
// Tag Name: Rows
// VR: VRInt32List
// VR Raw: UL
// VL: 0
// Value: [100]
// ]
//
// [
// Tag: (0028,0011)
// Tag Name: Columns
// VR: VRInt32List
// VR Raw: UL
// VL: 0
// Value: [200]
// ]
}