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?

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
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
154
155
156
157
158
159
160
161
162
# 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

  has_one :collection_object, through: :depiction, source_type: 'CollectionObject', source: :depiction_object 

  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.with_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].not_eq(nil).
      or(t[:buffered_determinations].not_eq(nil)).
      or(t[:buffered_other_labels].not_eq(nil))

    joins(:collection_object).where(q.to_sql)
  end

  def self.without_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].eq(nil).
      and(t[:buffered_determinations].eq(nil)).
      and(t[:buffered_other_labels].eq(nil))

    joins(:collection_object).where(q.to_sql)
  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
  end

  def self.last_without_data(project_id)
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(id: :asc).first : object
  end

  def self.last_with_data(project_id)

  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(force = true)
    return true if !File.exists?(depiction.image.image_file.path(:original))
    # don't rebuild if not forced and one or both cache is empty
    if !force
      if !result_ocr.blank? || !result_boundary_coordinates.blank?
        return true
      end
    end

    # otherwise rebuild
    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 project or user, 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
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
154
155
156
157
158
159
160
161
162
# 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

  has_one :collection_object, through: :depiction, source_type: 'CollectionObject', source: :depiction_object 

  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.with_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].not_eq(nil).
      or(t[:buffered_determinations].not_eq(nil)).
      or(t[:buffered_other_labels].not_eq(nil))

    joins(:collection_object).where(q.to_sql)
  end

  def self.without_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].eq(nil).
      and(t[:buffered_determinations].eq(nil)).
      and(t[:buffered_other_labels].eq(nil))

    joins(:collection_object).where(q.to_sql)
  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
  end

  def self.last_without_data(project_id)
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(id: :asc).first : object
  end

  def self.last_with_data(project_id)

  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(force = true)
    return true if !File.exists?(depiction.image.image_file.path(:original))
    # don't rebuild if not forced and one or both cache is empty
    if !force
      if !result_ocr.blank? || !result_boundary_coordinates.blank?
        return true
      end
    end

    # otherwise rebuild
    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 project or user, 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
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
154
155
156
157
158
159
160
161
162
# 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

  has_one :collection_object, through: :depiction, source_type: 'CollectionObject', source: :depiction_object 

  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.with_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].not_eq(nil).
      or(t[:buffered_determinations].not_eq(nil)).
      or(t[:buffered_other_labels].not_eq(nil))

    joins(:collection_object).where(q.to_sql)
  end

  def self.without_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].eq(nil).
      and(t[:buffered_determinations].eq(nil)).
      and(t[:buffered_other_labels].eq(nil))

    joins(:collection_object).where(q.to_sql)
  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
  end

  def self.last_without_data(project_id)
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(id: :asc).first : object
  end

  def self.last_with_data(project_id)

  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(force = true)
    return true if !File.exists?(depiction.image.image_file.path(:original))
    # don't rebuild if not forced and one or both cache is empty
    if !force
      if !result_ocr.blank? || !result_boundary_coordinates.blank?
        return true
      end
    end

    # otherwise rebuild
    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 project or user, 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
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
154
155
156
157
158
159
160
161
162
# 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

  has_one :collection_object, through: :depiction, source_type: 'CollectionObject', source: :depiction_object 

  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.with_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].not_eq(nil).
      or(t[:buffered_determinations].not_eq(nil)).
      or(t[:buffered_other_labels].not_eq(nil))

    joins(:collection_object).where(q.to_sql)
  end

  def self.without_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].eq(nil).
      and(t[:buffered_determinations].eq(nil)).
      and(t[:buffered_other_labels].eq(nil))

    joins(:collection_object).where(q.to_sql)
  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
  end

  def self.last_without_data(project_id)
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(id: :asc).first : object
  end

  def self.last_with_data(project_id)

  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(force = true)
    return true if !File.exists?(depiction.image.image_file.path(:original))
    # don't rebuild if not forced and one or both cache is empty
    if !force
      if !result_ocr.blank? || !result_boundary_coordinates.blank?
        return true
      end
    end

    # otherwise rebuild
    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 project or user, 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
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
154
155
156
157
158
159
160
161
162
# 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

  has_one :collection_object, through: :depiction, source_type: 'CollectionObject', source: :depiction_object 

  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.with_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].not_eq(nil).
      or(t[:buffered_determinations].not_eq(nil)).
      or(t[:buffered_other_labels].not_eq(nil))

    joins(:collection_object).where(q.to_sql)
  end

  def self.without_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].eq(nil).
      and(t[:buffered_determinations].eq(nil)).
      and(t[:buffered_other_labels].eq(nil))

    joins(:collection_object).where(q.to_sql)
  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
  end

  def self.last_without_data(project_id)
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(id: :asc).first : object
  end

  def self.last_with_data(project_id)

  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(force = true)
    return true if !File.exists?(depiction.image.image_file.path(:original))
    # don't rebuild if not forced and one or both cache is empty
    if !force
      if !result_ocr.blank? || !result_boundary_coordinates.blank?
        return true
      end
    end

    # otherwise rebuild
    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 project or user, 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
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
154
155
156
157
158
159
160
161
162
# 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

  has_one :collection_object, through: :depiction, source_type: 'CollectionObject', source: :depiction_object 

  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.with_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].not_eq(nil).
      or(t[:buffered_determinations].not_eq(nil)).
      or(t[:buffered_other_labels].not_eq(nil))

    joins(:collection_object).where(q.to_sql)
  end

  def self.without_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].eq(nil).
      and(t[:buffered_determinations].eq(nil)).
      and(t[:buffered_other_labels].eq(nil))

    joins(:collection_object).where(q.to_sql)
  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
  end

  def self.last_without_data(project_id)
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(id: :asc).first : object
  end

  def self.last_with_data(project_id)

  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(force = true)
    return true if !File.exists?(depiction.image.image_file.path(:original))
    # don't rebuild if not forced and one or both cache is empty
    if !force
      if !result_ocr.blank? || !result_boundary_coordinates.blank?
        return true
      end
    end

    # otherwise rebuild
    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 project or user, 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
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
154
155
156
157
158
159
160
161
162
# 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

  has_one :collection_object, through: :depiction, source_type: 'CollectionObject', source: :depiction_object 

  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.with_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].not_eq(nil).
      or(t[:buffered_determinations].not_eq(nil)).
      or(t[:buffered_other_labels].not_eq(nil))

    joins(:collection_object).where(q.to_sql)
  end

  def self.without_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].eq(nil).
      and(t[:buffered_determinations].eq(nil)).
      and(t[:buffered_other_labels].eq(nil))

    joins(:collection_object).where(q.to_sql)
  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
  end

  def self.last_without_data(project_id)
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(id: :asc).first : object
  end

  def self.last_with_data(project_id)

  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(force = true)
    return true if !File.exists?(depiction.image.image_file.path(:original))
    # don't rebuild if not forced and one or both cache is empty
    if !force
      if !result_ocr.blank? || !result_boundary_coordinates.blank?
        return true
      end
    end

    # otherwise rebuild
    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 project or user, 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
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
154
155
156
157
158
159
160
161
162
# 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

  has_one :collection_object, through: :depiction, source_type: 'CollectionObject', source: :depiction_object 

  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.with_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].not_eq(nil).
      or(t[:buffered_determinations].not_eq(nil)).
      or(t[:buffered_other_labels].not_eq(nil))

    joins(:collection_object).where(q.to_sql)
  end

  def self.without_collection_object_data
    t = CollectionObject.arel_table
    q = t[:buffered_collecting_event].eq(nil).
      and(t[:buffered_determinations].eq(nil)).
      and(t[:buffered_other_labels].eq(nil))

    joins(:collection_object).where(q.to_sql)
  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
  end

  def self.last_without_data(project_id)
    object = SqedDepiction.without_collection_object_data.with_project_id(project_id).order(:id).first
    object.nil? ? SqedDepiction.where(project_id: project_id).order(id: :asc).first : object
  end

  def self.last_with_data(project_id)

  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(force = true)
    return true if !File.exists?(depiction.image.image_file.path(:original))
    # don't rebuild if not forced and one or both cache is empty
    if !force
      if !result_ocr.blank? || !result_boundary_coordinates.blank?
        return true
      end
    end

    # otherwise rebuild
    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 project or user, 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

