Class: NIFTI::NObject

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string = nil, options = {}) ⇒ NObject

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)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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

Instance Attribute Details

#errorsObject (readonly)

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



7
8
9
# File 'lib/nifti/n_object.rb', line 7

def errors
  @errors
end

#extended_headerObject

A hash of extended attributes



17
18
19
# File 'lib/nifti/n_object.rb', line 17

def extended_header
  @extended_header
end

#headerObject

A hash of header information



15
16
17
# File 'lib/nifti/n_object.rb', line 15

def header
  @header
end

#imageObject

An array or narray of image values



19
20
21
# File 'lib/nifti/n_object.rb', line 19

def image
  @image
end

#read_successObject (readonly)

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



9
10
11
# File 'lib/nifti/n_object.rb', line 9

def read_success
  @read_success
end

#streamObject (readonly)

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



11
12
13
# File 'lib/nifti/n_object.rb', line 11

def stream
  @stream
end

#write_successObject (readonly)

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



13
14
15
# File 'lib/nifti/n_object.rb', line 13

def write_success
  @write_success
end

Instance Method Details

#get_imageObject

Reopen the NIFTI File and retrieve image data



79
80
81
82
83
84
# 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_nimageObject

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



71
72
73
74
75
76
# 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 = {}) ⇒ Object

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")


99
100
101
102
103
104
105
106
107
108
109
110
# 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