Class: RawImageDatasetResource

Inherits:
ActiveResource::Base
  • Object
show all
Defined in:
lib/metamri/raw_image_dataset_resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to_table(datasets) ⇒ Object

Creates an Hirb Table for pretty output of dataset info. It takes an array of either RawImageDatasets or RawImageDatasetResources



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/metamri/raw_image_dataset_resource.rb', line 130

def self.to_table(datasets)
  Hirb::Helpers::AutoTable.render(
    datasets.sort_by{ |ds| [ds.timestamp, File.basename(ds.path)] }, 
    :headers => { :relative_dataset_path => 'Dataset', :series_description => 'Series Details', :file_count => "File Count", :image_dataset_quality_checks_tablerow => "Quality Checks"}, 
    :fields => [:relative_dataset_path, :series_description, :file_count, :image_dataset_quality_checks_tablerow],
    :description => false, # Turn off rendering row count description at bottom.
    :resize => true
  )
rescue NameError => e
  raise e

  # Header Line
  printf "\t%-15s %-30s [%s]\n", "Directory", "Series Description", "Files"

  # Dataset Lines
  datasets.sort_by{|ds| [ds.timestamp, File.basename(ds.path)] }.each do |dataset|
    printf "\t%-15s %-30s [%s]\n", dataset.relative_dataset_path, dataset.series_description, dataset.file_count
  end

  # Reminder Line
  puts "(This would be much prettier if you installed hirb.)"
  return
end

Instance Method Details

#file_countObject



79
80
81
82
83
84
85
86
# File 'lib/metamri/raw_image_dataset_resource.rb', line 79

def file_count
  if pfile?
    file_count = 1
  else
    file_count = Dir.open(path).reject{ |branch| /(^\.|.yaml$)/.match(branch) }.length
  end
  return file_count
end

#image_dataset_quality_checksObject

Queries ActiveResource for an array of ImageDatasetQualityCheckResources



103
104
105
# File 'lib/metamri/raw_image_dataset_resource.rb', line 103

def image_dataset_quality_checks
  @image_dataset_quality_checks ||= ImageDatasetQualityCheckResource.find(:all, :params => {:image_dataset_id => id })
end

#image_dataset_quality_checks_tablerowObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/metamri/raw_image_dataset_resource.rb', line 107

def image_dataset_quality_checks_tablerow
  output = []
  unless image_dataset_quality_checks.empty?
    image_dataset_quality_checks.each do |qc|
      qc.failed_checks.each do |check|
        output << "* #{check[:name].capitalize.gsub("_", " ") } (#{check[:value]}): #{(check[:comment] + ".") if check[:comment]}"
      end

      output << "Concerns: #{qc.other_issues}" if qc.other_issues

      if output.empty? 
        output << "Good"
      end
    
      # Add QC date at end.
      output << "[#{qc.attribute_names['created_at'].strftime('%D')}]"
    end
  end
  return output.join(" ")
end

#pfile?Boolean

def file_count

unless @file_count
  if @raw_image_files.first.dicom?
    @file_count = Dir.open(@directory).reject{ |branch| /^\./.match(branch) }.length
  elsif @raw_image_files.first.pfile?
    @file_count = 1
  else raise "File not recognized as dicom or pfile."
  end
end
return @file_count

end

Returns:

  • (Boolean)


73
74
75
# File 'lib/metamri/raw_image_dataset_resource.rb', line 73

def pfile?
  scanned_file =~ /^P.{5}.7$/
end

#relative_dataset_path(visit_dir = nil) ⇒ Object

Returns a relative filepath to the dataset. Handles dicoms by returning the dataset directory, and pfiles by returning either the pfile filename or, if passed a visit directory, the relative path from the visit directory to the pfile (i.e. P00000.7 or raw/P00000.7).



92
93
94
95
96
97
98
99
100
# File 'lib/metamri/raw_image_dataset_resource.rb', line 92

def relative_dataset_path(visit_dir = nil)
  if pfile?
    relative_dataset_path = scanned_file
  else # Then it's a dicom.
    relative_dataset_path = File.basename(path)
  end
  
  return relative_dataset_path
end

#to_metamri_raw_image_datasetObject

Creates a Backwards Transfer to go from ActiveRecord to Metamri Classes ActiveResource will provide :attr methods for column names from the database, so check the current schema.rb file for those.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/metamri/raw_image_dataset_resource.rb', line 9

def to_metamri_raw_image_dataset
  # A Metamri Class requires at least one valid image file.
  # This is a little wasteful since we really only care about the variables, 
  # not rescanning them.
  
  filename = Pathname.new(File.join(path, scanned_file))
  filename_matches = /P\d{5}.7(.bz2)?/.match(filename)
  
  if filename_matches    # Pfile
    if filename_matches[1] # '.bz2' if present, nil if otherwise.
      filename = Pathname.new(File.join(filename, '.bz2'))
    end
          
    # The scanned file is always reported in unzipped format, so we don't
    # have to worry about stripping a .bz2 extension.
    # The actual file on the filesystem may be zipped or unzipped 
    # (although it Should! be zipped.  Check for that or return IOError.
    zipped_filename = filename.to_s.chomp + '.bz2'

    if filename.file?
      image_file = filename
    elsif Pathname.new(zipped_filename).file?
      image_file = Pathname.new(zipped_filename)
    else 
      raise IOError, "Could not find #{filename} or it's bz2 zipped equivalent #{zipped_filename}."
    end
    
    image_file.local_copy do |local_pfile| 
      @dataset = RawImageDataset.new( path, [RawImageFile.new(local_pfile)])
    end

  else # Dicom      
    Pathname.new(path).first_dicom do |fd|
      @dataset = RawImageDataset.new( path, [RawImageFile.new(fd)] )
    end
  end
  
  return @dataset
end