-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
parse_test.go
334 lines (303 loc) · 9.98 KB
/
parse_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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package dicom_test
import (
"bytes"
"encoding/json"
"fmt"
"image/jpeg"
"io"
"os"
"strings"
"testing"
"github.com/suyashkumar/dicom/pkg/tag"
"github.com/suyashkumar/dicom/pkg/frame"
"github.com/suyashkumar/dicom"
)
// TestParse is an end-to-end sanity check over DICOMs in testdata/. Currently,
// it only checks that no error is returned when parsing the files.
func TestParse(t *testing.T) {
files, err := os.ReadDir("./testdata")
if err != nil {
t.Fatalf("unable to read testdata/: %v", err)
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".dcm") {
t.Run(f.Name(), func(t *testing.T) {
dcm, err := os.Open("./testdata/" + f.Name())
if err != nil {
t.Errorf("Unable to open %s. Error: %v", f.Name(), err)
}
defer dcm.Close()
info, err := dcm.Stat()
if err != nil {
t.Errorf("Unable to stat %s. Error: %v", f.Name(), err)
}
_, err = dicom.Parse(dcm, info.Size(), nil)
if err != nil {
t.Errorf("dicom.Parse(%s) unexpected error: %v", f.Name(), err)
}
})
}
}
}
func TestParseUntilEOF(t *testing.T) {
files, err := os.ReadDir("./testdata")
if err != nil {
t.Fatalf("unable to read testdata/: %v", err)
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".dcm") {
t.Run(f.Name(), func(t *testing.T) {
dcm, err := os.Open("./testdata/" + f.Name())
if err != nil {
t.Errorf("Unable to open %s. Error: %v", f.Name(), err)
}
defer dcm.Close()
if err != nil {
t.Errorf("Unable to stat %s. Error: %v", f.Name(), err)
}
_, err = dicom.ParseUntilEOF(dcm, nil)
if err != nil {
t.Errorf("dicom.Parse(%s) unexpected error: %v", f.Name(), err)
}
})
}
}
}
// TestNewParserSkipMetadataReadOnNewParserInit tests that NewParser with the SkipMetadataReadOnNewParserInit option
// parses the specified dataset but not its header metadata.
func TestNewParserSkipMetadataReadOnNewParserInit(t *testing.T) {
fStat, err := os.Stat("./testdata/1.dcm")
if err != nil {
t.Fatalf("Unable to stat %s. Error: %v", fStat.Name(), err)
}
f, err := os.Open("./testdata/1.dcm")
if err != nil {
t.Fatalf("Unable to open %s. Error: %v", f.Name(), err)
}
p, err := dicom.NewParser(f, fStat.Size(), nil, dicom.SkipMetadataReadOnNewParserInit())
if err != nil {
t.Fatalf("dicom.Parse(%s) unexpected error: %v", f.Name(), err)
}
metadata := p.GetMetadata()
if len(metadata.Elements) > 0 {
t.Fatalf("Found %d metadata elements despite SkipMetadataReadOnNewParserInit()", len(metadata.Elements))
}
}
func TestParseFile_SkipPixelData(t *testing.T) {
t.Run("WithSkipPixelData", func(t *testing.T) {
runForEveryTestFile(t, func(t *testing.T, filename string) {
dataset, err := dicom.ParseFile(filename, nil, dicom.SkipPixelData())
if err != nil {
t.Errorf("Unexpected error parsing dataset: %v, dataset: %v", err, dataset)
}
// If PixelData present in this DICOM, check if it's populated
// correctly. The current test assumption is that if PixelData is
// missing, it was not originally in the dicom (which we should
// consider revisiting).
el, err := dataset.FindElementByTag(tag.PixelData)
if err == nil {
pixelData := dicom.MustGetPixelDataInfo(el.Value)
if !pixelData.IntentionallySkipped {
t.Errorf("Expected pixelData.IntentionallySkipped=true, got false")
}
if got := len(pixelData.Frames); got != 0 {
t.Errorf("unexpected frames length. got: %v, want: %v", got, 0)
}
}
})
})
t.Run("WithNOSkipPixelData", func(t *testing.T) {
runForEveryTestFile(t, func(t *testing.T, filename string) {
dataset, err := dicom.ParseFile(filename, nil)
if err != nil {
t.Errorf("Unexpected error parsing dataset: %v, dataset: %v", err, dataset)
}
// If PixelData present in this DICOM, check if it's populated
// correctly. The current test assumption is that if PixelData is
// missing, it was not originally in the dicom (which we should
// consider revisiting).
el, err := dataset.FindElementByTag(tag.PixelData)
if err == nil {
pixelData := dicom.MustGetPixelDataInfo(el.Value)
if pixelData.IntentionallySkipped {
t.Errorf("Expected pixelData.IntentionallySkipped=false when SkipPixelData option not present, got true")
}
if len(pixelData.Frames) == 0 {
t.Errorf("unexpected frames length when SkipPixelData=false. got: %v, want: >0", len(pixelData.Frames))
}
}
})
})
}
func TestParseFile_SkipProcessingPixelDataValue(t *testing.T) {
t.Run("WithSkipProcessingPixelDataValue", func(t *testing.T) {
runForEveryTestFile(t, func(t *testing.T, filename string) {
dataset, err := dicom.ParseFile(filename, nil, dicom.SkipProcessingPixelDataValue())
if err != nil {
t.Errorf("Unexpected error parsing dataset: %v", dataset)
}
el, err := dataset.FindElementByTag(tag.PixelData)
if err != nil {
t.Errorf("Unexpected error when finding PixelData in Dataset: %v", err)
}
pixelData := dicom.MustGetPixelDataInfo(el.Value)
if !pixelData.IntentionallyUnprocessed {
t.Errorf("Expected pixelData.IntentionallyUnprocessed=true, got false")
}
if got := len(pixelData.Frames); got != 0 {
t.Errorf("unexpected frames length. got: %v, want: %v", got, 0)
}
})
})
t.Run("WithNOSkipProcessingPixelDataValue", func(t *testing.T) {
runForEveryTestFile(t, func(t *testing.T, filename string) {
dataset, err := dicom.ParseFile(filename, nil)
if err != nil {
t.Errorf("Unexpected error parsing dataset: %v", dataset)
}
el, err := dataset.FindElementByTag(tag.PixelData)
if err != nil {
t.Errorf("Unexpected error when finding PixelData in Dataset: %v", err)
}
pixelData := dicom.MustGetPixelDataInfo(el.Value)
if pixelData.IntentionallyUnprocessed {
t.Errorf("Expected pixelData.IntentionallyUnprocessed=false when TestParseFile_SkipProcessingPixelDataValue option not present, got true")
}
if len(pixelData.Frames) == 0 {
t.Errorf("unexpected frames length when TestParseFile_SkipProcessingPixelDataValue=false. got: %v, want: >0", len(pixelData.Frames))
}
})
})
t.Run("WithAllowErrorMetaElementGroupLength", func(t *testing.T) {
runForEveryTestFile(t, func(t *testing.T, filename string) {
dataset, err := dicom.ParseFile(filename, nil, dicom.AllowMissingMetaElementGroupLength())
if err != nil {
t.Errorf("Unexpected error parsing dataset: %v", dataset)
}
})
})
}
// BenchmarkParse runs sanity benchmarks over the sample files in testdata.
func BenchmarkParse(b *testing.B) {
cases := []struct {
name string
opts []dicom.ParseOption
}{
{
name: "NoOptions",
},
{
name: "SkipPixelData",
opts: []dicom.ParseOption{dicom.SkipPixelData()},
},
{
name: "SkipProcessingPixelDataValue",
opts: []dicom.ParseOption{dicom.SkipProcessingPixelDataValue()},
},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
files, err := os.ReadDir("./testdata")
if err != nil {
b.Fatalf("unable to read testdata/: %v", err)
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".dcm") {
b.Run(f.Name(), func(b *testing.B) {
dcm, err := os.Open("./testdata/" + f.Name())
if err != nil {
b.Errorf("Unable to open %s. Error: %v", f.Name(), err)
}
defer dcm.Close()
data, err := io.ReadAll(dcm)
if err != nil {
b.Errorf("Unable to read file into memory for benchmark: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = dicom.Parse(bytes.NewBuffer(data), int64(len(data)), nil, tc.opts...)
}
})
}
}
})
}
}
func BenchmarkParser_NextAPI(b *testing.B) {
files, err := os.ReadDir("./testdata")
if err != nil {
b.Fatalf("unable to read testdata/: %v", err)
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".dcm") {
b.Run(f.Name(), func(b *testing.B) {
dcm, err := os.Open("./testdata/" + f.Name())
if err != nil {
b.Errorf("Unable to open %s. Error: %v", f.Name(), err)
}
defer dcm.Close()
data, err := io.ReadAll(dcm)
if err != nil {
b.Errorf("Unable to read file into memory for benchmark: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := bytes.NewReader(data)
p, _ := dicom.NewParser(r, int64(len(data)), nil)
var err error
for err == nil {
_, err = p.Next()
}
}
})
}
}
}
func Example_readFile() {
// See also: dicom.Parse, which uses a more generic io.Reader API.
dataset, _ := dicom.ParseFile("testdata/1.dcm", nil)
// Dataset will nicely print the DICOM dataset data out of the box.
fmt.Println(dataset)
// Dataset is also JSON serializable out of the box.
j, _ := json.Marshal(dataset)
fmt.Println(j)
}
func Example_streamingFrames() {
frameChan := make(chan *frame.Frame)
// Go routine to handle streaming frames as they are parsed. This may be
// useful when parsing a large DICOM with many frames from a network source,
// where image frames can start to be processed before the entire DICOM
// is parsed (or even read from storage).
go func() {
for fr := range frameChan {
fmt.Println(fr)
}
}()
dataset, _ := dicom.ParseFile("testdata/1.dcm", frameChan)
fmt.Println(dataset) // The full dataset
}
func Example_getImageFrames() {
dataset, _ := dicom.ParseFile("testdata/1.dcm", nil)
pixelDataElement, _ := dataset.FindElementByTag(tag.PixelData)
pixelDataInfo := dicom.MustGetPixelDataInfo(pixelDataElement.Value)
for i, fr := range pixelDataInfo.Frames {
img, _ := fr.GetImage() // The Go image.Image for this frame
f, _ := os.Create(fmt.Sprintf("image_%d.jpg", i))
_ = jpeg.Encode(f, img, &jpeg.Options{Quality: 100})
_ = f.Close()
}
}
func runForEveryTestFile(t *testing.T, testFunc func(t *testing.T, filename string)) {
files, err := os.ReadDir("./testdata")
if err != nil {
t.Fatalf("unable to read testdata/: %v", err)
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".dcm") {
fullName := "./testdata/" + f.Name()
t.Run(fullName, func(t *testing.T) {
testFunc(t, fullName)
})
}
}
}