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: http://localhost:3000/tasks/observation_matrices/image_matrix/36/key?observation_matrix_id=36&page=5 http://localhost:3000/tasks/observation_matrices/image_matrix/0/key?otu_filter=30947|22978|23065
Instance Attribute Summary collapse
- #depiction_matrix ⇒ Hash of Arrays
- #descriptor_available_keywords ⇒ Array of Objects or null
- #descriptor_available_languages ⇒ Array of Objects or null
-
#descriptors ⇒ Object
Descriptor scope.
- #descriptors_hash ⇒ null
- #descriptors_with_filter ⇒ null
- #eliminate_unknown ⇒ Boolean or null
- #identified_to_rank ⇒ String or null
- #image_hash ⇒ Hash
- #keyword_ids ⇒ String or null
- #language_id ⇒ String or null
- #language_to_use ⇒ Object or null
- #list_of_descriptors ⇒ Hash
-
#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
- #observation_matrix_citation ⇒ Object
- #observation_matrix_id ⇒ String
-
#otu_filter ⇒ String or null
Like "otu_filter=1|5|10".
- #otu_id_filter_array ⇒ array
- #pagination_next_page ⇒ Integer or null
- #pagination_page ⇒ Integer
- #pagination_per_page ⇒ Integer or null
- #pagination_previous_page ⇒ Integer or null
- #pagination_total ⇒ Integer or null
- #pagination_total_pages ⇒ Integer or null
- #per ⇒ Integer or null
- #project_id ⇒ String
- #row_filter ⇒ String or null
- #row_hash ⇒ null
- #row_id_filter_array ⇒ array
- #rows_with_filter ⇒ null
Instance Method Summary collapse
- #build_attribution_label(attribution) ⇒ Object private
- #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
Enumberable.
- #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 |
# 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&.origin_citation @language_id = language_id @keyword_ids = keyword_ids @per = (per.presence || 250) @page = (page.presence || 1) @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 # SHOULD BE []?! end |
Instance Attribute Details
#depiction_matrix ⇒ Hash of Arrays
118 119 120 |
# File 'lib/tools/image_matrix.rb', line 118 def depiction_matrix @depiction_matrix end |
#descriptor_available_keywords ⇒ Array of Objects or null
78 79 80 |
# File 'lib/tools/image_matrix.rb', line 78 def descriptor_available_keywords @descriptor_available_keywords end |
#descriptor_available_languages ⇒ Array of Objects or null
93 94 95 |
# File 'lib/tools/image_matrix.rb', line 93 def descriptor_available_languages @descriptor_available_languages end |
#descriptors ⇒ Object
Returns Descriptor scope.
73 74 75 |
# File 'lib/tools/image_matrix.rb', line 73 def descriptors @descriptors end |
#descriptors_hash ⇒ null
133 134 135 |
# File 'lib/tools/image_matrix.rb', line 133 def descriptors_hash @descriptors_hash end |
#descriptors_with_filter ⇒ null
88 89 90 |
# File 'lib/tools/image_matrix.rb', line 88 def descriptors_with_filter @descriptors_with_filter end |
#eliminate_unknown ⇒ Boolean or null
44 45 46 |
# File 'lib/tools/image_matrix.rb', line 44 def eliminate_unknown @eliminate_unknown end |
#identified_to_rank ⇒ String or null
49 50 51 |
# File 'lib/tools/image_matrix.rb', line 49 def identified_to_rank @identified_to_rank end |
#image_hash ⇒ Hash
123 124 125 |
# File 'lib/tools/image_matrix.rb', line 123 def image_hash @image_hash end |
#keyword_ids ⇒ String or null
28 29 30 |
# File 'lib/tools/image_matrix.rb', line 28 def keyword_ids @keyword_ids end |
#language_id ⇒ String or null
23 24 25 |
# File 'lib/tools/image_matrix.rb', line 23 def language_id @language_id end |
#language_to_use ⇒ Object or null
83 84 85 |
# File 'lib/tools/image_matrix.rb', line 83 def language_to_use @language_to_use end |
#list_of_descriptors ⇒ Hash
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
63 64 65 |
# File 'lib/tools/image_matrix.rb', line 63 def observation_matrix @observation_matrix end |
#observation_matrix_citation ⇒ Object
68 69 70 |
# File 'lib/tools/image_matrix.rb', line 68 def observation_matrix_citation @observation_matrix_citation end |
#observation_matrix_id ⇒ String
13 14 15 |
# File 'lib/tools/image_matrix.rb', line 13 def observation_matrix_id @observation_matrix_id end |
#otu_filter ⇒ String or null
Returns like "otu_filter=1|5|10".
38 39 40 |
# File 'lib/tools/image_matrix.rb', line 38 def otu_filter @otu_filter end |
#otu_id_filter_array ⇒ array
108 109 110 |
# File 'lib/tools/image_matrix.rb', line 108 def otu_id_filter_array @otu_id_filter_array end |
#pagination_next_page ⇒ Integer or null
148 149 150 |
# File 'lib/tools/image_matrix.rb', line 148 def pagination_next_page @pagination_next_page end |
#pagination_page ⇒ Integer
143 144 145 |
# File 'lib/tools/image_matrix.rb', line 143 def pagination_page @pagination_page end |
#pagination_per_page ⇒ Integer or null
158 159 160 |
# File 'lib/tools/image_matrix.rb', line 158 def pagination_per_page @pagination_per_page end |
#pagination_previous_page ⇒ Integer or null
153 154 155 |
# File 'lib/tools/image_matrix.rb', line 153 def pagination_previous_page @pagination_previous_page end |
#pagination_total ⇒ Integer or null
163 164 165 |
# File 'lib/tools/image_matrix.rb', line 163 def pagination_total @pagination_total end |
#pagination_total_pages ⇒ Integer or null
168 169 170 |
# File 'lib/tools/image_matrix.rb', line 168 def pagination_total_pages @pagination_total_pages end |
#per ⇒ Integer or null
54 55 56 |
# File 'lib/tools/image_matrix.rb', line 54 def per @per end |
#project_id ⇒ String
18 19 20 |
# File 'lib/tools/image_matrix.rb', line 18 def project_id @project_id end |
#row_filter ⇒ String or null
33 34 35 |
# File 'lib/tools/image_matrix.rb', line 33 def row_filter @row_filter end |
#row_hash ⇒ null
128 129 130 |
# File 'lib/tools/image_matrix.rb', line 128 def row_hash @row_hash end |
#row_id_filter_array ⇒ array
103 104 105 |
# File 'lib/tools/image_matrix.rb', line 103 def row_id_filter_array @row_id_filter_array end |
#rows_with_filter ⇒ null
98 99 100 |
# File 'lib/tools/image_matrix.rb', line 98 def rows_with_filter @rows_with_filter end |
Instance Method Details
#build_attribution_label(attribution) ⇒ Object (private)
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/tools/image_matrix.rb', line 494 def build_attribution_label(attribution) return nil if attribution.nil? parts = [] year = attribution.copyright_year holders = attribution.copyright_holder_roles.map(&:agent) if year || holders.any? s = '©' + [year, Utilities::Strings.(holders.map(&:name))].compact.join(' ') parts << s end creators = attribution.creator_roles.map(&:agent) parts << 'Created by ' + Utilities::Strings.(creators.map(&:name)) if creators.any? editors = attribution.editor_roles.map(&:agent) parts << 'Edited by ' + Utilities::Strings.(editors.map(&:name)) if editors.any? owners = attribution.owner_roles.map(&:agent) parts << 'Owned by ' + Utilities::Strings.(owners.map(&:name)) if owners.any? parts << 'License: ' + CREATIVE_COMMONS_LICENSES[attribution.license][:name] if attribution.license.present? parts.compact.join('. ') end |
#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 |
# 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 images = Image.where(id: list_of_image_ids).index_by(&:id) citations = Citation .select('citations.*, sources.cached, sources.cached_author_string, sources.year') .joins(:source) .where(citation_object_type: 'Image', citation_object_id: list_of_image_ids) attributions = Attribution .includes( creator_roles: [:person, :organization], editor_roles: [:person, :organization], owner_roles: [:person, :organization], copyright_holder_roles: [:person, :organization] ) .where(attribution_object_type: 'Image', attribution_object_id: list_of_image_ids) .index_by(&:attribution_object_id) h = images.transform_values { |img| { image: img, citations: [], attribution: build_attribution_label(attributions[img.id]) } } citations.each do |c| if h[c.citation_object_id] h[c.citation_object_id][:citations] << c end 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.present? 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
Returns Enumberable.
266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/tools/image_matrix.rb', line 266 def descriptors_with_keywords if observation_matrix_id.to_i == 0 && otu_filter.present? d = observation_depictions_from_otu_filter.pluck(:descriptor_id).uniq ds = Descriptor::Media.where(id: d).not_weight_zero.order(:position) 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.to_a end |
#find_observation_matrix ⇒ Object
228 229 230 |
# File 'lib/tools/image_matrix.rb', line 228 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 AND depictions.depiction_object_type = 'Observation'") .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.present? 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 |