Class: ImageMatrix

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

Overview

Contains methods used to build an image_matrix table

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.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/image_matrix.rb', line 120

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
  @descriptor_available_languages = descriptor_available_languages
  @language_id = language_id
  @language_to_use = language_to_use
  @keyword_ids = keyword_ids
  @descriptor_available_keywords = descriptor_available_keywords
  @descriptors_with_filter = descriptors_with_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
  ###main_logic
  @list_of_descriptors = build_list_of_descriptors
  @depiction_matrix = descriptors_hash_initiate
  ###delete temporary data
  @row_hash = nil
  @rows_with_filter = nil
  @descriptors_with_filter = nil
end

Instance Attribute Details

#depiction_matrixObject

Returns the table of observations with images.



108
109
110
# File 'lib/image_matrix.rb', line 108

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



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

def descriptor_available_keywords
  @descriptor_available_keywords
end

#descriptor_available_languagesObject

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



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

def descriptor_available_languages
  @descriptor_available_languages
end

#descriptorsObject

Temporary attribute. Used for validation.



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

def descriptors
  @descriptors
end

#descriptors_hashObject

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



118
119
120
# File 'lib/image_matrix.rb', line 118

def descriptors_hash
  @descriptors_hash
end

#descriptors_with_filterObject

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



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

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



40
41
42
# File 'lib/image_matrix.rb', line 40

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.



45
46
47
# File 'lib/image_matrix.rb', line 45

def identified_to_rank
  @identified_to_rank
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”



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

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)



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

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)



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

def language_to_use
  @language_to_use
end

#list_of_descriptorsObject

Return the list of descriptors and their states. Translated (if needed) and Sorted Each descriptor has an attribute :status, which could be 'used', 'useful', 'useless' for further identification



103
104
105
# File 'lib/image_matrix.rb', line 103

def list_of_descriptors
  @list_of_descriptors
end

#observation_matrixObject

Returns observation_matrix as an object



52
53
54
# File 'lib/image_matrix.rb', line 52

def observation_matrix
  @observation_matrix
end

#observation_matrix_citationObject

Returns observation_matrix_citation as an object



57
58
59
# File 'lib/image_matrix.rb', line 57

def observation_matrix_citation
  @observation_matrix_citation
end

#observation_matrix_idObject

Required attribude to build the key



9
10
11
# File 'lib/image_matrix.rb', line 9

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”



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

def otu_filter
  @otu_filter
end

#otu_id_filter_arrayObject

Array of otu_ids in the @otu_filter



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

def otu_id_filter_array
  @otu_id_filter_array
end

#project_idObject

Required attribute to build the key



14
15
16
# File 'lib/image_matrix.rb', line 14

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”



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

def row_filter
  @row_filter
end

#row_hashObject

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



113
114
115
# File 'lib/image_matrix.rb', line 113

def row_hash
  @row_hash
end

#row_id_filter_arrayObject

Array of row_ids in the @row_filter



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

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



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

def rows_with_filter
  @rows_with_filter
end

Instance Method Details

#build_list_of_descriptorsObject



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/image_matrix.rb', line 312

def build_list_of_descriptors
  language = @language_id.blank? ? nil : @language_id.to_i
  n = 0
  h = {}
  descriptors_with_keywords.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[:position] = d.position
    descriptor[:description] = d.description
    h[d.id] = descriptor
    n += 1
  end
  h
end

#descriptors_hash_initiateObject

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

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


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
283
284
285
286
# File 'lib/image_matrix.rb', line 258

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]                                                                #??????
    end
  end
  h
end

#descriptors_with_keywordsObject



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

def descriptors_with_keywords
  if @observation_matrix_id.to_i == 0 && !@otu_filter.blank?
    d = observation_depictions_from_otu_filter.pluck(:descriptor_id).uniq
    Descriptor.where('descriptors.id IN (?)', d).not_weight_zero.order(:position)
  elsif @keyword_ids
    descriptors.joins(:tags).where('tags.keyword_id IN (?)', @keyword_ids.to_s.split('|').map(&:to_i) )
  else
    descriptors
  end
end

#find_observation_matrixObject



155
156
157
# File 'lib/image_matrix.rb', line 155

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

#get_rows_with_filterObject



210
211
212
213
# File 'lib/image_matrix.rb', line 210

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

#observation_depictions_from_otu_filterObject



288
289
290
291
292
293
294
295
296
297
# File 'lib/image_matrix.rb', line 288

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.otu_id IN (?)', @otu_id_filter_array)
        .where('observations.project_id = (?)', @project_id)
        .order('depictions.position')
end

#otu_filter_arrayObject



206
207
208
# File 'lib/image_matrix.rb', line 206

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

#row_filter_arrayObject



202
203
204
# File 'lib/image_matrix.rb', line 202

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

#row_hash_initiateObject

row_hash: {:object, ### (collection_object or OTU)

:object_at_rank,   ### (converted to OTU or TN)
:row_id,
:otu_id,
:errors,           ### (calculated number of errors)
:status }         ### ('remaining', 'eliminated')


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/image_matrix.rb', line 221

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'
      h[otu_collection_object][:object_at_rank] = r.current_otu || r
    elsif @identified_to_rank
      h[otu_collection_object][:object_at_rank] = r&.current_taxon_name&.ancestor_at_rank(@identified_to_rank, inlude_self = true) || r
    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
    h[otu_collection_object][:errors] = 0
    h[otu_collection_object][:error_descriptors] = []
    h[otu_collection_object][:status] = 'remaining' ### if number of errors > @error_tolerance, replaced to 'eliminated'
  end
  h
end

#selected_descriptors_hash_initiateObject

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



300
301
302
303
304
305
306
307
308
309
310
# File 'lib/image_matrix.rb', line 300

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