gonii

package module
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2023 License: BSD-3-Clause Imports: 9 Imported by: 0

README

gonii

Standalone, pure golang NIfTI file parser that provide functionalities to read and write NIfTI file format (including NIfTI-1 and NIfTI-2)

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

To install this package, run:

go get github.com/okieraised/gonii

To parse a single NIfTI file:

package main

import (
	"fmt"
	"github.com/okieraised/gonii"
)

func main() {
	filePath := "./test_data/int16.nii.gz"

	// Init new reader with option to keep the header structure after parsing
	rd, err := gonii.NewNiiReader(gonii.WithReadImageFile(filePath), gonii.WithReadRetainHeader(true))
	if err != nil {
		panic(err)
	}

	// Parse the image
	err = rd.Parse()
	if err != nil {
		panic(err)
	}

	// Access the NIfTI image structure
	img := rd.GetNiiData()

	// to see the raw byte data
	fmt.Println(img.Volume[60000:70000])

	// Transform the raw byte slices to a 1-D slice of voxel value in float64
	voxels := rd.GetNiiData().GetVoxels()
	// to see the transformed value as a float64 slice
	fmt.Println(voxels.GetDataset()[60000:70000])
}

TODO

  • Improve NIfTI reader parsing speed for large file size
  • Add support for NIfTI writer to export data as NIfTI-2 format
  • Support overlapping bitmap annotations

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNiiReader

func NewNiiReader(options ...func(*nifti.NiiReader) error) (nifti.Reader, error)

NewNiiReader returns a new NIfTI reader

Options:

  • `WithReadInMemory(inMemory bool)` : Read the whole file into memory
  • `WithReadRetainHeader(retainHeader bool)` : Whether to retain the header structure after parsing
  • `WithReadHeaderFile(headerFile string)` : Specify a header file path in case of separate .hdr/.img file
  • `WithReadImageFile(niiFile string)` : Specify an image file path
  • `WithReadImageReader(r *bytes.Reader)` : Specify a header file reader in case of separate .hdr/.img file
  • `WithReadHeaderReader(r *bytes.Reader)` : Specify an image file reader

func NewNiiWriter

func NewNiiWriter(filePath string, options ...func(*nifti.NiiWriter)) (nifti.Writer, error)

NewNiiWriter returns a new NIfTI writer. If no version is specified, the writer will default to write to NIfTI version 1

func WithReadHeaderFile added in v0.2.0

func WithReadHeaderFile(headerFile string) func(*nifti.NiiReader) error

WithReadHeaderFile allows option to specify the separate header file in case of NIfTI pair .hdr/.img

func WithReadHeaderReader added in v0.2.0

func WithReadHeaderReader(r *bytes.Reader) func(*nifti.NiiReader) error

WithReadHeaderReader allows option for users to specify the separate header file reader in case of NIfTI pair .hdr/.img

func WithReadImageFile added in v0.2.0

func WithReadImageFile(niiFile string) func(*nifti.NiiReader) error

WithReadImageFile allows option to specify the NIfTI file (.nii.gz or .nii)

func WithReadImageReader added in v0.2.0

func WithReadImageReader(r *bytes.Reader) func(*nifti.NiiReader) error

WithReadImageReader allows option for users to specify the NIfTI bytes reader (.nii.gz or .nii)

func WithReadInMemory added in v0.2.0

func WithReadInMemory(inMemory bool) func(*nifti.NiiReader) error

WithReadInMemory allows option to read the whole file into memory. The default is true. This is for future implementation. Currently, all file is read into memory before parsing

func WithReadRetainHeader added in v0.2.0

func WithReadRetainHeader(retainHeader bool) func(*nifti.NiiReader) error

WithReadRetainHeader allows option to keep the header after parsing instead of just keeping the NIfTI data structure

func WithWriteCompression added in v0.2.0

func WithWriteCompression(withCompression bool) func(writer *nifti.NiiWriter)

WithWriteCompression sets the option to write compressed NIfTI image to a single file (.nii.gz)

If true, the whole file will be compressed. Default is false.

func WithWriteHeaderFile

