Class: Tools::ImageMatrix
- Inherits:
-
Object
- Object
- Tools::ImageMatrix
- Defined in:
- lib/tools/image_matrix.rb
Overview
Contains methods used to build an image_matrix table
endpoint: localhost:3000/tasks/observation_matrices/image_matrix/36/key?observation_matrix_id=36&page=5 localhost:3000/tasks/observation_matrices/image_matrix/0/key?otu_filter=30947|22978|23065
Instance Attribute Summary collapse
-
#depiction_matrix ⇒ Object
Returns the table of observations with images.
-
#descriptor_available_keywords ⇒ Object
Returns the list of all Tags used with the descriptors.
-
#descriptor_available_languages ⇒ Object
Returns the list of available Languages used as translations for descriptors (in translations are available).
-
#descriptors ⇒ Object
Temporary attribute.
-
#descriptors_hash ⇒ Object
temporary hash of descriptors; used for calculation of useful and not useful descriptors and their states.
-
#descriptors_with_filter ⇒ Object
Temporary attribute.
-
#eliminate_unknown ⇒ Object
Optional attribute to eliminate taxa with no scores on a used descriptor: ‘false’ - default or ‘true’ If true, the rows without scores will be eliminated.
-
#identified_to_rank ⇒ Object
Optional attribute to limit identification to OTU or a particular nomenclatural rank.
-
#image_hash ⇒ Object
Returns the hash with image attributes.
-
#keyword_ids ⇒ Object
Optional attribute to provide a list of tagIDs to limit the set of descriptors to those taged: “keyword_ids=1|5|15”.
-
#language_id ⇒ Object
Optional attribute to display the descriptors and character_states in a particular language (when translations are available).
-
#language_to_use ⇒ Object
Returns Language as an object if the language_id was provided (used to display descriptors in a particular language).
-
#list_of_descriptors ⇒ Object
Return the list of descriptors and their states.
-
#list_of_image_ids ⇒ Object
@! list_of_image_ids @return [null] temporary array of image.ids; used to build the @image_hash.
-
#observation_matrix ⇒ Object
Returns observation_matrix as an object.
-
#observation_matrix_citation ⇒ Object
Returns observation_matrix_citation as an object.
-
#observation_matrix_id ⇒ Object
Required attribude to build the key.
-
#otu_filter ⇒ Object
Optional attribute to provide a list of rowIDs to limit the set.
-
#otu_id_filter_array ⇒ Object
Array of otu_ids in the @otu_filter.
-
#pagination_next_page ⇒ Object
Returns the next page number.
-
#pagination_page ⇒ Object
Returns the page number.
-
#pagination_per_page ⇒ Object
Returns number of records per page.
-
#pagination_previous_page ⇒ Object
Returns the previous page number.
-
#pagination_total ⇒ Object
Returns total number of records.
-
#pagination_total_pages ⇒ Object
Returns total number of pages.
-
#per ⇒ Object
Optional attribute.
-
#project_id ⇒ Object
Required attribute to build the key.
-
#row_filter ⇒ Object
Optional attribute to provide a list of rowIDs to limit the set “row_filter=1|5|10”.
-
#row_hash ⇒ Object
Temporary hash of rows; used for calculation of remaining and eliminated rows.
-
#row_id_filter_array ⇒ Object
Array of row_ids in the @row_filter.
-
#rows_with_filter ⇒ Object
Temporary attribute.
Instance Method Summary collapse
- #build_image_hash ⇒ Object
- #descriptor_available_languages_list ⇒ Object
-
#descriptors_hash_initiate ⇒ Object
descriptors_hash: {:descriptor, ### (descriptor) :observations, ### (array of observations for ) :state_ids, ### {hash of state_ids used in the particular matrix }}.
- #descriptors_with_keywords ⇒ Object
- #find_observation_matrix ⇒ Object
-
#initialize(observation_matrix_id: nil, project_id: nil, language_id: nil, keyword_ids: nil, row_filter: nil, otu_filter: nil, identified_to_rank: nil, per: nil, page: nil, pagination_page: nil, pagination_next_page: nil, pagination_previous_page: nil, pagination_per_page: nil, pagination_total: nil, pagination_total_pages: nil) ⇒ ImageMatrix
constructor
A new instance of ImageMatrix.
-
#observation_depictions_from_otu_filter ⇒ Depiction scope
TODO: CHANGED FLAG REMOVE.
- #otu_filter_array ⇒ Object
- #row_filter_array ⇒ Object
- #row_hash_initiate ⇒ Object
-
#selected_descriptors_hash_initiate ⇒ Object
returns [‘1’, ‘3’], 125: [‘3’, ‘5’], 135: [‘2’], 136: [‘true’], 140: [‘5-10’].
Constructor Details
#initialize(observation_matrix_id: nil, project_id: nil, language_id: nil, keyword_ids: nil, row_filter: nil, otu_filter: nil, identified_to_rank: nil, per: nil, page: nil, pagination_page: nil, pagination_next_page: nil, pagination_previous_page: nil, pagination_per_page: nil, pagination_total: nil, pagination_total_pages: nil) ⇒ ImageMatrix
Returns a new instance of ImageMatrix.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/tools/image_matrix.rb', line 170 def initialize( observation_matrix_id: nil, project_id: nil, language_id: nil, keyword_ids: nil, row_filter: nil, otu_filter: nil, identified_to_rank: nil, per: nil, page: nil, pagination_page: nil, pagination_next_page: nil, pagination_previous_page: nil, pagination_per_page: nil, pagination_total: nil, pagination_total_pages: nil) @observation_matrix_id = observation_matrix_id @project_id = project_id @observation_matrix = find_observation_matrix @observation_matrix_citation = @observation_matrix&.source @language_id = language_id @keyword_ids = keyword_ids @per = per.blank? ? 250 : per @page = page.blank? ? 1 : page @descriptor_available_keywords = descriptor_available_keywords @row_filter = row_filter @otu_filter = otu_filter @row_id_filter_array = row_filter_array @otu_id_filter_array = otu_filter_array # Memoized now # @rows_with_filter = get_rows_with_filter @identified_to_rank = identified_to_rank @row_hash = row_hash_initiate @descriptors_with_filter = descriptors_with_keywords @descriptor_available_languages = descriptor_available_languages_list @language_to_use = language_to_use ###main_logic @list_of_image_ids = [] # Initiate on getter, memoized, only breaks then when requested # @list_of_descriptors = build_list_of_descriptors @depiction_matrix = descriptors_hash_initiate @image_hash = build_image_hash ###delete temporary data @row_hash = nil @rows_with_filter = [] @list_of_image_ids = nil @descriptors_with_filter = nil end |
Instance Attribute Details
#depiction_matrix ⇒ Object
Returns the table of observations with images.
118 119 120 |
# File 'lib/tools/image_matrix.rb', line 118 def depiction_matrix @depiction_matrix end |
#descriptor_available_keywords ⇒ Object
Returns the list of all Tags used with the descriptors. Descriptors could be filtered by tag_id
78 79 80 |
# File 'lib/tools/image_matrix.rb', line 78 def descriptor_available_keywords @descriptor_available_keywords end |
#descriptor_available_languages ⇒ Object
Returns the list of available Languages used as translations for descriptors (in translations are available)
93 94 95 |
# File 'lib/tools/image_matrix.rb', line 93 def descriptor_available_languages @descriptor_available_languages end |
#descriptors ⇒ Object
Temporary attribute. Used for validation.
73 74 75 |
# File 'lib/tools/image_matrix.rb', line 73 def descriptors @descriptors end |
#descriptors_hash ⇒ Object
temporary hash of descriptors; used for calculation of useful and not useful descriptors and their states
133 134 135 |
# File 'lib/tools/image_matrix.rb', line 133 def descriptors_hash @descriptors_hash end |
#descriptors_with_filter ⇒ Object
Temporary attribute. Used for validation. List of descriptors reduced by keyword_ids
88 89 90 |
# File 'lib/tools/image_matrix.rb', line 88 def descriptors_with_filter @descriptors_with_filter end |
#eliminate_unknown ⇒ Object
Optional attribute to eliminate taxa with no scores on a used descriptor: ‘false’ - default or ‘true’ If true, the rows without scores will be eliminated
44 45 46 |
# File 'lib/tools/image_matrix.rb', line 44 def eliminate_unknown @eliminate_unknown end |
#identified_to_rank ⇒ Object
Optional attribute to limit identification to OTU or a particular nomenclatural rank. Valid values are ‘otu’, ‘species’, ‘genus’, etc.
49 50 51 |
# File 'lib/tools/image_matrix.rb', line 49 def identified_to_rank @identified_to_rank end |
#image_hash ⇒ Object
Returns the hash with image attributes
123 124 125 |
# File 'lib/tools/image_matrix.rb', line 123 def image_hash @image_hash end |
#keyword_ids ⇒ Object
Optional attribute to provide a list of tagIDs to limit the set of descriptors to those taged: “keyword_ids=1|5|15”
28 29 30 |
# File 'lib/tools/image_matrix.rb', line 28 def keyword_ids @keyword_ids end |
#language_id ⇒ Object
Optional attribute to display the descriptors and character_states in a particular language (when translations are available)
23 24 25 |
# File 'lib/tools/image_matrix.rb', line 23 def language_id @language_id end |
#language_to_use ⇒ Object
Returns Language as an object if the language_id was provided (used to display descriptors in a particular language)
83 84 85 |
# File 'lib/tools/image_matrix.rb', line 83 def language_to_use @language_to_use end |
#list_of_descriptors ⇒ Object
Return the list of descriptors and their states. Translated (if needed) and Sorted
113 114 115 |
# File 'lib/tools/image_matrix.rb', line 113 def list_of_descriptors @list_of_descriptors end |
#list_of_image_ids ⇒ Object
@! list_of_image_ids
@return [null]
temporary array of image.ids; used to build the @image_hash
138 139 140 |
# File 'lib/tools/image_matrix.rb', line 138 def list_of_image_ids @list_of_image_ids end |
#observation_matrix ⇒ Object
Returns observation_matrix as an object
63 64 65 |
# File 'lib/tools/image_matrix.rb', line 63 def observation_matrix @observation_matrix end |
#observation_matrix_citation ⇒ Object
Returns observation_matrix_citation as an object
68 69 70 |
# File 'lib/tools/image_matrix.rb', line 68 def observation_matrix_citation @observation_matrix_citation end |
#observation_matrix_id ⇒ Object
Required attribude to build the key
13 14 15 |
# File 'lib/tools/image_matrix.rb', line 13 def observation_matrix_id @observation_matrix_id end |
#otu_filter ⇒ Object
Optional attribute to provide a list of rowIDs to limit the set
38 39 40 |
# File 'lib/tools/image_matrix.rb', line 38 def otu_filter @otu_filter end |
#otu_id_filter_array ⇒ Object
Array of otu_ids in the @otu_filter
108 109 110 |
# File 'lib/tools/image_matrix.rb', line 108 def otu_id_filter_array @otu_id_filter_array end |
#pagination_next_page ⇒ Object
Returns the next page number
148 149 150 |
# File 'lib/tools/image_matrix.rb', line 148 def pagination_next_page @pagination_next_page end |
#pagination_page ⇒ Object
Returns the page number
143 144 145 |
# File 'lib/tools/image_matrix.rb', line 143 def pagination_page @pagination_page end |
#pagination_per_page ⇒ Object
Returns number of records per page
158 159 160 |
# File 'lib/tools/image_matrix.rb', line 158 def pagination_per_page @pagination_per_page end |
#pagination_previous_page ⇒ Object
Returns the previous page number
153 154 155 |
# File 'lib/tools/image_matrix.rb', line 153 def pagination_previous_page @pagination_previous_page end |
#pagination_total ⇒ Object
Returns total number of records
163 164 165 |
# File 'lib/tools/image_matrix.rb', line 163 def pagination_total @pagination_total end |
#pagination_total_pages ⇒ Object
Returns total number of pages
168 169 170 |
# File 'lib/tools/image_matrix.rb', line 168 def pagination_total_pages @pagination_total_pages end |
#per ⇒ Object
Optional attribute. Number of rows displayed per page
54 55 56 |
# File 'lib/tools/image_matrix.rb', line 54 def per @per end |
#project_id ⇒ Object
Required attribute to build the key
18 19 20 |
# File 'lib/tools/image_matrix.rb', line 18 def project_id @project_id end |
#row_filter ⇒ Object
Optional attribute to provide a list of rowIDs to limit the set “row_filter=1|5|10”
33 34 35 |
# File 'lib/tools/image_matrix.rb', line 33 def row_filter @row_filter end |
#row_hash ⇒ Object
Temporary hash of rows; used for calculation of remaining and eliminated rows
128 129 130 |
# File 'lib/tools/image_matrix.rb', line 128 def row_hash @row_hash end |
#row_id_filter_array ⇒ Object
Array of row_ids in the @row_filter
103 104 105 |
# File 'lib/tools/image_matrix.rb', line 103 def row_id_filter_array @row_id_filter_array end |
#rows_with_filter ⇒ Object
Temporary attribute. Used for validation. list of rows to be included into the matrix
98 99 100 |
# File 'lib/tools/image_matrix.rb', line 98 def rows_with_filter @rows_with_filter end |
Instance Method Details
#build_image_hash ⇒ Object
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/tools/image_matrix.rb', line 458 def build_image_hash # if !otu_filter.blank? || !row_filter.blank? # img_ids = observation_depictions_from_otu_filter.pluck(:image_id).uniq # else # img_ids = observation_matrix.observation_depictions.pluck(:image_id).uniq # end h = {} imgs = Image.where('id IN (?)', list_of_image_ids ) imgs.each do |d| i = {} i[:global_id] = d.to_global_id.to_s i[:image_file_file_name] = d.image_file_file_name i[:image_file_file_size] = d.image_file_file_size i[:image_file_content_type] = d.image_file_content_type i[:user_file_name] = d.user_file_name i[:height] = d.height i[:width] = d.width i[:original_url] = d.image_file.url i[:medium_url] = d.image_file.url(:medium) i[:thumb_url] = d.image_file.url(:thumb) i[:citations] = [] h[d.id] = i end #cit = Citation.where(citation_object_type: 'Image').where('citation_object_id IN (?)', img_ids ) cit = Citation.select('citations.*, sources.cached, sources.cached_author_string, sources.year') .joins(:source) .where(citation_object_type: 'Image') .where('citation_object_id IN (?)', list_of_image_ids ) cit.each do |c| i = {} i[:id] = c.id i[:source_id] = c.source_id i[:pages] = c.pages i[:is_original] = c.is_original i[:cached] = c.cached i[:cached_author_string] = c. i[:year] = c.year i[:global_id] = c.to_global_id.to_s #i[:citation_object_id] = c.citation_object_id #i[:citation_object_type] = c.citation_object_type h[c.citation_object_id][:citations].push(i) end h end |
#descriptor_available_languages_list ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/tools/image_matrix.rb', line 238 def descriptor_available_languages_list return nil if descriptors_with_filter.nil? descriptor_ids = descriptors_with_filter.collect{|i| i.id} languages = Language.joins(:alternate_value_translations) .where(alternate_values: {alternate_value_object_type: 'Descriptor', type: 'AlternateValue::Translation'}) .where('alternate_values.alternate_value_object_id IN (?)', descriptor_ids ).order('languages.english_name').distinct.to_a unless languages.empty? languages = Language.where(english_name: 'English').to_a + languages end languages end |
#descriptors_hash_initiate ⇒ Object
descriptors_hash: {:descriptor, ### (descriptor)
:observations, ### (array of observations for )
:state_ids, ### {hash of state_ids used in the particular matrix
}}
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/tools/image_matrix.rb', line 368 def descriptors_hash_initiate h = {} # Depictions is depictions with other attributes added depictions = nil if observation_matrix_id.to_i == 0 && !otu_filter.blank? depictions = observation_depictions_from_otu_filter else return h if observation_matrix.nil? depictions = observation_matrix.observation_depictions end descriptors_count = list_of_descriptors.count otu_ids = {} row_hash.each do |r_key, r_value| if (row_id_filter_array.nil? && otu_id_filter_array.nil?) || (row_id_filter_array && row_id_filter_array.include?(r_value[:object].id)) || (otu_id_filter_array && otu_id_filter_array.include?(r_value[:otu_id])) h[r_key] = {object: r_value[:object_at_rank], row_id: r_value[:object].id, otu_id: r_value[:otu_id], depictions: Array.new(descriptors_count) {Array.new}, } if h[r_key].nil? otu_ids[r_value[:otu_id]] = true end end depictions.each do |o| if (o.observation_object_type == 'Otu' && otu_ids[o.observation_object_id].nil?) || (otu_id_filter_array && (o.observation_object_type == 'Otu' && !otu_id_filter_array.include?(o.observation_object_id))) next end otu_collection_object = o.observation_object_type + o.observation_object_id.to_s # id.to_s + '|' + o.collection_object_id.to_s if h[otu_collection_object] descriptor_index = list_of_descriptors[o.descriptor_id][:index] h[otu_collection_object][:depictions][descriptor_index] += [o] @list_of_image_ids.append(o.image_id) end end h end |
#descriptors_with_keywords ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/tools/image_matrix.rb', line 265 def descriptors_with_keywords if observation_matrix_id.to_i == 0 && !otu_filter.blank? d = observation_depictions_from_otu_filter.pluck(:descriptor_id).uniq ds = Descriptor.where("descriptors.type = 'Descriptor::Media' AND descriptors.id IN (?)", d).not_weight_zero elsif keyword_ids ds = descriptors.joins(:tags).where('tags.keyword_id IN (?)', keyword_ids.to_s.split('|').map(&:to_i) ) else ds = descriptors end return [] if ds.nil? || ds.empty? ds = ds.sort{|a,b| a.observation_matrix_columns.first.try(:position).to_i <=> b.observation_matrix_columns.first.try(:position).to_i} ds end |
#find_observation_matrix ⇒ Object
229 230 231 |
# File 'lib/tools/image_matrix.rb', line 229 def find_observation_matrix ObservationMatrix.where(id: observation_matrix_id.to_i, project_id: project_id).first end |
#observation_depictions_from_otu_filter ⇒ Depiction scope
TODO: CHANGED FLAG REMOVE
415 416 417 418 419 420 421 422 423 424 |
# File 'lib/tools/image_matrix.rb', line 415 def observation_depictions_from_otu_filter Depiction.select('depictions.*, observations.descriptor_id, observations.observation_object_id, observations.observation_object_type, sources.id AS source_id, sources.cached_author_string, sources.year, sources.cached AS source_cached') .joins("INNER JOIN observations ON observations.id = depictions.depiction_object_id") .joins("INNER JOIN images ON depictions.image_id = images.id") .joins("LEFT OUTER JOIN citations ON citations.citation_object_id = images.id AND citations.citation_object_type = 'Image' AND citations.is_original IS TRUE") .joins("LEFT OUTER JOIN sources ON citations.source_id = sources.id") .where("observations.type = 'Observation::Media' AND observations.observation_object_id IN (?)", otu_id_filter_array) .where('observations.project_id = (?)', project_id) .order('depictions.position') end |
#otu_filter_array ⇒ Object
283 284 285 |
# File 'lib/tools/image_matrix.rb', line 283 def otu_filter_array otu_filter.blank? ? nil : otu_filter.to_s.split('|').map(&:to_i) end |
#row_filter_array ⇒ Object
279 280 281 |
# File 'lib/tools/image_matrix.rb', line 279 def row_filter_array row_filter.blank? ? nil : row_filter.to_s.split('|').map(&:to_i) end |
#row_hash_initiate ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/tools/image_matrix.rb', line 302 def row_hash_initiate h = {} rows = nil # Of either Otu or ObservationMatrixRow of type Otu !! TODO: if observation_matrix_id.to_i == 0 && !otu_filter.blank? o = observation_depictions_from_otu_filter.where("observations.observation_object_type = 'Otu'").pluck(:observation_object_id).uniq @otu_id_filter_array = otu_id_filter_array & o rows = Otu.where(id: otu_id_filter_array) else rows = rows_with_filter end i = 0 per = @per.to_i page = @page.to_i @pagination_page = page @pagination_total = rows.count @pagination_total_pages = (@pagination_total.to_f / per).ceil @pagination_next_page = @pagination_total_pages > @pagination_page ? @pagination_page + 1 : nil @pagination_previous_page = @pagination_page > 1 ? @pagination_page - 1 : nil @pagination_per_page = per rows.each do |r| i += 1 next if i < per * (page - 1) + 1 break if i > per * page case r.class.to_s when 'Otu' otu_collection_object = 'Otu' + r.id.to_s # otu_collection_object = r.id.to_s + '|' when 'ObservationMatrixRow' otu_collection_object = r.observation_object_type + r.observation_object_id.to_s # r.otu_id.to_s + '|' + r.collection_object_id.to_s end h[otu_collection_object] = {} h[otu_collection_object][:object] = r if identified_to_rank == 'otu' case r.class.to_s when 'Otu' h[otu_collection_object][:object_at_rank] = r when 'ObservationMatrixRow' h[otu_collection_object][:object_at_rank] = r.current_otu || r end elsif identified_to_rank case r.class.to_s when 'Otu' h[otu_collection_object][:object_at_rank] = r&.taxon_name&.valid_taxon_name&.ancestor_at_rank(identified_to_rank, inlude_self = true) || r when 'ObservationMatrixRow' h[otu_collection_object][:object_at_rank] = r&.current_taxon_name&.ancestor_at_rank(identified_to_rank, inlude_self = true) || r end else h[otu_collection_object][:object_at_rank] = r end h[otu_collection_object][:otu_id] = r.class.to_s == 'Otu' ? r.id : r.observation_object_id # otu_id end h end |
#selected_descriptors_hash_initiate ⇒ Object
returns [‘1’, ‘3’], 125: [‘3’, ‘5’], 135: [‘2’], 136: [‘true’], 140: [‘5-10’]
427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/tools/image_matrix.rb', line 427 def selected_descriptors_hash_initiate # "123:1|3||125:3|5||135:2" h = {} return h if selected_descriptors.blank? a = selected_descriptors.include?('||') ? selected_descriptors.to_s.split('||') : [selected_descriptors] a.each do |i| d = i.split(':') h[d[0].to_i] = d[1].include?('|') ? d[1].split('|') : [d[1]] end h end |