Class: NIFTI::NRead
- Inherits:
-
Object
- Object
- NIFTI::NRead
- 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
-
#extended_header ⇒ Object
readonly
An array of nifti header extension hashes with keys esize, ecode and data.
-
#hdr ⇒ Object
readonly
A hash containing header attributes.
-
#image_narray ⇒ Object
readonly
A narray of image values reshapred to image dimensions.
-
#image_rubyarray ⇒ Object
readonly
An array of decoded image values.
-
#msg ⇒ Object
readonly
An array which records any status messages that are generated while parsing the DICOM string.
-
#success ⇒ Object
readonly
A boolean which reports whether the NIFTI string was parsed successfully (true) or not (false).
Instance Method Summary collapse
-
#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.
-
#initialize(source = nil, options = {}) ⇒ NRead
constructor
Create a NRead object to parse a nifti file or binary string and set header and image info instance variables.
-
#read_image ⇒ Object
Unpack an image array from vox_offset to the end of a nifti file.
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, ={}) [:image] = true if [:narray] @msg = [] @success = false set_stream(source, ) parse_header() return self end |
Instance Attribute Details
#extended_header ⇒ Object (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 |
#hdr ⇒ Object (readonly)
A hash containing header attributes.
12 13 14 |
# File 'lib/nifti/n_read.rb', line 12 def hdr @hdr end |
#image_narray ⇒ Object (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_rubyarray ⇒ Object (readonly)
An array of decoded image values
16 17 18 |
# File 'lib/nifti/n_read.rb', line 16 def image_rubyarray @image_rubyarray end |
#msg ⇒ Object (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 |
#success ⇒ Object (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_image ⇒ Object
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 |