Class: Vangrail::ScoreResult

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

Overview

Validated output from one risk reader.

Constant Summary collapse

STATUSES =
%i[ok abstained invalid].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader_id:, model_id:, feature_schema:, side:, scores: {}, status: :ok, reason: nil, cost: {}, intervals: {}, metadata: {}) ⇒ ScoreResult

Returns a new instance of ScoreResult.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vangrail/score.rb', line 11

def initialize(reader_id:, model_id:, feature_schema:, side:, scores: {},
               status: :ok, reason: nil, cost: {}, intervals: {}, metadata: {})
  @reader_id = reader_id.to_s.freeze
  @model_id = model_id.to_s.freeze
  @feature_schema = Array(feature_schema).map(&:to_s).uniq.freeze
  @side = side.to_sym
  @status = status.to_sym
  raise ArgumentError, "unknown score status #{status}" unless STATUSES.include?(@status)

  @scores = numeric_hash(scores)
  @cost = numeric_hash(cost)
  @intervals = interval_hash(intervals)
  @metadata = immutable()
  @reason = reason&.to_s&.freeze
  validate_ok!
  freeze
end

Instance Attribute Details

#costObject (readonly)

Returns the value of attribute cost.



8
9
10
# File 'lib/vangrail/score.rb', line 8

def cost
  @cost
end

#feature_schemaObject (readonly)

Returns the value of attribute feature_schema.



8
9
10
# File 'lib/vangrail/score.rb', line 8

def feature_schema
  @feature_schema
end

#intervalsObject (readonly)

Returns the value of attribute intervals.



8
9
10
# File 'lib/vangrail/score.rb', line 8

def intervals
  @intervals
end

#metadataObject (readonly)

Returns the value of attribute metadata.



8
9
10
# File 'lib/vangrail/score.rb', line 8

def 
  @metadata
end

#model_idObject (readonly)

Returns the value of attribute model_id.



8
9
10
# File 'lib/vangrail/score.rb', line 8

def model_id
  @model_id
end

#reader_idObject (readonly)

Returns the value of attribute reader_id.



8
9
10
# File 'lib/vangrail/score.rb', line 8

def reader_id
  @reader_id
end

#reasonObject (readonly)

Returns the value of attribute reason.



8
9
10
# File 'lib/vangrail/score.rb', line 8

def reason
  @reason
end

#scoresObject (readonly)

Returns the value of attribute scores.



8
9
10
# File 'lib/vangrail/score.rb', line 8

def scores
  @scores
end

#sideObject (readonly)

Returns the value of attribute side.



8
9
10
# File 'lib/vangrail/score.rb', line 8

def side
  @side
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/vangrail/score.rb', line 8

def status
  @status
end

Class Method Details

.abstained(reason:, **options) ⇒ Object



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

def self.abstained(reason:, **options)
  new(status: :abstained, reason: reason, scores: {}, **options)
end

.invalid(reason:, **options) ⇒ Object



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

def self.invalid(reason:, **options)
  new(status: :invalid, reason: reason, scores: {}, **options)
end

.ok(**options) ⇒ Object



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

def self.ok(**options)
  new(status: :ok, **options)
end

Instance Method Details

#abstained?Boolean

Returns:

  • (Boolean)


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

def abstained?
  status == :abstained
end

#invalid?Boolean

Returns:

  • (Boolean)


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

def invalid?
  status == :invalid
end

#to_hObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vangrail/score.rb', line 53

def to_h
  {
    'reader_id' => reader_id,
    'model_id' => model_id,
    'feature_schema' => feature_schema,
    'side' => side.to_s,
    'status' => status.to_s,
    'scores' => scores,
    'intervals' => (intervals unless intervals.empty?),
    'cost' => (cost unless cost.empty?),
    'metadata' => ( unless .empty?),
    'reason' => reason,
  }.compact
end

#valid?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/vangrail/score.rb', line 41

def valid?
  status == :ok
end