Class: Vendor::SqedToTaxonworks::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/sqed_to_taxonworks.rb

Overview

Stores and handles metadata linking a TW depiction to the Sqed library.

Constant Summary collapse

SMALL_WIDTH =
100
LARGE_WIDTH =
400
TEXT_MAP =
{
  annotated_specimen: :buffered_collecting_event,
  collecting_event_labels: :buffered_collecting_event,
  curator_metadata: :buffered_other_labels,
  determination_labels: :buffered_determinations,
  identifier: :buffered_other_labels,
  image_registration: nil,
  other_labels: :buffered_other_labels,
  labels: :buffered_collecting_event,
  nothing: nil,
  specimen: nil,
  stage: :buffered_collecting_event,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(depiction_id: nil, namespace_id: nil) ⇒ Result

Returns a new instance of Result.



41
42
43
44
# File 'lib/vendor/sqed_to_taxonworks.rb', line 41

def initialize(depiction_id: nil, namespace_id: nil)
  @depiction_id = depiction_id
  @namespace_id = namespace_id
end

Instance Attribute Details

#depictionObject

Returns the value of attribute depiction.



23
24
25
# File 'lib/vendor/sqed_to_taxonworks.rb', line 23

def depiction
  @depiction
end

#depiction_idObject

Returns the value of attribute depiction_id.



25
26
27
# File 'lib/vendor/sqed_to_taxonworks.rb', line 25

def depiction_id
  @depiction_id
end

#height_ratioObject

Returns the value of attribute height_ratio.



39
40
41
# File 'lib/vendor/sqed_to_taxonworks.rb', line 39

def height_ratio
  @height_ratio
end

#namespace_idObject

Returns the value of attribute namespace_id.



27
28
29
# File 'lib/vendor/sqed_to_taxonworks.rb', line 27

def namespace_id
  @namespace_id
end

#original_imageObject

Returns the value of attribute original_image.



37
38
39
# File 'lib/vendor/sqed_to_taxonworks.rb', line 37

def original_image
  @original_image
end

#sqedObject

Returns the value of attribute sqed.



31
32
33
# File 'lib/vendor/sqed_to_taxonworks.rb', line 31

def sqed
  @sqed
end

#sqed_depictionObject

Returns the value of attribute sqed_depiction.



29
30
31
# File 'lib/vendor/sqed_to_taxonworks.rb', line 29

def sqed_depiction
  @sqed_depiction
end

#sqed_resulta sqed result, false

Minimize use of this if possible, depend on the cached values when possible.

Returns:

  • (a sqed result, false)


35
36
37
# File 'lib/vendor/sqed_to_taxonworks.rb', line 35

def sqed_result
  @sqed_result
end

#width_ratioObject

Returns the value of attribute width_ratio.



39
40
41
# File 'lib/vendor/sqed_to_taxonworks.rb', line 39

def width_ratio
  @width_ratio
end

Instance Method Details

#boundaries_cached?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/vendor/sqed_to_taxonworks.rb', line 135

def boundaries_cached?
  !sqed_depiction.result_boundary_coordinates.nil?
end

#cache_allObject

TODO: should be set_cached



156
157
158
159
160
# File 'lib/vendor/sqed_to_taxonworks.rb', line 156

def cache_all
  cache_ocr
  cache_boundaries
  sqed_depiction.touch
end

#cache_boundariesObject



143
144
145
146
147
148
149
# File 'lib/vendor/sqed_to_taxonworks.rb', line 143

def cache_boundaries
  begin
    sqed_depiction.update_column(:result_boundary_coordinates, sqed.boundaries.coordinates)
  rescue Sqed::Error
    sqed_depiction.update_column(:result_boundary_coordinates, nil)
  end
end

#cache_ocrObject



151
152
153
# File 'lib/vendor/sqed_to_taxonworks.rb', line 151

def cache_ocr
  sqed_depiction.update_column(:result_ocr, (sqed_result || nil)&.text)
end

#coordinates_valid?Boolean

Returns if false then they are clearly not, if true then they might be.

Returns:

  • (Boolean)

    if false then they are clearly not, if true then they might be



262
263
264
265
266
267
268
269
270
# File 'lib/vendor/sqed_to_taxonworks.rb', line 262

def coordinates_valid?
  return false if sqed_depiction.result_boundary_coordinates.nil?
  zeroed = 0
  sqed_depiction.result_boundary_coordinates.each do |k, v|
    zeroed += 1 if v == [0, 0, 0, 0]
    return false if zeroed > 1
  end
  true
end

#coords_for(layout_section_type) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/vendor/sqed_to_taxonworks.rb', line 173

def coords_for(layout_section_type)
  index = sqed_depiction.[:metadata_map].key(layout_section_type)
  if boundaries_cached?
    sqed_depiction.result_boundary_coordinates[index.to_s].to_a # TODO- hmm, why the to_s needed here
  else # do not do the OCR if only coords asked for
    sqed_result
    cache_boundaries
    sqed.boundaries.for(index)
  end
end

#image_path_for(layout_section_type) ⇒ Object



184
185
186
187
# File 'lib/vendor/sqed_to_taxonworks.rb', line 184

def image_path_for(layout_section_type)
  c = coords_for(layout_section_type)
  "/images/#{depiction.image.id}/extract/#{c[0]}/#{c[1]}/#{c[2]}/#{c[3]}"
end

#image_path_for_large_image(layout_section_type) ⇒ Object



189
190
191
192
# File 'lib/vendor/sqed_to_taxonworks.rb', line 189

def image_path_for_large_image(layout_section_type)
  c = coords_for(layout_section_type)
  "/images/#{depiction.image.id}/scale_to_box/#{c[0]}/#{c[1]}/#{c[2]}/#{c[3]}/400/400"
end

#image_path_for_small_image(layout_section_type) ⇒ Object



194
195
196
197
# File 'lib/vendor/sqed_to_taxonworks.rb', line 194

def image_path_for_small_image(layout_section_type)
  c = coords_for(layout_section_type)
  "/images/#{depiction.image.id}/scale_to_box/#{c[0]}/#{c[1]}/#{c[2]}/#{c[3]}/150/150"
end

#image_sectionsArray

Returns:

  • (Array)


205
206
207
# File 'lib/vendor/sqed_to_taxonworks.rb', line 205

def image_sections
  (sqed_depiction.[:metadata_map].values - [:image_registration])
end

#image_unavailable?Boolean

Returns:

  • (Boolean)


255
256
257
258
# File 'lib/vendor/sqed_to_taxonworks.rb', line 255

def image_unavailable?
  return true if !File.exist?(depiction.image.image_file.path(:original))
  false
end

#large_dimensions_for(layout_section_type) ⇒ Object



249
250
251
252
253
# File 'lib/vendor/sqed_to_taxonworks.rb', line 249

def large_dimensions_for(layout_section_type)
  c = coords_for(layout_section_type)
  return nil if c.blank? || c[2] == 0 || c[3] == 0
  "0, 0, 400, #{( c[3].to_f / (c[2].to_f / 400)).to_i}"
end

#large_height_for(layout_section_type) ⇒ Object



244
245
246
247
# File 'lib/vendor/sqed_to_taxonworks.rb', line 244

def large_height_for(layout_section_type)
  c = coords_for(layout_section_type)
  "#{(c[3].to_f / (c[2].to_f / LARGE_WIDTH)).to_i}"
end

#large_height_width(layout_section_type) ⇒ Object



229
230
231
232
# File 'lib/vendor/sqed_to_taxonworks.rb', line 229

def large_height_width(layout_section_type)
  c = coords_for(layout_section_type)
  "#{LARGE_WIDTH}, #{large_height_for(layout_section_type)}"
end

#larger_height_width(layout_section_type) ⇒ Object



234
235
236
237
# File 'lib/vendor/sqed_to_taxonworks.rb', line 234

def larger_height_width(layout_section_type)
  c = coords_for(layout_section_type)
  "#{LARGE_WIDTH + 100}, #{large_height_for(layout_section_type).to_i + 100}"
end

#namespace_locked?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/vendor/sqed_to_taxonworks.rb', line 131

def namespace_locked?
  !namespace_id.nil?
end

#ocr_cached?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/vendor/sqed_to_taxonworks.rb', line 139

def ocr_cached?
  !sqed_depiction.result_ocr.nil?
end

#ocr_for(layout_section_type) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/vendor/sqed_to_taxonworks.rb', line 162

def ocr_for(layout_section_type)
  index = sqed_depiction.[:metadata_map].key(layout_section_type)
  if ocr_cached?
    sqed_depiction.result_ocr[layout_section_type.to_s] && sqed_depiction.result_ocr[layout_section_type.to_s]['text']
  else
    sqed_result
    cache_all
    sqed_result&.text_for(layout_section_type.to_sym) if sqed_result
  end
end

#ocr_path_for(layout_section_type) ⇒ Object



199
200
201
202
# File 'lib/vendor/sqed_to_taxonworks.rb', line 199

def ocr_path_for(layout_section_type)
  c = coords_for(layout_section_type)
  "/images/#{depiction.image.id}/ocr/#{c[0]}/#{c[1]}/#{c[2]}/#{c[3]}"
end

#primary_imageSymbol

Returns:

  • (Symbol)


210
211
212
# File 'lib/vendor/sqed_to_taxonworks.rb', line 210

def primary_image
  (image_sections & [:labels, :collecting_event_labels, :annotated_specimen]).first
end

#process_result(use_thumbnail: true) ⇒ Object

private



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
112
113
114
115
116
117
# File 'lib/vendor/sqed_to_taxonworks.rb', line 84

def process_result(use_thumbnail: true)
  begin
    sqed.use_thumbnail = use_thumbnail
    r = sqed.result
  rescue Sqed::Error
    r = false

  # TODO: likely remove TypeError
  rescue TypeError
    r = false

  rescue Magick::ImageMagickError => e
    if Rails.env.production? && e.message.include?('unable to open image')
      r = false
    else
      raise
    end

  rescue RTesseract::Error => e
    if Rails.env.production?
      r = false
    else
      raise
    end

  rescue RuntimeError => e
    if Rails.env.production? && e.message.include?('ImageMagick library function failed to return a result.')
      r = false
    else
      raise
    end
  end
  r
end

#secondary_sectionsArray

Returns:

  • (Array)


215
216
217
# File 'lib/vendor/sqed_to_taxonworks.rb', line 215

def secondary_sections
  image_sections - [primary_image]
end

#small_dimensions_for(layout_section_type) ⇒ Object



219
220
221
222
# File 'lib/vendor/sqed_to_taxonworks.rb', line 219

def small_dimensions_for(layout_section_type)
  c = coords_for(layout_section_type)
  "0, 0, #{SMALL_WIDTH}, #{(c[3].to_f / (c[2].to_f / SMALL_WIDTH)).to_i}"
end

#small_height_for(layout_section_type) ⇒ Object



239
240
241
242
# File 'lib/vendor/sqed_to_taxonworks.rb', line 239

def small_height_for(layout_section_type)
  c = coords_for(layout_section_type)
  "#{(c[3].to_f / (c[2].to_f / SMALL_WIDTH)).to_i}"
end

#small_height_width(layout_section_type) ⇒ Object



224
225
226
227
# File 'lib/vendor/sqed_to_taxonworks.rb', line 224

def small_height_width(layout_section_type)
  c = coords_for(layout_section_type)
  "#{SMALL_WIDTH}, #{small_height_for(layout_section_type)}"
end