Skip to content

Commit

Permalink
Update/get tag (#6)
Browse files Browse the repository at this point in the history
* add method to find value from tag

* update Export
  • Loading branch information
okieraised authored Sep 21, 2022
1 parent 6faced5 commit 4f30d7c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 37 deletions.
39 changes: 28 additions & 11 deletions conversion.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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{}
Expand All @@ -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 {
Expand All @@ -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,
}
Expand Down
15 changes: 14 additions & 1 deletion internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package utils

import "reflect"
import (
"reflect"
"strings"
)

func AppendToSlice(vl interface{}) []interface{} {
res := make([]interface{}, 0)
Expand All @@ -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
}
37 changes: 21 additions & 16 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

//----------------------------------------------------------------------------------------------------------------------
Expand Down
24 changes: 19 additions & 5 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions pkg/dicom/element/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ type Val struct {
func (v *Val) ToArray() {

}

//func (val Value) ToString() {
//
//}
5 changes: 5 additions & 0 deletions pkg/dicom/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4f30d7c

Please sign in to comment.