Class: RawImageDatasetThumbnail

Inherits:
Object
  • Object
show all
Defined in:
lib/metamri/raw_image_dataset_thumbnail.rb

Overview

This class is a ruby object encapsulating a .png 2D Thumbnail of a Dataset Initialize it with an #RawImageDataset

Constant Summary collapse

VALID_PROCESSORS =
[:rubydicom, :slicer]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset) ⇒ RawImageDatasetThumbnail

Creates a RawImageDatasetThumbnail instance by passing in a parent dataset to thumbnail.



27
28
29
30
31
32
33
# File 'lib/metamri/raw_image_dataset_thumbnail.rb', line 27

def initialize(dataset)
  if dataset.class == RawImageDataset
    @dataset = dataset
  else
    raise StandardError, "Dataset #{dataset} class must be RawImageDataset."
  end
end

Instance Attribute Details

#datasetObject (readonly)

The parent #RawImageDataset



18
19
20
# File 'lib/metamri/raw_image_dataset_thumbnail.rb', line 18

def dataset
  @dataset
end

#errorsObject (readonly)

An array of errors encountered during the reading process.



24
25
26
# File 'lib/metamri/raw_image_dataset_thumbnail.rb', line 24

def errors
  @errors
end

#pathObject (readonly)

The path to the thumbnail image if it’s already been created



20
21
22
# File 'lib/metamri/raw_image_dataset_thumbnail.rb', line 20

def path
  @path
end

#processorObject (readonly)

The processor for creating the thumbnail (:rubydicom or :slicer)



22
23
24
# File 'lib/metamri/raw_image_dataset_thumbnail.rb', line 22

def processor
  @processor
end

Instance Method Details

#create_thumbnail(output = nil, options = {:processor => :rubydicom}) ⇒ Object

Creates a thumbnail image (.png or .jpg) and returns the full file path of the thumbnail. Raises a ScriptError if the thumbnail could not be created. Raises a StandardError if the format is incorrect (i.e. P-file instead of DICOM)

Be sure your filename is a valid unix filename - no spaces.

Returns the full absolute filename to the new thumbnail image and sets it to @path instance variable.

Parameters

  • output: An optional string which specifies a directory or filename for the thumbnail image.

  • options: A hash of additional options.

Options

  • :processor – Symbol. Specifies which thumbnail processor to use. Defaults to :rubydicom, alternatively it could be :slicer

Examples

# Load a RawImageDataset 
ds = RawImageDataset('s01_assetcal', RawImageFile.new('./s01_assetcal/I0001.dcm'))
# Create a RawImageDatasetThumbnail instance
thumb = RawImageDatasetThumbnail.new(ds)
# Create a thumbnail in a temp directory without options, save it to a destination image, or force it to use FSL Slicer.
thumb.create_thumbnail
thumb.create_thumbnail('/tmp/asset_cal.png')
thumb.create_thumbnail('/tmp/asset_cal.png', :processor => :slicer)

Raises:

  • (StandardError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/metamri/raw_image_dataset_thumbnail.rb', line 67

def create_thumbnail(output = nil, options = {:processor => :rubydicom})
  raise StandardError, "Thumbnail available only for DICOM format." unless dataset.raw_image_files.first.dicom?
  raise ArgumentError, "Invalid :processor option #{options[:processor]}" unless VALID_PROCESSORS.include?(options[:processor])
  if output
    if File.directory?(output)
      # output is a directory.  Set the output directory but leave filepath nil.
      output_directory = output.escape_dirname
    else
      # output is a path.  Set the output_directory and specify that the full filepath is already complete.
      output_directory = File.dirname(output).escape_dirname
      filepath = output
    end
  else
    # If no output was given, default to a new temp directory.
    output_directory = Dir.mktmpdir
  end
  
  @processor = options[:processor]
  
  # Set a default filepath unless one was explicitly passed in.
  default_name = @dataset.series_description.escape_filename
  filepath ||= File.join(output_directory, default_name + '.png')
  

  begin
    case @processor
    when :rubydicom
      @path = create_thumbnail_with_rubydicom_dcmtk(filepath)
      #@path = create_thumbnail_with_rubydicom(filepath)
    when :slicer
      @path = create_thumbnail_with_fsl_slicer(filepath)
    end
  rescue RangeError, ScriptError => e
    unless @processor == :slicer 
      puts "Could not create thumbnail with rubydicom.  Trying FSL slicer."
      @processor = :slicer
      retry
    else 
      raise e
    end
  end
  
  raise ScriptError, "Could not create thumbnail from #{@dataset.series_description} - #{File.join(@dataset.directory, @dataset.scanned_file)}" unless @path && File.readable?(@path) 
  return @path
end

#thumbnailObject



35
36
37
# File 'lib/metamri/raw_image_dataset_thumbnail.rb', line 35

def thumbnail
  @path ||= create_thumbnail
end