Class: ImageMatrix

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

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

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) ⇒ ImageMatrix

Returns a new instance of ImageMatrix.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/image_matrix.rb', line 132

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)

  @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
  @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
  @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 = []
  @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_matrixObject

Returns the table of observations with images.



110
111
112
# File 'lib/image_matrix.rb', line 110

def depiction_matrix
  @depiction_matrix
end

#descriptor_available_keywordsObject

Returns the list of all Tags used with the descriptors. Descriptors could be filtered by tag_id



70
71
72
# File 'lib/image_matrix.rb', line 70

def descriptor_available_keywords
  @descriptor_available_keywords
end

#descriptor_available_languagesObject

Returns the list of available Languages used as translations for descriptors (in translations are available)



85
86
87
# File 'lib/image_matrix.rb', line 85

def descriptor_available_languages
  @descriptor_available_languages
end

#descriptorsObject

Temporary attribute. Used for validation.



65
66
67
# File 'lib/image_matrix.rb', line 65

def descriptors
  @descriptors
end

#descriptors_hashObject

temporary hash of descriptors; used for calculation of useful and not useful descriptors and their states



125
126
127
# File 'lib/image_matrix.rb', line 125

def descriptors_hash
  @descriptors_hash
end

#descriptors_with_filterObject

Temporary attribute. Used for validation. List of descriptors reduced by keyword_ids



80
81
82
# File 'lib/image_matrix.rb', line 80

def descriptors_with_filter
  @descriptors_with_filter
end

#eliminate_unknownObject

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



43
44
45
# File 'lib/image_matrix.rb', line 43

def eliminate_unknown
  @eliminate_unknown
end

#identified_to_rankObject

Optional attribute to limit identification to OTU or a particular nomenclatural rank. Valid values are 'otu', 'species', 'genus', etc.



48
49
50
# File 'lib/image_matrix.rb', line 48

def identified_to_rank
  @identified_to_rank
end

#image_hashObject

Returns the hash with image attributes



115
116
117
# File 'lib/image_matrix.rb', line 115

def image_hash
  @image_hash
end

#keyword_idsObject

Optional attribute to provide a list of tagIDs to limit the set of descriptors to those taged: “keyword_ids=1|5|15”



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

def keyword_ids
  @keyword_ids
end

#language_idObject

Optional attribute to display the descriptors and character_states in a particular language (when translations are available)



22
23
24
# File 'lib/image_matrix.rb', line 22

def language_id
  @language_id
end

#language_to_useObject

Returns Language as an object if the language_id was provided (used to display descriptors in a particular language)



75
76
77
# File 'lib/image_matrix.rb', line 75

def language_to_use
  @language_to_use
end

#list_of_descriptorsObject

Return the list of descriptors and their states. Translated (if needed) and Sorted



105
106
107
# File 'lib/image_matrix.rb', line 105

def list_of_descriptors
  @list_of_descriptors
end

#list_of_image_idsObject

@! list_of_image_ids

@return [null]

temporary array of image.ids; used to build the @image_hash



130
131
132
# File 'lib/image_matrix.rb', line 130

def list_of_image_ids
  @list_of_image_ids
end

#observation_matrixObject

Returns observation_matrix as an object



55
56
57
# File 'lib/image_matrix.rb', line 55

def observation_matrix
  @observation_matrix
end

#observation_matrix_citationObject

Returns observation_matrix_citation as an object



60
61
62
# File 'lib/image_matrix.rb', line 60

def observation_matrix_citation
  @observation_matrix_citation
end

#observation_matrix_idObject

Required attribude to build the key



12
13
14
# File 'lib/image_matrix.rb', line 12

def observation_matrix_id
  @observation_matrix_id
end

#otu_filterObject

Optional attribute to provide a list of rowIDs to limit the set “otu_filter=1|5|10”



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

def otu_filter
  @otu_filter
end

#otu_id_filter_arrayObject

Array of otu_ids in the @otu_filter



100
101
102
# File 'lib/image_matrix.rb', line 100

def otu_id_filter_array
  @otu_id_filter_array
end

#project_idObject

Required attribute to build the key



17
18
19
# File 'lib/image_matrix.rb', line 17

def project_id
  @project_id
end

#row_filterObject

Optional attribute to provide a list of rowIDs to limit the set “row_filter=1|5|10”



32
33
34
# File 'lib/image_matrix.rb', line 32

def row_filter
  @row_filter
end

#row_hashObject

Temporary hash of rows; used for calculation of remaining and eliminated rows



