Class: RTKIT::Selection

Inherits:
Object
  • Object
show all
Defined in:
lib/rtkit/selection.rb

Overview

Contains DICOM data and methods related to a Selection of pixels (indices) from the binary 2D NArray of a BinImage instance.

Relations

  • The Selection belongs to a BinImage.

  • A Contour has many Coordinates.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bin_image) ⇒ Selection

Creates a new Selection instance.

Parameters

  • bin_image – The BinImage instance that this Selection belongs to.

Raises:

  • (ArgumentError)


45
46
47
48
49
# File 'lib/rtkit/selection.rb', line 45

def initialize(bin_image)
  raise ArgumentError, "Invalid argument 'bin_image'. Expected BinImage, got #{bin_image.class}." unless bin_image.is_a?(BinImage)
  @bin_image = bin_image
  @indices = Array.new
end

Instance Attribute Details

#bin_imageObject (readonly)

The BinImage that the Selection belongs to.



14
15
16
# File 'lib/rtkit/selection.rb', line 14

def bin_image
  @bin_image
end

#indicesObject (readonly)

An array of (general) indices.



16
17
18
# File 'lib/rtkit/selection.rb', line 16

def indices
  @indices
end

Class Method Details

.create_from_array(indices, bin_image) ⇒ Object

Creates a new Selection instance from an Array (or NArray) of (general) indices. Returns the Selection instance.

Parameters

  • arr – An Array/NArray of general indices (Integers).

  • slice – The BinImage instance that this Selection belongs to.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
# File 'lib/rtkit/selection.rb', line 28

def self.create_from_array(indices, bin_image)
  raise ArgumentError, "Invalid argument 'indices'. Expected Array/NArray, got #{indices.class}." unless [NArray, Array].include?(indices.class)
  raise ArgumentError, "Invalid argument 'bin_image'. Expected BinImage, got #{bin_image.class}." unless bin_image.is_a?(BinImage)
  raise ArgumentError, "Invalid argument 'indices'. Expected Array to contain only integers, got #{indices.collect{|i| i.class}.uniq}." if indices.is_a?(Array) and not indices.collect{|i| i.class}.uniq == [Fixnum]
  # Create the Selection:
  s = self.new(bin_image)
  # Set the indices:
  s.add_indices(indices)
  return s
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Returns true if the argument is an instance with attributes equal to self.



53
54
55
56
57
# File 'lib/rtkit/selection.rb', line 53

def ==(other)
  if other.respond_to?(:to_selection)
    other.send(:state) == state
  end
end

#add_indices(indices) ⇒ Object

Adds an array of (general) indices to this Selection.

Raises:

  • (ArgumentError)


63
64
65
66
67
68
# File 'lib/rtkit/selection.rb', line 63

def add_indices(indices)
  raise ArgumentError, "Invalid argument 'indices'. Expected Array/NArray, got #{indices.class}." unless [NArray, Array].include?(indices.class)
  raise ArgumentError, "Invalid argument 'indices'. Expected Array to contain only integers, got #{indices.collect{|i| i.class}.uniq}." if indices.is_a?(Array) and not indices.collect{|i| i.class}.uniq == [Fixnum]
  indices = indices.to_a if indices.is_a?(NArray)
  @indices += indices
end

#columnsObject

Returns an array of column indices. Returns an empty array if the instance contains no indices.



73
74
75
# File 'lib/rtkit/selection.rb', line 73

def columns
  return @indices.collect {|index| index % @bin_image.columns}
end

#hashObject

Generates a Fixnum hash value for this instance.



79
80
81
# File 'lib/rtkit/selection.rb', line 79

def hash
  state.hash
end

#lengthObject

Returns the length (number of indices) of this selection.



85
86
87
# File 'lib/rtkit/selection.rb', line 85

def length
  return @indices.length
end

#rowsObject

Returns an array of row indices. Returns an empty array if the instance contains no indices.



92
93
94
# File 'lib/rtkit/selection.rb', line 92

def rows
  return @indices.collect {|index| index / @bin_image.columns}