func WithWriteHeaderFile(writeHeaderFile bool) func(*nifti.NiiWriter)

WithWriteHeaderFile sets the option to write NIfTI image to a header/image (.hdr/.img) file pair

If true, output will be two files for the header and the image. Default is false.

func WithWriteNIfTIData added in v0.2.0

func WithWriteNIfTIData(data *nifti.Nii) func(writer *nifti.NiiWriter)

WithWriteNIfTIData sets the option to allow user to provide predefined NIfTI-1 data structure.

func WithWriteNii1Header added in v0.2.0

func WithWriteNii1Header(header *nifti.Nii1Header) func(*nifti.NiiWriter)

WithWriteNii1Header sets the option to allow user to provide predefined NIfTI-1 header structure.

If no header provided, the header will be converted from the NIfTI image structure

func WithWriteNii2Header added in v0.2.0

func WithWriteNii2Header(header *nifti.Nii2Header) func(*nifti.NiiWriter)

WithWriteNii2Header sets the option to allow user to provide predefined NIfTI-2 header structure.

If no header provided, the header will be converted from the NIfTI image structure

func WithWriteVersion added in v0.2.0

func WithWriteVersion(version int) func(writer *nifti.NiiWriter)

WithWriteVersion sets the option to specify the exported NIfTI version (NIfTI-1 or 2). Default is NIfTI-1

Types

type SegmentCoordinate added in v0.2.0

type SegmentCoordinate struct {
	Value any   `json:"value"`
	X     int64 `json:"x"`
	Y     int64 `json:"y"`
	Z     int64 `json:"z"`
	T     int64 `json:"t"`
}

SegmentCoordinate defines the structure for segmentation coordinate

type Segmentation added in v0.2.0

type Segmentation struct {
	// contains filtered or unexported fields
}

func NewSegmentation added in v0.2.0

func NewSegmentation(opts ...SegmentationOption) *Segmentation

func (*Segmentation) AnnotationJsonToNii added in v0.2.0

func (s *Segmentation) AnnotationJsonToNii() error

AnnotationJsonToNii converts the annotation coordinates (x,y,z,t) array to a corresponding NIfTI file

func (*Segmentation) AnnotationNiiToJson added in v0.2.0

func (s *Segmentation) AnnotationNiiToJson() error

AnnotationNiiToJson converts the NIfTI file to a corresponding annotation coordinates (x,y,z,t) array

type SegmentationOption added in v0.2.0

type SegmentationOption func(s *Segmentation)

func WithAnnotations added in v0.2.0

func WithAnnotations(annotations []SegmentCoordinate) SegmentationOption

WithAnnotations allows users to specify the annotation coordinates to convert to NIfTI file

func WithImage added in v0.2.0

func WithImage(image *nifti.Nii) SegmentationOption

WithImage allows users to specify the NIfTI image structure to convert to (x,y,z,t) coordinate array

func WithNii1Hdr added in v0.2.0

func WithNii1Hdr(hdr *nifti.Nii1Header) SegmentationOption

WithNii1Hdr allows users to specify NIfTI-1 header to write the annotation file to

If both NIfTI-1 and NIfTI-2 header are specified. NIfTI-2 header takes precedence

func WithNii2Hdr added in v0.2.0

func WithNii2Hdr(hdr *nifti.Nii2Header) SegmentationOption

WithNii2Hdr allows users to specify NIfTI-1 header to write the annotation file to

If both NIfTI-1 and NIfTI-2 header are specified. NIfTI-2 header takes precedence

func WithOutFile added in v0.2.0

func WithOutFile(outFile string) SegmentationOption

WithOutFile allows user to specify the location to write the segmentation data to file

func WithSegCompression added in v0.2.0

func WithSegCompression(compression bool) SegmentationOption

WithSegCompression allows user to write the segmentation as compressed NIfTI file

func WithSegmentRLE added in v0.4.0

func WithSegmentRLE(segments []nifti.SegmentRLE) SegmentationOption

WithSegmentRLE allows users to specify the annotation as RLE-encoded array to convert to NIfTI file

Directories

Path Synopsis
examples
internal
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL