Skip to content

Commit

Permalink
update init tag dict, refine nifti test (#25)
Browse files Browse the repository at this point in the history
* update init tag dict, refine nifti test

# Conflicts:
#	nifti_test.go
#	parse.go

* commit NewDICOMReader

* tmp skip

Co-authored-by: phuctt <[email protected]>
  • Loading branch information
iamatsundere and phuctt authored Jan 6, 2023
1 parent e0c4c85 commit 060f629
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 30 deletions.
5 changes: 4 additions & 1 deletion nifti_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func TestNii1(t *testing.T) {
//filePath = "/home/tripg/Documents/nifti/ExBox11/structural_brain.nii.gz"
//filePath = "/home/tripg/Documents/nifti/JHU_MNI_SS_T1.nii.gz"
//filePath = "/home/tripg/Documents/nifti/avg152T1_LR_nifti2.nii.gz"
filePath = "/Users/TriPham/Downloads/avg152T1_RL_nifti.nii"
filePath = "/home/tripg/Documents/nifti/native_brain_mask.nii.gz"
filePath = "/home/tripg/Documents/nifti/task001_run003/bold.nii.gz"
filePath = "/home/tripg/Documents/nifti/113-02-FLAIR_processed_reg_withskull.nii.gz"
filePath = "/home/tripg/Documents/nifti/ds107_sub001_highres.nii"

niiReader, err := reader.NewNiiReader(filePath)
assert.NoError(err)
Expand Down
102 changes: 76 additions & 26 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"

"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/tag"
"github.com/okieraised/go2com/pkg/dicom/uid"
"io"
"strings"
)

// Parser implements the field required to parse the dicom file
type Parser struct {
filePath string
fileContent []byte
fileReader *io.Reader
reader reader.DcmReader
dataset dataset.Dataset
metadata dataset.Dataset
Expand All @@ -26,11 +30,13 @@ type Parser struct {
skipPixelData bool
}

// Deprecated: this initialization will be triggered by calling init() in tag pkg
func InitTagDict() {
tag.InitTagDict()
}

// NewParser returns a new dicom parser
// Deprecated: NewParser will be replaced by NewDCMFileParser
func NewParser(fileReader io.Reader, fileSize int64, skipPixelData, skipDataset bool) (*Parser, error) {
dcmReader := reader.NewDcmReader(bufio.NewReader(fileReader), skipPixelData)
parser := Parser{
Expand All @@ -42,41 +48,65 @@ func NewParser(fileReader io.Reader, fileSize int64, skipPixelData, skipDataset
return &parser, nil
}

func (p *Parser) Parse() error {
defer func() error {
if r := recover(); r != nil {
return fmt.Errorf("handled panic caused by the library: %s", fmt.Sprint(r))
}
return nil
}()

p.setFileSize()
err := p.validateDicom()
// NewDCMFileParser creates new parser from input file path with default options or with user-specified options
func NewDCMFileParser(filePath string, options ...func(*Parser)) (*Parser, error) {
f, err := os.Open(filePath)
if err != nil {
return err
panic(err)
}
err = p.parseMetadata()

fInfo, err := f.Stat()
if err != nil {
return err
return nil, err
}
if p.skipDataset {
return nil
dcmReader := reader.NewDICOMReader(bufio.NewReader(f))
parser := &Parser{
reader: dcmReader,
fileSize: fInfo.Size(),
}
err = p.parseDataset()
if err != nil {
return err
for _, opt := range options {
opt(parser)
}
return nil

return parser, nil
}

func WithSkipPixelData(skipPixelData bool) func(*Parser) {
return func(s *Parser) {
s.skipPixelData = skipPixelData
}
}

func WithSkipDataset(skipPixelDataset bool) func(*Parser) {
return func(s *Parser) {
s.skipPixelData = skipPixelDataset
}
}

//----------------------------------------------------------------------------------------------------------------------

func (p *Parser) Parse() error {
return p.parse()
}

// GetMetadata returns the file meta header
func (p *Parser) GetMetadata() dataset.Dataset {
return p.metadata
func (p *Parser) GetMetadata() (*dataset.Dataset, error) {
err := p.parse()
if err != nil {
return nil, err
}

return &p.metadata, nil
}

// GetDataset returns the dataset
func (p *Parser) GetDataset() dataset.Dataset {
return p.dataset
func (p *Parser) GetDataset() (*dataset.Dataset, error) {
err := p.parse()
if err != nil {
return nil, err
}

return &p.dataset, nil
}

// GetElementByTagString returns the element value of the input tag
Expand Down Expand Up @@ -105,6 +135,26 @@ func (p *Parser) GetElementByTagString(tagStr string) (interface{}, error) {
// Unexported methods
//----------------------------------------------------------------------------------------------------------------------

func (p *Parser) parse() error {
p.setFileSize()
err := p.validateDicom()
if err != nil {
return err
}
err = p.parseMetadata()
if err != nil {
return err
}
if p.skipDataset {
return nil
}
err = p.parseDataset()
if err != nil {
return err
}
return nil
}

// setFileSize sets the file size to the reader
func (p *Parser) setFileSize() {
_ = p.reader.SetFileSize(p.fileSize)
Expand Down
13 changes: 13 additions & 0 deletions pkg/dicom/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ type dcmReader struct {
fileSize int64
}

func NewDICOMReader(reader *bufio.Reader, options ...func(*dcmReader)) DcmReader {
parser := &dcmReader{
reader: reader,
binaryOrder: binary.LittleEndian,
isImplicit: false,
skipPixelData: false,
}
for _, opt := range options {
opt(parser)
}
return parser
}

func NewDcmReader(reader *bufio.Reader, skipPixelData bool) DcmReader {
return &dcmReader{
reader: reader,
Expand Down
11 changes: 8 additions & 3 deletions pkg/dicom/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,16 @@ func FindByName(name string) (TagInfo, error) {
return TagInfo{}, fmt.Errorf("could not find tag %s", name)
}

var lock = &sync.RWMutex{}
var once = sync.Once{}

// init generates pre-defined tags as a dictionary
func init() {
once.Do(func() {
InitTagDict()
})
}

func InitTagDict() map[DicomTag]TagInfo {
lock.Lock()
defer lock.Unlock()
if TagDict == nil {
initTag()
}
Expand Down

0 comments on commit 060f629

Please sign in to comment.