Class: SqedDepiction

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::Notes, Shared::Tags
Defined in:
app/models/sqed_depiction.rb

Overview

A SqedDepiction identifies a depiction as sqed (github.com/SpeciesFileGroup/sqed) parsable, and records the metadata required for parsing a stage image.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods included from ActiverecordUtilities

#trim_attributes

Instance Attribute Details

- (Symbol) boundary_color

Color of the boundaries in the image, default/recommendation is green.

Returns:

  • (Symbol)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/sqed_depiction.rb', line 36

class SqedDepiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes

  belongs_to :depiction
  has_one :image, through: :depiction

  validates_presence_of  :depiction
  validates_presence_of  :metadata_map, :boundary_color
  validates_inclusion_of :layout, in: SqedConfig::LAYOUTS.keys.map(&:to_s)
  validates_inclusion_of :boundary_finder, in: %w{Sqed::BoundaryFinder::ColorLineFinder Sqed::BoundaryFinder::Cross}
  validates_inclusion_of :has_border, in: [true, false]

  accepts_nested_attributes_for :depiction

  def 
    {
      boundary_color: boundary_color.to_sym,
      boundary_finder: boundary_finder,
      has_border: has_border,
      layout: layout.to_sym,
      metadata_map: 
    }
  end

  def depiction_object
    depiction.depiction_object
  end

  def self.without_collection_object_data
    CollectionObject.joins(:sqed_depictions).where(buffered_collecting_event: nil, buffered_determinations: nil, buffered_other_labels: nil)
  end

  # @return [SqedDepiction]
  #   the next record in which the collection object has no buffered data
  def next_without_data
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).where('collection_objects.id <> ?', depiction_object.id).where('sqed_depictions.id > ?', id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(:id).first : object.sqed_depictions.first
  end

  # @return [CollectionObject, nil]
  #    the next collection object, by :id, created from the addition of a SqedDepiction
  def next_collection_object
    object = CollectionObject.joins(:sqed_depictions).where(project_id: project_id).where('sqed_depictions.id > ?', id).where('collection_objects.id <> ?', depiction_object.id).order(:id).first
    object = CollectionObject.joins(:sqed_depictions).order(:id).first if object.nil?
    object
  end

  # @return [Array of symbols]
  #   the (named) sections in this depiction that may have collecting event label metadata
  def collecting_event_sections
      # !! master merge
    [:collecting_event_labels, :annotated_specimen] & [:metadata_map].values
  end

  def nearby_sqed_depictions(before = 5, after = 5)
    a = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(after)
    b = SqedDepiction.where(project_id: project_id).where('id < ?', id).order('id DESC').limit(before)
    return { before: b, after: a}
  end

  def next_sqed_depiction
    sd = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(1)
    sd.any? ? sd.first : SqedDepiction.where(project_id: project_id).first
  end

  def preprocess
    result = SqedToTaxonworks::Result.new(depiction_id: depiction.id)
    result.cache_all
  end

  # @return [Integer]
  #   caches section coordinates and ocr text for the first images that don't have such caches !! does not take into account, just finds and processes
  def self.preprocess_empty(total = 10)
    t = SqedDepiction.arel_table
    i = 0
    while i < total
      r = SqedDepiction.where(t[:result_ocr].eq(nil).or(t[:result_boundary_coordinates].eq(nil)).to_sql).limit(1).first
      return i if r.nil?
      r.preprocess
      i = i + 1
    end
    i
  end

  protected

  def 
    .inject({}){|hsh, i| hsh.merge(i[0].to_i => i[1].to_sym)}
  end

end

- (String) boundary_finder

Name of the sqed BoundaryFinder class to use, e.g. 'Sqed::BoundaryFinder::ColorLineFinder'

Returns:

  • (String)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/sqed_depiction.rb', line 36

class SqedDepiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes

  belongs_to :depiction
  has_one :image, through: :depiction

  validates_presence_of  :depiction
  validates_presence_of  :metadata_map, :boundary_color
  validates_inclusion_of :layout, in: SqedConfig::LAYOUTS.keys.map(&:to_s)
  validates_inclusion_of :boundary_finder, in: %w{Sqed::BoundaryFinder::ColorLineFinder Sqed::BoundaryFinder::Cross}
  validates_inclusion_of :has_border, in: [true, false]

  accepts_nested_attributes_for :depiction

  def 
    {
      boundary_color: boundary_color.to_sym,
      boundary_finder: boundary_finder,
      has_border: has_border,
      layout: layout.to_sym,
      metadata_map: 
    }
  end

  def depiction_object
    depiction.depiction_object
  end

  def self.without_collection_object_data
    CollectionObject.joins(:sqed_depictions).where(buffered_collecting_event: nil, buffered_determinations: nil, buffered_other_labels: nil)
  end

  # @return [SqedDepiction]
  #   the next record in which the collection object has no buffered data
  def next_without_data
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).where('collection_objects.id <> ?', depiction_object.id).where('sqed_depictions.id > ?', id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(:id).first : object.sqed_depictions.first
  end

  # @return [CollectionObject, nil]
  #    the next collection object, by :id, created from the addition of a SqedDepiction
  def next_collection_object
    object = CollectionObject.joins(:sqed_depictions).where(project_id: project_id).where('sqed_depictions.id > ?', id).where('collection_objects.id <> ?', depiction_object.id).order(:id).first
    object = CollectionObject.joins(:sqed_depictions).order(:id).first if object.nil?
    object
  end

  # @return [Array of symbols]
  #   the (named) sections in this depiction that may have collecting event label metadata
  def collecting_event_sections
      # !! master merge
    [:collecting_event_labels, :annotated_specimen] & [:metadata_map].values
  end

  def nearby_sqed_depictions(before = 5, after = 5)
    a = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(after)
    b = SqedDepiction.where(project_id: project_id).where('id < ?', id).order('id DESC').limit(before)
    return { before: b, after: a}
  end

  def next_sqed_depiction
    sd = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(1)
    sd.any? ? sd.first : SqedDepiction.where(project_id: project_id).first
  end

  def preprocess
    result = SqedToTaxonworks::Result.new(depiction_id: depiction.id)
    result.cache_all
  end

  # @return [Integer]
  #   caches section coordinates and ocr text for the first images that don't have such caches !! does not take into account, just finds and processes
  def self.preprocess_empty(total = 10)
    t = SqedDepiction.arel_table
    i = 0
    while i < total
      r = SqedDepiction.where(t[:result_ocr].eq(nil).or(t[:result_boundary_coordinates].eq(nil)).to_sql).limit(1).first
      return i if r.nil?
      r.preprocess
      i = i + 1
    end
    i
  end

  protected

  def 
    .inject({}){|hsh, i| hsh.merge(i[0].to_i => i[1].to_sym)}
  end

end

- (Boolean) has_border

Returns True if the stage image has a border than needs to be detected

Returns:

  • (Boolean)

    True if the stage image has a border than needs to be detected



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/sqed_depiction.rb', line 36

class SqedDepiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes

  belongs_to :depiction
  has_one :image, through: :depiction

  validates_presence_of  :depiction
  validates_presence_of  :metadata_map, :boundary_color
  validates_inclusion_of :layout, in: SqedConfig::LAYOUTS.keys.map(&:to_s)
  validates_inclusion_of :boundary_finder, in: %w{Sqed::BoundaryFinder::ColorLineFinder Sqed::BoundaryFinder::Cross}
  validates_inclusion_of :has_border, in: [true, false]

  accepts_nested_attributes_for :depiction

  def 
    {
      boundary_color: boundary_color.to_sym,
      boundary_finder: boundary_finder,
      has_border: has_border,
      layout: layout.to_sym,
      metadata_map: 
    }
  end

  def depiction_object
    depiction.depiction_object
  end

  def self.without_collection_object_data
    CollectionObject.joins(:sqed_depictions).where(buffered_collecting_event: nil, buffered_determinations: nil, buffered_other_labels: nil)
  end

  # @return [SqedDepiction]
  #   the next record in which the collection object has no buffered data
  def next_without_data
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).where('collection_objects.id <> ?', depiction_object.id).where('sqed_depictions.id > ?', id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(:id).first : object.sqed_depictions.first
  end

  # @return [CollectionObject, nil]
  #    the next collection object, by :id, created from the addition of a SqedDepiction
  def next_collection_object
    object = CollectionObject.joins(:sqed_depictions).where(project_id: project_id).where('sqed_depictions.id > ?', id).where('collection_objects.id <> ?', depiction_object.id).order(:id).first
    object = CollectionObject.joins(:sqed_depictions).order(:id).first if object.nil?
    object
  end

  # @return [Array of symbols]
  #   the (named) sections in this depiction that may have collecting event label metadata
  def collecting_event_sections
      # !! master merge
    [:collecting_event_labels, :annotated_specimen] & [:metadata_map].values
  end

  def nearby_sqed_depictions(before = 5, after = 5)
    a = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(after)
    b = SqedDepiction.where(project_id: project_id).where('id < ?', id).order('id DESC').limit(before)
    return { before: b, after: a}
  end

  def next_sqed_depiction
    sd = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(1)
    sd.any? ? sd.first : SqedDepiction.where(project_id: project_id).first
  end

  def preprocess
    result = SqedToTaxonworks::Result.new(depiction_id: depiction.id)
    result.cache_all
  end

  # @return [Integer]
  #   caches section coordinates and ocr text for the first images that don't have such caches !! does not take into account, just finds and processes
  def self.preprocess_empty(total = 10)
    t = SqedDepiction.arel_table
    i = 0
    while i < total
      r = SqedDepiction.where(t[:result_ocr].eq(nil).or(t[:result_boundary_coordinates].eq(nil)).to_sql).limit(1).first
      return i if r.nil?
      r.preprocess
      i = i + 1
    end
    i
  end

  protected

  def 
    .inject({}){|hsh, i| hsh.merge(i[0].to_i => i[1].to_sym)}
  end

end

- (Symbol) layout

The Sqed layout, like :cross, :equal_cross, :vertical_offset_cross, :internal_box etc.

Returns:

  • (Symbol)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/sqed_depiction.rb', line 36

class SqedDepiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes

  belongs_to :depiction
  has_one :image, through: :depiction

  validates_presence_of  :depiction
  validates_presence_of  :metadata_map, :boundary_color
  validates_inclusion_of :layout, in: SqedConfig::LAYOUTS.keys.map(&:to_s)
  validates_inclusion_of :boundary_finder, in: %w{Sqed::BoundaryFinder::ColorLineFinder Sqed::BoundaryFinder::Cross}
  validates_inclusion_of :has_border, in: [true, false]

  accepts_nested_attributes_for :depiction

  def 
    {
      boundary_color: boundary_color.to_sym,
      boundary_finder: boundary_finder,
      has_border: has_border,
      layout: layout.to_sym,
      metadata_map: 
    }
  end

  def depiction_object
    depiction.depiction_object
  end

  def self.without_collection_object_data
    CollectionObject.joins(:sqed_depictions).where(buffered_collecting_event: nil, buffered_determinations: nil, buffered_other_labels: nil)
  end

  # @return [SqedDepiction]
  #   the next record in which the collection object has no buffered data
  def next_without_data
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).where('collection_objects.id <> ?', depiction_object.id).where('sqed_depictions.id > ?', id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(:id).first : object.sqed_depictions.first
  end

  # @return [CollectionObject, nil]
  #    the next collection object, by :id, created from the addition of a SqedDepiction
  def next_collection_object
    object = CollectionObject.joins(:sqed_depictions).where(project_id: project_id).where('sqed_depictions.id > ?', id).where('collection_objects.id <> ?', depiction_object.id).order(:id).first
    object = CollectionObject.joins(:sqed_depictions).order(:id).first if object.nil?
    object
  end

  # @return [Array of symbols]
  #   the (named) sections in this depiction that may have collecting event label metadata
  def collecting_event_sections
      # !! master merge
    [:collecting_event_labels, :annotated_specimen] & [:metadata_map].values
  end

  def nearby_sqed_depictions(before = 5, after = 5)
    a = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(after)
    b = SqedDepiction.where(project_id: project_id).where('id < ?', id).order('id DESC').limit(before)
    return { before: b, after: a}
  end

  def next_sqed_depiction
    sd = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(1)
    sd.any? ? sd.first : SqedDepiction.where(project_id: project_id).first
  end

  def preprocess
    result = SqedToTaxonworks::Result.new(depiction_id: depiction.id)
    result.cache_all
  end

  # @return [Integer]
  #   caches section coordinates and ocr text for the first images that don't have such caches !! does not take into account, just finds and processes
  def self.preprocess_empty(total = 10)
    t = SqedDepiction.arel_table
    i = 0
    while i < total
      r = SqedDepiction.where(t[:result_ocr].eq(nil).or(t[:result_boundary_coordinates].eq(nil)).to_sql).limit(1).first
      return i if r.nil?
      r.preprocess
      i = i + 1
    end
    i
  end

  protected

  def 
    .inject({}){|hsh, i| hsh.merge(i[0].to_i => i[1].to_sym)}
  end

end

- (Hash) metadata_map

The sqed metadata map, e.g. {0 => :curator_metadata, 1 => :identifier, 2 => :image_registration, 3 => :annotated_specimen }.

Returns:

  • (Hash)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/sqed_depiction.rb', line 36

class SqedDepiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes

  belongs_to :depiction
  has_one :image, through: :depiction

  validates_presence_of  :depiction
  validates_presence_of  :metadata_map, :boundary_color
  validates_inclusion_of :layout, in: SqedConfig::LAYOUTS.keys.map(&:to_s)
  validates_inclusion_of :boundary_finder, in: %w{Sqed::BoundaryFinder::ColorLineFinder Sqed::BoundaryFinder::Cross}
  validates_inclusion_of :has_border, in: [true, false]

  accepts_nested_attributes_for :depiction

  def 
    {
      boundary_color: boundary_color.to_sym,
      boundary_finder: boundary_finder,
      has_border: has_border,
      layout: layout.to_sym,
      metadata_map: 
    }
  end

  def depiction_object
    depiction.depiction_object
  end

  def self.without_collection_object_data
    CollectionObject.joins(:sqed_depictions).where(buffered_collecting_event: nil, buffered_determinations: nil, buffered_other_labels: nil)
  end

  # @return [SqedDepiction]
  #   the next record in which the collection object has no buffered data
  def next_without_data
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).where('collection_objects.id <> ?', depiction_object.id).where('sqed_depictions.id > ?', id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(:id).first : object.sqed_depictions.first
  end

  # @return [CollectionObject, nil]
  #    the next collection object, by :id, created from the addition of a SqedDepiction
  def next_collection_object
    object = CollectionObject.joins(:sqed_depictions).where(project_id: project_id).where('sqed_depictions.id > ?', id).where('collection_objects.id <> ?', depiction_object.id).order(:id).first
    object = CollectionObject.joins(:sqed_depictions).order(:id).first if object.nil?
    object
  end

  # @return [Array of symbols]
  #   the (named) sections in this depiction that may have collecting event label metadata
  def collecting_event_sections
      # !! master merge
    [:collecting_event_labels, :annotated_specimen] & [:metadata_map].values
  end

  def nearby_sqed_depictions(before = 5, after = 5)
    a = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(after)
    b = SqedDepiction.where(project_id: project_id).where('id < ?', id).order('id DESC').limit(before)
    return { before: b, after: a}
  end

  def next_sqed_depiction
    sd = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(1)
    sd.any? ? sd.first : SqedDepiction.where(project_id: project_id).first
  end

  def preprocess
    result = SqedToTaxonworks::Result.new(depiction_id: depiction.id)
    result.cache_all
  end

  # @return [Integer]
  #   caches section coordinates and ocr text for the first images that don't have such caches !! does not take into account, just finds and processes
  def self.preprocess_empty(total = 10)
    t = SqedDepiction.arel_table
    i = 0
    while i < total
      r = SqedDepiction.where(t[:result_ocr].eq(nil).or(t[:result_boundary_coordinates].eq(nil)).to_sql).limit(1).first
      return i if r.nil?
      r.preprocess
      i = i + 1
    end
    i
  end

  protected

  def 
    .inject({}){|hsh, i| hsh.merge(i[0].to_i => i[1].to_sym)}
  end

end

- (Hash) result_boundaries

Returns A cache for the result

Returns:

  • (Hash)

    A cache for the result



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/sqed_depiction.rb', line 36

class SqedDepiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes

  belongs_to :depiction
  has_one :image, through: :depiction

  validates_presence_of  :depiction
  validates_presence_of  :metadata_map, :boundary_color
  validates_inclusion_of :layout, in: SqedConfig::LAYOUTS.keys.map(&:to_s)
  validates_inclusion_of :boundary_finder, in: %w{Sqed::BoundaryFinder::ColorLineFinder Sqed::BoundaryFinder::Cross}
  validates_inclusion_of :has_border, in: [true, false]

  accepts_nested_attributes_for :depiction

  def 
    {
      boundary_color: boundary_color.to_sym,
      boundary_finder: boundary_finder,
      has_border: has_border,
      layout: layout.to_sym,
      metadata_map: 
    }
  end

  def depiction_object
    depiction.depiction_object
  end

  def self.without_collection_object_data
    CollectionObject.joins(:sqed_depictions).where(buffered_collecting_event: nil, buffered_determinations: nil, buffered_other_labels: nil)
  end

  # @return [SqedDepiction]
  #   the next record in which the collection object has no buffered data
  def next_without_data
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).where('collection_objects.id <> ?', depiction_object.id).where('sqed_depictions.id > ?', id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(:id).first : object.sqed_depictions.first
  end

  # @return [CollectionObject, nil]
  #    the next collection object, by :id, created from the addition of a SqedDepiction
  def next_collection_object
    object = CollectionObject.joins(:sqed_depictions).where(project_id: project_id).where('sqed_depictions.id > ?', id).where('collection_objects.id <> ?', depiction_object.id).order(:id).first
    object = CollectionObject.joins(:sqed_depictions).order(:id).first if object.nil?
    object
  end

  # @return [Array of symbols]
  #   the (named) sections in this depiction that may have collecting event label metadata
  def collecting_event_sections
      # !! master merge
    [:collecting_event_labels, :annotated_specimen] & [:metadata_map].values
  end

  def nearby_sqed_depictions(before = 5, after = 5)
    a = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(after)
    b = SqedDepiction.where(project_id: project_id).where('id < ?', id).order('id DESC').limit(before)
    return { before: b, after: a}
  end

  def next_sqed_depiction
    sd = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(1)
    sd.any? ? sd.first : SqedDepiction.where(project_id: project_id).first
  end

  def preprocess
    result = SqedToTaxonworks::Result.new(depiction_id: depiction.id)
    result.cache_all
  end

  # @return [Integer]
  #   caches section coordinates and ocr text for the first images that don't have such caches !! does not take into account, just finds and processes
  def self.preprocess_empty(total = 10)
    t = SqedDepiction.arel_table
    i = 0
    while i < total
      r = SqedDepiction.where(t[:result_ocr].eq(nil).or(t[:result_boundary_coordinates].eq(nil)).to_sql).limit(1).first
      return i if r.nil?
      r.preprocess
      i = i + 1
    end
    i
  end

  protected

  def 
    .inject({}){|hsh, i| hsh.merge(i[0].to_i => i[1].to_sym)}
  end

end

- (Hash) result_ocr

Returns A cache for the ocr result

Returns:

  • (Hash)

    A cache for the ocr result



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/sqed_depiction.rb', line 36

class SqedDepiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes

  belongs_to :depiction
  has_one :image, through: :depiction

  validates_presence_of  :depiction
  validates_presence_of  :metadata_map, :boundary_color
  validates_inclusion_of :layout, in: SqedConfig::LAYOUTS.keys.map(&:to_s)
  validates_inclusion_of :boundary_finder, in: %w{Sqed::BoundaryFinder::ColorLineFinder Sqed::BoundaryFinder::Cross}
  validates_inclusion_of :has_border, in: [true, false]

  accepts_nested_attributes_for :depiction

  def 
    {
      boundary_color: boundary_color.to_sym,
      boundary_finder: boundary_finder,
      has_border: has_border,
      layout: layout.to_sym,
      metadata_map: 
    }
  end

  def depiction_object
    depiction.depiction_object
  end

  def self.without_collection_object_data
    CollectionObject.joins(:sqed_depictions).where(buffered_collecting_event: nil, buffered_determinations: nil, buffered_other_labels: nil)
  end

  # @return [SqedDepiction]
  #   the next record in which the collection object has no buffered data
  def next_without_data
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).where('collection_objects.id <> ?', depiction_object.id).where('sqed_depictions.id > ?', id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(:id).first : object.sqed_depictions.first
  end

  # @return [CollectionObject, nil]
  #    the next collection object, by :id, created from the addition of a SqedDepiction
  def next_collection_object
    object = CollectionObject.joins(:sqed_depictions).where(project_id: project_id).where('sqed_depictions.id > ?', id).where('collection_objects.id <> ?', depiction_object.id).order(:id).first
    object = CollectionObject.joins(:sqed_depictions).order(:id).first if object.nil?
    object
  end

  # @return [Array of symbols]
  #   the (named) sections in this depiction that may have collecting event label metadata
  def collecting_event_sections
      # !! master merge
    [:collecting_event_labels, :annotated_specimen] & [:metadata_map].values
  end

  def nearby_sqed_depictions(before = 5, after = 5)
    a = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(after)
    b = SqedDepiction.where(project_id: project_id).where('id < ?', id).order('id DESC').limit(before)
    return { before: b, after: a}
  end

  def next_sqed_depiction
    sd = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(1)
    sd.any? ? sd.first : SqedDepiction.where(project_id: project_id).first
  end

  def preprocess
    result = SqedToTaxonworks::Result.new(depiction_id: depiction.id)
    result.cache_all
  end

  # @return [Integer]
  #   caches section coordinates and ocr text for the first images that don't have such caches !! does not take into account, just finds and processes
  def self.preprocess_empty(total = 10)
    t = SqedDepiction.arel_table
    i = 0
    while i < total
      r = SqedDepiction.where(t[:result_ocr].eq(nil).or(t[:result_boundary_coordinates].eq(nil)).to_sql).limit(1).first
      return i if r.nil?
      r.preprocess
      i = i + 1
    end
    i
  end

  protected

  def 
    .inject({}){|hsh, i| hsh.merge(i[0].to_i => i[1].to_sym)}
  end

end

- (Hash) specimen_coordinates

Returns Not presently used, the specific coordinates bounding the specimen(s) only

Returns:

  • (Hash)

    Not presently used, the specific coordinates bounding the specimen(s) only



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/sqed_depiction.rb', line 36

class SqedDepiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes

  belongs_to :depiction
  has_one :image, through: :depiction

  validates_presence_of  :depiction
  validates_presence_of  :metadata_map, :boundary_color
  validates_inclusion_of :layout, in: SqedConfig::LAYOUTS.keys.map(&:to_s)
  validates_inclusion_of :boundary_finder, in: %w{Sqed::BoundaryFinder::ColorLineFinder Sqed::BoundaryFinder::Cross}
  validates_inclusion_of :has_border, in: [true, false]

  accepts_nested_attributes_for :depiction

  def 
    {
      boundary_color: boundary_color.to_sym,
      boundary_finder: boundary_finder,
      has_border: has_border,
      layout: layout.to_sym,
      metadata_map: 
    }
  end

  def depiction_object
    depiction.depiction_object
  end

  def self.without_collection_object_data
    CollectionObject.joins(:sqed_depictions).where(buffered_collecting_event: nil, buffered_determinations: nil, buffered_other_labels: nil)
  end

  # @return [SqedDepiction]
  #   the next record in which the collection object has no buffered data
  def next_without_data
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).where('collection_objects.id <> ?', depiction_object.id).where('sqed_depictions.id > ?', id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(:id).first : object.sqed_depictions.first
  end

  # @return [CollectionObject, nil]
  #    the next collection object, by :id, created from the addition of a SqedDepiction
  def next_collection_object
    object = CollectionObject.joins(:sqed_depictions).where(project_id: project_id).where('sqed_depictions.id > ?', id).where('collection_objects.id <> ?', depiction_object.id).order(:id).first
    object = CollectionObject.joins(:sqed_depictions).order(:id).first if object.nil?
    object
  end

  # @return [Array of symbols]
  #   the (named) sections in this depiction that may have collecting event label metadata
  def collecting_event_sections
      # !! master merge
    [:collecting_event_labels, :annotated_specimen] & [:metadata_map].values
  end

  def nearby_sqed_depictions(before = 5, after = 5)
    a = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(after)
    b = SqedDepiction.where(project_id: project_id).where('id < ?', id).order('id DESC').limit(before)
    return { before: b, after: a}
  end

  def next_sqed_depiction
    sd = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(1)
    sd.any? ? sd.first : SqedDepiction.where(project_id: project_id).first
  end

  def preprocess
    result = SqedToTaxonworks::Result.new(depiction_id: depiction.id)
    result.cache_all
  end

  # @return [Integer]
  #   caches section coordinates and ocr text for the first images that don't have such caches !! does not take into account, just finds and processes
  def self.preprocess_empty(total = 10)
    t = SqedDepiction.arel_table
    i = 0
    while i < total
      r = SqedDepiction.where(t[:result_ocr].eq(nil).or(t[:result_boundary_coordinates].eq(nil)).to_sql).limit(1).first
      return i if r.nil?
      r.preprocess
      i = i + 1
    end
    i
  end

  protected

  def 
    .inject({}){|hsh, i| hsh.merge(i[0].to_i => i[1].to_sym)}
  end

end

Class Method Details

+ (Integer) preprocess_empty(total = 10)

Returns caches section coordinates and ocr text for the first images that don't have such caches !! does not take into account, just finds and processes

Returns:

  • (Integer)

    caches section coordinates and ocr text for the first images that don't have such caches !! does not take into account, just finds and processes



110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/sqed_depiction.rb', line 110

def self.preprocess_empty(total = 10)
  t = SqedDepiction.arel_table
  i = 0
  while i < total
    r = SqedDepiction.where(t[:result_ocr].eq(nil).or(t[:result_boundary_coordinates].eq(nil)).to_sql).limit(1).first
    return i if r.nil?
    r.preprocess
    i = i + 1
  end
  i
end

+ (Object) without_collection_object_data



66
67
68
# File 'app/models/sqed_depiction.rb', line 66

def self.without_collection_object_data
  CollectionObject.joins(:sqed_depictions).where(buffered_collecting_event: nil, buffered_determinations: nil, buffered_other_labels: nil)
end

Instance Method Details

- (Array of symbols) collecting_event_sections

Returns the (named) sections in this depiction that may have collecting event label metadata

Returns:

  • (Array of symbols)

    the (named) sections in this depiction that may have collecting event label metadata



87
88
89
90
# File 'app/models/sqed_depiction.rb', line 87

def collecting_event_sections
    # !! master merge
  [:collecting_event_labels, :annotated_specimen] & [:metadata_map].values
end

- (Object) depiction_object



62
63
64
# File 'app/models/sqed_depiction.rb', line 62

def depiction_object
  depiction.depiction_object
end

- (Object) extraction_metadata



52
53
54
55
56
57
58
59
60
# File 'app/models/sqed_depiction.rb', line 52

def 
  {
    boundary_color: boundary_color.to_sym,
    boundary_finder: boundary_finder,
    has_border: has_border,
    layout: layout.to_sym,
    metadata_map: 
  }
end

- (Object) nearby_sqed_depictions(before = 5, after = 5)



92
93
94
95
96
# File 'app/models/sqed_depiction.rb', line 92

def nearby_sqed_depictions(before = 5, after = 5)
  a = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(after)
  b = SqedDepiction.where(project_id: project_id).where('id < ?', id).order('id DESC').limit(before)
  return { before: b, after: a}
end

- (CollectionObject?) next_collection_object

Returns the next collection object, by :id, created from the addition of a SqedDepiction

Returns:

  • (CollectionObject, nil)

    the next collection object, by :id, created from the addition of a SqedDepiction



79
80
81
82
83
# File 'app/models/sqed_depiction.rb', line 79

def next_collection_object
  object = CollectionObject.joins(:sqed_depictions).where(project_id: project_id).where('sqed_depictions.id > ?', id).where('collection_objects.id <> ?', depiction_object.id).order(:id).first
  object = CollectionObject.joins(:sqed_depictions).order(:id).first if object.nil?
  object
end

- (Object) next_sqed_depiction



98
99
100
101
# File 'app/models/sqed_depiction.rb', line 98

def next_sqed_depiction
  sd = SqedDepiction.where(project_id: project_id).where('id > ?', id).order(:id).limit(1)
  sd.any? ? sd.first : SqedDepiction.where(project_id: project_id).first
end

- (SqedDepiction) next_without_data

Returns the next record in which the collection object has no buffered data

Returns:

  • (SqedDepiction)

    the next record in which the collection object has no buffered data



72
73
74
75
# File 'app/models/sqed_depiction.rb', line 72

def next_without_data
  object = SqedDepiction.without_collection_object_data.with_project_id(project_id).where('collection_objects.id <> ?', depiction_object.id).where('sqed_depictions.id > ?', id).order(:id).first
  object.nil? ? SqedDepiction.where(project_id: project_id).order(:id).first : object.sqed_depictions.first
end

- (Object) preprocess



103
104
105
106
# File 'app/models/sqed_depiction.rb', line 103

def preprocess
  result = SqedToTaxonworks::Result.new(depiction_id: depiction.id)
  result.cache_all
end

- (Object) sqed_metadata_map (protected)



124
125
126
# File 'app/models/sqed_depiction.rb', line 124

def 
  .inject({}){|hsh, i| hsh.merge(i[0].to_i => i[1].to_sym)}
end