This repository has been archived by the owner on Nov 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
queryretrieve.go
249 lines (233 loc) · 5.96 KB
/
queryretrieve.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
package dicom
import (
"fmt"
"regexp"
"strconv"
"github.com/gobwas/glob"
"github.com/grailbio/go-dicom/dicomtag"
)
func querySequence(elem *Element, f *Element) (match bool, err error) {
// TODO(saito) Implement!
return true, nil
}
func matchString(pattern string, value string) (bool, error) {
g, err := glob.Compile(pattern)
if err != nil {
return false, err
}
return g.Match(value), nil
}
func queryElement(elem *Element, f *Element) (match bool, err error) {
if isEmptyQuery(f) {
// Universal match
return true, nil
}
if f.VR == "SQ" {
return querySequence(f, elem)
}
if elem == nil {
// TODO(saito) This is probably wrong. We shouldn't distinguish between
// nonexistent element and an empty element.
return false, err
}
if f.VR != elem.VR {
// This shouldn't happen, but be paranoid
return false, fmt.Errorf("VR mismatch: filter %v, value %v", f, elem)
}
if f.VR == "UI" {
// See if elem contains at last one uid listed in the filter.
for _, expected := range f.Value {
e := expected.(string)
for _, value := range elem.Value {
if value.(string) == e {
return true, nil
}
}
}
return false, nil
}
// TODO: handle date-range matches
switch v := f.Value[0].(type) {
case int32:
for _, value := range elem.Value {
if v == value.(int32) {
return true, nil
}
}
case int16:
for _, value := range elem.Value {
if v == value.(int16) {
return true, nil
}
}
case uint32:
for _, value := range elem.Value {
if v == value.(uint32) {
return true, nil
}
}
case uint16:
for _, value := range elem.Value {
if v == value.(uint16) {
return true, nil
}
}
case float32:
for _, value := range elem.Value {
if v == value.(float32) {
return true, nil
}
}
case float64:
for _, value := range elem.Value {
if v == value.(float64) {
return true, nil
}
}
case string:
for _, value := range elem.Value {
ok, err := matchString(v, value.(string))
if err != nil {
return false, err
}
return ok, nil
}
default:
panic(fmt.Sprintf("Unknown data: %v", f))
}
return false, nil
}
func isEmptyQuery(f *Element) bool {
// Check if the glob pattern is a sequence of '*'s.
// "*" is the same as an empty query. P3.4, C2.2.2.4.
isUniversalGlob := func(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] != '*' {
return false
}
}
return true
}
if len(f.Value) == 0 {
return true
}
switch dicomtag.GetVRKind(f.Tag, f.VR) {
case dicomtag.VRBytes:
if len(f.Value[0].([]byte)) == 0 {
return true
}
case dicomtag.VRString, dicomtag.VRDate:
pattern := f.Value[0].(string)
if len(pattern) == 0 {
return true
}
if isUniversalGlob(pattern) {
return true
}
case dicomtag.VRStringList:
pattern := f.Value[0].(string)
if isUniversalGlob(pattern) {
return true
}
}
return false
}
// Query checks if the dataset matches a QR condition "f". If so, it returns the
// <true, matched element, nil>. If "f" asks for a universal match (i.e., empty
// query value), and the element for f.Tag doesn't exist, the function returns
// <true, nil, nil>. If "f" is malformed, the function returns <false, nil,
// error reason>.
func Query(ds *DataSet, f *Element) (match bool, matchedElem *Element, err error) {
if len(f.Value) > 1 {
// A filter can't contain multiple values. P3.4, C.2.2.2.1
return false, nil, fmt.Errorf("Multiple values found in filter '%v'", f)
}
if f.Tag == dicomtag.QueryRetrieveLevel || f.Tag == dicomtag.SpecificCharacterSet {
return true, nil, nil
}
elem, err := ds.FindElementByTag(f.Tag)
if err != nil {
elem = nil
}
match, err = queryElement(elem, f)
if match {
return true, elem, nil
}
return false, nil, err
}
const (
InvalidYear = -1
InvalidMonth = -1
InvalidDay = -1
)
// DateInfo is a result of parsing a date string.
type DateInfo struct {
// Input string.
Str string
// Results of parsing Str
Year int // Year (CE), in range [0,9999]. E.g., 2015
Month int // Month of year, in range [1,12].
Day int // Day of month, in range [1,31].
}
func (d DateInfo) String() string {
// Convert to ISO-8601
return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day)
}
var (
dateRangeRE = regexp.MustCompile("^([\\d.]*)-([\\d.]*)$")
// A date string can be 8-byte Date string of form 19930822 or 10-byte
// ACR-NEMA300 string of form "1993.08.22". The latter is not compliant
// according to P3.5 6.2, but it still happens in real life.
newDateRE = regexp.MustCompile("^(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)$")
oldDateRE = regexp.MustCompile("^(\\d\\d\\d\\d)\\.(\\d\\d)\\.(\\d\\d)$")
)
// ParseDate parses a date string or date-range string as defined for the VR
// type "DA". See
// https://www.medicalconnections.co.uk/kb/Dicom_Query_DateTime_range.
//
// If "s" is for a point in time, startDate will show that point, and endDate
// will be {Year:-1,Month:-1,Day:-1}. Is "s" is for a range of dates, [startDate,
// endDate] stores the range (note: both ends are closed, even though the DICOM
// spec isn't clear about it.).
func ParseDate(s string) (startDate, endDate DateInfo, err error) {
m := dateRangeRE.FindStringSubmatch(s)
if m != nil { // Date range.
if m[1] == "" {
startDate = DateInfo{"", 0, 1, 1}
} else {
startDate, _, err = ParseDate(m[1])
if err != nil {
return startDate, endDate, err
}
}
if m[2] == "" {
endDate = DateInfo{"", 9999, 12, 31}
} else {
endDate, _, err = ParseDate(m[2])
if err != nil {
return startDate, endDate, err
}
}
return startDate, endDate, nil
}
parseInt := func(v string) int {
i, e := strconv.Atoi(v)
if e != nil {
if err == nil {
err = e
}
return -1
}
return i
}
m = newDateRE.FindStringSubmatch(s)
if m == nil {
m = oldDateRE.FindStringSubmatch(s)
if m == nil {
return startDate, endDate, fmt.Errorf("%s: Failed to parse as date", s)
}
}
startDate = DateInfo{s, parseInt(m[1]), parseInt(m[2]), parseInt(m[3])}
endDate = DateInfo{"", InvalidYear, InvalidMonth, InvalidDay}
return startDate, endDate, err
}