Class: SqedToTaxonworks::Result
- Inherits:
-
Object
- Object
- SqedToTaxonworks::Result
- 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
-
#depiction ⇒ Object
Returns the value of attribute depiction.
-
#depiction_id ⇒ Object
Returns the value of attribute depiction_id.
-
#height_ratio ⇒ Object
Returns the value of attribute height_ratio.
-
#namespace_id ⇒ Object
Returns the value of attribute namespace_id.
-
#original_image ⇒ Object
Returns the value of attribute original_image.
-
#sqed ⇒ Object
Returns the value of attribute sqed.
-
#sqed_depiction ⇒ Object
Returns the value of attribute sqed_depiction.
-
#sqed_result ⇒ a sqed result, false
Minimize use of this if possible, depend on the cached values when possible.
-
#width_ratio ⇒ Object
Returns the value of attribute width_ratio.
Instance Method Summary collapse
- #boundaries_cached? ⇒ Boolean
- #cache_all ⇒ Object
- #cache_boundaries ⇒ Object
- #cache_ocr ⇒ Object
-
#coordinates_valid? ⇒ Boolean
If false then they are clearly not, if true then they might be.
- #coords_for(layout_section_type) ⇒ Object
- #image_path_for(layout_section_type) ⇒ Object
- #image_path_for_large_image(layout_section_type) ⇒ Object
- #image_path_for_small_image(layout_section_type) ⇒ Object
- #image_sections ⇒ Array
- #image_unavailable? ⇒ Boolean
-
#initialize(depiction_id: nil, namespace_id: nil) ⇒ Result
constructor
A new instance of Result.
- #large_dimensions_for(layout_section_type) ⇒ Object
- #large_height_for(layout_section_type) ⇒ Object
- #large_height_width(layout_section_type) ⇒ Object
- #larger_height_width(layout_section_type) ⇒ Object
- #namespace_locked? ⇒ Boolean
- #ocr_cached? ⇒ Boolean
- #ocr_for(layout_section_type) ⇒ Object
- #ocr_path_for(layout_section_type) ⇒ Object
- #primary_image ⇒ Symbol
- #secondary_sections ⇒ Array
- #small_dimensions_for(layout_section_type) ⇒ Object
- #small_height_for(layout_section_type) ⇒ Object
- #small_height_width(layout_section_type) ⇒ Object
Constructor Details
#initialize(depiction_id: nil, namespace_id: nil) ⇒ Result
Returns a new instance of Result.
42 43 44 45 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 42 def initialize(depiction_id: nil, namespace_id: nil) @depiction_id = depiction_id @namespace_id = namespace_id end |
Instance Attribute Details
#depiction ⇒ Object
Returns the value of attribute depiction.
24 25 26 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 24 def depiction @depiction end |
#depiction_id ⇒ Object
Returns the value of attribute depiction_id.
26 27 28 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 26 def depiction_id @depiction_id end |
#height_ratio ⇒ Object
Returns the value of attribute height_ratio.
40 41 42 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 40 def height_ratio @height_ratio end |
#namespace_id ⇒ Object
Returns the value of attribute namespace_id.
28 29 30 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 28 def namespace_id @namespace_id end |
#original_image ⇒ Object
Returns the value of attribute original_image.
38 39 40 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 38 def original_image @original_image end |
#sqed ⇒ Object
Returns the value of attribute sqed.
32 33 34 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 32 def sqed @sqed end |
#sqed_depiction ⇒ Object
Returns the value of attribute sqed_depiction.
30 31 32 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 30 def sqed_depiction @sqed_depiction end |
#sqed_result ⇒ a sqed result, false
Minimize use of this if possible, depend on the cached values when possible.
36 37 38 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 36 def sqed_result @sqed_result end |
#width_ratio ⇒ Object
Returns the value of attribute width_ratio.
40 41 42 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 40 def width_ratio @width_ratio end |
Instance Method Details
#boundaries_cached? ⇒ Boolean
115 116 117 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 115 def boundaries_cached? !sqed_depiction.result_boundary_coordinates.nil? end |
#cache_all ⇒ Object
139 140 141 142 143 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 139 def cache_all cache_ocr cache_boundaries sqed_depiction.touch end |
#cache_boundaries ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 123 def cache_boundaries begin sqed_depiction.update_column(:result_boundary_coordinates, sqed.boundaries.coordinates) rescue RuntimeError => e if e..include?( 'No image provided') sqed_depiction.update_column(:result_boundary_coordinates, nil) else raise end end end |
#cache_ocr ⇒ Object
135 136 137 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 135 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.
245 246 247 248 249 250 251 252 253 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 245 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
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 156 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
167 168 169 170 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 167 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
172 173 174 175 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 172 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
177 178 179 180 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 177 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_sections ⇒ Array
188 189 190 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 188 def image_sections (sqed_depiction.[:metadata_map].values - [:image_registration]) end |
#image_unavailable? ⇒ Boolean
238 239 240 241 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 238 def image_unavailable? return true if !File.exists?(depiction.image.image_file.path(:original)) false end |
#large_dimensions_for(layout_section_type) ⇒ Object
232 233 234 235 236 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 232 def large_dimensions_for(layout_section_type) c = coords_for(layout_section_type) return nil if c == [] || (c[2] == 0 and 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
227 228 229 230 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 227 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
212 213 214 215 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 212 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
217 218 219 220 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 217 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
111 112 113 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 111 def namespace_locked? !namespace_id.nil? end |
#ocr_cached? ⇒ Boolean
119 120 121 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 119 def ocr_cached? !sqed_depiction.result_ocr.nil? end |
#ocr_for(layout_section_type) ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 145 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
182 183 184 185 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 182 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_image ⇒ Symbol
193 194 195 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 193 def primary_image (image_sections & [:labels, :collecting_event_labels, :annotated_specimen]).first end |
#secondary_sections ⇒ Array
198 199 200 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 198 def secondary_sections image_sections - [primary_image] end |
#small_dimensions_for(layout_section_type) ⇒ Object
202 203 204 205 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 202 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
222 223 224 225 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 222 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
207 208 209 210 |
# File 'lib/vendor/sqed_to_taxonworks.rb', line 207 def small_height_width(layout_section_type) c = coords_for(layout_section_type) "#{SMALL_WIDTH}, #{small_height_for(layout_section_type)}" end |