+ (Object) last_with_data(project_id)



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

def self.last_with_data(project_id)

end

+ (Object) last_without_data(project_id)



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

def self.last_without_data(project_id)
  object = SqedDepiction.without_collection_object_data.with_project_id(project_id).order(:id).first
  object.nil? ? SqedDepiction.where(project_id: project_id).order(id: :asc).first : object
end

+ (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 project or user, 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 project or user, just finds and processes



144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/sqed_depiction.rb', line 144

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) with_collection_object_data



68
69
70
71
72
73
74
75
# File 'app/models/sqed_depiction.rb', line 68

def self.with_collection_object_data
  t = CollectionObject.arel_table
  q = t[:buffered_collecting_event].not_eq(nil).
    or(t[:buffered_determinations].not_eq(nil)).
    or(t[:buffered_other_labels].not_eq(nil))

  joins(:collection_object).where(q.to_sql)
end

+ (Object) without_collection_object_data



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

def self.without_collection_object_data
  t = CollectionObject.arel_table
  q = t[:buffered_collecting_event].eq(nil).
    and(t[:buffered_determinations].eq(nil)).
    and(t[:buffered_other_labels].eq(nil))

  joins(:collection_object).where(q.to_sql)
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



112
113
114
115
# File 'app/models/sqed_depiction.rb', line 112

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

- (Object) depiction_object



64
65
66
# File 'app/models/sqed_depiction.rb', line 64

def depiction_object
  depiction.depiction_object
end

- (Object) extraction_metadata



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

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)



117
118
119
120
121
# File 'app/models/sqed_depiction.rb', line 117

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



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

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



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

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



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

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
end

- (Object) preprocess(force = true)



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/sqed_depiction.rb', line 128

def preprocess(force = true)
  return true if !File.exists?(depiction.image.image_file.path(:original))
  # don't rebuild if not forced and one or both cache is empty
  if !force
    if !result_ocr.blank? || !result_boundary_coordinates.blank?
      return true
    end
  end

  # otherwise rebuild
  result = SqedToTaxonworks::Result.new(depiction_id: depiction.id)
  result.cache_all
end

- (Object) sqed_metadata_map (protected)



158
159
160
# File 'app/models/sqed_depiction.rb', line 158

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