From 4f30d7ccc2e234eb285a58b5dd17c56d6a3201fa Mon Sep 17 00:00:00 2001 From: Thomas Pham Date: Wed, 21 Sep 2022 10:34:39 +0700 Subject: [PATCH] Update/get tag (#6) * add method to find value from tag * update Export --- conversion.go | 39 +++++++++++++++++++++++++++----------- internal/utils/utils.go | 15 ++++++++++++++- parse.go | 37 ++++++++++++++++++++---------------- parse_test.go | 24 ++++++++++++++++++----- pkg/dicom/element/value.go | 4 ---- pkg/dicom/tag/tag.go | 5 +++++ 6 files changed, 87 insertions(+), 37 deletions(-) diff --git a/conversion.go b/conversion.go index 35d607f2..07d4c2c2 100644 --- a/conversion.go +++ b/conversion.go @@ -1,20 +1,25 @@ package go2com import ( + "fmt" "github.com/okieraised/go2com/internal/utils" "github.com/okieraised/go2com/pkg/dicom/element" + "github.com/okieraised/go2com/pkg/dicom/tag" "strings" ) -type TagBrowser struct { - VR string `json:"vr"` - Value interface{} `json:"Value"` -} +type MappedTag map[string]tag.TagBrowser -type mappedTag map[string]TagBrowser +// Export returns the mapped tag/(vr,value) dictionary +func (p *Parser) Export(exportMeta bool) MappedTag { + res := make(MappedTag) + if exportMeta { + mt := p.metadata + for _, elem := range mt.Elements { + res.mapElement(elem) + } + } -func (p *Parser) Export() map[string]TagBrowser { - res := make(mappedTag) ds := p.dataset for _, elem := range ds.Elements { vrStr := elem.ValueRepresentationStr @@ -26,8 +31,20 @@ func (p *Parser) Export() map[string]TagBrowser { return res } +// GetElementByTagString returns the element value of the input tag +// Tag should be in (gggg,eeee) or ggggeeee format +func (m MappedTag) GetElementByTagString(tagStr string) (interface{}, error) { + tagStr = utils.FormatTag(tagStr) + + result, ok := m[tagStr] + if !ok { + return nil, fmt.Errorf("tag not found: %s", tagStr) + } + return result.Value, nil +} + // mapElement returns a map[string]interface{} with key as tag and value as the tag values -func (m mappedTag) mapElement(elem *element.Element) { +func (m MappedTag) mapElement(elem *element.Element) { tagStr := elem.Tag.StringWithoutParentheses() vrStr := elem.ValueRepresentationStr var vl interface{} @@ -42,12 +59,12 @@ func (m mappedTag) mapElement(elem *element.Element) { return } groupTag := vlArr[0].Tag.StringWithoutParentheses() - subElemGrp := make(mappedTag) + subElemGrp := make(MappedTag) for index, subVl := range vlArr { subTag := subVl.Tag.StringWithoutParentheses() if subTag == groupTag && index > 0 { subVL = append(subVL, subElemGrp) - subElemGrp = mappedTag{} + subElemGrp = MappedTag{} } subElemGrp.mapElement(subVl) if index == len(vlArr)-1 { @@ -59,7 +76,7 @@ func (m mappedTag) mapElement(elem *element.Element) { } else { vl = utils.AppendToSlice(elem.Value) } - m[tagStr] = TagBrowser{ + m[tagStr] = tag.TagBrowser{ VR: vrStr, Value: vl, } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index ffd3189b..5080234c 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -1,6 +1,9 @@ package utils -import "reflect" +import ( + "reflect" + "strings" +) func AppendToSlice(vl interface{}) []interface{} { res := make([]interface{}, 0) @@ -15,3 +18,13 @@ func AppendToSlice(vl interface{}) []interface{} { } return res } + +func FormatTag(tagStr string) string { + tagStr = strings.ReplaceAll(tagStr, "(", "") + tagStr = strings.ReplaceAll(tagStr, ")", "") + tagStr = strings.ReplaceAll(tagStr, ",", "") + tagStr = strings.TrimSpace(tagStr) + tagStr = strings.ToUpper(tagStr) + + return tagStr +} diff --git a/parse.go b/parse.go index 9140b924..85556d15 100644 --- a/parse.go +++ b/parse.go @@ -5,13 +5,14 @@ import ( "bytes" "fmt" "github.com/okieraised/go2com/internal/constants" + "github.com/okieraised/go2com/internal/utils" "github.com/okieraised/go2com/pkg/dicom/dataset" "github.com/okieraised/go2com/pkg/dicom/element" "github.com/okieraised/go2com/pkg/dicom/reader" "github.com/okieraised/go2com/pkg/dicom/tag" "github.com/okieraised/go2com/pkg/dicom/uid" "io" - "strconv" + "strings" ) // Parser implements the field required to parse the dicom file @@ -56,32 +57,36 @@ func (p *Parser) Parse() error { return nil } +// GetMetadata returns the file meta header func (p *Parser) GetMetadata() dataset.Dataset { return p.metadata } +// GetDataset returns the dataset func (p *Parser) GetDataset() dataset.Dataset { return p.dataset } -func (p *Parser) ConvertToMap() map[string]element.Element { - res := map[string]element.Element{} - fileMeta := p.GetMetadata() - for _, elem := range fileMeta.Elements { - res[elem.TagName] = *elem - } +// GetElementByTagString returns the element value of the input tag +// Tag should be in (gggg,eeee) or ggggeeee format +func (p *Parser) GetElementByTagString(tagStr string) (interface{}, error) { + tagStr = utils.FormatTag(tagStr) - fileDataset := p.GetDataset() - for _, elem := range fileDataset.Elements { - i := 0 - if elem.TagName == constants.PrivateTag { - res[elem.TagName+"_"+strconv.Itoa(i)] = *elem - i++ - } else { - res[elem.TagName] = *elem + if strings.HasPrefix(tagStr, "0002") { + for _, elem := range p.metadata.Elements { + if tagStr == elem.Tag.StringWithoutParentheses() { + return elem.Value, nil + } + } + return nil, fmt.Errorf("cannot find tag %s", tagStr) + } else { + for _, elem := range p.dataset.Elements { + if tagStr == elem.Tag.StringWithoutParentheses() { + return elem.Value, nil + } } + return nil, fmt.Errorf("cannot find tag %s", tagStr) } - return res } //---------------------------------------------------------------------------------------------------------------------- diff --git a/parse_test.go b/parse_test.go index cfd14416..d09bdc3f 100644 --- a/parse_test.go +++ b/parse_test.go @@ -14,8 +14,8 @@ const ( //filePath = "./test_data/File 1.dcm" //filePath = "./test_data/File 11636.dcm" //filePath = "./test_data/File 32000" - filePath = "./test_data/File 4000.dcm" - //filePath = "./test_data/File 8000" + //filePath = "./test_data/File 4000.dcm" + filePath = "./test_data/File 8000" //filePath = "./test_data/File 12000" //filePath = "./test_data/File 160.dcm" ) @@ -35,7 +35,7 @@ func TestNewParser(t *testing.T) { fileSize := info.Size() //------------------------------------------------------------------------------------------------------------------ - parser, err := NewParser(file, fileSize, true, false) + parser, err := NewParser(file, fileSize, false, false) if err != nil { fmt.Println(err) return @@ -45,11 +45,26 @@ func TestNewParser(t *testing.T) { fmt.Println(err) return } - mapTag := parser.Export() + + val2, err := parser.GetElementByTagString("7fe0,0010") + if err != nil { + fmt.Println(err) + return + } + fmt.Println("VAL2", val2) + + mapTag := parser.Export(true) //for key := range mapTag { // fmt.Println(key, mapTag[key]) //} + val, err := mapTag.GetElementByTagString("(0008,0008)") + if err != nil { + fmt.Println(err) + return + } + fmt.Println("VAL", val) + //------------------------------------------------------------------------------------------------------------------ b, err := json.Marshal(mapTag) if err != nil { @@ -81,7 +96,6 @@ func TestNewParser(t *testing.T) { //for _, d := range mt.Elements { // fmt.Println("res", d) //} - // //ds := parser.GetDataset() //for _, d := range ds.Elements { diff --git a/pkg/dicom/element/value.go b/pkg/dicom/element/value.go index 9ea28133..3ce2024b 100644 --- a/pkg/dicom/element/value.go +++ b/pkg/dicom/element/value.go @@ -9,7 +9,3 @@ type Val struct { func (v *Val) ToArray() { } - -//func (val Value) ToString() { -// -//} diff --git a/pkg/dicom/tag/tag.go b/pkg/dicom/tag/tag.go index c2936998..ff825c0e 100644 --- a/pkg/dicom/tag/tag.go +++ b/pkg/dicom/tag/tag.go @@ -10,6 +10,11 @@ const ( TagUnknown = "unknown_tag" ) +type TagBrowser struct { + VR string `json:"vr"` + Value interface{} `json:"Value"` +} + type DicomTag struct { Group uint16 Element uint16