Class: NIFTI::NImage

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

Overview

The NImage class is a container for the “raw” NIFTI image data making easier to deal with them.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array_image, dim, previous_indexes = []) ⇒ NImage

Creates an NImage instance.

The NImages instance provides a user friendly interface to the NIFTI Image A NImage is typically built by NObject instance

Parameters

  • array_image – The NIFTI image contained on and one dimensional array

  • dim – The dimensions array from the NIFTI header.

Examples

# Creates an NImage to deal with an 9 position array that represents a 3x3 matrix
img = Nimage.new(Array.new(9,0.0), [2,3,3])


22
23
24
25
26
# File 'lib/nifti/n_image.rb', line 22

def initialize(array_image, dim, previous_indexes=[])
  @array_image = array_image
  @dim = dim
  @previous_indexes = previous_indexes
end

Instance Attribute Details

#array_imageObject (readonly)

Returns the value of attribute array_image.



5
6
7
# File 'lib/nifti/n_image.rb', line 5

def array_image
  @array_image
end

#dimObject (readonly)

Returns the value of attribute dim.



5
6
7
# File 'lib/nifti/n_image.rb', line 5

def dim
  @dim
end

#previous_indexesObject (readonly)

Returns the value of attribute previous_indexes.



5
6
7
# File 'lib/nifti/n_image.rb', line 5

def previous_indexes
  @previous_indexes
end

Instance Method Details

#[](index) ⇒ Object

Retrieves an element or partition of the dataset

Parameters

  • index – The desired index on the dataset

Options

Examples

img[0][0]
img[0][0..1]


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nifti/n_image.rb', line 41

def [](index)
  # Dealing with Ranges is useful when the image represents a tensor
  if (index.is_a?(Fixnum) && index >= self.shape[0]) || (index.is_a?(Range) && index.last >= self.shape[0])
    raise IndexError.new("Index over bounds")
  elsif self.shape.count == 1
    if index.is_a?(Range)
      value = []
      index.each { |i| value << self.array_image[get_index_value(i)] }
      value
    else
      self.array_image[get_index_value(index)]
    end
  else
    NImage.new(self.array_image, self.dim, self.previous_indexes.clone << index)
  end
end

#[]=(index, value) ⇒ Object

Set the value for an element of the dataset

Parameters

  • index – The desired index on the dataset

  • value – The value that the will be set

Options

Examples

img[0][0] = 1.0


71
72
73
74
75
76
77
# File 'lib/nifti/n_image.rb', line 71

def []=(index,value)
  if self.shape.count != 1 or index >= self.shape[0]
    raise IndexError.new("You can only set values for array values")
  else
    @array_image[get_index_value(index)] = value
  end
end

#shapeObject

Dataset shape

Examples

img.shape


85
86
87
88
89
# File 'lib/nifti/n_image.rb', line 85

def shape
  start_index = 1
  self.previous_indexes.each {start_index += 1}
  self.dim[start_index..self.dim[0]]
end