end

#shift(delta_col, delta_row) ⇒ Object

Shifts the indices of this selection by the specified number of columns and rows. Positive arguments increases the column and row indices.

Restrictions

NB! No out of bounds check is performed for indices that are shifted past the image boundary.

Raises:

  • (ArgumentError)


104
105
106
107
108
109
110
111
# File 'lib/rtkit/selection.rb', line 104

def shift(delta_col, delta_row)
  raise ArgumentError, "Invalid argument 'delta_col'. Expected Integer, got #{delta_col.class}." unless delta_col.is_a?(Integer)
  raise ArgumentError, "Invalid argument 'delta_row'. Expected Integer, got #{delta_row.class}." unless delta_row.is_a?(Integer)
  new_columns = @indices.collect {|index| index % @bin_image.columns + delta_col}
  new_rows = @indices.collect {|index| index / @bin_image.columns + delta_row}
  # Set new indices:
  @indices = Array.new(new_rows.length) {|i| new_columns[i] + new_rows[i] * @bin_image.columns}
end

#shift_and_crop(delta_col, delta_row) ⇒ Object

Shifts the indices of this selection by the specified number of columns and rows, virtually ‘crops’ the original image by 2*columns and 2*rows, and adapts the indices to this virtually cropped image.

Notes

Negative arguments decreases the column and row indices and crops at the end of the columns and rows.

Positive arguments increases the column and row indices and crops at the start of the columns and rows.

Restrictions

NB! No out of bounds check is performed for indices that are shifted past the image boundary.

Raises:

  • (ArgumentError)


130
131
132
133
134
135
136
137
# File 'lib/rtkit/selection.rb', line 130

def shift_and_crop(delta_col, delta_row)
  raise ArgumentError, "Invalid argument 'delta_col'. Expected Integer, got #{delta_col.class}." unless delta_col.is_a?(Integer)
  raise ArgumentError, "Invalid argument 'delta_row'. Expected Integer, got #{delta_row.class}." unless delta_row.is_a?(Integer)
  new_columns = @indices.collect {|index| index % @bin_image.columns - delta_col.abs}
  new_rows = @indices.collect {|index| index / @bin_image.columns - delta_row.abs}
  # Set new indices:
  @indices = Array.new(new_rows.length) {|i| new_columns[i] + new_rows[i] * (@bin_image.columns - delta_col.abs * 2)}
end

#shift_columns(delta) ⇒ Object

Shifts the indices of this selection by the specified number of columns. A positive argument increases the column indices.

Restrictions

NB! No out of bounds check is performed for indices that are shifted past the image boundary.

Raises:

  • (ArgumentError)


147
148
149
150
151
152
153
# File 'lib/rtkit/selection.rb', line 147

def shift_columns(delta)
  raise ArgumentError, "Invalid argument 'delta'. Expected Integer, got #{delta.class}." unless delta.is_a?(Integer)
  new_columns = @indices.collect {|index| index % @bin_image.columns + delta}
  new_rows = rows
  # Set new indices:
  @indices = Array.new(new_columns.length) {|i| new_columns[i] + new_rows[i] * @bin_image.columns}
end

#shift_rows(delta) ⇒ Object

Shifts the indices of this selection by the specified number of rows. A positive argument increases the row indices.

Restrictions

NB! No out of bounds check is performed for indices that are shifted past the image boundary.

Raises:

  • (ArgumentError)


163
164
165
166
167
168
169
# File 'lib/rtkit/selection.rb', line 163

def shift_rows(delta)
  raise ArgumentError, "Invalid argument 'delta'. Expected Integer, got #{delta.class}." unless delta.is_a?(Integer)
  new_columns = columns
  new_rows = @indices.collect {|index| index / @bin_image.columns + delta}
  # Set new indices:
  @indices = Array.new(new_rows.length) {|i| new_columns[i] + new_rows[i] * @bin_image.columns}
end

#to_selectionObject

Returns self.



173
174
175
# File 'lib/rtkit/selection.rb', line 173

def to_selection
  self
end