Class: NIFTI::NRead

Inherits:
Object
  • Object
show all
Defined in:
lib/nifti/n_read.rb

Overview

The NRead class parses the NIFTI data from a binary string.

Constant Summary

MAGIC =

Valid Magic codes for the NIFTI Header

%w{ni1

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (NRead) initialize(source = nil, options = {})

Create a NRead object to parse a nifti file or binary string and set header and image info instance variables.

The nifti header will be checked for validity (header size and magic number) and will raise an IOError if invalid.

NIFTI header extensions are not yet supported and are not included in the header.

The header and image are accessible via the hdr and image instance variables. An optional narray matrix may also be available in image_narray if desired by passing in :narray => true as an option.

Parameters

  • source ??? A string which specifies either the path of a NIFTI file to be loaded, or a binary NIFTI string to be parsed.

  • options ??? A hash of parameters.

Options

  • :bin ??? Boolean. If set to true, string parameter will be interpreted as a binary NIFTI string, and not a path string, which is the default behaviour.

  • :image ??? Boolean. If set to true, automatically load the image into @image, otherwise only a header is collected and you can get an image

  • :narray ??? Boolean. If set to true, a properly shaped narray matrix will be set in the instance variable @image_narray. Automatically sets :image => true



40
41
42
43
44
45
46
47
48
# File 'lib/nifti/n_read.rb', line 40

def initialize(source=nil, options={})
  options[:image] = true if options[:narray]
  @msg = []
  @success = false
  set_stream(source, options)
  parse_header(options)
  
  return self
end

Instance Attribute Details

- (Object) extended_header (readonly)

An array of nifti header extension hashes with keys esize, ecode and data.



12
13
14
# File 'lib/nifti/n_read.rb', line 12

def extended_header
  @extended_header
end

- (Object) hdr (readonly)

A hash containing header attributes.



10
11
12
# File 'lib/nifti/n_read.rb', line 10

def hdr
  @hdr
end

- (Object) image_narray (readonly)

A narray of image values reshapred to image dimensions



16
17
18
# File 'lib/nifti/n_read.rb', line 16

def image_narray
  @image_narray
end

- (Object) image_rubyarray (readonly)

An array of decoded image values



14
15
16
# File 'lib/nifti/n_read.rb', line 14

def image_rubyarray
  @image_rubyarray
end

- (Object) msg (readonly)

An array which records any status messages that are generated while parsing the DICOM string.



6
7
8
# File 'lib/nifti/n_read.rb', line 6

def msg
  @msg
end

- (Object) success (readonly)

A boolean which reports whether the NIFTI string was parsed successfully (true) or not (false).



8
9
10
# File 'lib/nifti/n_read.rb', line 8

def success
  @success
end

Instance Method Details

- (Object) get_image_narray(image_array, dim)

Create an narray if the NArray is available Tests if a file is readable, and if so, opens it.

Parameters

  • image_array ??? Array. A vector of image data.

  • dim ??? Array. The dim array from the nifti header, specifing number of dimensions (dim) and dimension length of other dimensions to reshape narray into.



74
75
76
77
78
79
80
# File 'lib/nifti/n_read.rb', line 74

def get_image_narray(image_array, dim)
  if defined? NArray
    @image_narray = pixel_data = NArray.to_na(image_array).reshape!(*dim[1..dim[0]])
  else
    add_msg "Can't find NArray, no image_narray created.  Please `gem install narray`"
  end
end

- (Object) read_image

Unpack an image array from vox_offset to the end of a nifti file.

Parameters

There are no parameters - this reads from the binary string in the @string instance variable.

This sets @image_rubyarray to the image data vector and also returns it.



58
59
60
61
62
63
64
# File 'lib/nifti/n_read.rb', line 58

def read_image
  raw_image = []
  @stream.index = @hdr['vox_offset']
  type = NIFTI_DATATYPES[@hdr['datatype']]
  format = @stream.format[type]
  @image_rubyarray = @stream.decode(@stream.rest_length, type)
end