Class: ImageMatrix

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

Overview

Contains methods used to build an image_matrix table

localhost:3000/tasks/observation_matrices/image_matrix/0/key?otu_filter=30947|22978|23065

@proceps: TODO: remove all `@` when getting the value from @variable in methods. Only use `@` when setting the variable, like so:

attr_accessor :foo
@foo = 1
foo = 1 ? true : false

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.



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
169
170
171
172
173
174
175
176
177
178
# File 'lib/image_matrix.rb', line 138

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.



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

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



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

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)



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

def descriptor_available_languages
  @descriptor_available_languages
end

#descriptorsObject

Temporary attribute. Used for validation.



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

def descriptors
  @descriptors
end

#descriptors_hashObject

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



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

def descriptors_hash
  @descriptors_hash
end

#descriptors_with_filterObject

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



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

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



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

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.



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

def identified_to_rank
  @identified_to_rank
end

#image_hashObject

Returns the hash with image attributes



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

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”



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

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)



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

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)



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

def language_to_use
  @language_to_use
end

#list_of_descriptorsObject

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



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

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



136
137
138
# File 'lib/image_matrix.rb', line 136

def list_of_image_ids
  @list_of_image_ids
end

#observation_matrixObject

Returns observation_matrix as an object



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

def observation_matrix
  @observation_matrix
end

#observation_matrix_citationObject

Returns observation_matrix_citation as an object



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

def observation_matrix_citation
  @observation_matrix_citation
end

#observation_matrix_idObject

Required attribude to build the key



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

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”



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

def otu_filter
  @otu_filter
end

#otu_id_filter_arrayObject

Array of otu_ids in the @otu_filter



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

def otu_id_filter_array
  @otu_id_filter_array
end

#project_idObject

Required attribute to build the key



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

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”



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

def row_filter
  @row_filter
end

#row_hashObject

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



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

def row_hash
  @row_hash
end

#row_id_filter_arrayObject

Array of row_ids in the @row_filter



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

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



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

def rows_with_filter
  @rows_with_filter
end

Instance Method Details

#build_image_hashObject



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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/image_matrix.rb', line 363

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[:short_url] = short_url(d.image_file.url) #this does not work in a model
    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.cached_author_string
    i[:year] = c.year
    #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



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/image_matrix.rb', line 345

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



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/image_matrix.rb', line 189

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
}}


288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/image_matrix.rb', line 288

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,
                  otu_id: r_value[:otu_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



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/image_matrix.rb', line 216

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



180
181
182
# File 'lib/image_matrix.rb', line 180

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

#get_rows_with_filterObject



238
239
240
241
# File 'lib/image_matrix.rb', line 238

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

#observation_depictions_from_otu_filterObject



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

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



234
235
236
# File 'lib/image_matrix.rb', line 234

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

#row_filter_arrayObject



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

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

#row_hash_initiateObject



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
273
274
275
276
277
278
279
280
281
282
# File 'lib/image_matrix.rb', line 243

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']



333
334
335
336
337
338
339
340
341
342
343
# File 'lib/image_matrix.rb', line 333

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