class NIFTI::NObject

The NObject class is the main class for interacting with the NIFTI object. Reading from and writing to files is executed from instances of this class.

Attributes

errors[R]

An array which contain any notices/warnings/errors that have been recorded for the NObject instance.

extended_header[RW]

A hash of extended attributes

header[RW]

A hash of header information

image[RW]

An array or narray of image values

read_success[R]

A boolean which is set as true if a NIFTI file has been successfully read & parsed from a file (or binary string).

stream[R]

The Stream instance associated with this DObject instance (this attribute is mostly used internally).

write_success[R]

A boolean which is set as true if a DObject instance has been successfully written to file (or successfully encoded).

Public Class Methods

new(string=nil, options={}) click to toggle source

Creates an NObject instance (NObject is an abbreviation for “NIFTI object”).

The NObject instance holds references to the NIFTI Header and Image A NObject is typically built by reading and parsing a file or a binary string, but can also be built from an empty state by the user.

Parameters

  • string – A string which specifies either the path of a DICOM file to be loaded, or a binary DICOM string to be parsed. The parameter defaults to nil, in which case an empty DObject instance is created.

  • options – A hash of parameters.

Options

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

  • :syntax – String. If a syntax string is specified, the DRead class will be forced to use this transfer syntax when decoding the file/binary string.

  • :verbose – Boolean. If set to false, the NObject instance will run silently and not output warnings and error messages to the screen. Defaults to true.

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

  • :narray – Boolean. If set to true, the NObject will build a properly shaped narray from the image data.

Examples

# Load a NIFTI file's header information:
require 'nifti'
obj = NIFTI::NObject.new("test.nii")
# Read a NIFTI header and image into a numerical-ruby narray:
obj = Nfiti::NObject.new("test.nii", :image => true, :narray => true)
# Create an empty NIfTI object & choose non-verbose behaviour:
obj = NIFTI::NObject.new(nil, :verbose => false)
# File lib/nifti/n_object.rb, line 50
def initialize(string=nil, options={})
  # Process option values, setting defaults for the ones that are not specified:
  # Default verbosity is true if verbosity hasn't been specified (nil):
  @verbose = (options[:verbose] == false ? false : true)
  # Messages (errors, warnings or notices) will be accumulated in an array:
  @errors = Array.new
  # Structural information (default values):
  @file_endian = false
  # Control variables:
  @read_success = nil

  # Call the read method if a string has been supplied:
  if string.is_a?(String)
    @file = string unless options[:bin]
    read(string, options)
  elsif not string == nil
    raise ArgumentError, "Invalid argument. Expected String (or nil), got #{string.class}."
  end
end

Public Instance Methods

get_image() click to toggle source

Reopen the NIFTI File and retrieve image data

# File lib/nifti/n_object.rb, line 79
def get_image
  r = NRead.new(@string, :image => true)
  if r.success
    @image = r.image_rubyarray
  end
end
get_nimage() click to toggle source

Reopen the NIFTI File and retrieve image data, returning, if the retrieval was successful, it as an NImage object

# File lib/nifti/n_object.rb, line 71
def get_nimage
  image = self.get_image
  if !image.nil?
    NImage.new(image, self.header["dim"])
  end
end
write(file_name, options={}) click to toggle source

Passes the NObject to the DWrite class, which writes out the header and image to the specified file.

Parameters

  • file_name – A string which identifies the path & name of the NIfTI file which is to be written to disk.

  • options – A hash of parameters.

Options

Examples

obj.write(path + "test.dcm")
# File lib/nifti/n_object.rb, line 99
def write(file_name, options={})
  if file_name.is_a?(String)
    w = NWrite.new(self, file_name, options)
    w.write
    # Write process succesful?
    @write_success = w.success
    # If any messages has been recorded, send these to the message handling method:
    add_msg(w.msg) if w.msg.length > 0
  else
    raise ArgumentError, "Invalid file_name. Expected String, got #{file_name.class}."
  end
end

Private Instance Methods

add_msg(msg) click to toggle source

Adds one or more status messages to the instance array holding messages, and if the verbose instance variable is true, the status message(s) are printed to the screen as well.

Parameters

  • msg – Status message string, or an array containing one or more status message strings.

# File lib/nifti/n_object.rb, line 155
def add_msg(msg)
  puts msg if @verbose
  @errors << msg
  @errors.flatten
end
read(string, options={}) click to toggle source

Returns a NIFTI object by reading and parsing the specified file. This is accomplished by initializing the NRead class, which loads NIFTI information.

Notes

This method is called automatically when initializing the NObject class with a file parameter, and in practice should not be called by users.

# File lib/nifti/n_object.rb, line 123
def read(string, options={})
  if string.is_a?(String)
    @string = string
    r = NRead.new(string, options)
    # Store the data to the instance variables if the readout was a success:
    if r.success
      @read_success = true
      # Update instance variables based on the properties of the NRead object:
      @header = r.hdr
      @extended_header = r.extended_header
      if r.image_narray
        @image = r.image_narray
      elsif r.image_rubyarray
        @image = r.image_rubyarray
      end
    else
      @read_success = false
    end
    # If any messages have been recorded, send these to the message handling method:
    add_msg(r.msg) if r.msg.length > 0
  else
    raise ArgumentError, "Invalid argument. Expected String, got #{string.class}."
  end
end