120
121
122
# File 'lib/image_matrix.rb', line 120

def row_hash
  @row_hash
end

#row_id_filter_arrayObject

Array of row_ids in the @row_filter



95
96
97
# File 'lib/image_matrix.rb', line 95

def row_id_filter_array
  @row_id_filter_array
end

#rows_with_filterObject

Temporary attribute. Used for validation. list of rows to be included into the matrix



90
91
92
# File 'lib/image_matrix.rb', line 90

def rows_with_filter
  @rows_with_filter
end

Instance Method Details

#build_image_hashObject



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/image_matrix.rb', line 352

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 (?)', img_ids )
  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.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[: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

#build_list_of_descriptorsObject



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/image_matrix.rb', line 334

def build_list_of_descriptors
  language = @language_id.blank? ? nil : @language_id.to_i
  n = 0
  h = {}
  @descriptors_with_filter.each do |d|
    descriptor = {}
    descriptor[:index] = n
    descriptor[:id] = d.id
    descriptor[:type] = d.type
    descriptor[:name] = d.target_name(:key, language)
    descriptor[:weight] = d.weight
    descriptor[:description] = d.description
    h[d.id] = descriptor
    n += 1
  end
  h
end

#descriptor_available_languages_listObject



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/image_matrix.rb', line 179

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_initiateObject

descriptors_hash: {:descriptor, ### (descriptor)

:observations,         ### (array of observations for )
:state_ids,            ### {hash of state_ids used in the particular matrix
}}


278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/image_matrix.rb', line 278

def descriptors_hash_initiate
  h = {}
  if @observation_matrix_id.to_i == 0 && !@otu_filter.blank?
    depictions = observation_depictions_from_otu_filter
  else
    depictions = @observation_matrix.observation_depictions
  end

  descriptors_count = @list_of_descriptors.count
  @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,
                  depictions: Array.new(descriptors_count) {Array.new},
                  } if h[r_key].nil?
    end
  end

  depictions.each do |o|
    next if @otu_id_filter_array && !@otu_id_filter_array.include?(o.otu_id)
    otu_collection_object = o.otu_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_keywordsObject



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/image_matrix.rb', line 206

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_matrixObject



170
171
172
# File 'lib/image_matrix.rb', line 170

def find_observation_matrix
  ObservationMatrix.where(id: @observation_matrix_id.to_i, project_id: @project_id).first
end

#get_rows_with_filterObject



228
229
230
231
# File 'lib/image_matrix.rb', line 228

def get_rows_with_filter
  return [] if @observation_matrix.nil?
  @observation_matrix.observation_matrix_rows.order(:position)
end

#observation_depictions_from_otu_filterObject



310
311
312
313
314
315
316
317
318
319
# File 'lib/image_matrix.rb', line 310

def observation_depictions_from_otu_filter
    Depiction.select('depictions.*, observations.descriptor_id, observations.otu_id, observations.collection_object_id, 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.otu_id IN (?)", @otu_id_filter_array)
        .where('observations.project_id = (?)', @project_id)
        .order('depictions.position')
end

#otu_filter_arrayObject



224
225
226
# File 'lib/image_matrix.rb', line 224

def otu_filter_array
  @otu_filter.blank? ? nil : otu_filter.to_s.split('|').map(&:to_i)
end

#row_filter_arrayObject



220
221
222
# File 'lib/image_matrix.rb', line 220

def row_filter_array
  @row_filter.blank? ? nil : row_filter.to_s.split('|').map(&:to_i)
end

#row_hash_initiateObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/image_matrix.rb', line 233

def row_hash_initiate
  h = {}
  if @observation_matrix_id.to_i == 0 && !@otu_filter.blank?
    o = observation_depictions_from_otu_filter.pluck(:otu_id).uniq
    @otu_id_filter_array = @otu_id_filter_array & o
    rows = Otu.where('otus.id IN (?)', @otu_id_filter_array)
  else
    rows = @rows_with_filter
  end
  rows.each do |r|
    case r.class.to_s
      when 'Otu'
        otu_collection_object = r.id.to_s + '|'
      when 'ObservationMatrixRow'
        otu_collection_object = 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.otu_id
  end
  h
end

#selected_descriptors_hash_initiateObject

returns ['1', '3'], 125: ['3', '5'], 135: ['2'], 136: ['true'], 140: ['5-10']



322
323
324
325
326
327
328
329
330
331
332
# File 'lib/image_matrix.rb', line 322

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