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 collapse

MAGIC =

Valid Magic codes for the NIFTI Header

%w{ni1 n+1}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = nil, options = {}) ⇒ NRead

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



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

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

#extended_headerObject (readonly)

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



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

def extended_header
  @extended_header
end

#hdrObject (readonly)

A hash containing header attributes.



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

def hdr
  @hdr
end

#image_narrayObject (readonly)

A narray of image values reshapred to image dimensions



18
19
20
# File 'lib/nifti/n_read.rb', line 18

def image_narray
  @image_narray
end

#image_rubyarrayObject (readonly)

An array of decoded image values



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

def image_rubyarray
  @image_rubyarray
end

#msgObject (readonly)

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



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

def msg
  @msg
end

#successObject (readonly)

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



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

def success
  @success
end

Instance Method Details

#get_image_narray(image_array, dim) ⇒ Object

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.



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

def get_image_narray(image_array, dim)
  if Object.const_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

#read_imageObject

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.



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

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