Class: NIFTI::NObject
- Inherits:
-
Object
- Object
- NIFTI::NObject
- 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
-
#errors ⇒ Object
readonly
An array which contain any notices/warnings/errors that have been recorded for the NObject instance.
-
#extended_header ⇒ Object
A hash of extended attributes.
-
#header ⇒ Object
A hash of header information.
-
#image ⇒ Object
An array or narray of image values.
-
#read_success ⇒ Object
readonly
A boolean which is set as true if a NIFTI file has been successfully read & parsed from a file (or binary string).
-
#stream ⇒ Object
readonly
The Stream instance associated with this DObject instance (this attribute is mostly used internally).
-
#write_success ⇒ Object
readonly
A boolean which is set as true if a DObject instance has been successfully written to file (or successfully encoded).
Instance Method Summary collapse
-
#get_image ⇒ Object
Reopen the NIFTI File and retrieve image data.
-
#get_nimage ⇒ Object
Reopen the NIFTI File and retrieve image data, returning, if the retrieval was successful, it as an NImage object.
-
#initialize(string = nil, options = {}) ⇒ NObject
constructor
Creates an NObject instance (NObject is an abbreviation for “NIFTI object”).
-
#write(file_name, options = {}) ⇒ Object
Passes the NObject to the DWrite class, which writes out the header and image to the specified file.
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, ={}) # Process option values, setting defaults for the ones that are not specified: # Default verbosity is true if verbosity hasn't been specified (nil): @verbose = ([: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 [:bin] read(string, ) elsif not string == nil raise ArgumentError, "Invalid argument. Expected String (or nil), got #{string.class}." end end |
Instance Attribute Details
#errors ⇒ Object (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_header ⇒ Object
A hash of extended attributes
17 18 19 |
# File 'lib/nifti/n_object.rb', line 17 def extended_header @extended_header end |
#header ⇒ Object
A hash of header information
15 16 17 |
# File 'lib/nifti/n_object.rb', line 15 def header @header end |
#image ⇒ Object
An array or narray of image values
19 20 21 |
# File 'lib/nifti/n_object.rb', line 19 def image @image end |
#read_success ⇒ Object (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 |
#stream ⇒ Object (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_success ⇒ Object (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_image ⇒ Object
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_nimage ⇒ Object
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, ={}) if file_name.is_a?(String) w = NWrite.new(self, file_name, ) 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 |