Skip to content

Commit

Permalink
Added support for NIfTI and refactored DICOM reader (#29)
Browse files Browse the repository at this point in the history
Co-authored-by: phuctt <[email protected]>
  • Loading branch information
okieraised and phuctt authored Jan 18, 2023
1 parent 060f629 commit ce17fc3
Show file tree
Hide file tree
Showing 65 changed files with 2,565 additions and 2,032 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
test_data
.idea
40 changes: 14 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
# go2com (DICOM image parser)
# go2com (DICOM and NIFTI image parser)

** This package is under active development and can be in a very broken state. Please use the latest released version **

## TODO
- [ ] Improve NIfTI reader for large file size
- [ ] Improve NIfTI writer to export as NIfTI-2 format
- [ ] Improve DICOM frame parser
- [ ] Support for additional medical image formats
- [ ] Support changing value in NIfTI frame

## Example
To parse a DICOM file, import the package as `github.com/okieraised/go2com`
To parse a DICOM file
```go
package main

import (
"log"
"os"

"github.com/okieraised/go2com"
)
```

func main() {
file, err := os.Open("/path_to_dcm_file")
if err != nil {
log.Fatal(err)
}
defer file.Close()
info, err := file.Stat()
if err != nil {
log.Fatal(err)
}
fileSize := info.Size()
To parse a single NIfTI file:
```go

parser, err := go2com.NewParser(file, fileSize, false, false)
if err != nil {
log.Fatal(err)
}
err = parser.Parse()
}
```

## Supported Transfer Syntaxes
Expand Down
28 changes: 0 additions & 28 deletions conversion_test.go

This file was deleted.

150 changes: 150 additions & 0 deletions dicom_reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package go2com

import (
"bufio"
"fmt"
"github.com/okieraised/go2com/internal/utils"
"github.com/okieraised/go2com/pkg/dicom/dcm_io"
"github.com/stretchr/testify/assert"
_ "image/jpeg"
"os"
"strings"
"testing"
)

func Test_ReaderProfiling(t *testing.T) {
assert := assert.New(t)
f, err := os.Open("./test_data/01.dcm")
assert.NoError(err)

fInfo, err := f.Stat()
assert.NoError(err)

fn := func() {
dcmReader := dcm_io.NewDICOMReader(bufio.NewReader(f), dcm_io.WithSkipPixelData(false), dcm_io.WithSkipDataset(false), dcm_io.WithSetFileSize(fInfo.Size()))
err = dcmReader.Parse()
assert.NoError(err)
}

err = utils.CPUProfilingFunc(fn, "./cpu1.pprof")
assert.NoError(err)
}

func Test_NewParser1(t *testing.T) {
assert := assert.New(t)
filePaths, err := utils.ReadDirRecursively("/home/tripg/workspace/dicom/test_data")
assert.NoError(err)
for _, fPath := range filePaths {
fmt.Println("process:", fPath)

f, err := os.Open(fPath)
assert.NoError(err)

fInfo, err := f.Stat()
assert.NoError(err)

dcmReader := dcm_io.NewDICOMReader(bufio.NewReader(f), dcm_io.WithSetFileSize(fInfo.Size()))

err = dcmReader.Parse()
if err != nil {
fmt.Println(err)
return
}
assert.NoError(err)
}
}

func Test_NewParser2(t *testing.T) {
assert := assert.New(t)
filePaths, err := utils.ReadDirRecursively("/home/tripg/workspace/dicom/test_full")
assert.NoError(err)
for _, fPath := range filePaths {
fmt.Println("process:", fPath)
f, err := os.Open(fPath)
assert.NoError(err)

fInfo, err := f.Stat()
assert.NoError(err)

dcmReader := dcm_io.NewDICOMReader(bufio.NewReader(f), dcm_io.WithSetFileSize(fInfo.Size()))

err = dcmReader.Parse()
if err != nil {
if strings.Contains(err.Error(), "not in valid dicom format") {
continue
}
}
assert.NoError(err)
}
}

func Test_NewParser3(t *testing.T) {
assert := assert.New(t)
filePaths, err := utils.ReadDirRecursively("/home/tripg/workspace/dicom/mammo_dicoms")
assert.NoError(err)
for _, fPath := range filePaths {
fmt.Println("process:", fPath)

f, err := os.Open(fPath)
assert.NoError(err)

fInfo, err := f.Stat()
assert.NoError(err)

dcmReader := dcm_io.NewDICOMReader(bufio.NewReader(f), dcm_io.WithSetFileSize(fInfo.Size()))

err = dcmReader.Parse()
assert.NoError(err)

_ = dcmReader.ExportDatasetTags(false)
}
}

func Test_NewParser4(t *testing.T) {
assert := assert.New(t)
fPath := "/home/tripg/workspace/dicom/mammo_dicoms/1.2.840.113619.2.255.10452022879169.3670200508103440.2701.dicom"
f, err := os.Open(fPath)
assert.NoError(err)

fInfo, err := f.Stat()
assert.NoError(err)

parser := dcm_io.NewDICOMReader(bufio.NewReader(f), dcm_io.WithSkipPixelData(true), dcm_io.WithSkipDataset(true), dcm_io.WithSetFileSize(fInfo.Size()))
err = parser.Parse()
assert.NoError(err)

for _, elem := range parser.GetDataset().Elements {
fmt.Println(elem)
}
}

func Test_NewParser5(t *testing.T) {
assert := assert.New(t)
fPath := "/home/tripg/workspace/10142022/ALI_Technologies/UltraPACS/studies/w0019837/view0001"
f, err := os.Open(fPath)
assert.NoError(err)

fInfo, err := f.Stat()
assert.NoError(err)

parser := dcm_io.NewDICOMReader(bufio.NewReader(f), dcm_io.WithSkipPixelData(false), dcm_io.WithSkipDataset(false), dcm_io.WithSetFileSize(fInfo.Size()))
err = parser.Parse()
assert.NoError(err)

}

func Test_NewParser6(t *testing.T) {
assert := assert.New(t)
f, err := os.Open("/home/tripg/workspace/10142022/ALI_Technologies/UltraPACS/studies/w0055053/view0013")
//f, err = os.Open("/home/tripg/workspace/10142022/Acuson/Sequoia/EXAMS/EXAM0000/CLIPS/CLIP0031")
f, err = os.Open("/home/tripg/workspace/10142022/Hamamatsu/Dog_15x15_20x.dcm")
//f, err = os.Open("/home/tripg/workspace/dicom2/PrivateGEImplicitVRBigEndianTransferSyntax16Bits.dcm")
assert.NoError(err)

fInfo, err := f.Stat()
assert.NoError(err)

parser := dcm_io.NewDICOMReader(bufio.NewReader(f), dcm_io.WithSkipPixelData(false), dcm_io.WithSkipDataset(false), dcm_io.WithSetFileSize(fInfo.Size()))
err = parser.Parse()
assert.NoError(err)
}
Binary file added dicom_test/01.dcm
Binary file not shown.
Binary file added dicom_test/010.dcm
Binary file not shown.
Binary file added dicom_test/011.dcm
Binary file not shown.
Binary file added dicom_test/012.dcm
Binary file not shown.
Binary file added dicom_test/013.dcm
Binary file not shown.
Binary file added dicom_test/014.dcm
Binary file not shown.
Binary file added dicom_test/015.DCM
Binary file not shown.
Binary file added dicom_test/016.dcm
Binary file not shown.
Binary file added dicom_test/017.dcm
Binary file not shown.
Binary file added dicom_test/018.dcm
Binary file not shown.
Binary file added dicom_test/019.dcm
Binary file not shown.
Binary file added dicom_test/02.dcm
Binary file not shown.
Binary file added dicom_test/020.dcm
Binary file not shown.
Binary file added dicom_test/021.dcm
Binary file not shown.
Binary file added dicom_test/022.dcm
Binary file not shown.
Binary file added dicom_test/023.dcm
Binary file not shown.
Binary file added dicom_test/024.dcm
Binary file not shown.
Binary file added dicom_test/025.dcm
Binary file not shown.
Binary file added dicom_test/026.dcm
Binary file not shown.
Binary file added dicom_test/027.dcm
Binary file not shown.
Binary file added dicom_test/03.dcm
Binary file not shown.
Binary file added dicom_test/04.dcm
Binary file not shown.
Binary file added dicom_test/05.dcm
Binary file not shown.
Binary file added dicom_test/06.dcm
Binary file not shown.
Binary file added dicom_test/07.dcm
Binary file not shown.
Binary file added dicom_test/08.dcm
Binary file not shown.
Binary file added dicom_test/09.dcm
Binary file not shown.
61 changes: 27 additions & 34 deletions example/example1.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
package main

import (
"fmt"
"github.com/okieraised/go2com"
"log"
"os"
)

func main() {
go2com.InitTagDict()
file, err := os.Open("./test_data/01.dcm")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
info, err := file.Stat()
if err != nil {
fmt.Println(err)
return
}
fileSize := info.Size()

parser, err := go2com.NewParser(file, fileSize, true, false)
if err != nil {
log.Fatal(err)
return
}
err = parser.Parse()
if err != nil {
log.Fatal(err)
return
}
for _, elem := range parser.GetDataset().Elements {
fmt.Println(elem)
}
//go2com.InitTagDict()
//file, err := os.Open("./test_data/01.dcm")
//if err != nil {
// fmt.Println(err)
// return
//}
//defer file.Close()
//info, err := file.Stat()
//if err != nil {
// fmt.Println(err)
// return
//}
//fileSize := info.Size()
//
//parser, err := go2com.NewParser(file, fileSize, true, false)
//if err != nil {
// log.Fatal(err)
// return
//}
//err = parser.Parse()
//if err != nil {
// log.Fatal(err)
// return
//}
//for _, elem := range parser.GetDataset().Elements {
// fmt.Println(elem)
//}
}
20 changes: 0 additions & 20 deletions internal/constants/constants.go

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions internal/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func init() {
switch buf {
case [2]byte{0xCD, 0xAB}:
NativeEndian = binary.LittleEndian

case [2]byte{0xAB, 0xCD}:
NativeEndian = binary.BigEndian
default:
Expand Down
9 changes: 9 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
fpath = '/home/tripg/Documents/dicom/test_full/023.dcm'
fpath = '/home/tripg/Documents/dicom/10142022/ALI_Technologies/UltraPACS/studies/w0019837/view0001'
fpath = '/home/tripg/Documents/dicom/vinlab/Mini-batch0/1.3.12.2.1107.5.1.7.112561.30000019122607094739800003704/DICOM/1.3.12.2.1107.5.1.7.112561.30000019122622575003400000100.dcm'
fpath = '/home/tripg/Downloads/123.241606668321866.1722978010541148/DICOM/1.2.840.113619.2.427.84108138632.1643160910.120.dicom'
fpath = '/home/tripg/workspace/1 0142022/ALI_Technologies/UltraPACS/studies/w0055053/view0013'
fpath = '/home/tripg/workspace/10142022/Acuson/Sequoia/EXAMS/EXAM0000/CLIPS/CLIP0031'
fpath = '/home/tripg/workspace/10142022/Hamamatsu/Dog_15x15_20x.dcm'
fpath = '/home/tripg/Downloads/N2D0027.dcm'
fpath = '/home/tripg/Downloads/123.241606668321866.1724728615648318_en.dcm'
fpath = '/home/tripg/Downloads/1-1.dcm'
fpath = '/home/tripg/workspace/dicom3/winter.dcm'
fpath = '/home/tripg/Downloads/PrivateGEImplicitVRBigEndianTransferSyntax16Bits.dcm'

ds = dcmread(fpath, force=True)
print(ds)
Expand Down
Loading

0 comments on commit ce17fc3

Please sign in to comment.